Reputation: 11
I'm trying OpenGL for the first time, so I want to code a simple program that displays a box and lets me move the camera.
As there is no such thing as a camera in OpenGL, I just simulate every "camera movement" I want by making the whole world move in the exact opposite way. It goes well until I get to rotations.
What happens is that whenever I rotate the world (glRotatef(10, 0, 1, 0);
) the axes get rotated as well, so when I try to apply other transformations, such as (glTranslatef(0, 0, -10);
) that would before move the world closer to the camera, it now moves in a totally different direction.
How should I proceed to solve that?
Upvotes: 1
Views: 333
Reputation: 2576
First, you could use matrices to accumulate the transforms of your camera, just like OpenGL does.
But instead of using glRotate or glTransform, you could use a vector math library (i.e.: GLM) and apply analog operations (i.e.: glm::rotate, glm::translate) on a matrix variable (i.e: glm::mat4) you would call the 'local transforms' of your camera object.
Whenever you want to apply transforms on a different space (i.e.: (1,0,0)(0,1,0)(0,0,-1)) you just use a plain new matrix (just like glPopMatrix would do), something you would call 'world transform'.
At the end of each frame you would multiply both matrices (local * world) and use this result to feed OpenGL with a view matrix (i.e.: glMatrixMode(GL_MODELVIEW); glLoadMatrix(&(local * world)[0]);)
Oh, and don't forget to reset your 'world transform' matrix at the end of your frame.
Upvotes: 1