#include <gl\glut.h>
#define ww 500
#define wh 500
#define wz 500
#define UOR 6
void display();
void reshape(int w, int h);
void mouse(int btn, int state, int x, int y);
void timer(int v);
void solar();
int i = 0;
GLfloat tetra = 10.0;
GLfloat dtetra = 10.0;
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(ww, wh);
glutInitWindowPosition(0, 0);
glutCreateWindow("solor");
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutTimerFunc(1000/60, timer, 1);
glutMainLoop();
return 0;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
solar();
glutSwapBuffers();
}
void mouse(int btn, int state, int x, int y) {
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
i++;
if (i % 2 > 0) {
glViewport(0, 0, ww, wh);
glMatrixMode(GL_PROJECTION);
glutReshapeWindow(ww, wh);
glLoadIdentity();
gluLookAt(0, 1, 0, 0, 0, 0, 0, 0, -1);
glOrtho(-UOR, UOR, -UOR, UOR, -UOR, UOR);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
else {
glViewport(0, 0, ww, wh);
glMatrixMode(GL_PROJECTION);
glutReshapeWindow(ww, wh);
glLoadIdentity();
gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
glOrtho(-UOR, UOR, -UOR, UOR, -UOR, UOR);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
void reshape(int w, int h) {
glViewport(0, 0, ww, wh);
glMatrixMode(GL_PROJECTION);
glutReshapeWindow(ww, wh);
glLoadIdentity();
glOrtho(-UOR, UOR, -UOR, UOR, -UOR, UOR);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutSwapBuffers();
}
void solar() {
glColor3f(1.0, 0.0, 0.0);
glutSolidSphere(1.2, 50, 50);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(tetra + dtetra, 0.0, 0.0, 1.0);
glTranslated(3.5, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glutSolidSphere(0.4, 50, 50);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glRotated(tetra * 5 + dtetra, 0.0, 0.0, 1.0);
glTranslated(1.2, 0.0, 0.0);
glColor3f(1.0, 1.0, 0.0);
glutSolidSphere(0.24, 50, 50);
glPushMatrix();
}
void timer(int v) {
dtetra += 10;
glutPostRedisplay();
glutTimerFunc(1000/60, timer, v);
}
these codes are simple 3d solar animation about sun,earth and moon.
if click mouse left button its change camera view and its working.
but it appear too slow(about 2-3sec to reshape) or somtimes it doesn’t work in the SAME CODE!!
i adjust ‘1000/60’ this part but it didnt appear what i want.
i neeeeed help