]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_models.c
fixed bmodel realtime lighting/shadowing render crashes (and bogus shadows from them)
[divverent/darkplaces.git] / gl_models.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4 #include "r_shadow.h"
5
6 typedef struct
7 {
8         float m[3][4];
9 } zymbonematrix;
10
11 // LordHavoc: vertex arrays
12 int aliasvertmax = 0;
13 void *aliasvertarrays = NULL;
14 float *aliasvertcolor4fbuf = NULL;
15 float *aliasvertcolor4f = NULL; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend
16 float *aliasvert_vertex3f = NULL;
17 float *aliasvert_svector3f = NULL;
18 float *aliasvert_tvector3f = NULL;
19 float *aliasvert_normal3f = NULL;
20
21 float *aliasvertcolor2_4f = NULL;
22 int *aliasvertusage;
23 zymbonematrix *zymbonepose;
24
25 mempool_t *gl_models_mempool;
26
27 #define expandaliasvert(newmax) if ((newmax) > aliasvertmax) gl_models_allocarrays(newmax)
28
29 void gl_models_allocarrays(int newmax)
30 {
31         qbyte *data;
32         aliasvertmax = newmax;
33         if (aliasvertarrays != NULL)
34                 Mem_Free(aliasvertarrays);
35         aliasvertarrays = Mem_Alloc(gl_models_mempool, aliasvertmax * (sizeof(float[4+4+3+3+3+3]) + sizeof(int[3])));
36         data = aliasvertarrays;
37         aliasvertcolor4f = aliasvertcolor4fbuf = (void *)data;data += aliasvertmax * sizeof(float[4]);
38         aliasvertcolor2_4f = (void *)data;data += aliasvertmax * sizeof(float[4]); // used temporarily for tinted coloring
39         aliasvert_vertex3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
40         aliasvert_svector3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
41         aliasvert_tvector3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
42         aliasvert_normal3f = (void *)data;data += aliasvertmax * sizeof(float[3]);
43         aliasvertusage = (void *)data;data += aliasvertmax * sizeof(int[3]);
44 }
45
46 void gl_models_freearrays(void)
47 {
48         aliasvertmax = 0;
49         if (aliasvertarrays != NULL)
50                 Mem_Free(aliasvertarrays);
51         aliasvertarrays = NULL;
52         aliasvertcolor4f = aliasvertcolor4fbuf = NULL;
53         aliasvertcolor2_4f = NULL;
54         aliasvert_vertex3f = NULL;
55         aliasvert_svector3f = NULL;
56         aliasvert_tvector3f = NULL;
57         aliasvert_normal3f = NULL;
58         aliasvertusage = NULL;
59 }
60
61 void gl_models_start(void)
62 {
63         // allocate vertex processing arrays
64         gl_models_mempool = Mem_AllocPool("GL_Models");
65         zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256]));
66         gl_models_allocarrays(4096);
67 }
68
69 void gl_models_shutdown(void)
70 {
71         gl_models_freearrays();
72         Mem_FreePool(&gl_models_mempool);
73 }
74
75 void gl_models_newmap(void)
76 {
77 }
78
79 void GL_Models_Init(void)
80 {
81         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
82 }
83
84 #define MODELARRAY_VERTEX 0
85 #define MODELARRAY_SVECTOR 1
86 #define MODELARRAY_TVECTOR 2
87 #define MODELARRAY_NORMAL 3
88
89 void R_Model_Alias_GetMesh_Array3f(const entity_render_t *ent, const aliasmesh_t *mesh, int whicharray, float *out3f)
90 {
91         int i, vertcount;
92         float lerp1, lerp2, lerp3, lerp4;
93         const float *vertsbase, *verts1, *verts2, *verts3, *verts4;
94
95         switch(whicharray)
96         {
97         case MODELARRAY_VERTEX:vertsbase = mesh->data_aliasvertex3f;break;
98         case MODELARRAY_SVECTOR:vertsbase = mesh->data_aliassvector3f;break;
99         case MODELARRAY_TVECTOR:vertsbase = mesh->data_aliastvector3f;break;
100         case MODELARRAY_NORMAL:vertsbase = mesh->data_aliasnormal3f;break;
101         default:
102                 Host_Error("R_Model_Alias_GetBlendedArray: unknown whicharray %i\n", whicharray);
103                 return;
104         }
105
106         vertcount = mesh->num_vertices;
107         verts1 = vertsbase + ent->frameblend[0].frame * vertcount * 3;
108         lerp1 = ent->frameblend[0].lerp;
109         if (ent->frameblend[1].lerp)
110         {
111                 verts2 = vertsbase + ent->frameblend[1].frame * vertcount * 3;
112                 lerp2 = ent->frameblend[1].lerp;
113                 if (ent->frameblend[2].lerp)
114                 {
115                         verts3 = vertsbase + ent->frameblend[2].frame * vertcount * 3;
116                         lerp3 = ent->frameblend[2].lerp;
117                         if (ent->frameblend[3].lerp)
118                         {
119                                 verts4 = vertsbase + ent->frameblend[3].frame * vertcount * 3;
120                                 lerp4 = ent->frameblend[3].lerp;
121                                 for (i = 0;i < vertcount * 3;i++)
122                                         VectorMAMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, lerp4, verts4 + i, out3f + i);
123                         }
124                         else
125                                 for (i = 0;i < vertcount * 3;i++)
126                                         VectorMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, out3f + i);
127                 }
128                 else
129                         for (i = 0;i < vertcount * 3;i++)
130                                 VectorMAM(lerp1, verts1 + i, lerp2, verts2 + i, out3f + i);
131         }
132         else
133                 memcpy(out3f, verts1, vertcount * sizeof(float[3]));
134 }
135
136 aliaslayer_t r_aliasnoskinlayers[2] = {{ALIASLAYER_DIFFUSE, NULL, NULL}, {ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS, NULL, NULL}};
137 aliasskin_t r_aliasnoskin = {0, 2, r_aliasnoskinlayers};
138 aliasskin_t *R_FetchAliasSkin(const entity_render_t *ent, const aliasmesh_t *mesh)
139 {
140         model_t *model = ent->model;
141         if (model->numskins)
142         {
143                 int s = ent->skinnum;
144                 if ((unsigned int)s >= (unsigned int)model->numskins)
145                         s = 0;
146                 if (model->skinscenes[s].framecount > 1)
147                         s = model->skinscenes[s].firstframe + (int) (cl.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
148                 else
149                         s = model->skinscenes[s].firstframe;
150                 if (s >= mesh->num_skins)
151                         s = 0;
152                 return mesh->data_skins + s;
153         }
154         else
155         {
156                 r_aliasnoskinlayers[0].texture = r_notexture;
157                 return &r_aliasnoskin;
158         }
159 }
160
161 void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
162 {
163         int c, fullbright, layernum, firstpass;
164         float tint[3], fog, ifog, colorscale, ambientcolor4f[4], diffusecolor[3], diffusenormal[3];
165         vec3_t diff;
166         qbyte *bcolor;
167         rmeshstate_t m;
168         const entity_render_t *ent = calldata1;
169         aliasmesh_t *mesh = ent->model->alias.aliasdata_meshes + calldata2;
170         aliaslayer_t *layer;
171         aliasskin_t *skin;
172
173         R_Mesh_Matrix(&ent->matrix);
174
175         fog = 0;
176         if (fogenabled)
177         {
178                 VectorSubtract(ent->origin, r_vieworigin, diff);
179                 fog = DotProduct(diff,diff);
180                 if (fog < 0.01f)
181                         fog = 0.01f;
182                 fog = exp(fogdensity/fog);
183                 if (fog > 1)
184                         fog = 1;
185                 if (fog < 0.01f)
186                         fog = 0;
187                 // fog method: darken, additive fog
188                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
189                 // 2. render fog as additive
190         }
191         ifog = 1 - fog;
192
193         firstpass = true;
194         skin = R_FetchAliasSkin(ent, mesh);
195         for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
196         {
197                 if (!(layer->flags & ALIASLAYER_FORCEDRAW_IF_FIRSTPASS) || !firstpass)
198                 {
199                         if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
200                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
201                          || ((layer->flags & ALIASLAYER_FOG) && !fogenabled)
202                          ||  (layer->flags & ALIASLAYER_SPECULAR)
203                          || ((layer->flags & ALIASLAYER_DIFFUSE) && (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0 && r_ambient.integer <= 0 && r_fullbright.integer == 0 && !(ent->effects & EF_FULLBRIGHT))))
204                                 continue;
205                 }
206                 if (!firstpass || (ent->effects & EF_ADDITIVE))
207                 {
208                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
209                         GL_DepthMask(false);
210                 }
211                 else if ((skin->flags & ALIASSKIN_TRANSPARENT) || ent->alpha != 1.0)
212                 {
213                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
214                         GL_DepthMask(false);
215                 }
216                 else
217                 {
218                         GL_BlendFunc(GL_ONE, GL_ZERO);
219                         GL_DepthMask(true);
220                 }
221                 GL_DepthTest(true);
222                 firstpass = false;
223                 expandaliasvert(mesh->num_vertices);
224                 colorscale = 1.0f;
225
226                 memset(&m, 0, sizeof(m));
227                 if (layer->texture != NULL)
228                 {
229                         m.tex[0] = R_GetTexture(layer->texture);
230                         m.pointer_texcoord[0] = mesh->data_texcoord2f;
231                         if (gl_combine.integer && layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
232                         {
233                                 colorscale *= 0.25f;
234                                 m.texrgbscale[0] = 4;
235                         }
236                 }
237                 R_Mesh_State_Texture(&m);
238
239                 c_alias_polys += mesh->num_triangles;
240                 GL_VertexPointer(varray_vertex3f);
241                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
242                 if (layer->flags & ALIASLAYER_FOG)
243                 {
244                         colorscale *= fog;
245                         GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
246                 }
247                 else
248                 {
249                         fullbright = !(layer->flags & ALIASLAYER_DIFFUSE) || r_fullbright.integer || (ent->effects & EF_FULLBRIGHT);
250                         if (layer->flags & (ALIASLAYER_COLORMAP_PANTS | ALIASLAYER_COLORMAP_SHIRT))
251                         {
252                                 // 128-224 are backwards ranges
253                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
254                                         c = (ent->colormap & 0xF) << 4;
255                                 else //if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
256                                         c = (ent->colormap & 0xF0);
257                                 c += (c >= 128 && c < 224) ? 4 : 12;
258                                 bcolor = (qbyte *) (&palette_complete[c]);
259                                 fullbright = fullbright || c >= 224;
260                                 VectorScale(bcolor, (1.0f / 255.0f), tint);
261                         }
262                         else
263                                 tint[0] = tint[1] = tint[2] = 1;
264                         if (r_shadow_realtime_world.integer && !fullbright)
265                                 VectorScale(tint, r_shadow_realtime_world_lightmaps.value, tint);
266                         colorscale *= ifog;
267                         if (fullbright)
268                                 GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
269                         else
270                         {
271                                 if (R_LightModel(ambientcolor4f, diffusecolor, diffusenormal, ent, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha, false))
272                                 {
273                                         GL_ColorPointer(varray_color4f);
274                                         R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, varray_normal3f);
275                                         R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, mesh->num_vertices, varray_vertex3f, varray_normal3f, varray_color4f);
276                                 }
277                                 else
278                                         GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
279                         }
280                 }
281                 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
282         }
283 }
284
285 void R_Model_Alias_Draw(entity_render_t *ent)
286 {
287         int meshnum;
288         aliasmesh_t *mesh;
289         if (ent->alpha < (1.0f / 64.0f))
290                 return; // basically completely transparent
291
292         c_models++;
293
294         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
295         {
296                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->flags & ALIASSKIN_TRANSPARENT)
297                         R_MeshQueue_AddTransparent(ent->origin, R_DrawAliasModelCallback, ent, meshnum);
298                 else
299                         R_DrawAliasModelCallback(ent, meshnum);
300         }
301 }
302
303 void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
304 {
305         int meshnum;
306         aliasmesh_t *mesh;
307         aliasskin_t *skin;
308         float projectdistance;
309         if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
310                 return;
311         projectdistance = lightradius + ent->model->radius;// - sqrt(DotProduct(relativelightorigin, relativelightorigin));
312         if (projectdistance > 0.1)
313         {
314                 R_Mesh_Matrix(&ent->matrix);
315                 for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
316                 {
317                         skin = R_FetchAliasSkin(ent, mesh);
318                         if (skin->flags & ALIASSKIN_TRANSPARENT)
319                                 continue;
320                         R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
321                         R_Shadow_VolumeFromSphere(mesh->num_vertices, mesh->num_triangles, varray_vertex3f, mesh->data_element3i, mesh->data_neighbor3i, relativelightorigin, projectdistance, lightradius);
322                 }
323         }
324 }
325
326 void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
327 {
328         int c, meshnum, layernum;
329         float fog, ifog, lightcolor2[3];
330         vec3_t diff;
331         qbyte *bcolor;
332         aliasmesh_t *mesh;
333         aliaslayer_t *layer;
334         aliasskin_t *skin;
335
336         if (ent->effects & (EF_ADDITIVE | EF_FULLBRIGHT) || ent->alpha < 1)
337                 return;
338
339         R_Mesh_Matrix(&ent->matrix);
340
341         fog = 0;
342         if (fogenabled)
343         {
344                 VectorSubtract(ent->origin, r_vieworigin, diff);
345                 fog = DotProduct(diff,diff);
346                 if (fog < 0.01f)
347                         fog = 0.01f;
348                 fog = exp(fogdensity/fog);
349                 if (fog > 1)
350                         fog = 1;
351                 if (fog < 0.01f)
352                         fog = 0;
353                 // fog method: darken, additive fog
354                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
355                 // 2. render fog as additive
356         }
357         ifog = 1 - fog;
358
359         for (meshnum = 0, mesh = ent->model->alias.aliasdata_meshes;meshnum < ent->model->alias.aliasnum_meshes;meshnum++, mesh++)
360         {
361                 skin = R_FetchAliasSkin(ent, mesh);
362                 if (skin->flags & ALIASSKIN_TRANSPARENT)
363                         continue;
364                 expandaliasvert(mesh->num_vertices);
365                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, aliasvert_vertex3f);
366                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_SVECTOR, aliasvert_svector3f);
367                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_TVECTOR, aliasvert_tvector3f);
368                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, aliasvert_normal3f);
369                 for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
370                 {
371                         if (!(layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
372                          || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
373                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
374                                 continue;
375                         lightcolor2[0] = lightcolor[0] * ifog;
376                         lightcolor2[1] = lightcolor[1] * ifog;
377                         lightcolor2[2] = lightcolor[2] * ifog;
378                         if (layer->flags & ALIASLAYER_SPECULAR)
379                         {
380                                 c_alias_polys += mesh->num_triangles;
381                                 R_Shadow_SpecularLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, aliasvert_vertex3f, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, relativelightorigin, relativeeyeorigin, lightcolor2, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, lightcubemap);
382                         }
383                         else if (layer->flags & ALIASLAYER_DIFFUSE)
384                         {
385                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
386                                 {
387                                         // 128-224 are backwards ranges
388                                         c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
389                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
390                                         if (c >= 224)
391                                                 continue;
392                                         bcolor = (qbyte *) (&palette_complete[c]);
393                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
394                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
395                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
396                                 }
397                                 else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
398                                 {
399                                         // 128-224 are backwards ranges
400                                         c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
401                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
402                                         if (c >= 224)
403                                                 continue;
404                                         bcolor = (qbyte *) (&palette_complete[c]);
405                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
406                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
407                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
408                                 }
409                                 c_alias_polys += mesh->num_triangles;
410                                 R_Shadow_DiffuseLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, aliasvert_vertex3f, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, relativelightorigin, lightcolor2, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, lightcubemap);
411                         }
412                 }
413         }
414 }
415
416 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
417 {
418         int i;
419         float lerp1, lerp2, lerp3, lerp4;
420         zymbonematrix *out, rootmatrix, m;
421         const zymbonematrix *bone1, *bone2, *bone3, *bone4;
422
423         rootmatrix.m[0][0] = 1;
424         rootmatrix.m[0][1] = 0;
425         rootmatrix.m[0][2] = 0;
426         rootmatrix.m[0][3] = 0;
427         rootmatrix.m[1][0] = 0;
428         rootmatrix.m[1][1] = 1;
429         rootmatrix.m[1][2] = 0;
430         rootmatrix.m[1][3] = 0;
431         rootmatrix.m[2][0] = 0;
432         rootmatrix.m[2][1] = 0;
433         rootmatrix.m[2][2] = 1;
434         rootmatrix.m[2][3] = 0;
435
436         bone1 = bonebase + blend[0].frame * count;
437         lerp1 = blend[0].lerp;
438         if (blend[1].lerp)
439         {
440                 bone2 = bonebase + blend[1].frame * count;
441                 lerp2 = blend[1].lerp;
442                 if (blend[2].lerp)
443                 {
444                         bone3 = bonebase + blend[2].frame * count;
445                         lerp3 = blend[2].lerp;
446                         if (blend[3].lerp)
447                         {
448                                 // 4 poses
449                                 bone4 = bonebase + blend[3].frame * count;
450                                 lerp4 = blend[3].lerp;
451                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
452                                 {
453                                         // interpolate matrices
454                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
455                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
456                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
457                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
458                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
459                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
460                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
461                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
462                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
463                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
464                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
465                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
466                                         if (bone->parent >= 0)
467                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
468                                         else
469                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
470                                         bone1++;
471                                         bone2++;
472                                         bone3++;
473                                         bone4++;
474                                         bone++;
475                                 }
476                         }
477                         else
478                         {
479                                 // 3 poses
480                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
481                                 {
482                                         // interpolate matrices
483                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
484                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
485                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
486                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
487                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
488                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
489                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
490                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
491                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
492                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
493                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
494                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
495                                         if (bone->parent >= 0)
496                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
497                                         else
498                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
499                                         bone1++;
500                                         bone2++;
501                                         bone3++;
502                                         bone++;
503                                 }
504                         }
505                 }
506                 else
507                 {
508                         // 2 poses
509                         for (i = 0, out = zymbonepose;i < count;i++, out++)
510                         {
511                                 // interpolate matrices
512                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
513                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
514                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
515                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
516                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
517                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
518                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
519                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
520                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
521                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
522                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
523                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
524                                 if (bone->parent >= 0)
525                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
526                                 else
527                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
528                                 bone1++;
529                                 bone2++;
530                                 bone++;
531                         }
532                 }
533         }
534         else
535         {
536                 // 1 pose
537                 if (lerp1 != 1)
538                 {
539                         // lerp != 1.0
540                         for (i = 0, out = zymbonepose;i < count;i++, out++)
541                         {
542                                 // interpolate matrices
543                                 m.m[0][0] = bone1->m[0][0] * lerp1;
544                                 m.m[0][1] = bone1->m[0][1] * lerp1;
545                                 m.m[0][2] = bone1->m[0][2] * lerp1;
546                                 m.m[0][3] = bone1->m[0][3] * lerp1;
547                                 m.m[1][0] = bone1->m[1][0] * lerp1;
548                                 m.m[1][1] = bone1->m[1][1] * lerp1;
549                                 m.m[1][2] = bone1->m[1][2] * lerp1;
550                                 m.m[1][3] = bone1->m[1][3] * lerp1;
551                                 m.m[2][0] = bone1->m[2][0] * lerp1;
552                                 m.m[2][1] = bone1->m[2][1] * lerp1;
553                                 m.m[2][2] = bone1->m[2][2] * lerp1;
554                                 m.m[2][3] = bone1->m[2][3] * lerp1;
555                                 if (bone->parent >= 0)
556                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
557                                 else
558                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
559                                 bone1++;
560                                 bone++;
561                         }
562                 }
563                 else
564                 {
565                         // lerp == 1.0
566                         for (i = 0, out = zymbonepose;i < count;i++, out++)
567                         {
568                                 if (bone->parent >= 0)
569                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
570                                 else
571                                         R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
572                                 bone1++;
573                                 bone++;
574                         }
575                 }
576         }
577         return true;
578 }
579
580 void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymvertex_t *vert)
581 {
582         int c;
583         float *out = vertex;
584         zymbonematrix *matrix;
585         while(vertcount--)
586         {
587                 c = *bonecounts++;
588                 // FIXME: validate bonecounts at load time (must be >= 1)
589                 // FIXME: need 4th component in origin, for how much of the translate to blend in
590                 if (c == 1)
591                 {
592                         matrix = &zymbonepose[vert->bonenum];
593                         out[0] = vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
594                         out[1] = vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
595                         out[2] = vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
596                         vert++;
597                 }
598                 else
599                 {
600                         VectorClear(out);
601                         while(c--)
602                         {
603                                 matrix = &zymbonepose[vert->bonenum];
604                                 out[0] += vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
605                                 out[1] += vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
606                                 out[2] += vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
607                                 vert++;
608                         }
609                 }
610                 out += 3;
611         }
612 }
613
614 void ZymoticCalcNormal3f(int vertcount, float *vertex3f, float *normal3f, int shadercount, int *renderlist)
615 {
616         int a, b, c, d;
617         float *out, v1[3], v2[3], normal[3], s;
618         int *u;
619         // clear normals
620         memset(normal3f, 0, sizeof(float) * vertcount * 3);
621         memset(aliasvertusage, 0, sizeof(int) * vertcount);
622         // parse render list and accumulate surface normals
623         while(shadercount--)
624         {
625                 d = *renderlist++;
626                 while (d--)
627                 {
628                         a = renderlist[0]*4;
629                         b = renderlist[1]*4;
630                         c = renderlist[2]*4;
631                         v1[0] = vertex3f[a+0] - vertex3f[b+0];
632                         v1[1] = vertex3f[a+1] - vertex3f[b+1];
633                         v1[2] = vertex3f[a+2] - vertex3f[b+2];
634                         v2[0] = vertex3f[c+0] - vertex3f[b+0];
635                         v2[1] = vertex3f[c+1] - vertex3f[b+1];
636                         v2[2] = vertex3f[c+2] - vertex3f[b+2];
637                         CrossProduct(v1, v2, normal);
638                         VectorNormalizeFast(normal);
639                         // add surface normal to vertices
640                         a = renderlist[0] * 3;
641                         normal3f[a+0] += normal[0];
642                         normal3f[a+1] += normal[1];
643                         normal3f[a+2] += normal[2];
644                         aliasvertusage[renderlist[0]]++;
645                         a = renderlist[1] * 3;
646                         normal3f[a+0] += normal[0];
647                         normal3f[a+1] += normal[1];
648                         normal3f[a+2] += normal[2];
649                         aliasvertusage[renderlist[1]]++;
650                         a = renderlist[2] * 3;
651                         normal3f[a+0] += normal[0];
652                         normal3f[a+1] += normal[1];
653                         normal3f[a+2] += normal[2];
654                         aliasvertusage[renderlist[2]]++;
655                         renderlist += 3;
656                 }
657         }
658         // FIXME: precalc this
659         // average surface normals
660         out = normal3f;
661         u = aliasvertusage;
662         while(vertcount--)
663         {
664                 if (*u > 1)
665                 {
666                         s = ixtable[*u];
667                         out[0] *= s;
668                         out[1] *= s;
669                         out[2] *= s;
670                 }
671                 u++;
672                 out += 3;
673         }
674 }
675
676 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
677 {
678         float fog, ifog, colorscale, ambientcolor4f[4], diffusecolor[3], diffusenormal[3];
679         vec3_t diff;
680         int i, *renderlist, *elements;
681         rtexture_t *texture;
682         rmeshstate_t mstate;
683         const entity_render_t *ent = calldata1;
684         int shadernum = calldata2;
685         int numverts, numtriangles;
686
687         R_Mesh_Matrix(&ent->matrix);
688
689         // find the vertex index list and texture
690         renderlist = ent->model->alias.zymdata_renderlist;
691         for (i = 0;i < shadernum;i++)
692                 renderlist += renderlist[0] * 3 + 1;
693         texture = ent->model->alias.zymdata_textures[shadernum];
694
695         numverts = ent->model->alias.zymnum_verts;
696         numtriangles = *renderlist++;
697         elements = renderlist;
698
699         expandaliasvert(numverts);
700
701         fog = 0;
702         if (fogenabled)
703         {
704                 VectorSubtract(ent->origin, r_vieworigin, diff);
705                 fog = DotProduct(diff,diff);
706                 if (fog < 0.01f)
707                         fog = 0.01f;
708                 fog = exp(fogdensity/fog);
709                 if (fog > 1)
710                         fog = 1;
711                 if (fog < 0.01f)
712                         fog = 0;
713                 // fog method: darken, additive fog
714                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
715                 // 2. render fog as additive
716         }
717         ifog = 1 - fog;
718
719         if (ent->effects & EF_ADDITIVE)
720         {
721                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
722                 GL_DepthMask(false);
723         }
724         else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
725         {
726                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
727                 GL_DepthMask(false);
728         }
729         else
730         {
731                 GL_BlendFunc(GL_ONE, GL_ZERO);
732                 GL_DepthMask(true);
733         }
734         GL_DepthTest(true);
735         GL_VertexPointer(varray_vertex3f);
736
737         memset(&mstate, 0, sizeof(mstate));
738         colorscale = 1.0f;
739         if (gl_combine.integer)
740         {
741                 mstate.texrgbscale[0] = 4;
742                 colorscale *= 0.25f;
743         }
744         mstate.tex[0] = R_GetTexture(texture);
745         mstate.pointer_texcoord[0] = ent->model->alias.zymdata_texcoords;
746         R_Mesh_State_Texture(&mstate);
747
748         ZymoticLerpBones(ent->model->alias.zymnum_bones, (zymbonematrix *) ent->model->alias.zymdata_poses, ent->frameblend, ent->model->alias.zymdata_bones);
749
750         ZymoticTransformVerts(numverts, varray_vertex3f, ent->model->alias.zymdata_vertbonecounts, ent->model->alias.zymdata_verts);
751         ZymoticCalcNormal3f(numverts, varray_vertex3f, aliasvert_normal3f, ent->model->alias.zymnum_shaders, ent->model->alias.zymdata_renderlist);
752         if (R_LightModel(ambientcolor4f, diffusecolor, diffusenormal, ent, ifog * colorscale, ifog * colorscale, ifog * colorscale, ent->alpha, false))
753         {
754                 GL_ColorPointer(varray_color4f);
755                 R_LightModel_CalcVertexColors(ambientcolor4f, diffusecolor, diffusenormal, numverts, varray_vertex3f, aliasvert_normal3f, varray_color4f);
756         }
757         else
758                 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
759         R_Mesh_Draw(numverts, numtriangles, elements);
760         c_alias_polys += numtriangles;
761
762         if (fog)
763         {
764                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
765                 GL_DepthMask(false);
766                 GL_DepthTest(true);
767                 GL_VertexPointer(varray_vertex3f);
768
769                 memset(&mstate, 0, sizeof(mstate));
770                 // FIXME: need alpha mask for fogging...
771                 //mstate.tex[0] = R_GetTexture(texture);
772                 //mstate.pointer_texcoord = ent->model->alias.zymdata_texcoords;
773                 R_Mesh_State_Texture(&mstate);
774
775                 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha * fog);
776                 ZymoticTransformVerts(numverts, varray_vertex3f, ent->model->alias.zymdata_vertbonecounts, ent->model->alias.zymdata_verts);
777                 R_Mesh_Draw(numverts, numtriangles, elements);
778                 c_alias_polys += numtriangles;
779         }
780 }
781
782 void R_Model_Zymotic_Draw(entity_render_t *ent)
783 {
784         int i;
785
786         if (ent->alpha < (1.0f / 64.0f))
787                 return; // basically completely transparent
788
789         c_models++;
790
791         for (i = 0;i < ent->model->alias.zymnum_shaders;i++)
792         {
793                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(ent->model->alias.zymdata_textures[i]))
794                         R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
795                 else
796                         R_DrawZymoticModelMeshCallback(ent, i);
797         }
798 }
799
800 void R_Model_Zymotic_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
801 {
802         // FIXME
803 }
804
805 void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
806 {
807         // FIXME
808 }
809