glutPostRedisplay在循环内不起作用 [英] glutPostRedisplay not working inside a loop
问题描述
我试图让一个人在y轴上跳跃,所以我使用了2秒的循环,第一秒应该向下移动并弯曲膝盖,第二秒应该向上移动然后完成起始位置.现在,刚开始让那个人在第一秒摔倒并屈曲膝盖,我还没有编写动画的其余部分.问题在于glutPostRedisplay并没有根据需要刷新循环内的屏幕,因此它看起来就像一个动画,它只是在循环完成后才重新显示屏幕.如何让glutPostRedisplay刷新循环中的屏幕,使其看起来像是动画?
case ' ':
miliseconds = 0;
MoveManY = 0;
start = clock();
i=0;
while (miliseconds<2000)
{
finish = clock();
miliseconds = (finish - start);
Sleep(1);
if (miliseconds<1000)
{
MoveManY=MoveManY-0.02;
}
glutPostRedisplay();
}
break;
glutPostRedisplay
不会直接触发重新绘制屏幕.它仅向系统指示屏幕需要尽快重绘.就像您一样,在循环中多次调用它实际上具有与一次调用相同的效果.有关更多详细信息,请参见此处.
我无法确定您发布的代码是从哪里调用的.但是,您想要做的是使用glutIdleFunc
来指定在无事可做的情况下要调用的方法,并在其中执行动画单个帧所需的操作,并调用一次glutPostRedisplay
.这将需要将您的计时器移到其他位置.
我发现本教程对于GLUT动画确实很有帮助. /p>
I am trying to make a man jump on the y axis, so I use a loop of 2 seconds, the first second it should move down and bend its knees, and the second second it should move up and then finish in the starting position. Right now am just starting to make the man go down and bend it's knees in the first second, I haven't programmed the rest of the animation. The problem is that glutPostRedisplay is not refreshing the screen inside the loop as needed so it looks like an animation, it just re displays the screen after the loop has completed. How do I get glutPostRedisplay to refresh the screen inside my loop so it looks like an animation?
case ' ':
miliseconds = 0;
MoveManY = 0;
start = clock();
i=0;
while (miliseconds<2000)
{
finish = clock();
miliseconds = (finish - start);
Sleep(1);
if (miliseconds<1000)
{
MoveManY=MoveManY-0.02;
}
glutPostRedisplay();
}
break;
glutPostRedisplay
does not directly trigger redrawing the screen. It only indicates to the system that the screen needs redrawing as soon as possible. Calling it multiple times in a loop, like you do, actually has the same effect as calling it once. See here for more details.
I cannot tell where the code you posted is called from. However, what you want to do is use glutIdleFunc
to designate a method to be called when there's nothing else to do and, in it, do what you need for a single frame of your animation and call glutPostRedisplay
once. This will require to move your timer somewhere else.
I found this tutorial to be really helpful with GLUT animations.
这篇关于glutPostRedisplay在循环内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!