convert pose6s data to pose7s data to resolve quaternion W coordinate precision issues
[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         //unsigned long long ts = rdtsc();
20         bonepose = (float12_t *) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float12_t) * (model->num_bones*2 + model->surfmesh.num_blends));
21         boneposerelative = bonepose + model->num_bones;
22
23         if (skeleton && !skeleton->relativetransforms)
24                 skeleton = NULL;
25
26         // interpolate matrices
27         if (skeleton)
28         {
29                 for (i = 0;i < model->num_bones;i++)
30                 {
31                         Matrix4x4_ToArray12FloatD3D(&skeleton->relativetransforms[i], m);
32                         if (model->data_bones[i].parent >= 0)
33                                 R_ConcatTransforms(bonepose[model->data_bones[i].parent].f, m, bonepose[i].f);
34                         else
35                                 memcpy(bonepose[i].f, m, sizeof(m));
36
37                         // create a relative deformation matrix to describe displacement
38                         // from the base mesh, which is used by the actual weighting
39                         R_ConcatTransforms(bonepose[i].f, model->data_baseboneposeinverse + i * 12, boneposerelative[i].f);
40                 }
41         }
42         else
43         {
44                 float originscale = model->num_posescale;
45                 float x,y,z,w,lerp;
46                 const short * RESTRICT pose7s;
47
48                 for (i = 0;i < model->num_bones;i++)
49                 {
50                         memset(m, 0, sizeof(m));
51                         for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
52                         {
53                                 pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
54                                 lerp = frameblend[blends].lerp;
55                                 x = pose7s[3] * (1.0f / 32767.0f);
56                                 y = pose7s[4] * (1.0f / 32767.0f);
57                                 z = pose7s[5] * (1.0f / 32767.0f);
58                                 w = pose7s[6] * (1.0f / 32767.0f);
59                                 m[ 0] += (1-2*(y*y+z*z)) * lerp;
60                                 m[ 1] += (  2*(x*y-z*w)) * lerp;
61                                 m[ 2] += (  2*(x*z+y*w)) * lerp;
62                                 m[ 3] += (pose7s[0] * originscale) * lerp;
63                                 m[ 4] += (  2*(x*y+z*w)) * lerp;
64                                 m[ 5] += (1-2*(x*x+z*z)) * lerp;
65                                 m[ 6] += (  2*(y*z-x*w)) * lerp;
66                                 m[ 7] += (pose7s[1] * originscale) * lerp;
67                                 m[ 8] += (  2*(x*z-y*w)) * lerp;
68                                 m[ 9] += (  2*(y*z+x*w)) * lerp;
69                                 m[10] += (1-2*(x*x+y*y)) * lerp;
70                                 m[11] += (pose7s[2] * originscale) * lerp;
71                         }
72                         VectorNormalize(m       );
73                         VectorNormalize(m + 4);
74                         VectorNormalize(m + 8);
75                         if (i == r_skeletal_debugbone.integer)
76                                 m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
77                         m[3] *= r_skeletal_debugtranslatex.value;
78                         m[7] *= r_skeletal_debugtranslatey.value;
79                         m[11] *= r_skeletal_debugtranslatez.value;
80                         if (model->data_bones[i].parent >= 0)
81                                 R_ConcatTransforms(bonepose[model->data_bones[i].parent].f, m, bonepose[i].f);
82                         else
83                                 memcpy(bonepose[i].f, m, sizeof(m));
84                         // create a relative deformation matrix to describe displacement
85                         // from the base mesh, which is used by the actual weighting
86                         R_ConcatTransforms(bonepose[i].f, model->data_baseboneposeinverse + i * 12, boneposerelative[i].f);
87                 }
88         }
89
90         // generate matrices for all blend combinations
91         weights = model->surfmesh.data_blendweights;
92         for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
93         {
94                 float * RESTRICT b = boneposerelative[model->num_bones + i].f;
95                 const float * RESTRICT m = boneposerelative[weights->index[0]].f;
96                 float f = weights->influence[0] * (1.0f / 255.0f);
97                 b[ 0] = f*m[ 0]; b[ 1] = f*m[ 1]; b[ 2] = f*m[ 2]; b[ 3] = f*m[ 3];
98                 b[ 4] = f*m[ 4]; b[ 5] = f*m[ 5]; b[ 6] = f*m[ 6]; b[ 7] = f*m[ 7];
99                 b[ 8] = f*m[ 8]; b[ 9] = f*m[ 9]; b[10] = f*m[10]; b[11] = f*m[11];
100                 for (k = 1;k < 4 && weights->influence[k];k++)
101                 {
102                         m = boneposerelative[weights->index[k]].f;
103                         f = weights->influence[k] * (1.0f / 255.0f);
104                         b[ 0] += f*m[ 0]; b[ 1] += f*m[ 1]; b[ 2] += f*m[ 2]; b[ 3] += f*m[ 3];
105                         b[ 4] += f*m[ 4]; b[ 5] += f*m[ 5]; b[ 6] += f*m[ 6]; b[ 7] += f*m[ 7];
106                         b[ 8] += f*m[ 8]; b[ 9] += f*m[ 9]; b[10] += f*m[10]; b[11] += f*m[11];
107                 }
108         }
109
110 #define LOAD_MATRIX_SCALAR() const float * RESTRICT m = boneposerelative[*b].f
111
112 #define LOAD_MATRIX3() \
113         LOAD_MATRIX_SCALAR()
114 #define LOAD_MATRIX4() \
115         LOAD_MATRIX_SCALAR()
116
117 #define TRANSFORM_POSITION_SCALAR(in, out) \
118         (out)[0] = ((in)[0] * m[0] + (in)[1] * m[1] + (in)[2] * m[ 2] + m[3]); \
119         (out)[1] = ((in)[0] * m[4] + (in)[1] * m[5] + (in)[2] * m[ 6] + m[7]); \
120         (out)[2] = ((in)[0] * m[8] + (in)[1] * m[9] + (in)[2] * m[10] + m[11]);
121 #define TRANSFORM_VECTOR_SCALAR(in, out) \
122         (out)[0] = ((in)[0] * m[0] + (in)[1] * m[1] + (in)[2] * m[ 2]); \
123         (out)[1] = ((in)[0] * m[4] + (in)[1] * m[5] + (in)[2] * m[ 6]); \
124         (out)[2] = ((in)[0] * m[8] + (in)[1] * m[9] + (in)[2] * m[10]);
125
126 #define TRANSFORM_POSITION(in, out) \
127         TRANSFORM_POSITION_SCALAR(in, out)
128 #define TRANSFORM_VECTOR(in, out) \
129         TRANSFORM_VECTOR_SCALAR(in, out)
130
131         // transform vertex attributes by blended matrices
132         if (vertex3f)
133         {
134                 const float * RESTRICT v = model->surfmesh.data_vertex3f;
135                 const unsigned short * RESTRICT b = model->surfmesh.blends;
136                 // special case common combinations of attributes to avoid repeated loading of matrices
137                 if (normal3f)
138                 {
139                         const float * RESTRICT n = model->surfmesh.data_normal3f;
140                         if (svector3f && tvector3f)
141                         {
142                                 const float * RESTRICT sv = model->surfmesh.data_svector3f;
143                                 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
144
145                                 // Note that for SSE each iteration stores one element past end, so we break one vertex short
146                                 // and handle that with scalars in that case
147                                 for (i = 0; i < model->surfmesh.num_vertices; i++, v += 3, n += 3, sv += 3, tv += 3, b++,
148                                                 vertex3f += 3, normal3f += 3, svector3f += 3, tvector3f += 3)
149                                 {
150                                         LOAD_MATRIX4();
151                                         TRANSFORM_POSITION(v, vertex3f);
152                                         TRANSFORM_VECTOR(n, normal3f);
153                                         TRANSFORM_VECTOR(sv, svector3f);
154                                         TRANSFORM_VECTOR(tv, tvector3f);
155                                 }
156
157                                 return;
158                         }
159
160                         for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, n += 3, b++, vertex3f += 3, normal3f += 3)
161                         {
162                                 LOAD_MATRIX4();
163                                 TRANSFORM_POSITION(v, vertex3f);
164                                 TRANSFORM_VECTOR(n, normal3f);
165                         }
166                 }
167                 else
168                 {
169                         for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, b++, vertex3f += 3)
170                         {
171                                 LOAD_MATRIX4();
172                                 TRANSFORM_POSITION(v, vertex3f);
173                         }
174                 }
175         }
176
177         else if (normal3f)
178         {
179                 const float * RESTRICT n = model->surfmesh.data_normal3f;
180                 const unsigned short * RESTRICT b = model->surfmesh.blends;
181                 for (i = 0; i < model->surfmesh.num_vertices; i++, n += 3, b++, normal3f += 3)
182                 {
183                         LOAD_MATRIX3();
184                         TRANSFORM_VECTOR(n, normal3f);
185                 }
186         }
187
188         if (svector3f)
189         {
190                 const float * RESTRICT sv = model->surfmesh.data_svector3f;
191                 const unsigned short * RESTRICT b = model->surfmesh.blends;
192                 for (i = 0; i < model->surfmesh.num_vertices; i++, sv += 3, b++, svector3f += 3)
193                 {
194                         LOAD_MATRIX3();
195                         TRANSFORM_VECTOR(sv, svector3f);
196                 }
197         }
198
199         if (tvector3f)
200         {
201                 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
202                 const unsigned short * RESTRICT b = model->surfmesh.blends;
203                 for (i = 0; i < model->surfmesh.num_vertices; i++, tv += 3, b++, tvector3f += 3)
204                 {
205                         LOAD_MATRIX3();
206                         TRANSFORM_VECTOR(tv, tvector3f);
207                 }
208         }
209 }