Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL Rotations around World Origin when they should be around Local Origin
    text
    copied!<p>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):</p> <pre><code>//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(); </code></pre> <p>Then the code loops around again. My draw algorithm begins with a <code>glPushMatrix()</code> and ends in a <code>glPopMatrix()</code>.</p> <p>Each call to glRotatef() and glTranslatef() pushes the view forwards by the forwards speed in the direction of view.</p> <p>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.</p> <p>I end up with this strange effect of my camera orbiting (0,0,0) as opposed to rotating on the spot.</p> <p>I do not call <code>glLoadIdentity()</code> at all anywhere inside the loop, and I am sure that the Matrix Mode is set to <code>GL_MODELVIEW</code> for the entire loop.</p> <p>Another odd effect is if I put a <code>glLoadIdentity()</code> call inside the <code>draw(</code>) 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.</p> <p>Does anybody know what I've messed up in order to make this orbit (0,0,0) instead of rotate on the spot?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload