1 #include "mod_skeletal_animatevertices_generic.h"
9 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)
11 // vertex weighted skeletal
15 float12_t *boneposerelative;
17 const blendweights_t * RESTRICT weights;
19 if (!model->surfmesh.num_vertices)
22 if (!model->num_bones)
24 if (vertex3f) memcpy(vertex3f, model->surfmesh.data_vertex3f, model->surfmesh.num_vertices*sizeof(float[3]));
25 if (normal3f) memcpy(normal3f, model->surfmesh.data_normal3f, model->surfmesh.num_vertices*sizeof(float[3]));
26 if (svector3f) memcpy(svector3f, model->surfmesh.data_svector3f, model->surfmesh.num_vertices*sizeof(float[3]));
27 if (tvector3f) memcpy(tvector3f, model->surfmesh.data_tvector3f, model->surfmesh.num_vertices*sizeof(float[3]));
31 //unsigned long long ts = rdtsc();
32 bonepose = (float12_t *) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float12_t) * (model->num_bones*2 + model->surfmesh.num_blends));
33 boneposerelative = bonepose + model->num_bones;
35 if (skeleton && !skeleton->relativetransforms)
38 // interpolate matrices
41 for (i = 0;i < model->num_bones;i++)
43 Matrix4x4_ToArray12FloatD3D(&skeleton->relativetransforms[i], m);
44 if (model->data_bones[i].parent >= 0)
45 R_ConcatTransforms(bonepose[model->data_bones[i].parent].f, m, bonepose[i].f);
47 memcpy(bonepose[i].f, m, sizeof(m));
49 // create a relative deformation matrix to describe displacement
50 // from the base mesh, which is used by the actual weighting
51 R_ConcatTransforms(bonepose[i].f, model->data_baseboneposeinverse + i * 12, boneposerelative[i].f);
56 float originscale = model->num_posescale;
58 const short * RESTRICT pose6s;
60 for (i = 0;i < model->num_bones;i++)
62 memset(m, 0, sizeof(m));
63 for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
65 pose6s = model->data_poses6s + 6 * (frameblend[blends].subframe * model->num_bones + i);
66 lerp = frameblend[blends].lerp;
67 x = pose6s[3] * (1.0f / 32767.0f);
68 y = pose6s[4] * (1.0f / 32767.0f);
69 z = pose6s[5] * (1.0f / 32767.0f);
70 w = 1.0f - (x*x+y*y+z*z);
71 w = w > 0.0f ? -sqrt(w) : 0.0f;
72 m[ 0] += (1-2*(y*y+z*z)) * lerp;
73 m[ 1] += ( 2*(x*y-z*w)) * lerp;
74 m[ 2] += ( 2*(x*z+y*w)) * lerp;
75 m[ 3] += (pose6s[0] * originscale) * lerp;
76 m[ 4] += ( 2*(x*y+z*w)) * lerp;
77 m[ 5] += (1-2*(x*x+z*z)) * lerp;
78 m[ 6] += ( 2*(y*z-x*w)) * lerp;
79 m[ 7] += (pose6s[1] * originscale) * lerp;
80 m[ 8] += ( 2*(x*z-y*w)) * lerp;
81 m[ 9] += ( 2*(y*z+x*w)) * lerp;
82 m[10] += (1-2*(x*x+y*y)) * lerp;
83 m[11] += (pose6s[2] * originscale) * lerp;
86 VectorNormalize(m + 4);
87 VectorNormalize(m + 8);
88 if (i == r_skeletal_debugbone.integer)
89 m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
90 m[3] *= r_skeletal_debugtranslatex.value;
91 m[7] *= r_skeletal_debugtranslatey.value;
92 m[11] *= r_skeletal_debugtranslatez.value;
93 if (model->data_bones[i].parent >= 0)
94 R_ConcatTransforms(bonepose[model->data_bones[i].parent].f, m, bonepose[i].f);
96 memcpy(bonepose[i].f, m, sizeof(m));
97 // create a relative deformation matrix to describe displacement
98 // from the base mesh, which is used by the actual weighting
99 R_ConcatTransforms(bonepose[i].f, model->data_baseboneposeinverse + i * 12, boneposerelative[i].f);
103 // generate matrices for all blend combinations
104 weights = model->surfmesh.data_blendweights;
105 for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
107 float * RESTRICT b = boneposerelative[model->num_bones + i].f;
108 const float * RESTRICT m = boneposerelative[weights->index[0]].f;
109 float f = weights->influence[0] * (1.0f / 255.0f);
110 b[ 0] = f*m[ 0]; b[ 1] = f*m[ 1]; b[ 2] = f*m[ 2]; b[ 3] = f*m[ 3];
111 b[ 4] = f*m[ 4]; b[ 5] = f*m[ 5]; b[ 6] = f*m[ 6]; b[ 7] = f*m[ 7];
112 b[ 8] = f*m[ 8]; b[ 9] = f*m[ 9]; b[10] = f*m[10]; b[11] = f*m[11];
113 for (k = 1;k < 4 && weights->influence[k];k++)
115 m = boneposerelative[weights->index[k]].f;
116 f = weights->influence[k] * (1.0f / 255.0f);
117 b[ 0] += f*m[ 0]; b[ 1] += f*m[ 1]; b[ 2] += f*m[ 2]; b[ 3] += f*m[ 3];
118 b[ 4] += f*m[ 4]; b[ 5] += f*m[ 5]; b[ 6] += f*m[ 6]; b[ 7] += f*m[ 7];
119 b[ 8] += f*m[ 8]; b[ 9] += f*m[ 9]; b[10] += f*m[10]; b[11] += f*m[11];
123 #define LOAD_MATRIX_SCALAR() const float * RESTRICT m = boneposerelative[*b].f
125 #define LOAD_MATRIX3() \
127 #define LOAD_MATRIX4() \
130 #define TRANSFORM_POSITION_SCALAR(in, out) \
131 (out)[0] = ((in)[0] * m[0] + (in)[1] * m[1] + (in)[2] * m[ 2] + m[3]); \
132 (out)[1] = ((in)[0] * m[4] + (in)[1] * m[5] + (in)[2] * m[ 6] + m[7]); \
133 (out)[2] = ((in)[0] * m[8] + (in)[1] * m[9] + (in)[2] * m[10] + m[11]);
134 #define TRANSFORM_VECTOR_SCALAR(in, out) \
135 (out)[0] = ((in)[0] * m[0] + (in)[1] * m[1] + (in)[2] * m[ 2]); \
136 (out)[1] = ((in)[0] * m[4] + (in)[1] * m[5] + (in)[2] * m[ 6]); \
137 (out)[2] = ((in)[0] * m[8] + (in)[1] * m[9] + (in)[2] * m[10]);
139 #define TRANSFORM_POSITION(in, out) \
140 TRANSFORM_POSITION_SCALAR(in, out)
141 #define TRANSFORM_VECTOR(in, out) \
142 TRANSFORM_VECTOR_SCALAR(in, out)
144 // transform vertex attributes by blended matrices
147 const float * RESTRICT v = model->surfmesh.data_vertex3f;
148 const unsigned short * RESTRICT b = model->surfmesh.blends;
149 // special case common combinations of attributes to avoid repeated loading of matrices
152 const float * RESTRICT n = model->surfmesh.data_normal3f;
153 if (svector3f && tvector3f)
155 const float * RESTRICT sv = model->surfmesh.data_svector3f;
156 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
158 // Note that for SSE each iteration stores one element past end, so we break one vertex short
159 // and handle that with scalars in that case
160 for (i = 0; i < model->surfmesh.num_vertices; i++, v += 3, n += 3, sv += 3, tv += 3, b++,
161 vertex3f += 3, normal3f += 3, svector3f += 3, tvector3f += 3)
164 TRANSFORM_POSITION(v, vertex3f);
165 TRANSFORM_VECTOR(n, normal3f);
166 TRANSFORM_VECTOR(sv, svector3f);
167 TRANSFORM_VECTOR(tv, tvector3f);
173 for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, n += 3, b++, vertex3f += 3, normal3f += 3)
176 TRANSFORM_POSITION(v, vertex3f);
177 TRANSFORM_VECTOR(n, normal3f);
182 for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, b++, vertex3f += 3)
185 TRANSFORM_POSITION(v, vertex3f);
192 const float * RESTRICT n = model->surfmesh.data_normal3f;
193 const unsigned short * RESTRICT b = model->surfmesh.blends;
194 for (i = 0; i < model->surfmesh.num_vertices; i++, n += 3, b++, normal3f += 3)
197 TRANSFORM_VECTOR(n, normal3f);
203 const float * RESTRICT sv = model->surfmesh.data_svector3f;
204 const unsigned short * RESTRICT b = model->surfmesh.blends;
205 for (i = 0; i < model->surfmesh.num_vertices; i++, sv += 3, b++, svector3f += 3)
208 TRANSFORM_VECTOR(sv, svector3f);
214 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
215 const unsigned short * RESTRICT b = model->surfmesh.blends;
216 for (i = 0; i < model->surfmesh.num_vertices; i++, tv += 3, b++, tvector3f += 3)
219 TRANSFORM_VECTOR(tv, tvector3f);