gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:43:33 25 4
gpt4 key购买 nike

在 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/

25 4 0
文章推荐: 不使用
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com