c++ - 刷新显示每个运动(速度)(openGL)

标签 c++ opengl game-physics

在 Opengl 中有这个函数 dance() 我想移动一个对象并刷新屏幕然后再次移动,这样看起来对象一直在移动。现在它所做的只是移动很多,然后刷新屏幕。

这是我的 displ ay_Main:

void window::main () {
 static int argc = 0;
 glutInit (&argc, nullptr);
 glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
 glutInitWindowSize (window::width, window::height);
 glutInitWindowPosition (128, 128);
 glutCreateWindow (sys_info::execname().c_str());
 glutCloseFunc (window::close);
 glutEntryFunc (window::entry);
 glutDisplayFunc (window::display);
 glutReshapeFunc (window::reshape);
 glutKeyboardFunc (window::keyboard);
 glutSpecialFunc (window::special);
 glutMotionFunc (window::motion);
 glutPassiveMotionFunc (window::passivemotion);
 glutMouseFunc (window::mousefn);
 glutMainLoop();
 } 

这是我的显示代码:

void window::display() {
 glClear (GL_COLOR_BUFFER_BIT);
 for (auto& object: window::objects) object.draw();
 objects.at(selected_obj).draw_border();
 mus.draw();
 glutSwapBuffers();
}

这是我的移动代码:

 void move (GLfloat delta_x, GLfloat delta_y) {
     //center pertains to the object that is drawn
     center.xpos += delta_x;
     center.ypos += delta_y;
  }

现在我的舞蹈代码:

  static void dance(){
     int x =0; int y=0;
     for(;;){
       int r1 = rand() % 100;
       int r2 = rand() %100;
       vertex v = objects.at(selected_obj).get_vertex();
       if(v.xpos >= width) break;
       if(v.ypos >= height) break;
       x=move_pixels+r1; y=move_pixels+r2;
       objects.at(selected_obj).move(x,y);
       glutPostRedisplay();
      }
    }

在我的 dance() 中,我希望它移动然后更新,然后再次移动(如速度)。然而,它只是一次移动所有并在完成后更新。

最佳答案

glutPostDisplay 只是设置了一个标志,在事件循环的下一次迭代中应该调用显示函数。

这是事件驱动编程最重要的规则:永远不要将定时 Action (如动画)放入“回放”循环中,在返回事件系统之前循环播放整个动画。在您的情况下,您必须将 dance 函数分解为一个 idle 函数,每次处理完所有未决事件(包括重绘)时都会调用该函数。然后您可以前进到下一步更新动画并发出重绘。

关于c++ - 刷新显示每个运动(速度)(openGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465080/

相关文章:

c++ - 以下模板特化代码是非标准的还是 VS-C++ 中的错误?

c++ - 访问钩子(Hook)程序中的钩子(Hook)

c++ - 为什么这个 float 比较会产生错误?

结构数组上的 C++ qsort 内部函数

c++ - 无法将参数 1 从 'char *' 转换为 'LPCWSTR'

iphone - OpenGL 到 OpenGL-ES - glRectf()

c - OpenGL - 如何从中心缩放对象?

ios - 如果 physicsBody.dynamic 属性为 NO,则两个物理体不接触

javascript - Canvas 旋转星空场

algorithm - 寻找范围内的东西