每次移动(速度)刷新显示(openGL)

时间:2014-08-23 18:28:46

标签: c++ opengl game-physics

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

这是我的移动_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()中,我希望它移动然后更新,然后再移动(如速度)。但是,它只是一次移动并在完成时更新。

1 个答案:

答案 0 :(得分:1)

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

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