Cameras: the smart way?

category: general [glöplog]
Hi! I'm thinking about how to implement my cameras for my next 64k. Everyone knows the standard way of doing them: Take the camera position, view direction and up vector to create the view matrix, and then take the FoV, aspect ratio, near and far planes for the projection matrix.

I want to follow a different path so that I don't need the 3 vectors for the view matrix, instead only 2. I'd also like to avoid the gimbal lock problem.

I thought that I could represent the camera position with 3 floats, then the direction that the camera looks at with another 3 floats, and finally the roll angle along the view vector with another float, that makes 7 floats.

Does anybody know if that's sufficient information, and how to construct a view matrix out of those values? Any references would be gladly appreciated :)

added on the 2011-01-30 21:49:32 by xTr1m xTr1m
Why store the direction and roll in 4 floats? Dir vector and roll? Just store pitch, yaw, roll; that reduces it to 3.
added on the 2011-01-30 21:52:11 by ferris ferris
Ferris: That's equivalent to euler angles (http://en.wikipedia.org/wiki/Yaw,_pitch,_and_roll#Tait-Bryan_angles), which introduce singularities when two rotation planes become coplanar, aka gimbal lock. I'd like to avoid those :)
added on the 2011-01-30 21:55:14 by xTr1m xTr1m
I'd also like to avoid the gimbal lock problem.

Ah sorry, missed this :)
added on the 2011-01-30 21:56:29 by ferris ferris
Your proposed solution won't work either because your "roll angle" isn't defined. You'll need a reference for "where is roll angle 0" and as soon as you introduce any such reference, there's your gimbal lock problem again.

You can of course store the rotation in a quaternion and have 7 floats for the camera (3 pos, 4 rotation). Perfectly gimbal lock free when playing back; but in any way you'll have to devise a way to avoid gimbal lock when _creating_ the rotation.
added on the 2011-01-30 22:04:49 by kb_ kb_
up = cross(front, right);
added on the 2011-01-30 22:05:11 by xernobyl xernobyl
kb_: I was thinking of quaternions as well, I'm using my several-years-old math classes where I have a conversion from an unit quaternion to a rotation matrix. Theoretically speaking, I can represent the camera orientation with a quaternion, where I just specify a rotation axis and an angle to rotate from. These 4 values are represented by a quaternion. I'm just not an expert in quaternion maths.

Besides the inner workings of my camera system, I want to be able to freely change the camera parameters on design time, that is, change the cam position and orientation, and other values such as fov and dof.

Assuming I want to go the quaternion way, the initial quaternion would be (0, 0, 1, 0), that is 0 degrees rotation along the y-axis. Now when I want too "look around", I would need some logic to modify my quaternion so that the camera looks to the new direction. kb_: could you enlighten me in how to do this?

And since I plan on keyframe animations, I'd need a way to interpolate from all those values. I've heard about spherical linear interpolation (slerp) for quaternions, I'm not sure if that'd be necessary or if cubic hermite spline interpolation would suffice.
added on the 2011-01-31 10:24:27 by xTr1m xTr1m
xernobyl: In your case I'd need a "right" vector :)
added on the 2011-01-31 10:24:47 by xTr1m xTr1m
xTr1m: my experiences from 64ks is that storing orientations (which includes camera direction and roll) often can be best to do with the axis/angle representation. The axis-part needs limited precision, and the angle can support rotations above 360 degrees (unlike quaternions), so you can make long constant rotations with only two key-frames. You can convert the angle to a quaternion on the fly to be able to interpolate it smoothly.
added on the 2011-01-31 10:32:44 by kusma kusma