0
votes

I'm following some tutorials to learn openGL (from www.opengl-tutorial.org if it makes any difference) and there is an exercise that asks me to draw a cube and a triangle on the screen and it says as a hint that I'm supposed to calculate two MVP-matrices, one for each object. MVP matrix is given by Projection*View*Model and as far as I understand, the projection and view matrices are the same for all the objects on the screen (they are only affected by my choice of "camera" location and settings). However, the model matrix should change since it's supposed to give me the coordinates and rotation of the object in the global coordinates. Following the tutorials, for my cube the model matrix is just the unit matrix since it is located at the origin and there's no rotation or scaling. Then I draw my triangle so that its vertices are at (2,2,0), (2,3,0) and (3,2,0). Now my question is, what is the model matrix for my triangle?

My own reasoning says that if I don't want to rotate or scale it, the model matrix should be just translation matrix. But what gives the translation coordinates here? Should it include the location of one of the vertices or the center of the triangle or what? Or have I completely misunderstood what the model matrix is?

1

1 Answers

5
votes

The model matrix is like the other matrices (projection, view) a 4x4 matrix with the same layout. Depending on whether you're using column or row vectors the matrix consists of the x,y,z axis of your local frame and a t1,t2,t3 vector specifying the translation part

so for a column vector p the transformation matrix (M) looks like

x1, x2, x3, t1,
y1, y2, y3, t2,
z1, z2, z3, t3,
 0,  0,  0,  1 

p' = M * p

so for row vectors you could try to find out how the matrix layout must be. Also note that if you have row vectors p' = p * M.

If you have no rotational component your local frame has the usual x,y,z axis as the rows of the 3x3 submatrix of the model matrix..

1 0 0 t1 -> x axis 
0 1 0 t2 -> y axis 
0 0 1 t3 -> z axis 
0 0 0 1 

the forth column specifies the translation vector (t1,t2,t3). If you have a point p =

 1, 
 0,
 0,
 1 

in a local coordinate system and you want it to translate +1 in z direction to place it in the world coordinate system the model matrix is simply:

1 0 0 0  
0 1 0 0  
0 0 1 1  
0 0 0 1 

p' = M * p .. p' is the transformed point in world coordinates.

For your example above you could already specify the triangle in (2,2,0), (2,3,0) and (3,2,0) in your local coordinate system. Then the model matrix is trivial. Otherwise you have to find out how you compute rotation etc.. I recommend reading the first few chapters of mathematics for 3d game programming and computer graphics. It's a very simple 3d math book, there you should get the minimal information you need to handle the most of the 3d graphics math.