连续在OpenGL中暂停 [英] Pausing in OpenGL successively

查看:186
本文介绍了连续在OpenGL中暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • 上面是使用Windows 7中的GLUT库以C ++编写的代码段.

    • Above is the code snippet written in C++ using GLUT library in Windows 7.

      此功能使用字符键和鼠标坐标x,y,并在按f字符时连续3个步骤沿x方向执行平移.在每个步骤之间,程序应休眠100 ms.

      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.

      我们在使程序在3个步骤之间进入休眠状态时遇到了问题.上面的代码段有什么问题?

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

      推荐答案

      免责声明:对我来说,jozxyqk的答案似乎更好.这个答案以肮脏的方式解决了这个问题.

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

      您正在滥用glutPostRedisplay,如此答案所述.问题是,glutPostRedisplay将当前窗口标记为需要重新显示,但是只有再次进入glutMainLoop时,它才会完成.那只会发生一次,因此似乎只有一次睡眠可以工作.

      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.

      事实上,所有三个睡眠都起作用,但是300毫秒后您只有一次重画.

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


      假设您正在UNIX系统上工作.

      睡眠100毫秒

      sleep for 100 ms

      sleep(100);
      

      这里的问题是,您正在睡眠100秒,因为您可能正在使用<unistd.h>标头的sleep函数,该标头将sleep()定义为:

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

      您想要的可能是

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

      这篇关于连续在OpenGL中暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