作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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/
我正在用 Java 编写第一个真正执行 UI 的程序,所以如果答案很明显,请耐心等待。 我正在使用 JGraph 5 (5.14) 来可视化由 JGrapht (0.8.3) 创建的图形。 我可以使用
我是一名优秀的程序员,十分优秀!