• Advertisement
Sign in to follow this  

updating world matrix, identity needed?

This topic is 1670 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'm a bit confused when it comes to d3d9 matrix translation, rotation and scaling.

My assumption:

 

- when I calculate a new matrix for translation/rotation/ scaling, I always first have to set the identity matrix

- this is because I'm passing absolute values when recalculating the matrix, not 'incremental/ changes'.

 

The strange this is that the results are always the same, when I do and when I don't set the identity matrix.

The code snippet is below.

 

What would you say?

bool CD3dmeshInst::UpdateWorldMatrix()
{
	if(!mDynamic) return false;
	if(!mIsMoved && !mIsRotated && !mIsScaled) return false;

	if(mIsMoved)
	{
//		D3DXMatrixIdentity(&mMatTranslate);
		D3DXMatrixTranslation(&mMatTranslate, mWorldPos.x, mWorldPos.y, mWorldPos.z);
		mIsMoved = false;
	}
	if(mIsRotated)
	{
//		D3DXMatrixIdentity(&mMatRotateX);
		D3DXMatrixRotationX(&mMatRotateX, D3DXToRadian(mRot.x));
//		D3DXMatrixIdentity(&mMatRotateY);
		D3DXMatrixRotationY(&mMatRotateY, D3DXToRadian(mRot.y));
//		D3DXMatrixIdentity(&mMatRotateZ);
		D3DXMatrixRotationZ(&mMatRotateZ, D3DXToRadian(mRot.z));
		mIsRotated = false;
	}
	if(mIsScaled)
	{
//		D3DXMatrixIdentity(&mMatScale);
		D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);
		mIsScaled = false;
	}
	mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ*mMatTranslate);

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

Edited by cozzie

Share this post


Link to post
Share on other sites
Advertisement

This function should compose matrix from scale, axis-angle rotation and translation vectors in one step instead of lots of matrix multiplies (and what not) you currently have.

#include <cmath>
 
D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX* pout, CONST D3DXVECTOR3* pscale, CONST D3DXVECTOR3* paxis, FLOAT angle, CONST D3DXVECTOR3* ptranslation)
{
    D3DXVECTOR3 v;
    D3DXVec3Normalize(&v, paxis);

    pout->m[0][0] = ((1.0f - std::cos(angle)) * v.x * v.x + std::cos(angle)) * pscale->x;
    pout->m[0][1] = ((1.0f - std::cos(angle)) * v.y * v.x + std::sin(angle) * v.z) * pscale->x;
    pout->m[0][2] = ((1.0f - std::cos(angle)) * v.z * v.x - std::sin(angle) * v.y) * pscale->x;
    pout->m[0][3] = 0.0f;

    pout->m[1][0] = ((1.0f - std::cos(angle)) * v.x * v.y - std::sin(angle) * v.z) * pscale->y;
    pout->m[1][1] = ((1.0f - std::cos(angle)) * v.y * v.y + std::cos(angle)) * pscale->y;
    pout->m[1][2] = ((1.0f - std::cos(angle)) * v.z * v.y + std::sin(angle) * v.x) * pscale->y;
    pout->m[1][3] = 0.0f;

    pout->m[2][0] = ((1.0f - std::cos(angle)) * v.x * v.z + std::sin(angle) * v.y) * pscale->z;
    pout->m[2][1] = ((1.0f - std::cos(angle)) * v.y * v.z - std::sin(angle) * v.x) * pscale->z;
    pout->m[2][2] = ((1.0f - std::cos(angle)) * v.z * v.z + std::cos(angle)) * pscale->z;
    pout->m[2][3] = 0.0f;

    pout->m[3][0] = ptranslation->x;
    pout->m[3][1] = ptranslation->y;
    pout->m[3][2] = ptranslation->z;
    pout->m[3][3] = 1.0f;

    return pout;
}

 

Do you even need scale? If so, is it uniform? If yes, you can skip inverse-transpose part.

Share this post


Link to post
Share on other sites


The strange this is that the results are always the same, when I do and when I don't set the identity matrix

 

