major speedup to alias model fake shadow rendering
[xonotic/darkplaces.git] / gl_models.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 cvar_t r_quickmodels = {0, "r_quickmodels", "1"};
6
7 typedef struct
8 {
9         float m[3][4];
10 } zymbonematrix;
11
12 // LordHavoc: vertex arrays
13
14 float *aliasvertbuf;
15 float *aliasvertcolorbuf;
16 float *aliasvert; // this may point at aliasvertbuf or at vertex arrays in the mesh backend
17 float *aliasvertcolor; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend
18
19 float *aliasvertcolor2;
20 float *aliasvertnorm;
21 int *aliasvertusage;
22 zymbonematrix *zymbonepose;
23
24 mempool_t *gl_models_mempool;
25
26 void gl_models_start(void)
27 {
28         // allocate vertex processing arrays
29         gl_models_mempool = Mem_AllocPool("GL_Models");
30         aliasvert = aliasvertbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
31         aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
32         aliasvertnorm = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
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         Cvar_RegisterVariable(&r_quickmodels);
50
51         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
52 }
53
54 /*
55 void R_AliasTransformVerts(int vertcount)
56 {
57         vec3_t point;
58         float *av;
59         av = aliasvert;
60         while (vertcount >= 4)
61         {
62                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
63                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
64                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
65                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
66                 vertcount -= 4;
67         }
68         while(vertcount > 0)
69         {
70                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
71                 vertcount--;
72         }
73 }
74 */
75
76 void R_AliasLerpVerts(int vertcount, float *vertices, float *normals,
77                 float lerp1, const trivertx_t *verts1, const vec3_t fscale1, const vec3_t translate1,
78                 float lerp2, const trivertx_t *verts2, const vec3_t fscale2, const vec3_t translate2,
79                 float lerp3, const trivertx_t *verts3, const vec3_t fscale3, const vec3_t translate3,
80                 float lerp4, const trivertx_t *verts4, const vec3_t fscale4, const vec3_t translate4)
81 {
82         int i;
83         vec3_t scale1, scale2, scale3, scale4, translate;
84         const float *n1, *n2, *n3, *n4;
85         float *av, *avn;
86         av = vertices;
87         avn = normals;
88         VectorScale(fscale1, lerp1, scale1);
89         if (lerp2)
90         {
91                 VectorScale(fscale2, lerp2, scale2);
92                 if (lerp3)
93                 {
94                         VectorScale(fscale3, lerp3, scale3);
95                         if (lerp4)
96                         {
97                                 VectorScale(fscale4, lerp4, scale4);
98                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4;
99                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4;
100                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4;
101                                 // generate vertices
102                                 for (i = 0;i < vertcount;i++)
103                                 {
104                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + verts4->v[0] * scale4[0] + translate[0];
105                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + verts4->v[1] * scale4[1] + translate[1];
106                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + verts4->v[2] * scale4[2] + translate[2];
107                                         n1 = m_bytenormals[verts1->lightnormalindex];
108                                         n2 = m_bytenormals[verts2->lightnormalindex];
109                                         n3 = m_bytenormals[verts3->lightnormalindex];
110                                         n4 = m_bytenormals[verts4->lightnormalindex];
111                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4;
112                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4;
113                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4;
114                                         av += 4;
115                                         avn += 3;
116                                         verts1++;verts2++;verts3++;verts4++;
117                                 }
118                         }
119                         else
120                         {
121                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3;
122                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3;
123                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3;
124                                 // generate vertices
125                                 for (i = 0;i < vertcount;i++)
126                                 {
127                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + translate[0];
128                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + translate[1];
129                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + translate[2];
130                                         n1 = m_bytenormals[verts1->lightnormalindex];
131                                         n2 = m_bytenormals[verts2->lightnormalindex];
132                                         n3 = m_bytenormals[verts3->lightnormalindex];
133                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3;
134                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3;
135                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3;
136                                         av += 4;
137                                         avn += 3;
138                                         verts1++;verts2++;verts3++;
139                                 }
140                         }
141                 }
142                 else
143                 {
144                         translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2;
145                         translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2;
146                         translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2;
147                         // generate vertices
148                         for (i = 0;i < vertcount;i++)
149                         {
150                                 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + translate[0];
151                                 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + translate[1];
152                                 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + translate[2];
153                                 n1 = m_bytenormals[verts1->lightnormalindex];
154                                 n2 = m_bytenormals[verts2->lightnormalindex];
155                                 avn[0] = n1[0] * lerp1 + n2[0] * lerp2;
156                                 avn[1] = n1[1] * lerp1 + n2[1] * lerp2;
157                                 avn[2] = n1[2] * lerp1 + n2[2] * lerp2;
158                                 av += 4;
159                                 avn += 3;
160                                 verts1++;verts2++;
161                         }
162                 }
163         }
164         else
165         {
166                 translate[0] = translate1[0] * lerp1;
167                 translate[1] = translate1[1] * lerp1;
168                 translate[2] = translate1[2] * lerp1;
169                 // generate vertices
170                 if (lerp1 != 1)
171                 {
172                         // general but almost never used case
173                         for (i = 0;i < vertcount;i++)
174                         {
175                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
176                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
177                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
178                                 n1 = m_bytenormals[verts1->lightnormalindex];
179                                 avn[0] = n1[0] * lerp1;
180                                 avn[1] = n1[1] * lerp1;
181                                 avn[2] = n1[2] * lerp1;
182                                 av += 4;
183                                 avn += 3;
184                                 verts1++;
185                         }
186                 }
187                 else
188                 {
189                         // fast normal case
190                         for (i = 0;i < vertcount;i++)
191                         {
192                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
193                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
194                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
195                                 VectorCopy(m_bytenormals[verts1->lightnormalindex], avn);
196                                 av += 4;
197                                 avn += 3;
198                                 verts1++;
199                         }
200                 }
201         }
202 }
203
204 skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
205 {
206         model_t *model = ent->model;
207         unsigned int s = (unsigned int) ent->skinnum;
208         if (s >= model->numskins)
209                 s = 0;
210         if (model->skinscenes[s].framecount > 1)
211                 return &model->skinframes[model->skinscenes[s].firstframe + (int) (cl.time * 10) % model->skinscenes[s].framecount];
212         else
213                 return &model->skinframes[model->skinscenes[s].firstframe];
214 }
215
216 void R_LerpMDLMD2Vertices(const entity_render_t *ent, float *vertices, float *normals)
217 {
218         const md2frame_t *frame1, *frame2, *frame3, *frame4;
219         const trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
220         const model_t *model = ent->model;
221
222         frame1 = &model->mdlmd2data_frames[ent->frameblend[0].frame];
223         frame2 = &model->mdlmd2data_frames[ent->frameblend[1].frame];
224         frame3 = &model->mdlmd2data_frames[ent->frameblend[2].frame];
225         frame4 = &model->mdlmd2data_frames[ent->frameblend[3].frame];
226         frame1verts = &model->mdlmd2data_pose[ent->frameblend[0].frame * model->numverts];
227         frame2verts = &model->mdlmd2data_pose[ent->frameblend[1].frame * model->numverts];
228         frame3verts = &model->mdlmd2data_pose[ent->frameblend[2].frame * model->numverts];
229         frame4verts = &model->mdlmd2data_pose[ent->frameblend[3].frame * model->numverts];
230         R_AliasLerpVerts(model->numverts, vertices, normals,
231                 ent->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
232                 ent->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
233                 ent->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
234                 ent->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
235 }
236
237 void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2)
238 {
239         int i, c, pantsfullbright, shirtfullbright, colormapped, tex;
240         float pantscolor[3], shirtcolor[3];
241         float fog, colorscale;
242         vec3_t diff;
243         qbyte *bcolor;
244         rmeshstate_t m;
245         model_t *model;
246         skinframe_t *skinframe;
247         const entity_render_t *ent = calldata1;
248         int blendfunc1, blendfunc2;
249
250 //      softwaretransformforentity(ent);
251         R_Mesh_Matrix(&ent->matrix);
252
253         fog = 0;
254         if (fogenabled)
255         {
256                 VectorSubtract(ent->origin, r_origin, diff);
257                 fog = DotProduct(diff,diff);
258                 if (fog < 0.01f)
259                         fog = 0.01f;
260                 fog = exp(fogdensity/fog);
261                 if (fog > 1)
262                         fog = 1;
263                 if (fog < 0.01f)
264                         fog = 0;
265                 // fog method: darken, additive fog
266                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
267                 // 2. render fog as additive
268         }
269
270         model = ent->model;
271         R_Mesh_ResizeCheck(model->numverts);
272
273         skinframe = R_FetchSkinFrame(ent);
274
275         if (ent->effects & EF_ADDITIVE)
276         {
277                 blendfunc1 = GL_SRC_ALPHA;
278                 blendfunc2 = GL_ONE;
279         }
280         else if (ent->alpha != 1.0 || skinframe->fog != NULL)
281         {
282                 blendfunc1 = GL_SRC_ALPHA;
283                 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
284         }
285         else
286         {
287                 blendfunc1 = GL_ONE;
288                 blendfunc2 = GL_ZERO;
289         }
290
291         colorscale = r_colorscale;
292         if (gl_combine.integer)
293                 colorscale *= 0.25f;
294         
295         if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
296         {
297                 // untextured
298                 memset(&m, 0, sizeof(m));
299                 m.blendfunc1 = blendfunc1;
300                 m.blendfunc2 = blendfunc2;
301                 if (gl_combine.integer)
302                         m.texrgbscale[0] = 4;
303                 m.tex[0] = R_GetTexture(r_notexture);
304                 R_Mesh_State(&m);
305
306                 c_alias_polys += model->numtris;
307                 for (i = 0;i < model->numverts * 2;i++)
308                         varray_texcoord[0][i] = model->mdlmd2data_texcoords[i] * 8.0f;
309                 aliasvert = varray_vertex;
310                 aliasvertcolor = varray_color;
311                 R_LerpMDLMD2Vertices(ent, aliasvert, aliasvertnorm);
312                 R_LightModel(ent, model->numverts, colorscale, colorscale, colorscale, false);
313                 aliasvert = aliasvertbuf;
314                 aliasvertcolor = aliasvertcolorbuf;
315                 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
316                 return;
317         }
318
319
320         colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
321         if (!colormapped && !fog && !skinframe->glow && !skinframe->fog)
322         {
323                 // fastpath for the normal situation (one texture)
324                 memset(&m, 0, sizeof(m));
325                 m.blendfunc1 = blendfunc1;
326                 m.blendfunc2 = blendfunc2;
327                 if (gl_combine.integer)
328                         m.texrgbscale[0] = 4;
329                 m.tex[0] = R_GetTexture(skinframe->merged);
330                 R_Mesh_State(&m);
331
332                 c_alias_polys += model->numtris;
333                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
334                 aliasvert = varray_vertex;
335                 aliasvertcolor = varray_color;
336                 R_LerpMDLMD2Vertices(ent, aliasvert, aliasvertnorm);
337                 R_LightModel(ent, model->numverts, colorscale, colorscale, colorscale, false);
338                 aliasvert = aliasvertbuf;
339                 aliasvertcolor = aliasvertcolorbuf;
340                 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
341                 return;
342         }
343
344         R_LerpMDLMD2Vertices(ent, aliasvert, aliasvertnorm);
345         R_LightModel(ent, model->numverts, colorscale * (1 - fog), colorscale * (1 - fog), colorscale * (1 - fog), false);
346
347         if (colormapped)
348         {
349                 // 128-224 are backwards ranges
350                 c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
351                 bcolor = (qbyte *) (&d_8to24table[c]);
352                 pantsfullbright = c >= 224;
353                 VectorScale(bcolor, (1.0f / 255.0f), pantscolor);
354                 c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
355                 bcolor = (qbyte *) (&d_8to24table[c]);
356                 shirtfullbright = c >= 224;
357                 VectorScale(bcolor, (1.0f / 255.0f), shirtcolor);
358         }
359         else
360         {
361                 pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
362                 pantsfullbright = shirtfullbright = false;
363         }
364
365         tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
366         if (tex)
367         {
368                 memset(&m, 0, sizeof(m));
369                 m.blendfunc1 = blendfunc1;
370                 m.blendfunc2 = blendfunc2;
371                 if (gl_combine.integer)
372                         m.texrgbscale[0] = 4;
373                 m.tex[0] = tex;
374                 R_Mesh_State(&m);
375
376                 blendfunc1 = GL_SRC_ALPHA;
377                 blendfunc2 = GL_ONE;
378                 c_alias_polys += model->numtris;
379                 R_ModulateColors(aliasvertcolor, varray_color, model->numverts, colorscale, colorscale, colorscale);
380                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
381                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
382                 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
383         }
384
385         if (colormapped)
386         {
387                 if (skinframe->pants)
388                 {
389                         tex = R_GetTexture(skinframe->pants);
390                         if (tex)
391                         {
392                                 memset(&m, 0, sizeof(m));
393                                 m.blendfunc1 = blendfunc1;
394                                 m.blendfunc2 = blendfunc2;
395                                 if (gl_combine.integer)
396                                         m.texrgbscale[0] = 4;
397                                 m.tex[0] = tex;
398                                 R_Mesh_State(&m);
399
400                                 blendfunc1 = GL_SRC_ALPHA;
401                                 blendfunc2 = GL_ONE;
402                                 c_alias_polys += model->numtris;
403                                 if (pantsfullbright)
404                                         R_FillColors(varray_color, model->numverts, pantscolor[0] * colorscale, pantscolor[1] * colorscale, pantscolor[2] * colorscale, ent->alpha);
405                                 else
406                                         R_ModulateColors(aliasvertcolor, varray_color, model->numverts, pantscolor[0] * colorscale, pantscolor[1] * colorscale, pantscolor[2] * colorscale);
407                                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
408                                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
409                                 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
410                         }
411                 }
412                 if (skinframe->shirt)
413                 {
414                         tex = R_GetTexture(skinframe->shirt);
415                         if (tex)
416                         {
417                                 memset(&m, 0, sizeof(m));
418                                 m.blendfunc1 = blendfunc1;
419                                 m.blendfunc2 = blendfunc2;
420                                 if (gl_combine.integer)
421                                         m.texrgbscale[0] = 4;
422                                 m.tex[0] = tex;
423                                 R_Mesh_State(&m);
424
425                                 blendfunc1 = GL_SRC_ALPHA;
426                                 blendfunc2 = GL_ONE;
427                                 c_alias_polys += model->numtris;
428                                 if (shirtfullbright)
429                                         R_FillColors(varray_color, model->numverts, shirtcolor[0] * colorscale, shirtcolor[1] * colorscale, shirtcolor[2] * colorscale, ent->alpha);
430                                 else
431                                         R_ModulateColors(aliasvertcolor, varray_color, model->numverts, shirtcolor[0] * colorscale, shirtcolor[1] * colorscale, shirtcolor[2] * colorscale);
432                                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
433                                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
434                                 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
435                         }
436                 }
437         }
438         if (skinframe->glow)
439         {
440                 tex = R_GetTexture(skinframe->glow);
441                 if (tex)
442                 {
443                         memset(&m, 0, sizeof(m));
444                         m.blendfunc1 = blendfunc1;
445                         m.blendfunc2 = blendfunc2;
446                         m.tex[0] = tex;
447                         R_Mesh_State(&m);
448
449                         blendfunc1 = GL_SRC_ALPHA;
450                         blendfunc2 = GL_ONE;
451                         c_alias_polys += model->numtris;
452                         R_FillColors(varray_color, model->numverts, (1 - fog) * r_colorscale, (1 - fog) * r_colorscale, (1 - fog) * r_colorscale, ent->alpha);
453                         memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
454                         memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
455                         R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
456                 }
457         }
458         if (fog)
459         {
460                 memset(&m, 0, sizeof(m));
461                 m.blendfunc1 = GL_SRC_ALPHA;
462                 m.blendfunc2 = GL_ONE;
463                 m.tex[0] = R_GetTexture(skinframe->fog);
464                 R_Mesh_State(&m);
465
466                 c_alias_polys += model->numtris;
467                 R_FillColors(varray_color, model->numverts, fogcolor[0] * fog * r_colorscale, fogcolor[1] * fog * r_colorscale, fogcolor[2] * fog * r_colorscale, ent->alpha);
468                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
469                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
470                 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
471         }
472 }
473
474 void R_DrawQ1Q2AliasModelFakeShadow (entity_render_t *ent)
475 {
476         int i;
477         rmeshstate_t m;
478         model_t *model;
479         float *v, planenormal[3], planedist, dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
480
481         lightdirection[0] = 0.5;
482         lightdirection[1] = 0.2;
483         lightdirection[2] = -1;
484         VectorNormalizeFast(lightdirection);
485
486         VectorMA(ent->origin, 65536.0f, lightdirection, v2);
487         if (CL_TraceLine(ent->origin, v2, floororigin, surfnormal, 0, false, NULL) == 1)
488                 return;
489
490         R_Mesh_Matrix(&ent->matrix);
491
492         model = ent->model;
493         R_Mesh_ResizeCheck(model->numverts);
494
495         memset(&m, 0, sizeof(m));
496         m.blendfunc1 = GL_SRC_ALPHA;
497         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
498         R_Mesh_State(&m);
499
500         c_alias_polys += model->numtris;
501         R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvertnorm);
502         R_FillColors(varray_color, model->numverts, 0, 0, 0, 0.5);
503
504         // put a light direction in the entity's coordinate space
505         Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection);
506         VectorNormalizeFast(projection);
507
508         // put the plane's normal in the entity's coordinate space
509         Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal);
510         VectorNormalizeFast(planenormal);
511
512         // put the plane's distance in the entity's coordinate space
513         VectorSubtract(floororigin, ent->origin, floororigin);
514         planedist = DotProduct(floororigin, surfnormal) + 2;
515
516         dist = -1.0f / DotProduct(projection, planenormal);
517         VectorScale(projection, dist, projection);
518         for (i = 0, v = varray_vertex;i < model->numverts;i++, v += 4)
519         {
520                 dist = DotProduct(v, planenormal) - planedist;
521                 if (dist > 0)
522                 //if (i & 1)
523                         VectorMA(v, dist, projection, v);
524         }
525         R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
526 }
527
528 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
529 {
530         int i;
531         float lerp1, lerp2, lerp3, lerp4;
532         zymbonematrix *out, rootmatrix, m;
533         const zymbonematrix *bone1, *bone2, *bone3, *bone4;
534
535         /*
536         // LordHavoc: combine transform from zym coordinate space to quake coordinate space with model to world transform matrix
537         rootmatrix.m[0][0] = softwaretransform_matrix[0][1];
538         rootmatrix.m[0][1] = -softwaretransform_matrix[0][0];
539         rootmatrix.m[0][2] = softwaretransform_matrix[0][2];
540         rootmatrix.m[0][3] = softwaretransform_matrix[0][3];
541         rootmatrix.m[1][0] = softwaretransform_matrix[1][1];
542         rootmatrix.m[1][1] = -softwaretransform_matrix[1][0];
543         rootmatrix.m[1][2] = softwaretransform_matrix[1][2];
544         rootmatrix.m[1][3] = softwaretransform_matrix[1][3];
545         rootmatrix.m[2][0] = softwaretransform_matrix[2][1];
546         rootmatrix.m[2][1] = -softwaretransform_matrix[2][0];
547         rootmatrix.m[2][2] = softwaretransform_matrix[2][2];
548         rootmatrix.m[2][3] = softwaretransform_matrix[2][3];
549         */
550         rootmatrix.m[0][0] = 1;
551         rootmatrix.m[0][1] = 0;
552         rootmatrix.m[0][2] = 0;
553         rootmatrix.m[0][3] = 0;
554         rootmatrix.m[1][0] = 0;
555         rootmatrix.m[1][1] = 1;
556         rootmatrix.m[1][2] = 0;
557         rootmatrix.m[1][3] = 0;
558         rootmatrix.m[2][0] = 0;
559         rootmatrix.m[2][1] = 0;
560         rootmatrix.m[2][2] = 1;
561         rootmatrix.m[2][3] = 0;
562
563         bone1 = bonebase + blend[0].frame * count;
564         lerp1 = blend[0].lerp;
565         if (blend[1].lerp)
566         {
567                 bone2 = bonebase + blend[1].frame * count;
568                 lerp2 = blend[1].lerp;
569                 if (blend[2].lerp)
570                 {
571                         bone3 = bonebase + blend[2].frame * count;
572                         lerp3 = blend[2].lerp;
573                         if (blend[3].lerp)
574                         {
575                                 // 4 poses
576                                 bone4 = bonebase + blend[3].frame * count;
577                                 lerp4 = blend[3].lerp;
578                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
579                                 {
580                                         // interpolate matrices
581                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
582                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
583                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
584                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
585                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
586                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
587                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
588                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
589                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
590                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
591                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
592                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
593                                         if (bone->parent >= 0)
594                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
595                                         else
596                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
597                                         bone1++;
598                                         bone2++;
599                                         bone3++;
600                                         bone4++;
601                                         bone++;
602                                 }
603                         }
604                         else
605                         {
606                                 // 3 poses
607                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
608                                 {
609                                         // interpolate matrices
610                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
611                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
612                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
613                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
614                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
615                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
616                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
617                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
618                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
619                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
620                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
621                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
622                                         if (bone->parent >= 0)
623                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
624                                         else
625                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
626                                         bone1++;
627                                         bone2++;
628                                         bone3++;
629                                         bone++;
630                                 }
631                         }
632                 }
633                 else
634                 {
635                         // 2 poses
636                         for (i = 0, out = zymbonepose;i < count;i++, out++)
637                         {
638                                 // interpolate matrices
639                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
640                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
641                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
642                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
643                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
644                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
645                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
646                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
647                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
648                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
649                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
650                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
651                                 if (bone->parent >= 0)
652                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
653                                 else
654                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
655                                 bone1++;
656                                 bone2++;
657                                 bone++;
658                         }
659                 }
660         }
661         else
662         {
663                 // 1 pose
664                 if (lerp1 != 1)
665                 {
666                         // lerp != 1.0
667                         for (i = 0, out = zymbonepose;i < count;i++, out++)
668                         {
669                                 // interpolate matrices
670                                 m.m[0][0] = bone1->m[0][0] * lerp1;
671                                 m.m[0][1] = bone1->m[0][1] * lerp1;
672                                 m.m[0][2] = bone1->m[0][2] * lerp1;
673                                 m.m[0][3] = bone1->m[0][3] * lerp1;
674                                 m.m[1][0] = bone1->m[1][0] * lerp1;
675                                 m.m[1][1] = bone1->m[1][1] * lerp1;
676                                 m.m[1][2] = bone1->m[1][2] * lerp1;
677                                 m.m[1][3] = bone1->m[1][3] * lerp1;
678                                 m.m[2][0] = bone1->m[2][0] * lerp1;
679                                 m.m[2][1] = bone1->m[2][1] * lerp1;
680                                 m.m[2][2] = bone1->m[2][2] * lerp1;
681                                 m.m[2][3] = bone1->m[2][3] * lerp1;
682                                 if (bone->parent >= 0)
683                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
684                                 else
685                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
686                                 bone1++;
687                                 bone++;
688                         }
689                 }
690                 else
691                 {
692                         // lerp == 1.0
693                         for (i = 0, out = zymbonepose;i < count;i++, out++)
694                         {
695                                 if (bone->parent >= 0)
696                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
697                                 else
698                                         R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
699                                 bone1++;
700                                 bone++;
701                         }
702                 }
703         }
704         return true;
705 }
706
707 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
708 {
709         int c;
710         float *out = aliasvert;
711         zymbonematrix *matrix;
712         while(vertcount--)
713         {
714                 c = *bonecounts++;
715                 // FIXME: validate bonecounts at load time (must be >= 1)
716                 // FIXME: need 4th component in origin, for how much of the translate to blend in
717                 if (c == 1)
718                 {
719                         matrix = &zymbonepose[vert->bonenum];
720                         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];
721                         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];
722                         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];
723                         vert++;
724                 }
725                 else
726                 {
727                         VectorClear(out);
728                         while(c--)
729                         {
730                                 matrix = &zymbonepose[vert->bonenum];
731                                 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];
732                                 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];
733                                 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];
734                                 vert++;
735                         }
736                 }
737                 out += 4;
738         }
739 }
740
741 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
742 {
743         int a, b, c, d;
744         float *out, v1[3], v2[3], normal[3], s;
745         int *u;
746         // clear normals
747         memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3);
748         memset(aliasvertusage, 0, sizeof(int) * vertcount);
749         // parse render list and accumulate surface normals
750         while(shadercount--)
751         {
752                 d = *renderlist++;
753                 while (d--)
754                 {
755                         a = renderlist[0]*4;
756                         b = renderlist[1]*4;
757                         c = renderlist[2]*4;
758                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
759                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
760                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
761                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
762                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
763                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
764                         CrossProduct(v1, v2, normal);
765                         VectorNormalizeFast(normal);
766                         // add surface normal to vertices
767                         a = renderlist[0] * 3;
768                         aliasvertnorm[a+0] += normal[0];
769                         aliasvertnorm[a+1] += normal[1];
770                         aliasvertnorm[a+2] += normal[2];
771                         aliasvertusage[renderlist[0]]++;
772                         a = renderlist[1] * 3;
773                         aliasvertnorm[a+0] += normal[0];
774                         aliasvertnorm[a+1] += normal[1];
775                         aliasvertnorm[a+2] += normal[2];
776                         aliasvertusage[renderlist[1]]++;
777                         a = renderlist[2] * 3;
778                         aliasvertnorm[a+0] += normal[0];
779                         aliasvertnorm[a+1] += normal[1];
780                         aliasvertnorm[a+2] += normal[2];
781                         aliasvertusage[renderlist[2]]++;
782                         renderlist += 3;
783                 }
784         }
785         // FIXME: precalc this
786         // average surface normals
787         out = aliasvertnorm;
788         u = aliasvertusage;
789         while(vertcount--)
790         {
791                 if (*u > 1)
792                 {
793                         s = ixtable[*u];
794                         out[0] *= s;
795                         out[1] *= s;
796                         out[2] *= s;
797                 }
798                 u++;
799                 out += 3;
800         }
801 }
802
803 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
804 {
805         float fog, colorscale;
806         vec3_t diff;
807         int i, *renderlist, *elements;
808         zymtype1header_t *m;
809         rtexture_t *texture;
810         rmeshstate_t mstate;
811         const entity_render_t *ent = calldata1;
812         int shadernum = calldata2;
813         int numverts, numtriangles;
814
815         R_Mesh_Matrix(&ent->matrix);
816
817         // find the vertex index list and texture
818         m = ent->model->zymdata_header;
819         renderlist = (int *)(m->lump_render.start + (int) m);
820         for (i = 0;i < shadernum;i++)
821                 renderlist += renderlist[0] * 3 + 1;
822         texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[shadernum];
823
824         numverts = m->numverts;
825         numtriangles = *renderlist++;
826         elements = renderlist;
827         R_Mesh_ResizeCheck(numverts);
828
829         fog = 0;
830         if (fogenabled)
831         {
832                 VectorSubtract(ent->origin, r_origin, diff);
833                 fog = DotProduct(diff,diff);
834                 if (fog < 0.01f)
835                         fog = 0.01f;
836                 fog = exp(fogdensity/fog);
837                 if (fog > 1)
838                         fog = 1;
839                 if (fog < 0.01f)
840                         fog = 0;
841                 // fog method: darken, additive fog
842                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
843                 // 2. render fog as additive
844         }
845
846         ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), ent->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m));
847         ZymoticTransformVerts(numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
848         ZymoticCalcNormals(numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
849
850         R_LightModel(ent, numverts, 1 - fog, 1 - fog, 1 - fog, false);
851
852         memset(&mstate, 0, sizeof(mstate));
853         if (ent->effects & EF_ADDITIVE)
854         {
855                 mstate.blendfunc1 = GL_SRC_ALPHA;
856                 mstate.blendfunc2 = GL_ONE;
857         }
858         else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
859         {
860                 mstate.blendfunc1 = GL_SRC_ALPHA;
861                 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
862         }
863         else
864         {
865                 mstate.blendfunc1 = GL_ONE;
866                 mstate.blendfunc2 = GL_ZERO;
867         }
868         colorscale = r_colorscale;
869         if (gl_combine.integer)
870         {
871                 mstate.texrgbscale[0] = 4;
872                 colorscale *= 0.25f;
873         }
874         mstate.tex[0] = R_GetTexture(texture);
875         R_Mesh_State(&mstate);
876
877         c_alias_polys += numtriangles;
878         memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
879         R_ModulateColors(aliasvertcolor, varray_color, numverts, colorscale, colorscale, colorscale);
880         memcpy(varray_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
881         R_Mesh_Draw(numverts, numtriangles, elements);
882
883         if (fog)
884         {
885                 memset(&mstate, 0, sizeof(mstate));
886                 mstate.blendfunc1 = GL_SRC_ALPHA;
887                 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
888                 // FIXME: need alpha mask for fogging...
889                 //mstate.tex[0] = R_GetTexture(texture);
890                 R_Mesh_State(&mstate);
891
892                 c_alias_polys += numtriangles;
893                 memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
894                 R_FillColors(varray_color, numverts, fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, ent->alpha * fog);
895                 //memcpy(mesh_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
896                 R_Mesh_Draw(numverts, numtriangles, elements);
897         }
898 }
899
900 void R_DrawZymoticModel (entity_render_t *ent)
901 {
902         int i;
903         zymtype1header_t *m;
904         rtexture_t *texture;
905
906         if (ent->alpha < (1.0f / 64.0f))
907                 return; // basically completely transparent
908
909         c_models++;
910
911         m = ent->model->zymdata_header;
912         for (i = 0;i < m->numshaders;i++)
913         {
914                 texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[i];
915                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(texture))
916                         R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
917                 else
918                         R_DrawZymoticModelMeshCallback(ent, i);
919         }
920 }
921
922 void R_DrawQ1Q2AliasModel(entity_render_t *ent)
923 {
924         if (ent->alpha < (1.0f / 64.0f))
925                 return; // basically completely transparent
926
927         c_models++;
928
929         if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchSkinFrame(ent)->fog != NULL)
930                 R_MeshQueue_AddTransparent(ent->origin, R_DrawQ1Q2AliasModelCallback, ent, 0);
931         else
932                 R_DrawQ1Q2AliasModelCallback(ent, 0);
933 }