added R_ScrollTexCoord2f (copies while texture coordinates with an offset)
[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, 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];
165         vec3_t diff;
166         qbyte *bcolor;
167         rmeshstate_t m;
168         const entity_render_t *ent = calldata1;
169         aliasmesh_t *mesh = ent->model->aliasdata_meshes + calldata2;
170         aliaslayer_t *layer;
171         aliasskin_t *skin;
172         rcachearrayrequest_t request;
173
174         R_Mesh_Matrix(&ent->matrix);
175
176         fog = 0;
177         if (fogenabled)
178         {
179                 VectorSubtract(ent->origin, r_origin, diff);
180                 fog = DotProduct(diff,diff);
181                 if (fog < 0.01f)
182                         fog = 0.01f;
183                 fog = exp(fogdensity/fog);
184                 if (fog > 1)
185                         fog = 1;
186                 if (fog < 0.01f)
187                         fog = 0;
188                 // fog method: darken, additive fog
189                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
190                 // 2. render fog as additive
191         }
192         ifog = 1 - fog;
193
194         firstpass = true;
195         skin = R_FetchAliasSkin(ent, mesh);
196         for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
197         {
198                 if (!(layer->flags & ALIASLAYER_FORCEDRAW_IF_FIRSTPASS) || !firstpass)
199                 {
200                         if (((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
201                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0)
202                          || ((layer->flags & ALIASLAYER_FOG) && !fogenabled)
203                          ||  (layer->flags & ALIASLAYER_SPECULAR)
204                          || ((layer->flags & ALIASLAYER_DIFFUSE) && (r_shadow_realtime_world.integer && r_ambient.integer <= 0 && r_fullbright.integer == 0 && !(ent->effects & EF_FULLBRIGHT))))
205                                 continue;
206                 }
207                 memset(&m, 0, sizeof(m));
208                 if (!firstpass || (ent->effects & EF_ADDITIVE))
209                 {
210                         m.blendfunc1 = GL_SRC_ALPHA;
211                         m.blendfunc2 = GL_ONE;
212                 }
213                 else if ((skin->flags & ALIASSKIN_TRANSPARENT) || ent->alpha != 1.0)
214                 {
215                         m.blendfunc1 = GL_SRC_ALPHA;
216                         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
217                 }
218                 else
219                 {
220                         m.blendfunc1 = GL_ONE;
221                         m.blendfunc2 = GL_ZERO;
222                 }
223                 firstpass = false;
224                 expandaliasvert(mesh->num_vertices);
225                 colorscale = r_colorscale;
226                 m.texrgbscale[0] = 1;
227                 m.tex[0] = R_GetTexture(layer->texture);
228                 if (gl_combine.integer && layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
229                 {
230                         colorscale *= 0.25f;
231                         m.texrgbscale[0] = 4;
232                 }
233                 c_alias_polys += mesh->num_triangles;
234                 if (gl_mesh_copyarrays.integer)
235                 {
236                         R_Mesh_State(&m);
237                         R_Mesh_GetSpace(mesh->num_vertices);
238                         if (layer->texture != NULL)
239                                 R_Mesh_CopyTexCoord2f(0, mesh->data_texcoord2f, mesh->num_vertices);
240                         R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
241                 }
242                 else
243                 {
244                         m.pointervertexcount = mesh->num_vertices;
245                         memset(&request, 0, sizeof(request));
246                         request.data_size = mesh->num_vertices * sizeof(float[3]);
247                         request.id_pointer2 = mesh->data_aliasvertex3f;
248                         request.id_number1 = layernum;
249                         request.id_number2 = 0;
250                         request.id_number3 = CRC_Block((void *)ent->frameblend, sizeof(ent->frameblend));
251                         if (R_Mesh_CacheArray(&request))
252                                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, request.data);
253                         m.pointer_vertex = request.data;
254                         m.pointer_texcoord[0] = layer->texture != NULL ? mesh->data_texcoord2f : NULL;
255                 }
256                 if (layer->flags & ALIASLAYER_FOG)
257                 {
258                         colorscale *= fog;
259                         GL_Color(fogcolor[0] * colorscale, fogcolor[1] * colorscale, fogcolor[2] * colorscale, ent->alpha);
260                 }
261                 else
262                 {
263                         if (layer->flags & (ALIASLAYER_COLORMAP_PANTS | ALIASLAYER_COLORMAP_SHIRT))
264                         {
265                                 // 128-224 are backwards ranges
266                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
267                                         c = (ent->colormap & 0xF) << 4;
268                                 else //if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
269                                         c = (ent->colormap & 0xF0);
270                                 c += (c >= 128 && c < 224) ? 4 : 12;
271                                 bcolor = (qbyte *) (&palette_complete[c]);
272                                 fullbright = c >= 224;
273                                 VectorScale(bcolor, (1.0f / 255.0f), tint);
274                         }
275                         else
276                         {
277                                 tint[0] = tint[1] = tint[2] = 1;
278                                 fullbright = false;
279                         }
280                         colorscale *= ifog;
281                         if (fullbright || !(layer->flags & ALIASLAYER_DIFFUSE) || r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
282                                 GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
283                         else if (r_shadow_realtime_world.integer)
284                         {
285                                 colorscale *= r_ambient.value * (2.0f / 128.0f);
286                                 GL_Color(tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha);
287                         }
288                         else
289                         {
290                                 if (R_LightModel(ambientcolor4f, ent, tint[0] * colorscale, tint[1] * colorscale, tint[2] * colorscale, ent->alpha, false))
291                                 {
292                                         GL_UseColorArray();
293                                         if (gl_mesh_copyarrays.integer)
294                                         {
295                                                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, aliasvert_normal3f);
296                                                 R_LightModel_CalcVertexColors(ambientcolor4f, mesh->num_vertices, varray_vertex3f, aliasvert_normal3f, varray_color4f);
297                                         }
298                                         else
299                                         {
300                                                 // request color4f cache
301                                                 request.data_size = mesh->num_vertices * sizeof(float[4]);
302                                                 request.id_pointer1 = ent;
303                                                 request.id_number2 = 2;
304                                                 request.id_number3 = CRC_Block((void *)ent->frameblend, sizeof(ent->frameblend)) + CRC_Block((void *)&ent->entlightstime, sizeof(ent->entlightstime));
305                                                 if (R_Mesh_CacheArray(&request))
306                                                 {
307                                                         // save off the color pointer before we blow away the request
308                                                         m.pointer_color = request.data;
309                                                         // request normal3f cache
310                                                         request.data_size = mesh->num_vertices * sizeof(float[3]);
311                                                         request.id_pointer1 = NULL;
312                                                         request.id_number2 = 3;
313                                                         request.id_number3 = CRC_Block((void *)ent->frameblend, sizeof(ent->frameblend));
314                                                         if (R_Mesh_CacheArray(&request))
315                                                                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, request.data);
316                                                         R_LightModel_CalcVertexColors(ambientcolor4f, mesh->num_vertices, m.pointer_vertex, request.data, m.pointer_color);
317                                                 }
318                                                 else
319                                                         m.pointer_color = request.data;
320                                         }
321                                 }
322                                 else
323                                         GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
324                         }
325                 }
326                 if (!gl_mesh_copyarrays.integer)
327                         R_Mesh_State(&m);
328                 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
329         }
330 }
331
332 void R_Model_Alias_Draw(entity_render_t *ent)
333 {
334         int meshnum;
335         aliasmesh_t *mesh;
336         if (ent->alpha < (1.0f / 64.0f))
337                 return; // basically completely transparent
338
339         c_models++;
340
341         for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++, mesh++)
342         {
343                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchAliasSkin(ent, mesh)->flags & ALIASSKIN_TRANSPARENT)
344                         R_MeshQueue_AddTransparent(ent->origin, R_DrawAliasModelCallback, ent, meshnum);
345                 else
346                         R_DrawAliasModelCallback(ent, meshnum);
347         }
348 }
349
350 void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
351 {
352         int i, meshnum;
353         aliasmesh_t *mesh;
354         aliasskin_t *skin;
355         rmeshstate_t m;
356         float *v, plane[4], dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
357         rcachearrayrequest_t request;
358
359         if ((ent->effects & EF_ADDITIVE) || ent->alpha < 1)
360                 return;
361
362         lightdirection[0] = 0.5;
363         lightdirection[1] = 0.2;
364         lightdirection[2] = -1;
365         VectorNormalizeFast(lightdirection);
366
367         VectorMA(ent->origin, 65536.0f, lightdirection, v2);
368         if (CL_TraceLine(ent->origin, v2, floororigin, surfnormal, 0, false, NULL) == 1)
369                 return;
370
371         R_Mesh_Matrix(&ent->matrix);
372
373         memset(&m, 0, sizeof(m));
374         m.blendfunc1 = GL_SRC_ALPHA;
375         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
376         if (gl_mesh_copyarrays.integer)
377                 R_Mesh_State(&m);
378         GL_Color(0, 0, 0, 0.5);
379
380         // put a light direction in the entity's coordinate space
381         Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection);
382         VectorNormalizeFast(projection);
383
384         // put the plane's normal in the entity's coordinate space
385         Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, plane);
386         VectorNormalizeFast(plane);
387
388         // put the plane's distance in the entity's coordinate space
389         VectorSubtract(floororigin, ent->origin, floororigin);
390         plane[3] = DotProduct(floororigin, surfnormal) + 2;
391
392         dist = -1.0f / DotProduct(projection, plane);
393         VectorScale(projection, dist, projection);
394         memset(&request, 0, sizeof(request));
395         for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++)
396         {
397                 skin = R_FetchAliasSkin(ent, mesh);
398                 if (skin->flags & ALIASSKIN_TRANSPARENT)
399                         continue;
400                 if (gl_mesh_copyarrays.integer)
401                 {
402                         R_Mesh_GetSpace(mesh->num_vertices);
403                         R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
404                         for (i = 0, v = varray_vertex3f;i < mesh->num_vertices;i++, v += 3)
405                         {
406                                 dist = DotProduct(v, plane) - plane[3];
407                                 if (dist > 0)
408                                         VectorMA(v, dist, projection, v);
409                         }
410                 }
411                 else
412                 {
413                         request.data_size = mesh->num_vertices * sizeof(float[3]);
414                         request.id_pointer1 = mesh;
415                         request.id_number1 = CRC_Block((void *)&ent->matrix, sizeof(ent->matrix));
416                         request.id_number2 = CRC_Block((void *)&plane, sizeof(plane));
417                         request.id_number3 = CRC_Block((void *)&ent->frameblend, sizeof(ent->frameblend));
418                         m.pointervertexcount = mesh->num_vertices;
419                         if (R_Mesh_CacheArray(&request))
420                         {
421                                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, request.data);
422                                 for (i = 0, v = request.data;i < mesh->num_vertices;i++, v += 3)
423                                 {
424                                         dist = DotProduct(v, plane) - plane[3];
425                                         if (dist > 0)
426                                                 VectorMA(v, dist, projection, v);
427                                 }
428                         }
429                         m.pointer_vertex = request.data;
430                         R_Mesh_State(&m);
431                 }
432                 c_alias_polys += mesh->num_triangles;
433                 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
434         }
435 }
436
437 void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
438 {
439         int meshnum;
440         aliasmesh_t *mesh;
441         aliasskin_t *skin;
442         float projectdistance;
443         if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
444                 return;
445         projectdistance = lightradius + ent->model->radius - sqrt(DotProduct(relativelightorigin, relativelightorigin));
446         if (projectdistance > 0.1)
447         {
448                 R_Mesh_Matrix(&ent->matrix);
449                 for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++, mesh++)
450                 {
451                         skin = R_FetchAliasSkin(ent, mesh);
452                         if (skin->flags & ALIASSKIN_TRANSPARENT)
453                                 continue;
454                         R_Mesh_GetSpace(mesh->num_vertices * 2);
455                         R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, varray_vertex3f);
456                         R_Shadow_Volume(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, mesh->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
457                 }
458         }
459 }
460
461 void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
462 {
463         int c, meshnum, layernum;
464         float fog, ifog, lightcolor2[3];
465         vec3_t diff;
466         qbyte *bcolor;
467         aliasmesh_t *mesh;
468         aliaslayer_t *layer;
469         aliasskin_t *skin;
470
471         if (ent->effects & (EF_ADDITIVE | EF_FULLBRIGHT) || ent->alpha < 1)
472                 return;
473
474         R_Mesh_Matrix(&ent->matrix);
475
476         fog = 0;
477         if (fogenabled)
478         {
479                 VectorSubtract(ent->origin, r_origin, diff);
480                 fog = DotProduct(diff,diff);
481                 if (fog < 0.01f)
482                         fog = 0.01f;
483                 fog = exp(fogdensity/fog);
484                 if (fog > 1)
485                         fog = 1;
486                 if (fog < 0.01f)
487                         fog = 0;
488                 // fog method: darken, additive fog
489                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
490                 // 2. render fog as additive
491         }
492         ifog = 1 - fog;
493
494         for (meshnum = 0, mesh = ent->model->aliasdata_meshes;meshnum < ent->model->aliasnum_meshes;meshnum++, mesh++)
495         {
496                 skin = R_FetchAliasSkin(ent, mesh);
497                 if (skin->flags & ALIASSKIN_TRANSPARENT)
498                         continue;
499                 expandaliasvert(mesh->num_vertices);
500                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_VERTEX, aliasvert_vertex3f);
501                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_SVECTOR, aliasvert_svector3f);
502                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_TVECTOR, aliasvert_tvector3f);
503                 R_Model_Alias_GetMesh_Array3f(ent, mesh, MODELARRAY_NORMAL, aliasvert_normal3f);
504                 for (layernum = 0, layer = skin->data_layers;layernum < skin->num_layers;layernum++, layer++)
505                 {
506                         if (!(layer->flags & (ALIASLAYER_DIFFUSE | ALIASLAYER_SPECULAR))
507                          || ((layer->flags & ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED) && ent->colormap < 0)
508                          || ((layer->flags & ALIASLAYER_NODRAW_IF_COLORMAPPED) && ent->colormap >= 0))
509                                 continue;
510                         lightcolor2[0] = lightcolor[0] * ifog;
511                         lightcolor2[1] = lightcolor[1] * ifog;
512                         lightcolor2[2] = lightcolor[2] * ifog;
513                         if (layer->flags & ALIASLAYER_SPECULAR)
514                         {
515                                 c_alias_polys += mesh->num_triangles;
516                                 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, lightradius, lightcolor2, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
517                         }
518                         else if (layer->flags & ALIASLAYER_DIFFUSE)
519                         {
520                                 if (layer->flags & ALIASLAYER_COLORMAP_PANTS)
521                                 {
522                                         // 128-224 are backwards ranges
523                                         c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
524                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
525                                         if (c >= 224)
526                                                 continue;
527                                         bcolor = (qbyte *) (&palette_complete[c]);
528                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
529                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
530                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
531                                 }
532                                 else if (layer->flags & ALIASLAYER_COLORMAP_SHIRT)
533                                 {
534                                         // 128-224 are backwards ranges
535                                         c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
536                                         // fullbright passes were already taken care of, so skip them in realtime lighting passes
537                                         if (c >= 224)
538                                                 continue;
539                                         bcolor = (qbyte *) (&palette_complete[c]);
540                                         lightcolor2[0] *= bcolor[0] * (1.0f / 255.0f);
541                                         lightcolor2[1] *= bcolor[1] * (1.0f / 255.0f);
542                                         lightcolor2[2] *= bcolor[2] * (1.0f / 255.0f);
543                                 }
544                                 c_alias_polys += mesh->num_triangles;
545                                 R_Shadow_DiffuseLighting(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i, aliasvert_vertex3f, aliasvert_svector3f, aliasvert_tvector3f, aliasvert_normal3f, mesh->data_texcoord2f, relativelightorigin, lightradius, lightcolor2, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, layer->texture, layer->nmap, NULL);
546                         }
547                 }
548         }
549 }
550
551 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
552 {
553         int i;
554         float lerp1, lerp2, lerp3, lerp4;
555         zymbonematrix *out, rootmatrix, m;
556         const zymbonematrix *bone1, *bone2, *bone3, *bone4;
557
558         rootmatrix.m[0][0] = 1;
559         rootmatrix.m[0][1] = 0;
560         rootmatrix.m[0][2] = 0;
561         rootmatrix.m[0][3] = 0;
562         rootmatrix.m[1][0] = 0;
563         rootmatrix.m[1][1] = 1;
564         rootmatrix.m[1][2] = 0;
565         rootmatrix.m[1][3] = 0;
566         rootmatrix.m[2][0] = 0;
567         rootmatrix.m[2][1] = 0;
568         rootmatrix.m[2][2] = 1;
569         rootmatrix.m[2][3] = 0;
570
571         bone1 = bonebase + blend[0].frame * count;
572         lerp1 = blend[0].lerp;
573         if (blend[1].lerp)
574         {
575                 bone2 = bonebase + blend[1].frame * count;
576                 lerp2 = blend[1].lerp;
577                 if (blend[2].lerp)
578                 {
579                         bone3 = bonebase + blend[2].frame * count;
580                         lerp3 = blend[2].lerp;
581                         if (blend[3].lerp)
582                         {
583                                 // 4 poses
584                                 bone4 = bonebase + blend[3].frame * count;
585                                 lerp4 = blend[3].lerp;
586                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
587                                 {
588                                         // interpolate matrices
589                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
590                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
591                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
592                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
593                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
594                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
595                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
596                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
597                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
598                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
599                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
600                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
601                                         if (bone->parent >= 0)
602                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
603                                         else
604                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
605                                         bone1++;
606                                         bone2++;
607                                         bone3++;
608                                         bone4++;
609                                         bone++;
610                                 }
611                         }
612                         else
613                         {
614                                 // 3 poses
615                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
616                                 {
617                                         // interpolate matrices
618                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
619                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
620                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
621                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
622                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
623                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
624                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
625                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
626                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
627                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
628                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
629                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
630                                         if (bone->parent >= 0)
631                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
632                                         else
633                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
634                                         bone1++;
635                                         bone2++;
636                                         bone3++;
637                                         bone++;
638                                 }
639                         }
640                 }
641                 else
642                 {
643                         // 2 poses
644                         for (i = 0, out = zymbonepose;i < count;i++, out++)
645                         {
646                                 // interpolate matrices
647                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
648                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
649                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
650                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
651                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
652                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
653                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
654                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
655                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
656                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
657                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
658                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
659                                 if (bone->parent >= 0)
660                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
661                                 else
662                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
663                                 bone1++;
664                                 bone2++;
665                                 bone++;
666                         }
667                 }
668         }
669         else
670         {
671                 // 1 pose
672                 if (lerp1 != 1)
673                 {
674                         // lerp != 1.0
675                         for (i = 0, out = zymbonepose;i < count;i++, out++)
676                         {
677                                 // interpolate matrices
678                                 m.m[0][0] = bone1->m[0][0] * lerp1;
679                                 m.m[0][1] = bone1->m[0][1] * lerp1;
680                                 m.m[0][2] = bone1->m[0][2] * lerp1;
681                                 m.m[0][3] = bone1->m[0][3] * lerp1;
682                                 m.m[1][0] = bone1->m[1][0] * lerp1;
683                                 m.m[1][1] = bone1->m[1][1] * lerp1;
684                                 m.m[1][2] = bone1->m[1][2] * lerp1;
685                                 m.m[1][3] = bone1->m[1][3] * lerp1;
686                                 m.m[2][0] = bone1->m[2][0] * lerp1;
687                                 m.m[2][1] = bone1->m[2][1] * lerp1;
688                                 m.m[2][2] = bone1->m[2][2] * lerp1;
689                                 m.m[2][3] = bone1->m[2][3] * lerp1;
690                                 if (bone->parent >= 0)
691                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
692                                 else
693                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
694                                 bone1++;
695                                 bone++;
696                         }
697                 }
698                 else
699                 {
700                         // lerp == 1.0
701                         for (i = 0, out = zymbonepose;i < count;i++, out++)
702                         {
703                                 if (bone->parent >= 0)
704                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
705                                 else
706                                         R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
707                                 bone1++;
708                                 bone++;
709                         }
710                 }
711         }
712         return true;
713 }
714
715 void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymvertex_t *vert)
716 {
717         int c;
718         float *out = vertex;
719         zymbonematrix *matrix;
720         while(vertcount--)
721         {
722                 c = *bonecounts++;
723                 // FIXME: validate bonecounts at load time (must be >= 1)
724                 // FIXME: need 4th component in origin, for how much of the translate to blend in
725                 if (c == 1)
726                 {
727                         matrix = &zymbonepose[vert->bonenum];
728                         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];
729                         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];
730                         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];
731                         vert++;
732                 }
733                 else
734                 {
735                         VectorClear(out);
736                         while(c--)
737                         {
738                                 matrix = &zymbonepose[vert->bonenum];
739                                 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];
740                                 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];
741                                 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];
742                                 vert++;
743                         }
744                 }
745                 out += 3;
746         }
747 }
748
749 void ZymoticCalcNormal3f(int vertcount, float *vertex3f, float *normal3f, int shadercount, int *renderlist)
750 {
751         int a, b, c, d;
752         float *out, v1[3], v2[3], normal[3], s;
753         int *u;
754         // clear normals
755         memset(normal3f, 0, sizeof(float) * vertcount * 3);
756         memset(aliasvertusage, 0, sizeof(int) * vertcount);
757         // parse render list and accumulate surface normals
758         while(shadercount--)
759         {
760                 d = *renderlist++;
761                 while (d--)
762                 {
763                         a = renderlist[0]*4;
764                         b = renderlist[1]*4;
765                         c = renderlist[2]*4;
766                         v1[0] = vertex3f[a+0] - vertex3f[b+0];
767                         v1[1] = vertex3f[a+1] - vertex3f[b+1];
768                         v1[2] = vertex3f[a+2] - vertex3f[b+2];
769                         v2[0] = vertex3f[c+0] - vertex3f[b+0];
770                         v2[1] = vertex3f[c+1] - vertex3f[b+1];
771                         v2[2] = vertex3f[c+2] - vertex3f[b+2];
772                         CrossProduct(v1, v2, normal);
773                         VectorNormalizeFast(normal);
774                         // add surface normal to vertices
775                         a = renderlist[0] * 3;
776                         normal3f[a+0] += normal[0];
777                         normal3f[a+1] += normal[1];
778                         normal3f[a+2] += normal[2];
779                         aliasvertusage[renderlist[0]]++;
780                         a = renderlist[1] * 3;
781                         normal3f[a+0] += normal[0];
782                         normal3f[a+1] += normal[1];
783                         normal3f[a+2] += normal[2];
784                         aliasvertusage[renderlist[1]]++;
785                         a = renderlist[2] * 3;
786                         normal3f[a+0] += normal[0];
787                         normal3f[a+1] += normal[1];
788                         normal3f[a+2] += normal[2];
789                         aliasvertusage[renderlist[2]]++;
790                         renderlist += 3;
791                 }
792         }
793         // FIXME: precalc this
794         // average surface normals
795         out = normal3f;
796         u = aliasvertusage;
797         while(vertcount--)
798         {
799                 if (*u > 1)
800                 {
801                         s = ixtable[*u];
802                         out[0] *= s;
803                         out[1] *= s;
804                         out[2] *= s;
805                 }
806                 u++;
807                 out += 3;
808         }
809 }
810
811 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
812 {
813         float fog, ifog, colorscale, ambientcolor4f[4];
814         vec3_t diff;
815         int i, *renderlist, *elements;
816         rtexture_t *texture;
817         rmeshstate_t mstate;
818         const entity_render_t *ent = calldata1;
819         int shadernum = calldata2;
820         int numverts, numtriangles;
821
822         R_Mesh_Matrix(&ent->matrix);
823
824         // find the vertex index list and texture
825         renderlist = ent->model->zymdata_renderlist;
826         for (i = 0;i < shadernum;i++)
827                 renderlist += renderlist[0] * 3 + 1;
828         texture = ent->model->zymdata_textures[shadernum];
829
830         numverts = ent->model->zymnum_verts;
831         numtriangles = *renderlist++;
832         elements = renderlist;
833
834         expandaliasvert(numverts);
835
836         fog = 0;
837         if (fogenabled)
838         {
839                 VectorSubtract(ent->origin, r_origin, diff);
840                 fog = DotProduct(diff,diff);
841                 if (fog < 0.01f)
842                         fog = 0.01f;
843                 fog = exp(fogdensity/fog);
844                 if (fog > 1)
845                         fog = 1;
846                 if (fog < 0.01f)
847                         fog = 0;
848                 // fog method: darken, additive fog
849                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
850                 // 2. render fog as additive
851         }
852         ifog = 1 - fog;
853
854         memset(&mstate, 0, sizeof(mstate));
855         if (ent->effects & EF_ADDITIVE)
856         {
857                 mstate.blendfunc1 = GL_SRC_ALPHA;
858                 mstate.blendfunc2 = GL_ONE;
859         }
860         else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
861         {
862                 mstate.blendfunc1 = GL_SRC_ALPHA;
863                 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
864         }
865         else
866         {
867                 mstate.blendfunc1 = GL_ONE;
868                 mstate.blendfunc2 = GL_ZERO;
869         }
870         colorscale = r_colorscale;
871         if (gl_combine.integer)
872         {
873                 mstate.texrgbscale[0] = 4;
874                 colorscale *= 0.25f;
875         }
876         mstate.tex[0] = R_GetTexture(texture);
877         R_Mesh_State(&mstate);
878         ZymoticLerpBones(ent->model->zymnum_bones, (zymbonematrix *) ent->model->zymdata_poses, ent->frameblend, ent->model->zymdata_bones);
879
880         R_Mesh_GetSpace(numverts);
881         ZymoticTransformVerts(numverts, varray_vertex3f, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
882         R_Mesh_CopyTexCoord2f(0, ent->model->zymdata_texcoords, ent->model->zymnum_verts);
883         ZymoticCalcNormal3f(numverts, varray_vertex3f, aliasvert_normal3f, ent->model->zymnum_shaders, ent->model->zymdata_renderlist);
884         if (R_LightModel(ambientcolor4f, ent, ifog * colorscale, ifog * colorscale, ifog * colorscale, ent->alpha, false))
885         {
886                 GL_UseColorArray();
887                 R_LightModel_CalcVertexColors(ambientcolor4f, numverts, varray_vertex3f, aliasvert_normal3f, varray_color4f);
888         }
889         else
890                 GL_Color(ambientcolor4f[0], ambientcolor4f[1], ambientcolor4f[2], ambientcolor4f[3]);
891         R_Mesh_Draw(numverts, numtriangles, elements);
892         c_alias_polys += numtriangles;
893
894         if (fog)
895         {
896                 memset(&mstate, 0, sizeof(mstate));
897                 mstate.blendfunc1 = GL_SRC_ALPHA;
898                 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
899                 // FIXME: need alpha mask for fogging...
900                 //mstate.tex[0] = R_GetTexture(texture);
901                 R_Mesh_State(&mstate);
902                 GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, ent->alpha * fog);
903                 R_Mesh_GetSpace(numverts);
904                 ZymoticTransformVerts(numverts, varray_vertex3f, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
905                 R_Mesh_Draw(numverts, numtriangles, elements);
906                 c_alias_polys += numtriangles;
907         }
908 }
909
910 void R_Model_Zymotic_Draw(entity_render_t *ent)
911 {
912         int i;
913
914         if (ent->alpha < (1.0f / 64.0f))
915                 return; // basically completely transparent
916
917         c_models++;
918
919         for (i = 0;i < ent->model->zymnum_shaders;i++)
920         {
921                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(ent->model->zymdata_textures[i]))
922                         R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
923                 else
924                         R_DrawZymoticModelMeshCallback(ent, i);
925         }
926 }
927
928 void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent)
929 {
930         // FIXME
931 }
932
933 void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, float lightradius2, float lightdistbias, float lightsubtract, float *lightcolor)
934 {
935         // FIXME
936 }
937
938 void R_Model_Zymotic_DrawOntoLight(entity_render_t *ent)
939 {
940         // FIXME
941 }
942