简体   繁体   English

OpenGL:从读取文件的while循环调用glutPostRedisplay()

[英]OpenGL: Calling glutPostRedisplay() from a while loop that reads a file

So I am trying to read sensor data from an IMU and update angles accordingly. 所以我试图从IMU读取传感器数据并相应地更新角度。 I open the sensor data file, read line by line, convert quaternion to rotations and then update my model. 我打开传感器数据文件,逐行读取,将四元数转换为旋转,然后更新我的模型。 The problem is that when calling the glutPostRedisplay() from the while loop the loop continues while glutPostRedisplay() operates in paralell. 问题是当从while循环调用glutPostRedisplay()时,循环继续,而glutPostRedisplay()在并行操作。 This makes it appear that everything happens instantly. 这使得一切都会立即发生。 What I want to do is to force the program to halt until the display is updated. 我想要做的是强制程序停止,直到显示更新。

I can't think of another way to do this because I don't want to constantly open and close a file or keep track of where in the file I currently am. 我想不出另一种方法,因为我不想经常打开和关闭文件或跟踪我目前在文件中的位置。 It would be easier if I could just read the line, process it, then force OpenGL to render, then read the next line etc. 如果我能读取该行,处理它,然后强制OpenGL渲染,然后读取下一行等,将会更容易。

Does anyone have any suggestions? 有没有人有什么建议?

Note: Currently the while loop fully executes by the time I am able to render. 注意:目前while循环在我能够渲染时完全执行。 I have tried using glutSwapBuffers() directly after the glutPostRedisplay() 我试过在glutPostRedisplay()之后直接使用glutSwapBuffers()

GLUT doesn't work that way. GLUT不会那样工作。 You will have to stop your loop and return from whatever function you want in order for GLUT to issue a rendering command. 您必须停止循环并从您想要的任何函数返回,以便GLUT发出渲染命令。 Generally you put this kind of stuff in an idle func. 一般来说,你把这种东西放在空闲的功能中。

Or you could use GLFW, which allows you to have ownership of the rendering loop. 或者您可以使用GLFW,它允许您拥有渲染循环的所有权。 Or you could use FreeGLUT's glutMainLoopEvent , which allows you to have ownership of the rendering loop (though you need a bit of code rewriting to make this work). 或者您可以使用FreeGLUT的glutMainLoopEvent ,它允许您拥有渲染循环的所有权(尽管您需要一些代码重写才能使其工作)。

Instead of using a while loop, you can register a function to be called when GLUT isn't busy with glutIdleFunc . 当GLUT不忙于glutIdleFunc时,您可以注册要调用的函数,而不是使用while循环。 Read from the IMU in this idle function and call glutPostRedisplay when you need to force a redisplay. 在此空闲函数中从IMU读取,并在需要强制重新显示时调用glutPostRedisplay。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM