]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
factor out skeletal animation transform generation so that it can be reused
authoreihrul <eihrul@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 26 Mar 2013 19:34:00 +0000 (19:34 +0000)
committerRudolf Polzer <divverent@xonotic.org>
Thu, 28 Mar 2013 15:41:37 +0000 (16:41 +0100)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11927 d7cf8633-e32d-0410-b094-e92efae38249
::stable-branch::merge=a86f2139beecdb548c90b37abab5132b985833c6

gl_rmain.c
mod_skeletal_animatevertices_generic.c
model_alias.c
model_alias.h

index d71f3b0ba610290a3a8e7c7a8d5153dfddd84607..ffb11d702c8f86790cc5c89a5e70f0f4837dae2c 100644 (file)
@@ -4873,103 +4873,11 @@ qboolean R_AnimCache_GetEntity(entity_render_t *ent, qboolean wantnormals, qbool
        if (r_gpuskeletal && model->num_bones > 0 && model->surfmesh.data_skeletalindex4ub)
        {
                // cache the skeleton so the vertex shader can use it
-               int i;
-               int blends;
-               const skeleton_t *skeleton = ent->skeleton;
-               const frameblend_t *frameblend = ent->frameblend;
-               float *boneposerelative;
-               float m[12];
-               static float bonepose[256][12];
                r_refdef.stats[r_stat_animcache_skeletal_count] += 1;
                r_refdef.stats[r_stat_animcache_skeletal_bones] += model->num_bones;
                r_refdef.stats[r_stat_animcache_skeletal_maxbones] = max(r_refdef.stats[r_stat_animcache_skeletal_maxbones], model->num_bones);
                ent->animcache_skeletaltransform3x4 = (float *)R_FrameData_Alloc(sizeof(float[3][4]) * model->num_bones);
-               boneposerelative = ent->animcache_skeletaltransform3x4;
-               if (skeleton && !skeleton->relativetransforms)
-                       skeleton = NULL;
-               // resolve hierarchy and make relative transforms (deforms) which the shader wants
-               if (skeleton)
-               {
-                       for (i = 0;i < model->num_bones;i++)
-                       {
-                               Matrix4x4_ToArray12FloatD3D(&skeleton->relativetransforms[i], m);
-                               if (model->data_bones[i].parent >= 0)
-                                       R_ConcatTransforms(bonepose[model->data_bones[i].parent], m, bonepose[i]);
-                               else
-                                       memcpy(bonepose[i], m, sizeof(m));
-
-                               // create a relative deformation matrix to describe displacement
-                               // from the base mesh, which is used by the actual weighting
-                               R_ConcatTransforms(bonepose[i], model->data_baseboneposeinverse + i * 12, boneposerelative + i * 12);
-                       }
-               }
-               else
-               {
-                       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, sx, sy, sz, sw;
-                               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);
-                               sx = rx * scale;
-                               sy = ry * scale;
-                               sz = rz * scale;
-                               sw = rw * scale;
-                               m[0] = sw*rw + sx*rx - sy*ry - sz*rz;
-                               m[1] = 2*(sx*ry - sw*rz);
-                               m[2] = 2*(sx*rz + sw*ry);
-                               m[3] = model->num_posescale*(dx*sw - dy*sz + dz*sy - dw*sx);
-                               m[4] = 2*(sx*ry + sw*rz);
-                               m[5] = sw*rw + sy*ry - sx*rx - sz*rz;
-                               m[6] = 2*(sy*rz - sw*rx);
-                               m[7] = model->num_posescale*(dx*sz + dy*sw - dz*sx - dw*sy);
-                               m[8] = 2*(sx*rz - sw*ry);
-                               m[9] = 2*(sy*rz + sw*rx);
-                               m[10] = sw*rw + sz*rz - sx*rx - sy*ry;
-                               m[11] = model->num_posescale*(dy*sx + dz*sw - dx*sy - dw*sz);
-                               if (i == r_skeletal_debugbone.integer)
-                                       m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
-                               m[3] *= r_skeletal_debugtranslatex.value;
-                               m[7] *= r_skeletal_debugtranslatey.value;
-                               m[11] *= r_skeletal_debugtranslatez.value;
-                               if (model->data_bones[i].parent >= 0)
-                                       R_ConcatTransforms(bonepose[model->data_bones[i].parent], m, bonepose[i]);
-                               else
-                                       memcpy(bonepose[i], m, sizeof(m));
-                               // create a relative deformation matrix to describe displacement
-                               // from the base mesh, which is used by the actual weighting
-                               R_ConcatTransforms(bonepose[i], model->data_baseboneposeinverse + i * 12, boneposerelative + i * 12);
-                       }
-               }
+               Mod_Skeletal_BuildTransforms(model, ent->frameblend, ent->skeleton, NULL, ent->animcache_skeletaltransform3x4); 
                // note: this can fail if the buffer is at the grow limit
                ent->animcache_skeletaltransform3x4size = sizeof(float[3][4]) * model->num_bones;
                ent->animcache_skeletaltransform3x4buffer = R_BufferData_Store(ent->animcache_skeletaltransform3x4size, ent->animcache_skeletaltransform3x4, R_BUFFERDATA_UNIFORM, &ent->animcache_skeletaltransform3x4offset, true);
