Cameras

Cameras are needed in the 3d world to view the objects. They are needed if we want to move around in the world, and they are needed for many other things too. But nomatter what, we nearly always need cameras when we do 3d programming. Cameras can have many functions like zoom, targeting, field of view, eg. But we just need the basic routines. To start with, we need martices and everything we do, will be about matrices. If you do not know how matrices work, please visit the math cornor where they are described. Now, lets look at some of the features we get with cameras. When we include camera in our code we can make a "walkable" 3d world. With that I mean we can use e.g the cursor keys to move forward and to the sides, look up and down, and roll. This is what many 3d programmers wish to do, since this gives the most impressive results. But all we need to know to do all this is very simple, and we will end up with one simple matrix, which should do the trick. But for now, lets take a look at cameras, and how they are defined.
Any camera can be defined by a position and the angles yaw, pitch and roll, where yaw is the angle around y, pitch around x and roll around z. with the position we know where the camera is and with the angles we know in which direction it's looking. KNow all we have to do with all the vertices in our world is to translate them from WCS (world coordinate system) to CCS (camera coordinate system). This is done by first moving all the vertices, so they match the camera position. To put this in a matrix it looks like this:

   [x'] [1 0 0 -camerax] [x]

   [y'] [0 1 0 -cameray] [y]

   [z'] [0 0 1 -cameraz] [z]

   [ 1] [0 0 0     1   ] [1]

Now we have moved all the vertices in the WCC to the CCS, but we still have to think of the rotation angles. Therefore we make three new matrices, each rotating around an axis:

Around yaw - the y axis

   [x'] [cos(-yaw)        0           sin(-yaw)] [x]

   [y'] [   0             1              0     ] [y]

   [z'] [-sin(-yaw)       0           cos(-yaw)] [z]

Around pitch - the x axis

   [x'] [   1             0              0     ] [x]

   [y'] [   0        cos(-pitch)   -sin(-pitch)] [y]

   [z'] [   0        sin(-pitch)    cos(-pitch)] [z]

Around roll - the z axis

   [x'] [cos(-roll)  -sin(-roll)         0     ] [x]

   [y'] [sin(-roll)   cos(-roll)         0     ] [y]

   [z'] [   0             0              1     ] [z]

Now as with the rotation matrix, we know that:

-sin(angle) = sin(-angle)

cos(angle)  = cos(-angle)

and therefore we can change the above matrices to the following:
Around yaw - the y axis

   [x'] [cos(yaw)         0           -sin(yaw)] [x]

   [y'] [   0             1              0     ] [y]

   [z'] [sin(yaw)         0            cos(yaw)] [z]

Around pitch - the x axis

   [x'] [   1             0              0     ] [x]

   [y'] [   0        cos(pitch)      sin(pitch)] [y]

   [z'] [   0        -sin(pitch)     cos(pitch)] [z]

Around roll - the z axis

   [x'] [cos(roll)     sin(roll)         0     ] [x]

   [y'] [-sin(roll)    cos(roll)         0     ] [y]

   [z'] [   0             0              1     ] [z]

These rotation matrices we know from ordinary rotations, where we also end up with one big matrix. Now, lets make that big matrix. If we calculate all the matrices together we would end up with the following matrix:

   [x'] [m11 m12 m13 m14] [x]

   [y'] [m21 m22 m23 m24] [y]

   [z'] [m31 m32 m33 m34] [z]

   [1'] [m41 m42 m43 m44] [1]


where:

   m11 = cos(yaw)sin(roll) + sin(yaw)sin(pitch)sin(roll)

   m12 = sin(pitch)sin(roll)

   m13 = -sin(yaw)cos(roll) + cos(yaw)sin(pitch)sin(roll)

   m14 = -(cx*m11 + cy*m12 + cz*m13)

   m21 = -cos(yaw)sin(roll) + sin(yaw)sin(pitch)cos(roll)

   m22 = cos(pitch)cos(roll)

   m23 = sin(yaw)sin(roll) + cos(yaw)sin(pitch)cos(roll)

   m24 = -(cx*m21 + cy*m22 + cz*m23)

   m31 = sin(yaw)cos(pitch)

   m32 = -sin(pitch)

   m33 = cos(yaw)cos(pitch)

   m34 = -(cx*m31 + cy*m32 + cz*m33)

   m41 = 0

   m42 = 0

   m43 = 0

   m44 = 1

As you can see there is very little diffrence from normal rotation to camera converting. Note that cx, cy and cz equals camera x, y and z position in space.