handle no-bones case for skeletal animation
[xonotic/darkplaces.git] / mod_skeletal_animatevertices_generic.c
1 #include "mod_skeletal_animatevertices_generic.h"
2
3 typedef struct
4 {
5         float f[12];
6 }
7 float12_t;
8
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)
10 {
11         // vertex weighted skeletal
12         int i, k;
13         int blends;
14         float12_t *bonepose;
15         float12_t *boneposerelative;
16         float m[12];
17         const blendweights_t * RESTRICT weights;
18
19         if (!model->surfmesh.num_vertices)
20                 return;
21
22         if (!model->num_bones)
23         {
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]));
28                 return;
29         }
30
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;
34
35         if (skeleton && !skeleton->relativetransforms)
36                 skeleton = NULL;
37
38         // interpolate matrices
39         if (skeleton)
40         {
41                 for (i = 0;i < model->num_bones;i++)
42                 {
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);
46                         else
47                                 memcpy(bonepose[i].f, m, sizeof(m));
48
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);
52                 }
53         }
54         else
55         {
56                 float originscale = model->num_posescale;
57                 float x,y,z,w,lerp;
58                 const short * RESTRICT pose6s;
59
60                 for (i = 0;i < model->num_bones;i++)
61                 {
62                         memset(m, 0, sizeof(m));
63                         for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
64                         {
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;
84                         }
85                         VectorNormalize(m       );
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);
95                         else
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);
100                 }
101         }
102
103         // generate matrices for all blend combinations
104         weights = model->surfmesh.data_blendweights;
105         for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
106         {
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++)
114                 {
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];
120                 }
121         }
122
123 #define LOAD_MATRIX_SCALAR() const float * RESTRICT m = boneposerelative[*b].f
124
125 #define LOAD_MATRIX3() \
126         LOAD_MATRIX_SCALAR()
127 #define LOAD_MATRIX4() \
128         LOAD_MATRIX_SCALAR()
129
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]);
138
139 #define TRANSFORM_POSITION(in, out) \
140         TRANSFORM_POSITION_SCALAR(in, out)
141 #define TRANSFORM_VECTOR(in, out) \
142         TRANSFORM_VECTOR_SCALAR(in, out)
143
144         // transform vertex attributes by blended matrices
145         if (vertex3f)
146         {
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
150                 if (normal3f)
151                 {
152                         const float * RESTRICT n = model->surfmesh.data_normal3f;
153                         if (svector3f && tvector3f)
154                         {
155                                 const float * RESTRICT sv = model->surfmesh.data_svector3f;
156                                 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
157
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)
162                                 {
163                                         LOAD_MATRIX4();
164                                         TRANSFORM_POSITION(v, vertex3f);
165                                         TRANSFORM_VECTOR(n, normal3f);
166                                         TRANSFORM_VECTOR(sv, svector3f);
167                                         TRANSFORM_VECTOR(tv, tvector3f);
168                                 }
169
170                                 return;
171                         }
172
173                         for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, n += 3, b++, vertex3f += 3, normal3f += 3)
174                         {
175                                 LOAD_MATRIX4();
176                                 TRANSFORM_POSITION(v, vertex3f);
177                                 TRANSFORM_VECTOR(n, normal3f);
178                         }
179                 }
180                 else
181                 {
182                         for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, b++, vertex3f += 3)
183                         {
184                                 LOAD_MATRIX4();
185                                 TRANSFORM_POSITION(v, vertex3f);
186                         }
187                 }
188         }
189
190         else if (normal3f)
191         {
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)
195                 {
196                         LOAD_MATRIX3();
197                         TRANSFORM_VECTOR(n, normal3f);
198                 }
199         }
200
201         if (svector3f)
202         {
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)
206                 {
207                         LOAD_MATRIX3();
208                         TRANSFORM_VECTOR(sv, svector3f);
209                 }
210         }
211
212         if (tvector3f)
213         {
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)
217                 {
218                         LOAD_MATRIX3();
219                         TRANSFORM_VECTOR(tv, tvector3f);
220                 }
221         }
222 }