0

I'm translating a bit of code into javascript. I'm trying to replicate the functionality of

 D3DXVECTOR3* D3DXVec3TransformCoord(
 __inout  D3DXVECTOR3 *pOut,
 __in     const D3DXVECTOR3 *pV,
 __in     const D3DXMATRIX *pM
 );

This applies Mat4 transformation to the Vec3 (which would lead to an invalid mat4). How do I project the 4d matrix into the 3d matrix and perform the operation? In what order would you do the math? We can assume that W=1, as in the following post:

DirectX Math function confusion

Community
  • 1
  • 1
FlavorScape
  • 13,301
  • 12
  • 75
  • 117
  • Are you really asking how to transform a matrix by a vector? – Nicol Bolas Jan 25 '12 at 01:58
  • Look here: http://www.gamedev.net/topic/11906-d3dxvec3transformcoord/ – GManNickG Jan 25 '12 at 03:07
  • Well, between built in c++ and D3DXVec3Transform and D3DXVec3TransformCoord, I just want to be certain about what exact math they perform. You never know if its doing a little more or a little less than you expect based on a name. That link helps. Now I know where to find the internals. – FlavorScape Jan 26 '12 at 20:21
  • related question: https://stackoverflow.com/q/13690070/6908282 – Gangula Nov 18 '22 at 12:42

2 Answers2

1

The documentation for this function says "Transforms a 3D vector by a given matrix, projecting the result back into w = 1".

In other words the function extends the vector [x,y,z] to [x,y,z,1] and then performs the multiplication, truncating the result to three elements after the operation.

EDIT: According to this thread the operation is xA, i.e multiplication with a row vector on the left, which is equivalent to the homogeneous transformation: xM(0) + yM(1) + zM(2) + M(3), where M(i) is the i'th column of the matrix.

  • Is this the same as normal c++ Vec3 * Mat4? I see that operator getting used, but I wonder what it's actually doing. – FlavorScape Jan 26 '12 at 20:16
  • Mathematically Vec3 * Mat4 is not defined, the vector length has to match the number of rows in the matrix. If you take the w=1 "projection" into account then you should get the same result as a Vec4 * Mat4 routine. –  Jan 27 '12 at 10:00
0

Other than building it yourself you can also use an existing npm package called gl-matrix which has a transformMat4 function in the vec3.js module of the package.

OR

you can use the below function to do the transformation yourself

function transformMat4AG(out, a, m) {
    var x = a[0],
        y = a[1],
        z = a[2];
    var w = m[3] * x + m[7] * y + m[11] * z + m[15];
    w = w || 1.0;
    out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
    out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
    out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
    return out;
}
Gangula
  • 5,193
  • 4
  • 30
  • 59