OpenGL Rotations around World Origin when they should be around Local Origin

守給你的承諾、 提交于 2019-12-31 02:17:09

问题


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();  

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

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

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.

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

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.

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.

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


回答1:


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.

I think what you need is this

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();



回答2:


Swap your rotate and translate calls around :)

Since they post-multiply the matrix stack the last the be called is the 'first' to be applied conceptually, if you care about that sort of thing.




回答3:


I think you first have to translate your camera to point (0,0,0) then rotate, then translate it back.



来源:https://stackoverflow.com/questions/1977737/opengl-rotations-around-world-origin-when-they-should-be-around-local-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!