DirectX Math function confusion

Started by
6 comments, last by Narf the Mouse 12 years, 3 months ago
While I was going over some older DirectX code, I came by the functions D3DXVec3Transform and D3DXVec3TransformCoord. Upon looking up the descriptions for them, they seemed rather similar but I don't see what the difference between the two are. Can someone please explain what is the difference between D3DXVec3Transform and D3DXVec3TransformCoord are, along with the math behind them?
Advertisement
D3DXVec3Transform() assumes the input vector is a vector with a w coordinate of 0. D3DXVec3TransformCoord() assumes the input vector is a point with a w coordinate of 1. In somewhat simplified terms, basically it's the difference between using the translation part of the matrix or not.
I believe I somewhat understand it more now, do you (or anyone) happen to have an example of how the code behind those functions are impelemented? I feel like that will help me understand it more.
The matrices are 4x4, right? The difference between the two is if the matrix is multiplied by [x y z 0] or [x y z 1]. They're just normal matrix multiplications otherwise.
From trying to figure out the math, would this be the correct way to calculate them?


D3DXVECTOR3 Vec3Transform(D3DXVECTOR3 &vCoordinate, D3DXMATRIX &mTransform)
{
D3DXVECTOR4 vWorking;

vWorking.x = (vCoordinate.x * mTransform._11) + (vCoordinate.y * mTransform._21) + (vCoordinate.z * mTransform._31) + mTransform._41;
vWorking.y = (vCoordinate.x * mTransform._12) + (vCoordinate.y * mTransform._22) + (vCoordinate.z * mTransform._32) + mTransform._42;
vWorking.z = (vCoordinate.x * mTransform._13) + (vCoordinate.y * mTransform._23) + (vCoordinate.z * mTransform._33) + mTransform._43;

return D3DXVECTOR3(vWorking.x, vWorking.y, vWorking.z);
}

D3DXVECTOR3 Vec3TransformCoordinate(D3DXVECTOR3 &vCoordinate, D3DXMATRIX &mTransform)
{
D3DXVECTOR4 vWorking;

vWorking.x = (vCoordinate.x * mTransform._11) + (vCoordinate.y * mTransform._21) + (vCoordinate.z * mTransform._31) + mTransform._41;
vWorking.y = (vCoordinate.x * mTransform._12) + (vCoordinate.y * mTransform._22) + (vCoordinate.z * mTransform._32) + mTransform._42;
vWorking.z = (vCoordinate.x * mTransform._13) + (vCoordinate.y * mTransform._23) + (vCoordinate.z * mTransform._33) + mTransform._43;
vWorking.w = 1 / ((vCoordinate.x * mTransform._14) + (vCoordinate.y * mTransform._24) + (vCoordinate.z * mTransform._34) + mTransform._44);

return D3DXVECTOR3(vWorking.x * vWorking.w, vWorking.y * vWorking.w, vWorking.z * vWorking.w);
}
If row 1 is multiplied by X, row 2 by Y and row 3 by Z, then row 4? That's the thing to consider to fix the errors in that math.

If row 1 is multiplied by X, row 2 by Y and row 3 by Z, then row 4? That's the thing to consider to fix the errors in that math.


This is what it seems like I did there, what error are you refering to?

[quote name='Narf the Mouse' timestamp='1325206505' post='4897996']
If row 1 is multiplied by X, row 2 by Y and row 3 by Z, then row 4? That's the thing to consider to fix the errors in that math.


This is what it seems like I did there, what error are you refering to?
[/quote]
When transforming a Vector3 by a 4x4 matrix, you need an assumed W cordinate of either 1 or 0, depending on whether it's a cordinate transform or a direction transform, as discussed.

What happens with the matrix 41, 42 and 43 position changes in the first Transform function, when you multiply them by an assumed W cordinate of 0?

This topic is closed to new replies.

Advertisement