index bc26811701a1184e7e5ed635061a1b83b2d06634..5c1045e460a700c611a60381fc36a9787b6b76a4 100644 (file)
 #include "mod_skeletal_animatevertices_generic.h"
 
-typedef struct
-{
-       float f[12];
-}
-float12_t;
-
 void Mod_Skeletal_AnimateVertices_Generic(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f)
 {
        // vertex weighted skeletal
        int i, k;
-       int blends;
-       float12_t *bonepose;
-       float12_t *boneposerelative;
-       float m[12];
+       float *bonepose;
+       float *boneposerelative;
        const blendweights_t * RESTRICT weights;
 
        //unsigned long long ts = rdtsc();
-       bonepose = (float12_t *) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float12_t) * (model->num_bones*2 + model->surfmesh.num_blends));
+       bonepose = (float *) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float[12]) * (model->num_bones*2 + model->surfmesh.num_blends));
        boneposerelative = bonepose + model->num_bones;
 
-       if (skeleton && !skeleton->relativetransforms)
-               skeleton = NULL;
-
-       // interpolate matrices
-       if (skeleton)
-       {
-               for (i = 0;i < model->num_bones;i++)
-               {
-                       Matrix4x4_ToArray12FloatD3D(&skeleton->relativetransforms[i], m);
-                       if (model->data_bones[i].parent >= 0)
-                               R_ConcatTransforms(bonepose[model->data_bones[i].parent].f, m, bonepose[i].f);
-                       else
-                               memcpy(bonepose[i].f, m, sizeof(m));
-
-                       // create a relative deformation matrix to describe displacement
-                       // from the base mesh, which is used by the actual weighting
-                       R_ConcatTransforms(bonepose[i].f, model->data_baseboneposeinverse + i * 12, boneposerelative[i].f);
-               }
-       }
-       else
-       {
-               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, sx, sy, sz, sw;
-                       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);
-                       sx = rx * scale;
-                       sy = ry * scale;
-                       sz = rz * scale;
-                       sw = rw * scale;
-                       m[0] = sw*rw + sx*rx - sy*ry - sz*rz;
-                       m[1] = 2*(sx*ry - sw*rz);
-                       m[2] = 2*(sx*rz + sw*ry);
-                       m[3] = model->num_posescale*(dx*sw - dy*sz + dz*sy - dw*sx);
-                       m[4] = 2*(sx*ry + sw*rz);
-                       m[5] = sw*rw + sy*ry - sx*rx - sz*rz;
-                       m[6] = 2*(sy*rz - sw*rx);
-                       m[7] = model->num_posescale*(dx*sz + dy*sw - dz*sx - dw*sy);
-                       m[8] = 2*(sx*rz - sw*ry);
-                       m[9] = 2*(sy*rz + sw*rx);
-                       m[10] = sw*rw + sz*rz - sx*rx - sy*ry;
-                       m[11] = model->num_posescale*(dy*sx + dz*sw - dx*sy - dw*sz);
-                       if (i == r_skeletal_debugbone.integer)
-                               m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
-                       m[3] *= r_skeletal_debugtranslatex.value;
-                       m[7] *= r_skeletal_debugtranslatey.value;
-                       m[11] *= r_skeletal_debugtranslatez.value;
-                       if (model->data_bones[i].parent >= 0)
-                               R_ConcatTransforms(bonepose[model->data_bones[i].parent].f, m, bonepose[i].f);
-                       else
-                               memcpy(bonepose[i].f, m, sizeof(m));
-                       // create a relative deformation matrix to describe displacement
-                       // from the base mesh, which is used by the actual weighting
-                       R_ConcatTransforms(bonepose[i].f, model->data_baseboneposeinverse + i * 12, boneposerelative[i].f);
-               }
-       }
+       Mod_Skeletal_BuildTransforms(model, frameblend, skeleton, bonepose, boneposerelative);
 
        // generate matrices for all blend combinations
        weights = model->surfmesh.data_blendweights;
        for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
        {
-               float * RESTRICT b = boneposerelative[model->num_bones + i].f;
-               const float * RESTRICT m = boneposerelative[weights->index[0]].f;
+               float * RESTRICT b = boneposerelative + 12 * (model->num_bones + i);
+               const float * RESTRICT m = boneposerelative + 12 * (unsigned int)weights->index[0];
                float f = weights->influence[0] * (1.0f / 255.0f);
                b[ 0] = f*m[ 0]; b[ 1] = f*m[ 1]; b[ 2] = f*m[ 2]; b[ 3] = f*m[ 3];
                b[ 4] = f*m[ 4]; b[ 5] = f*m[ 5]; b[ 6] = f*m[ 6]; b[ 7] = f*m[ 7];
                b[ 8] = f*m[ 8]; b[ 9] = f*m[ 9]; b[10] = f*m[10]; b[11] = f*m[11];
                for (k = 1;k < 4 && weights->influence[k];k++)
                {
-                       m = boneposerelative[weights->index[k]].f;
+                       m = boneposerelative + 12 * (unsigned int)weights->index[k];
                        f = weights->influence[k] * (1.0f / 255.0f);
                        b[ 0] += f*m[ 0]; b[ 1] += f*m[ 1]; b[ 2] += f*m[ 2]; b[ 3] += f*m[ 3];
                        b[ 4] += f*m[ 4]; b[ 5] += f*m[ 5]; b[ 6] += f*m[ 6]; b[ 7] += f*m[ 7];
@@ -127,7 +34,7 @@ void Mod_Skeletal_AnimateVertices_Generic(const dp_model_t * RESTRICT model, con
                }
        }
 
-#define LOAD_MATRIX_SCALAR() const float * RESTRICT m = boneposerelative[*b].f
+#define LOAD_MATRIX_SCALAR() const float * RESTRICT m = boneposerelative + 12 * (unsigned int)*b
 
 #define LOAD_MATRIX3() \
        LOAD_MATRIX_SCALAR()
index 981188ea714dd1d16318ac652f6ed2693ed5a500..ddcd849e63e487a838e3dcf4c836a0cbc5261567 100644 (file)
@@ -62,6 +62,104 @@ void *Mod_Skeletal_AnimateVertices_AllocBuffers(size_t nbytes)
        return Mod_Skeletal_AnimateVertices_bonepose;
 }
 
