opengl+glut glutPostRedisplay where?

这一生的挚爱 提交于 2019-12-13 07:38:59

问题


I'm programming in C with GLUT and OPENGL, i want my window redrawing itself again and again. I know that i can rerender 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);
    ...
}

回答1:


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)
    ...
}



回答2:


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();
}



回答3:


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...



来源:https://stackoverflow.com/questions/4608232/openglglut-glutpostredisplay-where

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!