启动和停止 GLUT 动画
我正在用 c 语言开发一个项目,我将在其中进行一些繁重的物理计算,并且我希望能够在完成后看到结果。现在的工作方式是我在主线程上运行 GLUT,并使用单独的线程(pthread)进行输入(来自终端)和计算。我目前使用 glutTimerFunc 来做动画,但问题是该函数无论如何都会在每个给定的时间间隔触发。我可以通过在动画函数中使用 if 语句来停止动画,从而阻止变量更新,但这使用了很多不必要的资源(我认为)。
为了解决这个问题,我想我可以使用一个带有自定义计时器函数的额外线程,我可以自己控制它(没有 glutMainLoop 搞乱事情)。目前,这是我的测试函数,用于检查这是否有效(意味着该函数本身还没有完成)。它在 glutMainLoop 之前在单独的线程 createt 中运行:
void *threadAnimation() {
while (1) {
if (animationRun) {
rotate = rotate+0.00001;
if (rotate>360) {
rotate = rotate-360;
}
glutSetWindow(window);
glutPostRedisplay();
}
}
}
我遇到的具体问题是动画只运行几秒钟,然后停止。有人知道我该如何解决这个问题吗?我计划稍后使用计时器等,但我正在寻找一种方法来确保 glutPostRedisplay 将发送到正确的位置。我认为 glutSetWindow(window) 是解决方案,但显然不是。如果我删除 glutSetWindow(window) 动画仍然有效,只是时间不长,但运行速度更快(所以也许 glutSetWindow(window) 需要大量资源?)
顺便说一句,变量“window”是这样创建的:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(854, 540);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Animation View");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
timerInt = pthread_create(&timerThread, NULL, &threadAnimation, NULL);
glutMainLoop();
我不实际上不知道这是否正确,但它编译得很好。任何帮助都非常感谢!
I'm working on a project in c, where I'm going to make some heavy physics calculations, and I want the ability to see the results when I'm finished. The way it works now is that I run GLUT on the main thread, and use a seperate thread (pthread) to do input (from terminal) and calculations. I currently use glutTimerFunc to do the animation, but the problem is that that function will fire every given time intervall no matter what. I can stop the animation by using an if statement in the animation function, stopping the variables from being updatet, but this uses a lot of unnecessary resources (I think).
To fix this problem I was thinking that I could use an extra thread with a custom timer function that I could controll myself (without glutMainLoop messing things up). Currently this is my test function to check if this would work (meaning that the function itself is in no way finished). It runs in a seperate thread createt just before glutMainLoop:
void *threadAnimation() {
while (1) {
if (animationRun) {
rotate = rotate+0.00001;
if (rotate>360) {
rotate = rotate-360;
}
glutSetWindow(window);
glutPostRedisplay();
}
}
}
The specific problem I have is that the animation just runs for a couple of seconds, and then stops. Does anybody know how I can fix this? I am planning to use timers and so on later, but what I'm looking for is a way to ensure that glutPostRedisplay will be sent the right place. I tought glutSetWindow(window) was the solution, but apparently not. If I remove glutSetWindow(window) the animation still works, just not for as long, but runs much faster (so maybe glutSetWindow(window) takes a lot of resources?)
btw the variable "window" is created like this:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(854, 540);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Animation View");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
timerInt = pthread_create(&timerThread, NULL, &threadAnimation, NULL);
glutMainLoop();
I don't acctually know if this is correct, but it compiles just fine. Any help is greatly appriciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一个小想法,创建包含所有动态设置的类:
主应用程序将包含这些设置:
在动画线程中绘制(可能对一个变量使用一些线程锁):
在计算中:
再次是锁定一个变量。
Here's a little idea, create class that will contain all dynamic settings:
And than main application will contain those:
In animation thread just draw (maybe use some threads locks for one variable):
In calculations:
Again it's locking one variable.