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