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