use dual quaternions for frame blends
authoreihrul <eihrul@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 7 Feb 2012 17:13:12 +0000 (17:13 +0000)
committereihrul <eihrul@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 7 Feb 2012 17:13:12 +0000 (17:13 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11669 d7cf8633-e32d-0410-b094-e92efae38249

mod_skeletal_animatevertices_generic.c
mod_skeletal_animatevertices_sse.c

index fcaf4ed..8c5f7d0 100644 (file)
@@ -41,37 +41,54 @@ void Mod_Skeletal_AnimateVertices_Generic(const dp_model_t * RESTRICT model, con
        }
        else
        {
-               float originscale = model->num_posescale;
-               float x,y,z,w,lerp;
-               const short * RESTRICT pose7s;
-
+               float originscale = -model->num_posescale;
                for (i = 0;i < model->num_bones;i++)
                {
-                       memset(m, 0, sizeof(m));
-                       for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
+                   const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[0].subframe * model->num_bones + i);
+                       float lerp = frameblend[0].lerp,
+                               tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
+                               rx = pose7s[3] * lerp,
+                               ry = pose7s[4] * lerp,
+                               rz = pose7s[5] * lerp,
+                               rw = pose7s[6] * lerp,
+                               dx = tx*rw + ty*rz - tz*ry,
+                               dy = -tx*rz + ty*rw + tz*rx,
+                               dz = tx*ry - ty*rx + tz*rw,
+                               dw = -tx*rx - ty*ry - tz*rz,
+                               scale;
+                       for (blends = 1;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
                        {
-                               pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
-                               lerp = frameblend[blends].lerp;
-                               x = pose7s[3] * (1.0f / 32767.0f);
-                               y = pose7s[4] * (1.0f / 32767.0f);
-                               z = pose7s[5] * (1.0f / 32767.0f);
-                               w = pose7s[6] * (1.0f / 32767.0f);
-                               m[ 0] += (1-2*(y*y+z*z)) * lerp;
-                               m[ 1] += (  2*(x*y-z*w)) * lerp;
-                               m[ 2] += (  2*(x*z+y*w)) * lerp;
-                               m[ 3] += (pose7s[0] * originscale) * lerp;
-                               m[ 4] += (  2*(x*y+z*w)) * lerp;
-                               m[ 5] += (1-2*(x*x+z*z)) * lerp;
-                               m[ 6] += (  2*(y*z-x*w)) * lerp;
-                               m[ 7] += (pose7s[1] * originscale) * lerp;
-                               m[ 8] += (  2*(x*z-y*w)) * lerp;
-                               m[ 9] += (  2*(y*z+x*w)) * lerp;
-                               m[10] += (1-2*(x*x+y*y)) * lerp;
-                               m[11] += (pose7s[2] * originscale) * lerp;
+                               const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
+                               float lerp = frameblend[blends].lerp,
+                                       tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
+                                       qx = pose7s[3], qy = pose7s[4], qz = pose7s[5], qw = pose7s[6];
+                               if(rx*qx + ry*qy + rz*qz + rw*qw < 0) lerp = -lerp;
+                               qx *= lerp;
+                               qy *= lerp;
+                               qz *= lerp;
+                               qw *= lerp;
+                               rx += qx;
+                               ry += qy;
+                               rz += qz;
+                               rw += qw;
+                               dx += tx*qw + ty*qz - tz*qy;
+                               dy += -tx*qz + ty*qw + tz*qx;
+                               dz += tx*qy - ty*qx + tz*qw;
+                               dw += -tx*qx - ty*qy - tz*qz;
                        }
-                       VectorNormalize(m       );
-                       VectorNormalize(m + 4);
-                       VectorNormalize(m + 8);
+                       scale = 1.0f / (rx*rx + ry*ry + rz*rz + rw*rw);
+                       m[0] = scale*(rw*rw + rx*rx - ry*ry - rz*rz);
+                       m[1] = 2*scale*(rx*ry - rw*rz);
+                       m[2] = 2*scale*(rx*rz + rw*ry);
+                       m[3] = originscale*scale*(dw*rx - dx*rw + dy*rz - dz*ry);
+                       m[4] = 2*scale*(rx*ry + rw*rz);
+                       m[5] = scale*(rw*rw + ry*ry - rx*rx - rz*rz);
+                       m[6] = 2*scale*(ry*rz - rw*rx);
+                       m[7] = originscale*scale*(dw*ry - dx*rz - dy*rw + dz*rx);
+                       m[8] = 2*scale*(rx*rz - rw*ry);
+                       m[9] = 2*scale*(ry*rz + rw*rx);
+                       m[10] = scale*(rw*rw + rz*rz - rx*rx - ry*ry);
+                       m[11] = originscale*scale*(dw*rz + dx*ry - dy*rx - dz*rw);
                        if (i == r_skeletal_debugbone.integer)
                                m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
                        m[3] *= r_skeletal_debugtranslatex.value;
index e5db8d8..c6b33f4 100644 (file)
@@ -50,37 +50,54 @@ void Mod_Skeletal_AnimateVertices_SSE(const dp_model_t * RESTRICT model, const f
        }
        else
        {
-               float originscale = model->num_posescale;
-               float x,y,z,w,lerp;
-               const short * RESTRICT pose7s;
-
-               for (i = 0;i < model->num_bones;i++)
-               {
-                       memset(m, 0, sizeof(m));
-                       for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
-                       {
-                               pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
-                               lerp = frameblend[blends].lerp;
-                               x = pose7s[3] * (1.0f / 32767.0f);
-                               y = pose7s[4] * (1.0f / 32767.0f);
-                               z = pose7s[5] * (1.0f / 32767.0f);
-                               w = pose7s[6] * (1.0f / 32767.0f);
-                               m[ 0] += (1-2*(y*y+z*z)) * lerp;
-                               m[ 1] += (  2*(x*y-z*w)) * lerp;
-                               m[ 2] += (  2*(x*z+y*w)) * lerp;
-                               m[ 3] += (pose7s[0] * originscale) * lerp;
-                               m[ 4] += (  2*(x*y+z*w)) * lerp;
-                               m[ 5] += (1-2*(x*x+z*z)) * lerp;
-                               m[ 6] += (  2*(y*z-x*w)) * lerp;
-                               m[ 7] += (pose7s[1] * originscale) * lerp;
-                               m[ 8] += (  2*(x*z-y*w)) * lerp;
-                               m[ 9] += (  2*(y*z+x*w)) * lerp;
-                               m[10] += (1-2*(x*x+y*y)) * lerp;
-                               m[11] += (pose7s[2] * originscale) * lerp;
-                       }
-                       VectorNormalize(m       );
-                       VectorNormalize(m + 4);
-                       VectorNormalize(m + 8);
+        float originscale = -model->num_posescale;
+        for (i = 0;i < model->num_bones;i++)
+        {
+            const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[0].subframe * model->num_bones + i);
+            float lerp = frameblend[0].lerp,
+                tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
+                rx = pose7s[3] * lerp,
+                ry = pose7s[4] * lerp,
+                rz = pose7s[5] * lerp,
+                rw = pose7s[6] * lerp,
+                dx = tx*rw + ty*rz - tz*ry,
+                dy = -tx*rz + ty*rw + tz*rx,
+                dz = tx*ry - ty*rx + tz*rw,
+                dw = -tx*rx - ty*ry - tz*rz,
+                scale;
+            for (blends = 1;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
+            {
+                const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
+                float lerp = frameblend[blends].lerp,
+                    tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
+                    qx = pose7s[3], qy = pose7s[4], qz = pose7s[5], qw = pose7s[6];
+                if(rx*qx + ry*qy + rz*qz + rw*qw < 0) lerp = -lerp;
+                qx *= lerp;
+                qy *= lerp;
+                qz *= lerp;
+                qw *= lerp;
+                rx += qx;
+                ry += qy;
+                rz += qz;
+                rw += qw;
+                dx += tx*qw + ty*qz - tz*qy;
+                dy += -tx*qz + ty*qw + tz*qx;
+                dz += tx*qy - ty*qx + tz*qw;
+                dw += -tx*qx - ty*qy - tz*qz;
+            }
+            scale = 1.0f / (rx*rx + ry*ry + rz*rz + rw*rw);
+            m[0] = scale*(rw*rw + rx*rx - ry*ry - rz*rz);
+            m[1] = 2*scale*(rx*ry - rw*rz);
+            m[2] = 2*scale*(rx*rz + rw*ry);
+            m[3] = originscale*scale*(dw*rx - dx*rw + dy*rz - dz*ry);
+            m[4] = 2*scale*(rx*ry + rw*rz);
+            m[5] = scale*(rw*rw + ry*ry - rx*rx - rz*rz);
+            m[6] = 2*scale*(ry*rz - rw*rx);
+            m[7] = originscale*scale*(dw*ry - dx*rz - dy*rw + dz*rx);
+            m[8] = 2*scale*(rx*rz - rw*ry);
+            m[9] = 2*scale*(ry*rz + rw*rx);
+            m[10] = scale*(rw*rw + rz*rz - rx*rx - ry*ry);
+            m[11] = originscale*scale*(dw*rz + dx*ry - dy*rx - dz*rw);
                        if (i == r_skeletal_debugbone.integer)
                                m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
                        m[3] *= r_skeletal_debugtranslatex.value;