Game Development Reference
In-Depth Information
Converting between the camera matrix and
view matrix
We calculate the view matrix by computing the inverse of the camera transformation
matrix, V =M -1 , as shown in the following code snippet:
var viewMatrix=mat4.create();
mat4.invert(viewMatrix, cameraMatrix);
If we have the view matrix, we can calculate the camera transformation matrix by
computing the inverse view matrix. Using the camera matrix, we can derive the
position and orientation of the camera, M=V -1 , as shown in the following code snippet:
mat4.invert(cameraMatrix, viewMatrix);
Now let's look at a simple function that we evolved to compute the view matrix:
var m = mat4.create();
mat4.inverse(m, cameraMatrix);
return m;
We do not recommend that you use the preceding code to
create the view matrix. However, the code is important as
we will use the explanation to calculate the model matrix
of stage objects. We have used a handy function in the
glMatrix library, the lookAt function to calculate the
view matrix. We will use this function in our game.
Using the lookAt function
In this section, we will understand how glMatrix calculates the view matrix from
the orthogonal vectors. Take a look at the following code:
var matView=mat4.create();
var lookAtPosition=vec3.create();
vec3.add(lookAtPosition, this.pos, this.dir);
mat4.lookAt(matView, this.pos, lookAtPosition, this.up);
The lookAt function takes three parameters and then computes our matView matrix
(view matrix). The first parameter is position that represents the position of the
camera, the second parameter is lookAtPosition that denotes the point in the world
scene that our camera is looking at, and the third parameter is the up vector. For a
generic camera, lookAtPosition is calculated by adding the position, this.pos ,
and the look vector of the camera, this.dir in our case.
 
Search WWH ::




Custom Search