# 3D camera implementation

I've been bashing my head against the concept of a 'camera' for my 3D world for some time now, and I'm not sure I'm doing this right anymore. Maybe I've been working on this for too long and have confused myself, or I have a fundamental mis-understanding of the topic. Therefor, I could use a refresher/help on the concept of a 3D camera object and how that is supposed to operate. I have a camera class with a position, an orientation (quaternion), and a derived transformation matrix. Currently, my draw loop looks something like this:

Start Frame
Mult current matrix with matrix: Camera.Transform.Inverse
Save current matrix
Render all objects
Restore matrix
End Frame


However, this makes the camera's position reflected about the origin (ie, the camera's position vector has negative values but DRAWS as if it had positive values) and modification of position and rotation are consequently negated from their expected values (to get closer to the origin from z=3, I must add...).

So here is my issue: am I using this correctly? How is a camera supposed to work? Say I want to billboard something, how would I use the camera's information to do this?

Additionally, the clipping plane suddenly cuts out my scene if I rotate too far in any direction and I'm not sure why. How can I fix/avoid this using camera information? Any help is much appreciated!

I see that you doesn't mention projection matrix in your code. Multiplication must go in this order: world * view * projection. Another order could work for special cases but they are not correct.

Position and orientation is not enough for view matrix. You have to specify UP vector.

Are you using some framework like Directx/opengl? Both of them contains lookAt function. If not, find specification and implement it by yourself. Here is directx implementation: http://msdn.microsoft.com/en-us/library/bb281710(v=VS.85).aspx.

Also double check if you are using same left handed or right handed coordinate system for all of your matrices.

• Hmmm. Your answer, while good for a new reader, doesn't really address my issue. Yes, I set the projection matrix before rendering (only once, because it doesn't change unless I switch cameras); yes, my camera is capable of calculating it's own forward, up, and left vectors by transforming the axis unit vectors by it's transformation matrix; I am using OpenGL but do not have access to GLU, so gluLookAt is not an option atm; besides, gluLookAt is good for focusing on certain objects, but I'm not sure it's appropriate for a free camera; PS, OpenGL means right-handed coordinates. – Grimless Dec 6 '10 at 3:58
• Ok i red you profile after posting and i told myself that my informations must be useless for you, because you seem to be pretty expirienced :). – Notabene Dec 6 '10 at 4:05
• Did you try not to perform inverse to your camera transformation matrix? – Notabene Dec 6 '10 at 4:09
• I did try to multiply by the normal (non-inverse) camera matrix, and it all seems to work just fine until I attempt to rotate my camera (using quaternion math). When I rotate the camera, my forward, up, and left vectors don't seem to work correctly anymore and my camera will only translate along world axes instead of camera axes. Really weird stuff. – Grimless Dec 6 '10 at 4:48
• Ok i think that you really should try to implement gluLookAt function. You can use it for "fly camera" very easily. Distance of lookAt-point is not important so you can compute values very easily. camPos is camPos, up is up. And lookat-point is yours forward vect+camera position. That should work. – Notabene Dec 6 '10 at 22:18

Are you clear on the inversing process? From a math standpoint, i mean.

If you were to manipulate a world space Matrix to represent the camera orientation and position, the matrix you send to the effect is the inverse of that. An inverted a matrix is not simply the result of negation of its components. There is more to it mathematically, even more than transposing. It seems like you probably already know this, and you may be using a framework method for inversing, but just making sure.

Typically, I store and manipulate a world space quat & position, which I visualize as a real world movie camera, bundle them into a world space Matrix, then invert that matrix properly and send the result off to the effect.

I use the world space version's component vectors for any needed local space transformations. Typically these values are different than simply negated inverted version's values.

• Yes, I understand matrix inversion and I believe I understand what such an inversion represents (local->world or world->local space). I suppose what I am trying to ask here is: How is a camera used in rendering a scene? Again, I think I have all of the necessary components to use it: position, orientation, transform, forward, up, left, maths for manipulating all of those. The problem is I'm confused on how to apply these things to make an intuitive camera object. – Grimless Dec 6 '10 at 20:03