PDA

View Full Version : i really dont know why the glutTimerFunc doesn't work



rangora
04-29-2017, 08:37 PM
#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 :(

Dark Photon
04-30-2017, 03:56 PM
Some of what you're doing in your resize() callback really isn't kosher.

Here I see a consistent frame rate. But whenever I try to resize the window, it snaps back to the 500 x 500 you're consistently re-forcing it to.

Anyway, what's not kosher: Calling glutReshapeWindow() requests that the window be resized later, which will trigger a call to the reshape() callback (that's expected). However, you're also calling glutReshapeWindow() "inside" the reshape() callback? See the problem? In response to the "last" resize request, you're requesting yet another resize (including calling your resize() callback). Basically, you're asking GLUT to help you burn down the CPU processing needless, redundant resize events. Depending on the GLUT implementation, this could hang your program or generate a high CPU usage.

Call glutReshapeWindow() elsewhere (if needed). Then inside the resize callback, just react to that change in window size (e.g. change the glViewport, etc.).

You also shouldn't be calling glutSwapBuffers() inside of a resize callback. Only call that inside your display() function.

Commenting out the two GLUT function calls inside your reshape() callback (glutReshapeWindow(), glutSwapBuffers()) makes your solar animation run at least 2X faster here on my box.