+void Mod_Skeletal_BuildTransforms(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT bonepose, float * RESTRICT boneposerelative)
+{
+       int i, blends;
+       float m[12];
+
+       if (!bonepose)
+               bonepose = (float * RESTRICT) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float[12]) * model->num_bones);
+               
+       if (skeleton && !skeleton->relativetransforms)
+               skeleton = NULL;
+
+       // interpolate matrices
+       if (skeleton)
+       {
+               for (i = 0;i < model->num_bones;i++)
+               {
+                       Matrix4x4_ToArray12FloatD3D(&skeleton->relativetransforms[i], m);
+                       if (model->data_bones[i].parent >= 0)
+                               R_ConcatTransforms(bonepose + model->data_bones[i].parent * 12, m, bonepose + i * 12);
+                       else
+                               memcpy(bonepose + i * 12, m, sizeof(m));
+
+                       // create a relative deformation matrix to describe displacement
+                       // from the base mesh, which is used by the actual weighting
+                       R_ConcatTransforms(bonepose + i * 12, model->data_baseboneposeinverse + i * 12, boneposerelative + i * 12);
+               }
+       }
+       else
+       {
+               for (i = 0;i < model->num_bones;i++)
+               {
+                       // blend by transform each quaternion/translation into a dual-quaternion first, then blending
+                       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, sx, sy, sz, sw;
+                       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;
+                       }
+                       // generate a matrix from the dual-quaternion, implicitly normalizing it in the process
+                       scale = 1.0f / (rx*rx + ry*ry + rz*rz + rw*rw);
+                       sx = rx * scale;
+                       sy = ry * scale;
+                       sz = rz * scale;
+                       sw = rw * scale;
+                       m[0] = sw*rw + sx*rx - sy*ry - sz*rz;
+                       m[1] = 2*(sx*ry - sw*rz);
+                       m[2] = 2*(sx*rz + sw*ry);
+                       m[3] = model->num_posescale*(dx*sw - dy*sz + dz*sy - dw*sx);
+                       m[4] = 2*(sx*ry + sw*rz);
+                       m[5] = sw*rw + sy*ry - sx*rx - sz*rz;
+                       m[6] = 2*(sy*rz - sw*rx);
+                       m[7] = model->num_posescale*(dx*sz + dy*sw - dz*sx - dw*sy);
+                       m[8] = 2*(sx*rz - sw*ry);
+                       m[9] = 2*(sy*rz + sw*rx);
+                       m[10] = sw*rw + sz*rz - sx*rx - sy*ry;
+                       m[11] = model->num_posescale*(dy*sx + dz*sw - dx*sy - dw*sz);
+                       if (i == r_skeletal_debugbone.integer)
+                               m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
+                       m[3] *= r_skeletal_debugtranslatex.value;
+                       m[7] *= r_skeletal_debugtranslatey.value;
+                       m[11] *= r_skeletal_debugtranslatez.value;
+                       if (model->data_bones[i].parent >= 0)
+                               R_ConcatTransforms(bonepose + model->data_bones[i].parent * 12, m, bonepose + i * 12);
+                       else
+                               memcpy(bonepose + i * 12, m, sizeof(m));
+                       // create a relative deformation matrix to describe displacement
+                       // from the base mesh, which is used by the actual weighting
+                       R_ConcatTransforms(bonepose + i * 12, model->data_baseboneposeinverse + i * 12, boneposerelative + i * 12);
+               }
+       }
+}
+
 static void Mod_Skeletal_AnimateVertices(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f)
 {
 
index ddf8e5c07bece1da1f2cdab49293623fcf9e0d99..61c1099a3a033c8833fd4e39fb6c8e3191bfcb4b 100644 (file)
@@ -242,7 +242,11 @@ extern cvar_t r_skeletal_debugtranslatex;
 extern cvar_t r_skeletal_debugtranslatey;
 extern cvar_t r_skeletal_debugtranslatez;
 
+struct model_s;
+struct frameblend_s;
+
 void *Mod_Skeletal_AnimateVertices_AllocBuffers(size_t nbytes);
+void Mod_Skeletal_BuildTransforms(const struct model_s * RESTRICT model, const struct frameblend_s * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT bonepose, float * RESTRICT boneposerelative);
 
 #endif