Tuesday, March 23, 2021

What is a Transformation Matrix


Homogeneous coordinates

Until then, we only considered 3D vertices as a (x,y,z) triplet. Let’s introduce w. We will now have (x,y,z,w) vectors.

This will be more clear soon, but for now, just remember this :

  • If w == 1, then the vector (x,y,z,1) is a position in space.
  • If w == 0, then the vector (x,y,z,0) is a direction.



An introduction to matrices


Simply put, a matrix is an array of numbers with a predefined number of rows and colums.


In 3D graphics we will mostly use 4x4 matrices. They will allow us to transform our (x,y,z,w) vertices. This is done by multiplying the vertex with the matrix :


Matrix x Vertex (in this order !!) = TransformedVertex


Translation matrices


These are the most simple tranformation matrices to understand. A translation matrix look like this :


1, 0, 0, X

0, 1, 0, Y

0, 0, 0, Z 

0, 0, 0, 1


where X,Y,Z are the values that you want to add to your position.


So if we want to translate the Vector (10,10,10,1) of 10 units in X direction, below is what to be done 


1,0,0,10

0,1,0,0

0,0,1,0

0,0,0,1 


X 


10,

10,

10,

1 


= 


1*10 + 0 * 10 + 0 * 10 + 10 *1 

0 * 10 + 1 * 10 + 0 * 10 + 0 * 1

0 * 10 + 0 * 0 + 1 * 0 + 0 * 0 

0 * 10 + 0 * 10 + 0 * 10 + 1 * 1 


= 


20

10,

10,

1 



The Identity matrix

This one is special. It doesn’t do anything. 


Scaling matrices

So if you want to scale a vector (position or direction, it doesn’t matter) by 2.0 in all directions :


2, 0, 0, 0 

0, 2, 0, 0

0, 0, 2, 0 

0, 0, 0, 1


X 


X

Y,

Z,

W 


= 


2 * X + 0 * Y + 0 * Z + 0 * W 

0 * X + 2 * Y + 0 * Z + 0 * W

0 * X + 0 * Y + 2 * Z + 0 * W

0 * X + 0 * Y + 0 * Z + 1 * W


= 


2 *X 

2 * Y 

2 * Z 

W 




References

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/




No comments:

Post a Comment