As long as those functions don't care about the matrix that's passed in, and always set every component of the matrix on output, then of course it won't matter. Basically, a function like D3DXMatrixTranslation is just going to stomp every value in the matrix that's passed in. 

 

However, your code has conditionals in it. For instance, if mIsMoved is not true, you'll never set any value into mMatTranslate. So it will keep whatever value it had last time (I'm assuming based on the name that it's a member variable). So you have a bug if any of those conditionals is ever false. You should move all the D3DXMatrixIdentity calls outside of the conditionals so they are set every time.

Share this post


Link to post
Share on other sites
@Phil_t: thanks, I think you're right that the d3dxmatrix functions 'start from scratch'. What I dont understand in that case, is why one would need the d3dxmatrixidentity function. For now I'll just leave out the d3dxmatrixidentity calls (I execute them once when the world matrix is created initially). When a mesh instance is not moved etc, indeed the existing matrix is kept and used in calculating the end result world matrix.

@belfegor: thanks, I'll try it out, sounds good that it saves me a lot of matrix multiplications.
How can I pass in different rotation angles for x, y and z? (I now see one float for angle)
For now I need the inverse transpose just for my lighting shader, if I can change this I could skip it, because your right, I'm never scaling with different values for x/y/z.

Share this post


Link to post
Share on other sites
Ps; I think I know why I might need the d3dxmatrixidentity matrix.
In my case I dont, because I'm always doing a translation, but say I wanted only rotation as component in a (world)matrix, I would need to set translation to identity or not use the translation matrix in the end multiplication.
Makes sense?

Share this post


Link to post
Share on other sites

Yup, that's kind of what I tried to explain in the 2nd paragraph of my response.

Share this post


Link to post
Share on other sites

How can I pass in different rotation angles for x, y and z? (I now see one float for angle)

 

I think that this should work for euler angles:

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX *pout, CONST D3DXVECTOR3* pscale, FLOAT pitch, FLOAT yaw, FLOAT roll, CONST D3DXVECTOR3* ptranslation)
{
    pout->m[0][0] = std::cos(yaw) * std::cos(roll) * pscale->x;
    pout->m[0][1] = std::cos(yaw) * std::sin(roll) * pscale->x;
    pout->m[0][2] = -std::sin(yaw) * pscale->x;
    pout->m[0][3] = 0.0f;

    pout->m[1][0] = (((std::sin(pitch) * std::sin(yaw)) * std::cos(roll)) + (std::cos(pitch) * -std::sin(roll))) * pscale->y;
    pout->m[1][1] = (((std::sin(pitch) * std::sin(yaw)) * std::sin(roll)) + (std::cos(pitch) * std::cos(roll))) * pscale->y;
    pout->m[1][2] = std::sin(pitch) * std::cos(yaw) * pscale->y;
    pout->m[1][3] = 0.0f;

    pout->m[2][0] = (((std::cos(pitch) * std::sin(yaw)) * std::cos(roll)) + (-std::sin(pitch) * -std::sin(roll))) * pscale->z;
    pout->m[2][1] = (((std::cos(pitch) * std::sin(yaw)) * std::sin(roll)) + (-std::sin(pitch) * std::cos(roll))) * pscale->z;
    pout->m[2][2] = std::cos(pitch) * std::cos(yaw) * pscale->z;
    pout->m[2][3] = 0.0f;

    pout->m[3][0] = ptranslation->x;
    pout->m[3][1] = ptranslation->y;
    pout->m[3][2] = ptranslation->z;
    pout->m[3][3] = 1.0f;

    return pout;
}

