From 41029cc88c7592adc392f94b83120682cf3906d9 Mon Sep 17 00:00:00 2001 From: David Adam Date: Wed, 21 Nov 2007 14:34:32 +0100 Subject: [PATCH] d3dx8: Implement D3DXQuaternionLn. --- dlls/d3dx8/d3dx8.spec | 2 +- dlls/d3dx8/math.c | 28 ++++++++++++++++++++++++++++ dlls/d3dx8/tests/math.c | 20 ++++++++++++++++++-- include/d3dx8math.h | 1 + 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/dlls/d3dx8/d3dx8.spec b/dlls/d3dx8/d3dx8.spec index d3987918291..d65dce0b2d4 100644 --- a/dlls/d3dx8/d3dx8.spec +++ b/dlls/d3dx8/d3dx8.spec @@ -56,7 +56,7 @@ @ stdcall D3DXQuaternionMultiply(ptr ptr ptr) @ stdcall D3DXQuaternionNormalize(ptr ptr) @ stdcall D3DXQuaternionInverse(ptr ptr) -@ stub D3DXQuaternionLn +@ stdcall D3DXQuaternionLn(ptr ptr) @ stub D3DXQuaternionExp @ stdcall D3DXQuaternionSlerp(ptr ptr ptr long) @ stdcall D3DXQuaternionSquad(ptr ptr ptr ptr ptr long) diff --git a/dlls/d3dx8/math.c b/dlls/d3dx8/math.c index aa8664fb8b0..19d6a9ff7d0 100644 --- a/dlls/d3dx8/math.c +++ b/dlls/d3dx8/math.c @@ -612,6 +612,34 @@ D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUA return pout; } +D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq) +{ + FLOAT norm, normvec, theta; + + norm = D3DXQuaternionLengthSq(pq); + if ( norm > 1.0001f ) + { + pout->x = pq->x; + pout->y = pq->y; + pout->z = pq->z; + pout->w = 0.0f; + } + else if( norm > 0.99999f) + { + normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z ); + theta = atan2(normvec, pq->w) / normvec; + pout->x = theta * pq->x; + pout->y = theta * pq->y; + pout->z = theta * pq->z; + pout->w = 0.0f; + } + else + { + FIXME("The quaternion (%f, %f, %f, %f) has a norm <1. This should not happen. Windows returns a result anyway. This case is not implemented yet.\n", pq->x, pq->y, pq->z, pq->w); + } + return pout; +} + D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2) { pout->x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y; diff --git a/dlls/d3dx8/tests/math.c b/dlls/d3dx8/tests/math.c index 950bc5eb560..6dfe49cc191 100644 --- a/dlls/d3dx8/tests/math.c +++ b/dlls/d3dx8/tests/math.c @@ -553,7 +553,7 @@ static void D3DXPlaneTest(void) static void D3X8QuaternionTest(void) { D3DXMATRIX mat; - D3DXQUATERNION expectedquat, gotquat, Nq, nul, q, r, s, t, u; + D3DXQUATERNION expectedquat, gotquat, Nq, Nq1, nul, q, r, s, t, u; LPD3DXQUATERNION funcpointer; D3DXVECTOR3 axis, expectedvec; FLOAT angle, expected, got, scale, scale2; @@ -643,6 +643,23 @@ static void D3X8QuaternionTest(void) got = D3DXQuaternionLengthSq(NULL); ok(fabs( got - expected ) < admitted_error, "Expected: %f, Got: %f\n", expected, got); +/*_______________D3DXQuaternionLn______________________________*/ + expectedquat.x = 1.0f; expectedquat.y = 2.0f; expectedquat.z = 4.0f; expectedquat.w = 0.0f; + D3DXQuaternionLn(&gotquat,&q); + expect_vec4(expectedquat,gotquat); + expectedquat.x = -3.0f; expectedquat.y = 4.0f; expectedquat.z = -5.0f; expectedquat.w = 0.0f; + D3DXQuaternionLn(&gotquat,&r); + expect_vec4(expectedquat,gotquat); + Nq.x = 1.0f/11.0f; Nq.y = 2.0f/11.0f; Nq.z = 4.0f/11.0f; Nq.w=10.0f/11.0f; + expectedquat.x = 0.093768f; expectedquat.y = 0.187536f; expectedquat.z = 0.375073f; expectedquat.w = 0.0f; + D3DXQuaternionLn(&gotquat,&Nq); + expect_vec4(expectedquat,gotquat); + /* Test the cas where the norm of the quaternion is <1 */ + Nq1.x = 0.2f; Nq1.y = 0.1f; Nq1.z = 0.3; Nq1.w= 0.9f; + expectedquat.x = 0.206945f; expectedquat.y = 0.103473f; expectedquat.z = 0.310418f; expectedquat.w = 0.0f; + D3DXQuaternionLn(&gotquat,&Nq1); + todo_wine{ expect_vec4(expectedquat,gotquat) }; + /*_______________D3DXQuaternionMultiply________________________*/ expectedquat.x = 3.0f; expectedquat.y = 61.0f; expectedquat.z = -32.0f; expectedquat.w = 85.0f; D3DXQuaternionMultiply(&gotquat,&q,&r); @@ -706,7 +723,6 @@ static void D3X8QuaternionTest(void) D3DXQuaternionRotationYawPitchRoll(&gotquat,D3DX_PI/4.0f,D3DX_PI/11.0f,D3DX_PI/3.0f); expect_vec4(expectedquat,gotquat); - /*_______________D3DXQuaternionSlerp________________________*/ expectedquat.x = -0.2f; expectedquat.y = 2.6f; expectedquat.z = 1.3f; expectedquat.w = 9.1f; D3DXQuaternionSlerp(&gotquat,&q,&r,scale); diff --git a/include/d3dx8math.h b/include/d3dx8math.h index 5ef85b42054..6c5f18f69fd 100644 --- a/include/d3dx8math.h +++ b/include/d3dx8math.h @@ -304,6 +304,7 @@ D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, C D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g); D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq); +D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq); D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2); D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq); D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle); -- 2.11.4.GIT