Advertisement

Order of transformation confuse

Started by January 30, 2016 07:58 AM
3 comments, last by Khatharr 8 years, 10 months ago

Im confuse, when you said scale rotate and translate does that mean like this


model = glm::translate(model, glm::vec3(1.0f)); 
model = glm::rotate(model, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.5f)); 

or like this


model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.5f)); 
model = glm::rotate(model, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::translate(model, glm::vec3(1.0f));

if my shader is like this


gl_Position = projection * view * model * vec4(position, 1.0f);

Opengl reads in reverse order so ,Is it the first one or the second one? just want to clarify this as the two output different results and I dont know which one is right and which one is wrong.

The matrix product is associative. That means you can put (decently) pairs of parentheses around the individual term at your desire. Now, when you choose parentheses so that you start with the product including the vector, e.g. like so for column vectors (i.e. the vector is on the right and the matrix on the left, typical for OpenGL)

M1 * M2 * ( M3 * v )

and continue to the outer terms, like so

= M1 * ( M2 * ( M3 * v ) )

then the parentheses tell you about the locality of the transformation. The wording for this is often "first apply M3, then M2, and then M1", although that wording is imprecise.
Notice that this works for row vectors as well
( ( v' * M3' ) * M2' )* M1'
So, if you want to apply scaling to the model, rotate the scaled model, and translate the rotated/scaled model,** and you use column vectors, then the formula with parentheses looks like
T * ( R * ( S * v ) )
what, obviously, corresponds to your 2nd variant of code.
**Notice please that I've chosen a wording here that is not as blurry as "first … then …" ;)

Opengl reads in reverse order ...

As said above, "order" without further context is misleading here. For example, you can calculate

( ( M1 * M2 ) * M3 ) * v

as well and get the same result.
Advertisement

Hi, Thanks,

But its weird really. I tried both of them and it seems that the second variant of code recognize the z-axis as the x-axis with the following vertices



std::vector<glm::vec3> wallPosition{
glm::vec3(2.0f, 0.0f, 0.0f),
glm::vec3(-2.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, -2.0f),
glm::vec3(0.0f, 0.0f, 2.0f)
};

The 1st variant of code

SRT.jpg

and the 2nd variant of code

TRS.jpg

Didnt change anything aside from the order of the transformation.

You could draw out both options on paper, where you do the different orders of actions.

This helps in illustrating whap happens (SRT, TSR etc.).

Here's a snippet of my 'old' engine code:


void CD3dmeshInst::CreateWorldMatrix()
{
	D3DXMatrixTranslation(&mMatTranslate, mWorldPos.x, mWorldPos.y, mWorldPos.z);
	D3DXMatrixRotationX(&mMatRotateX, D3DXToRadian(mRot.x));
	D3DXMatrixRotationY(&mMatRotateY, D3DXToRadian(mRot.y));
	D3DXMatrixRotationZ(&mMatRotateZ, D3DXToRadian(mRot.z));
	D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);

	mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ*mMatTranslate);

	D3DXMatrixInverse(&mMatWorldInv, NULL, &mMatWorld);
	D3DXMatrixTranspose(&mMatWorldInvTransp, &mMatWorldInv);
}

I believe it shouldn't matter if you're using OpenGL or Direct3D (not 100% sure though, if not, someone will correct me :))

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

A) Something is wrong with that mesh.
B) Desk check - do the math on paper to determine your desired matrix, then inspect the matrices being generated.
C) If you want to see which order is giving the correct transform you're going to need another object around the place to compare visually. For a simple box like this you're going to get (should get, anyway) a box from either transform.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement