glRotatef问题
我正在尝试一个非常基本的代码示例。我绘制一个 opengl 对象,然后用键盘旋转它。我在不使用键盘的情况下尝试了同样的操作。我打开一个线程,每 5 秒调用相同的 glRotatef
函数(与我在键盘函数中使用的相同的 glrotatef
)。但没有轮换。问题是什么?下面是我的线程代码:
void movePlayer2()
{
while(1)
//if(key == 'a')
{
Sleep(5000);
glRotatef(25,1,0,0);
//gluLookAt(0,0,0.01,0,0,-5,0,1,0);
display();
}
}
I am trying a very basic code sample. I draw an opengl object and with a keyboard I rotate it. I tried same thing without using keyboard. I open a thread and I invoked same glRotatef
function (same glrotatef
I use in keyboard function) for every 5 seconds. But there is no rotation. What is the problem? Here is my thread code below:
void movePlayer2()
{
while(1)
//if(key == 'a')
{
Sleep(5000);
glRotatef(25,1,0,0);
//gluLookAt(0,0,0.01,0,0,-5,0,1,0);
display();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的基本代码:
here is my basic code :
OpenGL 上下文与线程绑定,即只要 OpenGL 上下文被绑定(通过 gl*MakeCurrent),只有从上下文所绑定的线程进行的函数调用才会影响该上下文。您可以将上下文绑定到另一个线程(首先将其取消绑定)。或者简单地说,它根本不按照你的想法工作。
然而你的做法无论如何都是完全错误的。 OpenGL 不是场景图。 glRotatef 仅影响模型视图矩阵。 OpenGL 不认识对象,它只认识基元。
OpenGL 中的基本模式是,您在绘制之前设置所需的所有状态。因此,您在循环中调用显示函数,在其中设置视口和投影,然后迭代所有对象,为每个对象设置适当的模型视图矩阵。根据您的应用程序,您可以在显示功能中多次执行这些步骤。在您的情况下,您的计时器应该做的是增加旋转角度,然后在显示函数中使用该角度来创建正确的模型视图矩阵。
OpenGL contexts are tied to threads, i.e. as long as a OpenGL context is bound (by gl*MakeCurrent) only functions calls made from the thread the context has been bound to, will affect that context. You can bind a context to another thread (first unbind it). Or in simple words, it simply doesn't work the way you think.
However you approach is totally wrong anyway. OpenGL is not a scene graph. glRotatef affects just the modelview matrix. And OpenGL doesn't know objects, it just knows primitives.
The basic pattern in OpenGL is, that you set all the state you need, just right before drawing. So you call the display function in a loop, therein set viewport and projection, then iterate through all objects, for each object set the proper modelview matrix. And depending on your application you may do those steps multiple times within the display function. And in your case, what your timer should do is incrementing the rotation angle, which is then used within the display function to create the proper modelview matrix.