Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

JohnBolton

Member Since 03 Apr 2002
Offline Last Active Jul 06 2009 05:19 AM

#2817579 Matrix in d3d and Opengl?

Posted by JohnBolton on 14 December 2004 - 04:16 PM

There is some misleading (and wrong) information in these replies.

First of all, there is no reason why you can't use the D3DX matrix and matrix functions for OpenGL (though there isn't really a good reason to do it).

Matrices in D3D and OpenGL both functionally have the same layout in memory: Xx,Xy,Xz,Xw,Yx,Yy,Yz,Yw,Zx,Zy,Zz,Zw,Tx,Ty,Tz,Tw. X, Y, Z, and T are the basis vectors.

Since there is no difference between D3DX and OpenGL matrices in memory, if you have to use D3DXMatrixTranspose, you are probably doing something wrong.

OpenGL is right-handed and D3D is left-handed, but that is totally irrelevant to matrix operations. Matrix operations give the same result for right-handed and left-handed systems.

What is important is matching the order of operation in OpenGL to the order of the parameters in D3DX. In OpenGL, matrix operations are pre-concatenated. That is, operation M1 followed by M2 results in this: v' = M1M2v, which is D3DXMatrixMultiply( &M, &M2, &M1 ) in D3DX.

Now I will show you an example using your code sample.

TP = transformation matrix equivalent of glTranslatef(position[0],position[1],position[2])
TC = transformation matrix equivalent of glTranslatef(centerd[0],centerd[1],centerd[2])
RA = transformation matrix equivalent of glRotatef(rotateAngle,axis[0],axis[1],axis[2])
RS = transformation matrix equivalent of glRotatef(ScaleRotateAngle,Saxis[0],Saxis[1],Saxis[2])
S = transformation matrix equivalent of glScalef(scalef[0],scalef[1],scalef[2])
RS-1 = transformation matrix equivalent of glRotatef(0-ScaleRotateAngle,Saxis[0],Saxis[1],Saxis[2])
TC-1 = transformation matrix equivalent of glTranslatef(0-centerd[0],0-centerd[1],0-centerd[2])

If you expressed that code in column-major notation (used by OpenGL), you get this:

v' = TPTCRARSSRS-1TC-1v

The row-major (used by D3D) equivalent is this:

v' = vTC-1RS-1SRSRATCTP

The D3DX code equivalent is this:
D3DXTranslation( &TCI, -centerd );
D3DXRotationAxis( &RSI, Saxis, -ScaleRotateAngle );
D3DXScaling( &S, scalef );
D3DXRotationAxis( &RS, Saxis, ScaleRotateAngle );
D3DXRotationAxis( &RA, axis, rotateAngle );
D3DXTranslation( &TC, centerd );
D3DXTranslation( &TP, position );

D3DXMatrixIdentity( &M );
D3DXMatrixMultiply( &M, &TP, &M );
D3DXMatrixMultiply( &M, &TC, &M );
D3DXMatrixMultiply( &M, &RA, &M );
D3DXMatrixMultiply( &M, &RS, &M );
D3DXMatrixMultiply( &M, &S, &M );
D3DXMatrixMultiply( &M, &RSI, &M );
D3DXMatrixMultiply( &M, &TCI, &M );
glLoadMatrixf( &M );
... or ...
D3DXMatrixIdentity( &M );
D3DXMatrixMultiply( &M, &M, &TCI );
D3DXMatrixMultiply( &M, &M, &RSI );
D3DXMatrixMultiply( &M, &M, &S );
D3DXMatrixMultiply( &M, &M, &RS );
D3DXMatrixMultiply( &M, &M, &RA );
D3DXMatrixMultiply( &M, &M, &TC );
D3DXMatrixMultiply( &M, &M, &TP );
glLoadMatrixf( &M );


Notice how the order of the first D3DX implementation is exactly the same as in the OpenGL code. Notice how the difference in order between the two D3DX implementations simply depends on the order of the parameters. Notice how the values for tranlation, rotation and scale are the same.

[Edited by - JohnBolton on December 14, 2004 10:16:28 PM]


PARTNERS