Sign in to follow this  

3D Transformations into Single Matrix

This topic is 4559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i am a beginner in 3D graphics and have learned about various matrix like rotation in X,Y & Z axis, scaling, translation, identity etc. I have also read that these various transformations can be put into a SINGLE MATRIX, which i don't know how to do it? Which one is ideal - doing each transformation one by one and draw to the screen or to convert all the transformations into one matrix and then do the drawing part. i have gone through many tutorials but not getting a clear understanding on this. Can anyone help. Thanks & Regards, Pramod

Share this post


Link to post
Share on other sites
Combining the transformations for one object into a single matrix is more optimal, because most device can transform the object's vertices for you using this single matrix.

Given three independent transformations, say Tt, Tr and Ts (you may say a translation, a rotation and a scaling matrix) you can combine them using matrix multiplication:

Tw = Ts * Tr * Tt

The obtained matrix Tw is usually called the world matrix for that object. This example assumed 4x4 transformation matrices in all cases. You should be able to find out how matrix multiplication works.

Greetz,

Illco

Matrix Multiplication
Matrix Math

Share this post


Link to post
Share on other sites
Rotation/scaling, etc, would normaly require 3x3 matrix. However, by using 4x4 matrix (which is normaly used by 3D APIs and hardware), you can create single matrix with multiple transformation. If you have for example rotation and tranlation, and wish to create one matrix, you just have to multiply these two matrices (matrix concatenatation).

R = rotation matrix
T = translation matrix

C = R*T

C - single matrix with all transformations

Order of multiplication IS important. Transformation by matrix from the left is perfomed first, then matrices to right. In above example, vertices (objects)
will be first rotated in and after then they will be translated.
Then just use C matrix for drawing part (single draw).

It is well explained in "Transforms" topic, "Concatenating Matrices" section in DirectX SDK docs.

Share this post


Link to post
Share on other sites
Here i have got the Matrix multiplication code:

void multiply(float a[4][4],float b[4][4], float c[4][4] ){
int i,j,k;
for( i=0; i<4; i++ ){
for( j=0; j<4; j++ ){
c[i][j] = 0;
for( k=0; k<4; k++ ){
c[i][j] += a[i][k] * b[k][j]
}
}
}
}
multiplies the 4×4 matrices A × B to produce C

But for example look at this Rotation matrix on X axis:
1 0 0 0 <- x' = x
0 cos (Ø) -sin (Ø) 0 <- y' = (cos Ø) * y - (sin Ø) * z
0 sin (Ø) cos (Ø) 0 <- z' = (sin Ø) * y + (cos Ø) * z
0 0 0 1 <- w = 1

here i have calculated the new values of x,y,z in x', y', z' as per the above formula. Now how can i use the matrix multiplication using the above C code since the rotated values are in x,y,z,w!!!?

Share this post


Link to post
Share on other sites
Actually, you can just skip the w. It's only there to make it possible to multiply with the matrix from what I understand. Have you seen this article? In the end of that article is an example of multiplying a matrix with a vertex.

Share this post


Link to post
Share on other sites
You need to multiply matrices, not matrix with point. Only when you get FINAL transformation matrix, multiply point (x,y,z,w) with it to get final transforem point (x',y',z',w').

P = (x,y,z,1) - untransformed point/vertex
A = 4x4 translation matrix
B = 4x4 rotation matrix

C = A*B
P' = P*C

Ok?
btw: you also need matrix*vector code, not just matrix*matrix code...

Share this post


Link to post
Share on other sites
it is somewhat clear now. In every tutorial i have seen that the examples are shown for multiplying between two matrices let say Matrix A * Matrix B and the results in Matrix C. This part is OK. But how do i multiply if i have 4 matrices let say for X-Axis Rotation, Y-Axis Rotation, Scaling and Translation.
The following code will only multiply two matrices at a time. How can this code be modified to take multiple matrix multiplications.

void multiply(float a[4][4],float b[4][4], float c[4][4])
{
int i,j,k;
for( i=0; i<4; i++ )
{
for( j=0; j<4; j++ )
{
c[i][j] = 0;
for( k=0; k<4; k++ )
{
c[i][j] += a[i][k] * b[k][j]
}
}
}
}

Share this post


Link to post
Share on other sites
Don`t change multiplication code, multiply multiple times instead [wink]
float a[4][4];
float b[4][4];
float c[4][4];
float d[4][4];
float e[4][4];
float t1[4][4];
float t2[4][4];


....
//e = a*b*c*d;
//t1=a*b
multiply(a,b,t1);
//t2=a*b*c
multiply(t1,c,t2);
//e=a*b*c*d
multiply(t2,d,e);

something like that...
Why do you write this code yourself??? Use some library...

Share this post


Link to post
Share on other sites
thanks bulma. now i clearly understood the concept and its application.

In the last reply u have said that the matrix * vector code is also required. Is it for making the faces (QUAD) of vertices? Any help on that.

[Edited by - tppramod on June 24, 2005 12:10:51 AM]

Share this post


Link to post
Share on other sites
You need code for this:

P' = P*C

C is your final matrix, but P is a vector (x,y,z,w). You need code/operator which multiplies this vector with matrix. It is basicaly the same code as matrix*matrix, but you have only one row on left side (i=0).

edit:
you don`t need this code if you use 3D API for drawing, because you just need to provide final transformation matrix, not to transform vertices yourself...

Share this post


Link to post
Share on other sites
ya balma i understood what u r saying. That is, after all the transformation are done using Matrix*Matrix multiplication the product Matrix has to be again multiplied by values of x,y,z,w to get a new position value for x',y',z',w'. Am I right?. This is what u called vector multiplication.

Regards,

pramod

Share this post


Link to post
Share on other sites

This topic is 4559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this