Sign in to follow this  
Pudutzki

Tranformation matrices and glTranslate/glRotate

Recommended Posts

Hello everybody,

long story short:

What if I translate and rotate a Mesh like this:
[source lang="java"]

glRotatef(mesh.getXrot(), 1.0f, 0.0f, 0.0f);
glRotatef(mesh.getYrot(), 0.0f, 1.0f, 0.0f);
glRotatef(mesh.getZrot(), 0.0f, 0.0f, 1.0f);

glTranslatef(mesh.getLocation().getX(), mesh.getLocation().getY(), mesh.getLocation().getZ());
[/source]
and want to translate/rotate it the opposite like this:

[source lang="java"]
glRotatef(-mesh.getXrot(), 1.0f, 0.0f, 0.0f);
glRotatef(-mesh.getYrot(), 0.0f, 1.0f, 0.0f);
glRotatef(-mesh.getZrot(), 0.0f, 0.0f, 1.0f);

glTranslatef(-mesh.getLocation().getX(), -mesh.getLocation().getY(), -mesh.getLocation().getZ());
[/source]

Consider, I have the mesh's transformation data in a 4x4 matrix, now I want to do the same with glMultMatrix.

How do I have to transform that matrix to achieve the same result?

Thanks in advance for any hint
Matthias

Share this post


Link to post
Share on other sites
[code]
Matrix mx, my, mz, mr;
mx.setToRotateX(mesh.getXrot());
my.setToRotateY(mesh.getYrot());
mz.setToRotateZ(mesh.getZrot());
mr = mz * my * mx;
mr[3][0] = mesh.getLocation().getX();
mr[3][1] = mesh.getLocation().getY();
mr[3][2] = mesh.getLocation().getZ();

glMultMatrixf( & mr[0][0] );
[/code]

[code]
Matrix mx, my, mz, mr;
mx.setToRotateX(-mesh.getXrot());
my.setToRotateY(-mesh.getYrot());
mz.setToRotateZ(-mesh.getZrot());
mr = mz * my * mx;
mr[3][0] = -mesh.getLocation().getX();
mr[3][1] = -mesh.getLocation().getY();
mr[3][2] = -mesh.getLocation().getZ();
glMultMatrixf( & mr[0][0] );

[/code] Edited by RobTheBloke

Share this post


Link to post
Share on other sites
Thank you for quick answer.

But, I think I have been misunderstood.

How do I transform a given 4x4 Matrix which does the same transform as
[source lang="java"]
glRotatef(mesh.getXrot(), 1.0f, 0.0f, 0.0f);
glRotatef(mesh.getYrot(), 0.0f, 1.0f, 0.0f);
glRotatef(mesh.getZrot(), 0.0f, 0.0f, 1.0f);

glTranslatef(mesh.getLocation().getX(), mesh.getLocation().getY(), mesh.getLocation().getZ());
[/source]
such that the transformation is done in the opposite direction?

Share this post


Link to post
Share on other sites
Currently, your matrix M is composed as follows:
M = R[sub]x[/sub] * R[sub]y[/sub] * R[sub]z[/sub] * T
What you now want (I assume) is the inverse transform. For a general matrix this is not completely trivial: [url="http://en.wikipedia.org/wiki/Matrix_inversion#Methods_of_matrix_inversion"]http://en.wikipedia....atrix_inversion[/url] although matrix libraries like GLM will generally have something implemented for this. However, in this special case we know more about the matrix:
M[sup]-1 [/sup]= (R[sub]x[/sub] * R[sub]y[/sub] * R[sub]z[/sub] * T)[sup]-1[/sup] = T[sup]-1[/sup] * R[sub]z[/sub][sup]-1[/sup] * R[sub]y[/sub][sup]-1[/sup] * R[sub]x[/sub][sup]-1[/sup]
So the inverse matrix can also be created as
[code]
glTranslatef(-mesh.getLocation().getX(), -mesh.getLocation().getY(), -mesh.getLocation().getZ());

glRotatef(-mesh.getZrot(), 0.0f, 0.0f, 1.0f);
glRotatef(-mesh.getYrot(), 0.0f, 1.0f, 0.0f);
glRotatef(-mesh.getXrot(), 1.0f, 0.0f, 0.0f);[/code] Edited by BitMaster

Share this post


Link to post
Share on other sites
Hi,

I am not sure about that, but I think, I do not need the "inverse transform".

I am referring to this thread: [url="http://www.gamedev.net/topic/628129-stencil-buffer-shadows-and-object-transformations/"]http://www.gamedev.n...ransformations/[/url]

There I use sort of fake light transform, to get the shadows calculated right.

Currently I use
[source lang="java"]
glRotatef(mesh.getXrot(), 1.0f, 0.0f, 0.0f);
glRotatef(mesh.getYrot(), 0.0f, 1.0f, 0.0f);
glRotatef(mesh.getZrot(), 0.0f, 0.0f, 1.0f);

glTranslatef(mesh.getLocation().getX(), mesh.getLocation().getY(), mesh.getLocation().getZ());
[/source]

to transform the mesh.

And to calculate the faked light position I use

[source lang="java"]
public Vector4f getFakeTransform(Mesh mesh) {

Vector4f lightPos = null;
Vector4f fakeLightPos = null;

glPushMatrix(); {

glLoadIdentity();

glRotatef(-mesh.getXrot(), 1.0f, 0.0f, 0.0f);
glRotatef(-mesh.getYrot(), 0.0f, 1.0f, 0.0f);
glRotatef(-mesh.getZrot(), 0.0f, 0.0f, 1.0f);

glTranslatef(-mesh.getLocation().getX(), -mesh.getLocation().getY(), -mesh.getLocation().getZ());

neg.clear();
glGetFloat(GL_MODELVIEW_MATRIX,neg);

lightPos = new Vector4f(location.x,location.y,location.z,1.0f);

modelView.load(neg);

fakeLightPos = Matrix4f.transform(modelView,lightPos,null);
}

glPopMatrix();

return fakeLightPos;
}
[/source]

inside the Light class.

Now I am using JBullet for physics and therefore get the mesh's transformation only as 4x4 matrix or quaternion.
Since I did not find a simple way to convert a rotation matrix or a quaternion to axis-angle form, I would like to switch the mesh transformation
to glMultMatrix. It is more readable either.

And the part with the negative translate and rotate inside the getFakeTransform method is currently driving me nuts. Edited by Pudutzki

Share this post


Link to post
Share on other sites

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