And here is the version with quaternion rotation:

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX* pout, CONST D3DXVECTOR3* pscale, CONST D3DXQUATERNION* prot, CONST D3DXVECTOR3* ptranslation)
{
    pout->m[0][0] = (1.0f - 2.0f * (prot->y * prot->y + prot->z * prot->z)) * pscale->x;
    pout->m[0][1] = (2.0f * (prot->x *prot->y + prot->z * prot->w)) * pscale->x;
    pout->m[0][2] = (2.0f * (prot->x * prot->z - prot->y * prot->w)) * pscale->x;
    pout->m[0][3] = 0.0f;

    pout->m[1][0] = (2.0f * (prot->x * prot->y - prot->z * prot->w)) * pscale->y;
    pout->m[1][1] = (1.0f - 2.0f * (prot->x * prot->x + prot->z * prot->z)) * pscale->y;
    pout->m[1][2] = (2.0f * (prot->y *prot->z + prot->x * prot->w)) * pscale->y;
    pout->m[1][3] = 0.0f;

    pout->m[2][0] = (2.0f * (prot->x * prot->z + prot->y * prot->w)) * pscale->z;
    pout->m[2][1] = (2.0f * (prot->y * prot->z - prot->x * prot->w)) * pscale->z;
    pout->m[2][2] = (1.0f - 2.0f * (prot->x * prot->x + prot->y * prot->y)) * pscale->z;
    pout->m[2][3] = 0.0f;

    pout->m[3][0] = ptranslation->x;
    pout->m[3][1] = ptranslation->y;
    pout->m[3][2] = ptranslation->z;
    pout->m[3][3] = 1.0f;

    return pout;
}
Edited by belfegor

Share this post


Link to post
Share on other sites
Great, thanks.
I'll use the euler angle version and study it to understand :)

I didn't use quaternion rotations yet.

Share this post


Link to post
Share on other sites

@belfegor: I got it working, thanks.

One strange thing though, rotation around Z and Y where switched, yaw and pitch (either in the compose matrix function or in my engine/ models :))

 

It seems to work fine.

I assume I will now always have to create a new world matrix if either a mesh instance has moved, rotated or scaled?

(won't be better in performance to keep older matrices and check which of the three things has happened)

Share this post


Link to post
Share on other sites

It will simplify your code if you just create a new world matrix from scratch each time. Don't optimize unless you have a reason to believe this is a performance bottleneck.

Share this post


Link to post
Share on other sites

Thanks, got it in right away.

Strangely there's something weird with the X/Y and Z rotations, when I adapt the function as is, I read:

 

- Y rotation, Z rotation, X rotation

 

But I have to pass my rotation values, in this order for:

 

- X rotation, Y rotation, Z rotation

 

I have to get into that, and compare the result with before (when I used the separate rotation matrices).

Share this post


Link to post
Share on other sites

Well, you had before in this order:

mMatRotateX*mMatRotateY*mMatRotateZ

so i assumed it is what you want?

 


One strange thing though, rotation around Z and Y where switched, yaw and pitch...

I assume pitch = X, yaw = Y and roll = Z

Edited by belfegor

Share this post


Link to post
Share on other sites

Got it, sorry. Probably messed it up, while changing pitch/yaw/roll to X/Y/Z rot.

Thanks again.

 

It all looks good now, I see a little difference for a specific rotation of a mesh instance compared to the earlier lot of multiplications with the d3dx functions. I'll get into that and see what causes the difference.

 

This is now the cleaned up function, to update the world matrix for dynamic mesh instances:

bool CD3dmeshInst::UpdateWorldMatrix()
{
	if(!mDynamic) return false;
	if(!mIsMoved && !mIsRotated && !mIsScaled) return false;
	else
	{
		D3DXMatrixCompose(&mMatWorld, &D3DXVECTOR3(mScale, mScale, mScale), mRot.x, mRot.y, mRot.z, &mWorldPos);
	}

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

Later on I might remove the inverse transpose part, after changing my pixelshader/ lighting shader.

Share this post


Link to post
Share on other sites

Got it, stupid!!! I passed radians instead of degrees in the new improved function wacko.png

Edited by cozzie

Share this post


Link to post
Share on other sites

You should pass radians not degrees, like you did before with D3DXToRadian macro (if your mRot is in degrees):

D3DXMatrixCompose(&mMatWorld, &D3DXVECTOR3(mScale, mScale, mScale), D3DXToRadian(mRot.x), D3DXToRadian(mRot.y), D3DXToRadian(mRot.z), &mWorldPos);

Like Phil_t said, rebuild your matrix every time, there is no point to check if it is "moved".

Edited by belfegor

Share this post


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

  • Advertisement