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