Pausing in OpenGL successively

流过昼夜 提交于 2020-01-21 23:29:29

问题


void keyPress(unsigned char key,int x,int y){
    int i;
    switch(key){
    case 'f':
        i = 3;
        while(i--){
            x_pos += 3;
            sleep(100);
            glutPostRedisplay();
        }
    }
}
  • Above is the code snippet written in C++ using GLUT library in Windows 7.

  • This function takes a character key and mouse co-ordinates x,y and performs translation along x-direction in 3 successive steps on pressing f character. Between each step the program should sleep for 100 ms.

  • We want to move a robot, and pause successively when he moves forward steps.

  • We are facing a problem in making the program sleep between the 3 steps. What is the problem in the above code snippet?


回答1:


Disclaimer: The answer of jozxyqk seems better to me. This answer solves the problem in a dirty way.


You are misusing glutPostRedisplay, as is stated in this answer. The problem being, that glutPostRedisplay marks the current window as needing to be redisplayed, but it will only be done once you get in the glutMainLoop again. That does happen only once, hence only one sleep seems to work.

In fact all three sleeps work, but you get only one redraw after 300 ms.

To solve this, you have to find another way of redrawing the scene.

while(i--){
    x_pos += 3;
    sleep(100);
    yourDrawFunction();
}

Assuming that you are working on a UNIX system.

sleep for 100 ms

sleep(100);

The problem here is, that you are sleeping for 100 seconds, as you are probably using the sleep function of the <unistd.h> header, which defines sleep() as:

extern unsigned int sleep (unsigned int __seconds);

What you want is probably something like

usleep(100000); //sleeps for 100000 microseconds == 100 ms



回答2:


I believe the issue with your code is your sleep is messing with glut's main loop. The call stack might look something like this

main() -> glutMainLoop() -> keyPress() -> sleep()

#but can't get to this...
main() -> glutMainLoop() -> display()

Until keyPress() returns, glut's main loop cannot continue to render the next frame. It's waiting for the function to return. All glutPostRedisplay() does is say "hey, something's changed so the image is stale and we need to redraw the next time the main loop iterates". It doesn't actually call display().

You'll have to structure your code such that the main loop can continue as normal, but still include a delay between drawing. For example:

  • In keyPress(), set a moving = true state. Let the function return.
  • In the idle() function, call sleep() if moving or maybe if you have moved last time (really you might want to look into calculating elapsed time and do the timing yourself so you don't block the entire program)
  • Again in idle() increase x_pos and decrease your move count, let the function return, glut will draw, then call idle again and you can sleep/move again.


来源:https://stackoverflow.com/questions/26339036/pausing-in-opengl-successively

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