• Advertisement
Sign in to follow this  

Tranformation matrices and glTranslate/glRotate

This topic is 2030 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

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
Advertisement

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] );



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] );

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: http://en.wikipedia....atrix_inversion 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

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);
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: http://www.gamedev.n...ransformations/

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
Ok, works all fine now.

I was the inverse transform what I needed. Thanks for pointing that out.

Greetings
Matthias

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement