围绕世界原点的OpenGL旋转,当它们应该围绕本地原点时 [英] OpenGL Rotations around World Origin when they should be around Local Origin

查看:129
本文介绍了围绕世界原点的OpenGL旋转,当它们应该围绕本地原点时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在OpenGL中实现一个简单的相机系统.我在投影矩阵下设置了gluPerspective,然后在ModelView矩阵上使用了gluLookAt.之后,我有一个主要的渲染循环,用于检查键盘事件,如果按下了任何箭头键,则可以修改角度和前进速度(我只绕过y轴旋转,而绕过z轴(向前)).然后,我使用以下代码移动视图(deltaTime是自上一帧渲染以来经过的时间(以秒为单位,以使移动与帧速率脱钩):

I'm implementing a simple camera system in OpenGL. I set up gluPerspective under the projection matrix and then use gluLookAt on the ModelView matrix. After this I have my main render loop which checks for keyboard events and, if any of the arrow keys are pressed, modifies angular and forward speeds (I only rotate through the y axis and move through the z (forwards)). Then I move the view using the following code (deltaTime is the amount of time since the last frame was rendered in seconds, in order to decouple movement from framerate):

//place our camera  
newTime = RunTime(); //get the time since app start  
deltaTime = newTime - time; //get the time since the last frame was rendered  
time = newTime;  
glRotatef(view.angularSpeed*deltaTime,0,1,0); //rotate  
glTranslatef(0,0,view.forwardSpeed*deltaTime); //move forwards  
//draw our vertices  
draw();  
//swap buffers  
Swap_Buffers();  

然后代码再次循环.我的绘制算法以glPushMatrix()开始,以glPopMatrix()结尾.

Then the code loops around again. My draw algorithm begins with a glPushMatrix() and ends in a glPopMatrix().

每次调用glRotatef()和glTranslatef()都会以视图方向上的前进速度将视图向前推动.

Each call to glRotatef() and glTranslatef() pushes the view forwards by the forwards speed in the direction of view.

但是,当我运行代码时,我的对象被绘制在正确的位置,但是当我移动代码时,移动是以世界原点(0,0,0-面向Z轴)的方向而不是本地位置(我指向的位置),当我旋转时,旋转是围绕(0,0,0)进行的,而不是照相机的位置.

However when I run the code, my object is drawn in the correct place, but when I move the movement is done with the orientation of the world origin (0,0,0 - facing along the Z axis) as opposed to the local orientation (where I'm pointing) and when I rotate, the rotation is done about (0,0,0) and not the position of the camera.

最后,我的摄影机绕轨道旋转(0,0,0),而不是在原地旋转,从而产生了这种奇怪的效果.

I end up with this strange effect of my camera orbiting (0,0,0) as opposed to rotating on the spot.

我不会在循环内的任何地方调用glLoadIdentity(),并且我确定整个循环的矩阵模式"都设置为GL_MODELVIEW.

I do not call glLoadIdentity() at all anywhere inside the loop, and I am sure that the Matrix Mode is set to GL_MODELVIEW for the entire loop.

另一个奇怪的效果是,如果我将glLoadIdentity()调用放入draw(函数中(在PushMatrix和PopMatrix调用之间),屏幕只会变黑,无论我在哪里看都找不到我绘制的对象

Another odd effect is if I put a glLoadIdentity() call inside the draw() function (between the PushMatrix and PopMatrix calls, the screen just goes black and no matter where I look I can't find the object I draw.

有人知道我为了使该轨道(0,0,0)而不是在原地旋转而搞砸了吗?

Does anybody know what I've messed up in order to make this orbit (0,0,0) instead of rotate on the spot?

推荐答案

glRotate()围绕世界原点旋转ModelView矩阵,因此要绕任意点旋转,您需要平移矩阵以使该点位于原点,旋转,然后平移回开始的位置.

glRotate() rotates the ModelView Matrix around the World Origin, so to rotate around some arbitrary point, you need to translate your matrix to have that point at the origin, rotate and then translate back to where you started.

我认为您需要的是这个

float x, y, z;//point you want to rotate around

glTranslatef(0,0,view.forwardSpeed*deltaTime); //move forwards

glTranslatef(x,y,z); //translate to origin
glRotatef(view.angularSpeed*deltaTime,0,1,0); //rotate
glTranslatef(-x,-y,-z); //translate back
//draw our vertices
draw();
//swap buffers
Swap_Buffers();

这篇关于围绕世界原点的OpenGL旋转,当它们应该围绕本地原点时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