WHAT IS IT ? 
 LIGHT VERSION 
 
 



For many people, it seems quite difficult to set up and use a position-target based camera, even if the process is simple enough and it requires only some elementar knowledges in vector and matrix math. One can benefit from this type of viewing system by beeing able to track other objects, while moving the camera itself in the 3d world, etc... So, here are my experiences on UVN viewing systems.


What do we exactly need to include this type of camera in our 3D engines ? Well, the camera is defined by a camera struct which contains a position vector, a target vector and a roll angle. I myself use precalculated sin and cos tables of 256 degrees for faster trygonometric functions accessing. The following code is stripped from my old engine to precalculate the tables.

float sintable[256],costable[256];

for (i=0;i<256;i++) {
sintable[i] = sin(i*PI/128.0);
costable[i] = cos(i*PI/128.0);
}

Then, my camera structure is defined like this :

typedef struct {
Vector position;
Vector target;
int roll;
float matrix[4][4];
} Camera;

The Vector structure contains only x, y and z floats.


Ok. You may ask yourselves now what ? Now we should decide what functions do we actually need for the structure. Well, we should be able to set camera's position, target, roll angle and also generate a rotation matrix from our datas to transform the vertices from World Space to Camera Space.
For the setting functions we only need to set up the structure's members. Example given :

void SetCameraTarget (Camera *camera, float x, float y, float z) {
if (NULL != camera) {
camera->position.x = x;
camera->position.y = y;
camera->position.z = z;
}
}

The position and roll setting functions are similar. Ok, what are we up to next ? We're up to the big adventurous camera matrix generation.

Problem : Given these datas (position, target, roll angle), generate the camera matrix.

Solution :
1. generate a direction vector from our position and target vectors.This will be our N vector
2. find the dot product between the worldup vector (0,1,0) and our direction vector. We'll call it scale
3. substract from wordup vector (0,1,0) our vector scaled with the scale value we found out earlyer.This will be our V vector
4. Find the U vector with a simple crossproduct between the N and V vectors.
5. Construct a matrix from these vectors and pre-translate it with the negative position of the camera in world space.
6. Generate a Z rotation matrix with the inverse angle of your roll value and apply the transformation to the previous matrix.


Where's the code ? Ok, here's a sample of function GetCameraMatrix which takes as parameter a pointer to a Camera type and calculates the camera matrix. Note that this shouldn't be called every frame in the rendering pipeline, but only when modifying any parameters of the camera.

void GetCameraMatrix (Camera *cam) {

float trans[4][4], rollmat[4][4], scale;
Vector U, V, N, worldup = {0.0,1.0,0.0};

CreateTranslationMatrix (trans, -cam->position.x, -cam->position.y, -cam->position.z);

N.x = cam->target.x - cam->position.x;
N.y = cam->target.y - cam->position.y;
N.z = cam->target.z - cam->position.z;
VectorNormalize (&N);

scale = VectorDotProduct (&N, &worldup);
V.x = worldup.x - (scale*N.x);
V.y = worldup.y - (scale*N.y);
V.z = worldup.z - (scale*N.z);
VectorNormalize (&V);

U = VectorCrossProduct (&N,&V);
VectorNormalize (&U);

SetIndentityMatrix (cam->matrix);

cam->matrix[0][0] = U.x;
cam->matrix[0][1] = U.y;
cam->matrix[0][2] = U.z;

cam->matrix[1][0] = V.x;
cam->matrix[1][1] = V.y;
cam->matrix[1][2] = V.z;

cam->matrix[2][0] = N.x;
cam->matrix[2][1] = N.y;
cam->matrix[2][2] = N.z;

CreateRotationMatrix_Z (rollmat, ((256 - cam->roll) & 255));

MatrixMultiply (trans,cam->matrix);
MatrixCopy (trans,cam->matrix);
MatrixMul (cam->matrix, rollmat);

}

Please bear in mind the fact that all the code is written from my head so some errors may exist. Ok, now don't expect this code to compile as it is. If you want to use this in the present state, however, you will just need to rename some functions as in your engine and watch out for their parameters, too. Note that (256-cam->roll) & 255 in the CreateRotationMatrix_Z ? What are we doing there ? Well, we have the roll angle and we need a rotation matrix around Z axis with the negative angle of the roll angle. So, by 256-cam->roll we're just creating the 'negative' angle in our trygonometric system (remember that the system i defined has a complete roatation of 256 degrees). The & 255 is a trick to gain speed instead of using in that a % 256 modulo function.


Anything else ? Perhaps some litle explanations on the algorythm. A viewing system like the one i presented is called UVN viewing system. What does UVN mean, you ask ? Well, UVN are three vectors othogonals on each other that combined with a position in space, define a coordinate system. After all, camera transformation is just a change of coordinate system, vertices beeing brought from world space coordinates down to camera space. Imagine yourself a cube and the UVN vectors representing three edges sharing the same vertex. This is a coordinate system. Now move that cube and rotate it in whatever way you like. Observe that the three vectors never change their position relative to the point they all share, and that each edge is always othogonal on the other two. U defines actually the X axis, V the Y one and N the Z component.
However there are two degenerate cases which need to be handled, otherwise a crash is almost iminent. The degenerate cases apear when our direction vector which has been calculated substracting the camera position from it's target is (0,1,0) or (0,-1,0). Why ? Because that's when our scale variable becomes 1, so our V vector will become (0,0,0). In these situations (when looking directly up or down) it's possible to apear a divide by zero at the normalize function. And anyway, if you handle the division by zero in the normalize function, the camera matrix will be screwed up.


Soooo ... What to do about it ? Well, thankfully there actually exists a solution to this problem, and I read that this is the way 3D Studio handles it, too. Simply, after normalising the N vector, verify if it's x and z components are 0 (eg. if ((0.0 == N.x) && (0.0 == N.z)) ). If the expresion is true, rebuild the worldup vector after the following values : (-N.y, 0.0, 0.0). This should be enough.


Kewl, it works ! Now what ? Ummmmm ... now just play around with it :o). And if you have any comments regarding the document, mail me.


Happy coding !


 

 
OJ can send you all our news by email or give you an access to them using a data file.
You can access jobs, releases, music and/or generals news.

Two systems exist :
- ONDS : only a small .js file you can add on your own site (like chscene.net).
- ONDSpro : news by email (like NoError) or news by a data file like pouet.net

Contact us if you want to be in the scene news circle or if you need more information.