|
Transformations are a very important part of Computer Graphics. If you understand it well, you can use it to create a lot of eye candy effects that later we talk about (like shadowing, billboards and...) how ever simplest use of transformations are in positioning and rotating the objects and the camera.
There are a lot of tutorials over the net about this topic, some of them just speak about the OpenGL commands and some just speak about the math, here I want to talk about both because you need to know both to be able to use transformations correctly, So put that piece of pizza over there and prepare yourself for a little math! You always hate it?! It"s just the start; you need to know a lot of math to be a game programmer!
OpenGL uses two 4x4 matrices to calculate the transformations: "Perspective" Matrix and "Model View" Matrix. When you specify a vertex, OpenGL multiplies these two matrices by the vertex position before storing the vertex in the frame buffer.
Wait a minute! You are confusing me! What are these matrices for?!
Well, let"s first talk about the projection matrix. The purpose of OpenGL (and of course 3D Computer Graphics) is to create 2D images from a specified 3D scene. In order to create this 2D picture we need to project all the vertices in the scene on a plane. Mathematicians found out a way for us to do this, and that is the Matrix. Hey I"m not talking about that Hollywood movie; a matrix is an N x M structure that the math guys use for a lot of things.
Yet another matrix we use and it"s "Model View" matrix. We use this matrix for positioning camera and the objects on the scene. For example here is a simple translation matrix that translates a vertex [a] units along X axis, [b] units along Y axis and [c] units along Z axis:
| 1 0 0 a |
| 0 1 0 b |
| 0 0 1 c |
| 0 0 0 1 | |
How can I use that matrix?!
Well, consider you have a 3D point P(3, 5, 2) and you want to move it 2 units along X axis and 3 units along Y axis, First you need to create your translation matrix with that formula it"s so easy just put a=2, b=3,c=0 then we have:
| 1 0 0 2 |
| 0 1 0 3 |
| 0 0 1 0 |
| 0 0 0 1 | |
Then we need create a matrix for our point to perform a multiplication, So we need a 4 x 1 matrix to be able to multiply it, and we have a three dimensional point so let the 4th parameter be 1 for now! (I don"t want to confuse you with the affine transformations for those of you who interested tell me to write a tutorial about!)
So it will be:
Then simply multiply those matrices to find out the transformed point:
| 1 0 0 2 | | 3 | | 5 |
| 0 1 0 3 | | 5 | | 8 |
| 0 0 1 0 | | 2 | = | 2 |
| 0 0 0 1 | | 1 | | 1 | |
As you can see the result is Q(5,8,2) and it"s obviously correct. There also some other matrices for rotation and scaling but don"t worry I"m not going to talk about it right now and you don"t need it for now because OpenGL will do these kinds of transformations for you. For example for the recent problem we don"t need to create that transformation matrix, OpenGL will create it simply by calling:
glLoadIdentity();
glTranslatef(2,3,0); |
After calling those commands every vertex that you specify with glVertex will be multiplies by that translation matrix and the result will be projected on the frame buffer later.
So let"s explain some OpenGL commands that we used in the code:
glMatrixMode(GLenum mode);
This command will select a matrix as the current matrix, can be GL_MODELVIEW, GL_PROJECTION or GL_TEXTURE (We will explain about GL_TEXTURE in later tutorials). So for example calling glMatrixMode(GL_MODELVIEW) causes that every call to the transformation commands apply to the Model View matrix until next glMatrixMode call.
glLoadIdentity();
This command loads the current matrix with the Identity matrix. You need to call this when you want to create a new transformation matrix otherwise the previous matrix will effect on your matrix
glTranslatef(GLfloat x, GLfloat y, GLfloat z);
This command multiplies current matrix by a translation matrix that translates a vertex [x] unit through X axis, [y] unit through Y axis and [z] unit through Z axis.
glRotatef(GLfloat angle, Glfloat x, GLfloat y, GLfloat z);
This command multiplies current matrix by a rotation matrix that rotates a vertex [angle] degrees about the vector V(x, y, z)
glScalef(GLfloat x, GLfloat y, GLfloat z);
This command multiplies current matrix by an scale matrix that scales the vertices [x] unit through X axis, [y] units through Y axis and [z] unit through Z axis
gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
GLU(OpenGL Utility Library) provides this command to easily create a projection matrix that sets up a frustum with specified values. Here is the explanation of those arguments:
fovy
The field of view angle, in degrees, in the y-direction.
aspect
The aspect ratio that determines the field of view in the x-direction. The aspect ratio is the ratio of x (width) to y (height).
zNear
The distance from the viewer to the near clipping plane (always positive).
zFar
The distance from the viewer to the far clipping plane (always positive).
I tried to explain the necessary things and you should practice your self to be able to use those commands to create the desired transformations. Now go on and let me know if you have any problem. I"m going to play the "God of War" now, so I should go!
|