[PATCH] Implement D3DXQuaternionRotationMatrix with a test
David Adam
David.Adam at math.cnrs.fr
Tue Nov 20 07:56:15 CST 2007
---
dlls/d3dx8/d3dx8.spec | 2 +-
dlls/d3dx8/math.c | 51 ++++++++++++++++++++++++++++++++++++++++++++=
+++
dlls/d3dx8/tests/math.c | 33 ++++++++++++++++++++++++++++++
include/d3dx8math.h | 1 +
4 files changed, 86 insertions(+), 1 deletions(-)
diff --git a/dlls/d3dx8/d3dx8.spec b/dlls/d3dx8/d3dx8.spec
index d3b0ffa..4e7996a 100644
--- a/dlls/d3dx8/d3dx8.spec
+++ b/dlls/d3dx8/d3dx8.spec
@@ -50,7 +50,7 @@
@ stdcall D3DXMatrixShadow(ptr ptr ptr)
@ stdcall D3DXMatrixReflect(ptr ptr)
@ stdcall D3DXQuaternionToAxisAngle(ptr ptr ptr)
-@ stub D3DXQuaternionRotationMatrix
+@ stdcall D3DXQuaternionRotationMatrix(ptr ptr)
@ stdcall D3DXQuaternionRotationAxis(ptr ptr long)
@ stub D3DXQuaternionRotationYawPitchRoll
@ stdcall D3DXQuaternionMultiply(ptr ptr ptr)
diff --git a/dlls/d3dx8/math.c b/dlls/d3dx8/math.c
index adc100d..f1cbf22 100644
--- a/dlls/d3dx8/math.c
+++ b/dlls/d3dx8/math.c
@@ -655,6 +655,57 @@ D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQ=
UATERNION *pout, CONST D3
return pout;
}
=20
+D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, C=
ONST D3DXMATRIX *pm)
+{
+ int i, maxi;
+ FLOAT maxdiag, S, trace;
+
+ trace =3D pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
+ if ( trace > 0.0f)
+ {
+ pout->x =3D ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) )=
;
+ pout->y =3D ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) )=
;
+ pout->z =3D ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) )=
;
+ pout->w =3D sqrt(trace) / 2.0f;
+ return pout;
+ }
+ maxi =3D 0;
+ maxdiag =3D pm->u.m[0][0];
+ for (i=3D1; i<3; i++)
+ {
+ if ( pm->u.m[i][i] > maxdiag )
+ {
+ maxi =3D i;
+ maxdiag =3D pm->u.m[i][i];
+ }
+ }
+ switch( maxi )
+ {
+ case 0:
+ S =3D 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][=
2]);
+ pout->x =3D 0.25f * S;
+ pout->y =3D ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
+ pout->z =3D ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
+ pout->w =3D ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
+ break;
+ case 1:
+ S =3D 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][=
2]);
+ pout->x =3D ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
+ pout->y =3D 0.25f * S;
+ pout->z =3D ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
+ pout->w =3D ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
+ break;
+ case 2:
+ S =3D 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][=
1]);
+ pout->x =3D ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
+ pout->y =3D ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
+ pout->z =3D 0.25f * S;
+ pout->w =3D ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
+ break;
+ }
+ return pout;
+}
+
D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DX=
QUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
{
FLOAT dot, epsilon;
diff --git a/dlls/d3dx8/tests/math.c b/dlls/d3dx8/tests/math.c
index 589bb2a..6a63fd8 100644
--- a/dlls/d3dx8/tests/math.c
+++ b/dlls/d3dx8/tests/math.c
@@ -552,6 +552,7 @@ static void D3DXPlaneTest(void)
=20
static void D3X8QuaternionTest(void)
{
+ D3DXMATRIX mat;
D3DXQUATERNION expectedquat, gotquat, Nq, nul, q, r, s, t, u;
LPD3DXQUATERNION funcpointer;
D3DXVECTOR3 axis, expectedvec;
@@ -668,6 +669,38 @@ static void D3X8QuaternionTest(void)
D3DXQuaternionRotationAxis(&gotquat,&axis,angle);
expect_vec4(expectedquat,gotquat);
=20
+/*_______________D3DXQuaternionRotationMatrix___________________*/
+ /* test when the trace is >0 */
+ U(mat).m[0][1] =3D 5.0f; U(mat).m[0][2] =3D 7.0f; U(mat).m[0][3] =3D 8.=
0f;
+ U(mat).m[1][0] =3D 11.0f; U(mat).m[1][2] =3D 16.0f; U(mat).m[1][3] =3D =
33.0f;
+ U(mat).m[2][0] =3D 19.0f; U(mat).m[2][1] =3D -21.0f; U(mat).m[2][3] =3D=
43.0f;
+ U(mat).m[3][0] =3D 2.0f; U(mat).m[3][1] =3D 3.0f; U(mat).m[3][2] =3D -4=
.0f;
+ U(mat).m[0][0] =3D 10.0f; U(mat).m[1][1] =3D 20.0f; U(mat).m[2][2] =3D =
30.0f;
+ U(mat).m[3][3] =3D 48.0f;
+ expectedquat.x =3D 2.368682f; expectedquat.y =3D 0.768221f; expectedqua=
t.z =3D -0.384111f; expectedquat.w =3D 3.905125f;
+ D3DXQuaternionRotationMatrix(&gotquat,&mat);
+ expect_vec4(expectedquat,gotquat);
+ /* test the case when the greater element is (2,2) */
+ U(mat).m[0][1] =3D 5.0f; U(mat).m[0][2] =3D 7.0f; U(mat).m[0][3] =3D 8.=
0f;
+ U(mat).m[1][0] =3D 11.0f; U(mat).m[1][2] =3D 16.0f; U(mat).m[1][3] =3D =
33.0f;
+ U(mat).m[2][0] =3D 19.0f; U(mat).m[2][1] =3D -21.0f; U(mat).m[2][3] =3D=
43.0f;
+ U(mat).m[3][0] =3D 2.0f; U(mat).m[3][1] =3D 3.0f; U(mat).m[3][2] =3D -4=
.0f;
+ U(mat).m[0][0] =3D -10.0f; U(mat).m[1][1] =3D -60.0f; U(mat).m[2][2] =
=3D 40.0f;
+ U(mat).m[3][3] =3D 48.0f;
+ expectedquat.x =3D 1.233905f; expectedquat.y =3D -0.237290f; expectedqu=
at.z =3D 5.267827f; expectedquat.w =3D -0.284747f;
+ D3DXQuaternionRotationMatrix(&gotquat,&mat);
+ expect_vec4(expectedquat,gotquat);
+ /* test the case when the greater element is (1,1) */
+ U(mat).m[0][1] =3D 5.0f; U(mat).m[0][2] =3D 7.0f; U(mat).m[0][3] =3D 8.=
0f;
+ U(mat).m[1][0] =3D 11.0f; U(mat).m[1][2] =3D 16.0f; U(mat).m[1][3] =3D =
33.0f;
+ U(mat).m[2][0] =3D 19.0f; U(mat).m[2][1] =3D -21.0f; U(mat).m[2][3] =3D=
43.0f;
+ U(mat).m[3][0] =3D 2.0f; U(mat).m[3][1] =3D 3.0f; U(mat).m[3][2] =3D -4=
.0f;
+ U(mat).m[0][0] =3D -10.0f; U(mat).m[1][1] =3D 60.0f; U(mat).m[2][2] =3D=
-80.0f;
+ U(mat).m[3][3] =3D 48.0f;
+ expectedquat.x =3D 0.651031f; expectedquat.y =3D 6.144103f; expectedqua=
t.z =3D -0.203447f; expectedquat.w =3D 0.488273f;
+ D3DXQuaternionRotationMatrix(&gotquat,&mat);
+ expect_vec4(expectedquat,gotquat);
+
/*_______________D3DXQuaternionSlerp________________________*/
expectedquat.x =3D -0.2f; expectedquat.y =3D 2.6f; expectedquat.z =3D 1=
.3f; expectedquat.w =3D 9.1f;
D3DXQuaternionSlerp(&gotquat,&q,&r,scale);
diff --git a/include/d3dx8math.h b/include/d3dx8math.h
index c5f18a2..f9bc777 100644
--- a/include/d3dx8math.h
+++ b/include/d3dx8math.h
@@ -307,6 +307,7 @@ D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERN=
ION *pout, CONST D3DXQUA
D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D=
3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2);
D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST =
D3DXQUATERNION *pq);
D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CON=
ST D3DXVECTOR3 *pv, FLOAT angle);
+D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, C=
ONST D3DXMATRIX *pm);
D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DX=
QUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t);
D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DX=
QUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST=
D3DXQUATERNION *pq4, FLOAT t);
void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3=
*paxis, FLOAT *pangle);
--=20
1.5.3.2
--=_7ghkke0n8q04--
More information about the wine-patches
mailing list