开发者

opengl+glut glutPostRedisplay where?

I'm programming in C with GLUT and OPENGL, i want my window redrawing itself again and again. I know that i can rer开发者_Go百科ender with glutPostRedisplay(), if I put it in the idle function of Glut my pc lags.

My code is following atm

void on_idle() {
    glutPostRedisplay();
}
void on_draw() {
    ...
    glClearColor(1.f, 1.f, 1.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT);
    ...
    glFlush();
}
int main(int argc, char** argv) {
    ...
    glutDisplayFunc(&on_draw);
    glutIdleFunc(&on_idle);
    ...
}


Try this:

void on_timer(int value) {
    glutPostRedisplay();
    glutTimerFunc(33, on_timer, 0);
}
void on_draw() {
    ...
    glClearColor(1.f, 1.f, 1.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT);
    ...
    glFlush();
}
int main(int argc, char** argv) {
    ...
    glutDisplayFunc(on_draw);
    glutTimerFunc(33, on_timer, 0)
    ...
}


Make idle yielding any left CPU cycles on the time slice right before the glutPostRedisplay:

void on_idle() {
#ifdef WIN32
    Sleep(0); // zero sleep = yield
#else ifdef _POSIX_PRIORITY_SCHEDULING
    sched_yield(); // #include <sched.h>
#endif
    glutPostRedisplay();
}


I don't quite understand your question... what do yo mean "you want your window redrawing itself again and again" ?

GLUT does that itself with the glutMainLoop() function that keeps calling the display call back function (Usually the problem is the reversed... people ask how they can leave the infinite loop programmatically.. which is impossible with GLUT, but not with FreeGLUT)

No need to put the redisplay in the idle function, which is only called when nothing else is happening...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