rewrote RecursiveHullCheck, no longer gets stuck on angle changes, and is generally...
[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 byte *aliasvertcolor;
16 byte *aliasvertcolor2;
17 zymbonematrix *zymbonepose;
18 int *aliasvertusage;
19
20 rtexture_t *chrometexture;
21
22 int arraylocked = false;
23 void GL_LockArray(int first, int count)
24 {
25         if (gl_supportslockarrays && gl_lockarrays.value)
26         {
27                 qglLockArraysEXT(first, count);
28                 arraylocked = true;
29         }
30 }
31
32 void GL_UnlockArray(void)
33 {
34         if (arraylocked)
35         {
36                 qglUnlockArraysEXT();
37                 arraylocked = false;
38         }
39 }
40
41 void GL_SetupModelTransform (vec3_t origin, vec3_t angles, vec_t scale)
42 {
43     glTranslatef (origin[0], origin[1], origin[2]);
44
45         if (scale != 1)
46                 glScalef (scale, scale, scale);
47         if (angles[1])
48             glRotatef (angles[1],  0, 0, 1);
49         if (angles[0])
50             glRotatef (-angles[0],  0, 1, 0);
51         if (angles[2])
52             glRotatef (angles[2],  1, 0, 0);
53 }
54
55 // currently unused reflection effect texture
56 void makechrometexture(void)
57 {
58         int i;
59         byte noise[64*64];
60         byte data[64*64][4];
61
62         fractalnoise(noise, 64, 8);
63
64         // convert to RGBA data
65         for (i = 0;i < 64*64;i++)
66         {
67                 data[i][0] = data[i][1] = data[i][2] = noise[i];
68                 data[i][3] = 255;
69         }
70
71         chrometexture = R_LoadTexture ("chrometexture", 64, 64, &data[0][0], TEXF_MIPMAP | TEXF_RGBA | TEXF_PRECACHE);
72 }
73
74 void gl_models_start(void)
75 {
76         // allocate vertex processing arrays
77         aliasvert = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
78         aliasvertnorm = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
79         aliasvertcolor = qmalloc(sizeof(byte[MD2MAX_VERTS][4]));
80         aliasvertcolor2 = qmalloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
81         zymbonepose = qmalloc(sizeof(zymbonematrix[256]));
82         aliasvertusage = qmalloc(sizeof(int[MD2MAX_VERTS]));
83         makechrometexture();
84 }
85
86 void gl_models_shutdown(void)
87 {
88         qfree(aliasvert);
89         qfree(aliasvertnorm);
90         qfree(aliasvertcolor);
91         qfree(aliasvertcolor2);
92         qfree(zymbonepose);
93         qfree(aliasvertusage);
94 }
95
96 void gl_models_newmap(void)
97 {
98 }
99
100 void GL_Models_Init(void)
101 {
102         Cvar_RegisterVariable(&gl_transform);
103         Cvar_RegisterVariable(&gl_lockarrays);
104
105         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
106 }
107
108 void R_AliasTransformVerts(int vertcount)
109 {
110         int i;
111         vec3_t point, matrix_x, matrix_y, matrix_z;
112         float *av, *avn;
113         av = aliasvert;
114         avn = aliasvertnorm;
115         matrix_x[0] = softwaretransform_x[0] * softwaretransform_scale;
116         matrix_x[1] = softwaretransform_y[0] * softwaretransform_scale;
117         matrix_x[2] = softwaretransform_z[0] * softwaretransform_scale;
118         matrix_y[0] = softwaretransform_x[1] * softwaretransform_scale;
119         matrix_y[1] = softwaretransform_y[1] * softwaretransform_scale;
120         matrix_y[2] = softwaretransform_z[1] * softwaretransform_scale;
121         matrix_z[0] = softwaretransform_x[2] * softwaretransform_scale;
122         matrix_z[1] = softwaretransform_y[2] * softwaretransform_scale;
123         matrix_z[2] = softwaretransform_z[2] * softwaretransform_scale;
124         for (i = 0;i < vertcount;i++)
125         {
126                 // rotate, scale, and translate the vertex locations
127                 VectorCopy(av, point);
128                 av[0] = DotProduct(point, matrix_x) + softwaretransform_offset[0];
129                 av[1] = DotProduct(point, matrix_y) + softwaretransform_offset[1];
130                 av[2] = DotProduct(point, matrix_z) + softwaretransform_offset[2];
131                 // rotate the normals
132                 VectorCopy(avn, point);
133                 avn[0] = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
134                 avn[1] = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
135                 avn[2] = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
136                 av += 3;
137                 avn += 3;
138         }
139 }
140
141 void R_AliasLerpVerts(int vertcount,
142                                           float lerp1, trivertx_t *verts1, vec3_t fscale1, vec3_t translate1,
143                                           float lerp2, trivertx_t *verts2, vec3_t fscale2, vec3_t translate2,
144                                           float lerp3, trivertx_t *verts3, vec3_t fscale3, vec3_t translate3,
145                                           float lerp4, trivertx_t *verts4, vec3_t fscale4, vec3_t translate4)
146 {
147         int i;
148         vec3_t scale1, scale2, scale3, scale4, translate;
149         float *n1, *n2, *n3, *n4;
150         float *av, *avn;
151         av = aliasvert;
152         avn = aliasvertnorm;
153         VectorScale(fscale1, lerp1, scale1);
154         if (lerp2)
155         {
156                 VectorScale(fscale2, lerp2, scale2);
157                 if (lerp3)
158                 {
159                         VectorScale(fscale3, lerp3, scale3);
160                         if (lerp4)
161                         {
162                                 VectorScale(fscale4, lerp4, scale4);
163                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4;
164                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4;
165                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4;
166                                 // generate vertices
167                                 for (i = 0;i < vertcount;i++)
168                                 {
169                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + verts4->v[0] * scale4[0] + translate[0];
170                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + verts4->v[1] * scale4[1] + translate[1];
171                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + verts4->v[2] * scale4[2] + translate[2];
172                                         n1 = m_bytenormals[verts1->lightnormalindex];
173                                         n2 = m_bytenormals[verts2->lightnormalindex];
174                                         n3 = m_bytenormals[verts3->lightnormalindex];
175                                         n4 = m_bytenormals[verts4->lightnormalindex];
176                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4;
177                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4;
178                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4;
179                                         av += 3;
180                                         avn += 3;
181                                         verts1++;verts2++;verts3++;verts4++;
182                                 }
183                         }
184                         else
185                         {
186                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3;
187                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3;
188                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3;
189                                 // generate vertices
190                                 for (i = 0;i < vertcount;i++)
191                                 {
192                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + translate[0];
193                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + translate[1];
194                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + translate[2];
195                                         n1 = m_bytenormals[verts1->lightnormalindex];
196                                         n2 = m_bytenormals[verts2->lightnormalindex];
197                                         n3 = m_bytenormals[verts3->lightnormalindex];
198                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3;
199                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3;
200                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3;
201                                         av += 3;
202                                         avn += 3;
203                                         verts1++;verts2++;verts3++;
204                                 }
205                         }
206                 }
207                 else
208                 {
209                         translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2;
210                         translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2;
211                         translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2;
212                         // generate vertices
213                         for (i = 0;i < vertcount;i++)
214                         {
215                                 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + translate[0];
216                                 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + translate[1];
217                                 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + translate[2];
218                                 n1 = m_bytenormals[verts1->lightnormalindex];
219                                 n2 = m_bytenormals[verts2->lightnormalindex];
220                                 avn[0] = n1[0] * lerp1 + n2[0] * lerp2;
221                                 avn[1] = n1[1] * lerp1 + n2[1] * lerp2;
222                                 avn[2] = n1[2] * lerp1 + n2[2] * lerp2;
223                                 av += 3;
224                                 avn += 3;
225                                 verts1++;verts2++;
226                         }
227                 }
228         }
229         else
230         {
231                 translate[0] = translate1[0] * lerp1;
232                 translate[1] = translate1[1] * lerp1;
233                 translate[2] = translate1[2] * lerp1;
234                 // generate vertices
235                 if (lerp1 != 1)
236                 {
237                         // general but almost never used case
238                         for (i = 0;i < vertcount;i++)
239                         {
240                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
241                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
242                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
243                                 n1 = m_bytenormals[verts1->lightnormalindex];
244                                 avn[0] = n1[0] * lerp1;
245                                 avn[1] = n1[1] * lerp1;
246                                 avn[2] = n1[2] * lerp1;
247                                 av += 3;
248                                 avn += 3;
249                                 verts1++;
250                         }
251                 }
252                 else
253                 {
254                         // fast normal case
255                         for (i = 0;i < vertcount;i++)
256                         {
257                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
258                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
259                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
260                                 VectorCopy(m_bytenormals[verts1->lightnormalindex], avn);
261                                 av += 3;
262                                 avn += 3;
263                                 verts1++;
264                         }
265                 }
266         }
267 }
268
269 void GL_DrawModelMesh(rtexture_t *skin, byte *colors, maliashdr_t *m)
270 {
271         if (!r_render.value)
272                 return;
273         glBindTexture(GL_TEXTURE_2D, R_GetTexture(skin));
274         if (!colors)
275         {
276                 if (lighthalf)
277                         glColor3f(0.5f, 0.5f, 0.5f);
278                 else
279                         glColor3f(1.0f, 1.0f, 1.0f);
280         }
281         if (colors)
282         {
283                 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(byte[4]), colors);
284                 glEnableClientState(GL_COLOR_ARRAY);
285         }
286
287         glDrawElements(GL_TRIANGLES, m->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) m + m->tridata));
288
289         if (colors)
290                 glDisableClientState(GL_COLOR_ARRAY);
291         // leave it in a state for additional passes
292         glDepthMask(0);
293         glEnable(GL_BLEND);
294         glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive
295 }
296
297 void R_TintModel(byte *in, byte *out, int verts, byte *color)
298 {
299         int i;
300         byte r = color[0];
301         byte g = color[1];
302         byte b = color[2];
303         for (i = 0;i < verts;i++)
304         {
305                 out[0] = (byte) ((in[0] * r) >> 8);
306                 out[1] = (byte) ((in[1] * g) >> 8);
307                 out[2] = (byte) ((in[2] * b) >> 8);
308                 out[3] =          in[3];
309                 in += 4;
310                 out += 4;
311         }
312 }
313
314 /*
315 =================
316 R_DrawAliasFrame
317
318 =================
319 */
320 void R_DrawAliasFrame (void)
321 {
322         maliashdr_t *m = Mod_Extradata(currentrenderentity->model);
323 //      int *skinanimrange = (int *) (currentrenderentity->model->skinanimrange + (int) modelheader) + skin * 2;
324 //      int *skinanim = (int *) (currentrenderentity->model->skinanim + (int) modelheader);
325         int *skinanimrange = currentrenderentity->model->skinanimrange;
326         int skin;
327         rtexture_t **skinanim = currentrenderentity->model->skinanim;
328         rtexture_t **skinset;
329
330         skinanimrange += currentrenderentity->skinnum * 2;
331         skin = skinanimrange[0];
332         if (skinanimrange[1] > 1) // animated
333                 skin += (int) (cl.time * 10) % skinanimrange[1];
334         skinset = skinanim + skin * 5;
335
336         if (gl_transform.value)
337         {
338                 if (r_render.value)
339                 {
340                         glPushMatrix();
341                         GL_SetupModelTransform(currentrenderentity->origin, currentrenderentity->angles, currentrenderentity->scale);
342                 }
343         }
344         // always needed, for model lighting
345         softwaretransformforentity(currentrenderentity);
346
347         R_AliasLerpVerts(m->numverts,
348                 currentrenderentity->frameblend[0].lerp, ((trivertx_t *)((int) m + m->posedata)) + currentrenderentity->frameblend[0].frame * m->numverts, m->scale, m->scale_origin,
349                 currentrenderentity->frameblend[1].lerp, ((trivertx_t *)((int) m + m->posedata)) + currentrenderentity->frameblend[1].frame * m->numverts, m->scale, m->scale_origin,
350                 currentrenderentity->frameblend[2].lerp, ((trivertx_t *)((int) m + m->posedata)) + currentrenderentity->frameblend[2].frame * m->numverts, m->scale, m->scale_origin,
351                 currentrenderentity->frameblend[3].lerp, ((trivertx_t *)((int) m + m->posedata)) + currentrenderentity->frameblend[3].frame * m->numverts, m->scale, m->scale_origin);
352
353         if (!gl_transform.value)
354                 R_AliasTransformVerts(m->numverts);
355
356         // prep the vertex array as early as possible
357         if (r_render.value)
358         {
359                 glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
360                 glEnableClientState(GL_VERTEX_ARRAY);
361                 glTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), (void *)((int) m->texdata + (int) m));
362                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
363                 GL_LockArray(0, m->numverts);
364         }
365
366         R_LightModel(m->numverts);
367
368         if (!r_render.value)
369                 return;
370         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
371 //      glShadeModel(GL_SMOOTH);
372         if (currentrenderentity->effects & EF_ADDITIVE)
373         {
374                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
375                 glEnable(GL_BLEND);
376                 glDepthMask(0);
377         }
378         else if (currentrenderentity->alpha != 1.0 || (currentrenderentity->model->flags2 & MODF_TRANSPARENT))
379         {
380                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
381                 glEnable(GL_BLEND);
382                 glDepthMask(0);
383         }
384         else
385         {
386                 glDisable(GL_BLEND);
387                 glDepthMask(1);
388         }
389
390         if (skinset[0] || skinset[1] || skinset[2] || skinset[3] || skinset[4])
391         {
392                 if (currentrenderentity->colormap >= 0 && (skinset[0] || skinset[1] || skinset[2]))
393                 {
394                         int c;
395                         if (skinset[0])
396                                 GL_DrawModelMesh(skinset[0], aliasvertcolor, m);
397                         if (skinset[1])
398                         {
399                                 c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
400                                 R_TintModel(aliasvertcolor, aliasvertcolor2, m->numverts, (byte *) (&d_8to24table[c]));
401                                 GL_DrawModelMesh(skinset[1], aliasvertcolor2, m);
402                         }
403                         if (skinset[2])
404                         {
405                                 c = currentrenderentity->colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
406                                 R_TintModel(aliasvertcolor, aliasvertcolor2, m->numverts, (byte *) (&d_8to24table[c]));
407                                 GL_DrawModelMesh(skinset[2], aliasvertcolor2, m);
408                         }
409                 }
410                 else
411                 {
412                         if (skinset[4])
413                                 GL_DrawModelMesh(skinset[4], aliasvertcolor, m);
414                         else
415                         {
416                                 if (skinset[0]) GL_DrawModelMesh(skinset[0], aliasvertcolor, m);
417                                 if (skinset[1]) GL_DrawModelMesh(skinset[1], aliasvertcolor, m);
418                                 if (skinset[2]) GL_DrawModelMesh(skinset[2], aliasvertcolor, m);
419                         }
420                 }
421                 if (skinset[3]) GL_DrawModelMesh(skinset[3], NULL, m);
422         }
423         else
424                 GL_DrawModelMesh(0, NULL, m);
425
426         if (fogenabled)
427         {
428                 vec3_t diff;
429                 glDisable (GL_TEXTURE_2D);
430                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
431                 glEnable (GL_BLEND);
432                 glDepthMask(0); // disable zbuffer updates
433
434                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
435                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
436
437                 glDrawElements(GL_TRIANGLES, m->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) m + m->tridata));
438
439                 glEnable (GL_TEXTURE_2D);
440                 glColor3f (1,1,1);
441         }
442
443         GL_UnlockArray();
444         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
445         glDisableClientState(GL_VERTEX_ARRAY);
446
447         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
448         glEnable (GL_BLEND);
449         glDepthMask(1);
450
451         glPopMatrix();
452 }
453
454 /*
455 =================
456 R_DrawQ2AliasFrame
457
458 =================
459 */
460 void R_DrawQ2AliasFrame (void)
461 {
462         int *order, count;
463         md2frame_t *frame1, *frame2, *frame3, *frame4;
464         vec3_t diff;
465         md2mem_t *m = Mod_Extradata(currentrenderentity->model);
466 //      int *skinanimrange = (int *) (currentrenderentity->model->skinanimrange + (int) modelheader) + skin * 2;
467 //      int *skinanim = (int *) (currentrenderentity->model->skinanim + (int) modelheader);
468         int *skinanimrange = currentrenderentity->model->skinanimrange;
469         int skin;
470         rtexture_t **skinanim = currentrenderentity->model->skinanim;
471         rtexture_t **skinset;
472
473         skinanimrange += currentrenderentity->skinnum * 2;
474         skin = skinanimrange[0];
475         if (skinanimrange[1] > 1) // animated
476                 skin += (int) (cl.time * 10) % skinanimrange[1];
477         skinset = skinanim + skin * 5;
478
479         if (r_render.value)
480                 glBindTexture(GL_TEXTURE_2D, R_GetTexture(skinset[0]));
481
482         if (gl_transform.value)
483         {
484                 if (r_render.value)
485                 {
486                         glPushMatrix();
487                         GL_SetupModelTransform(currentrenderentity->origin, currentrenderentity->angles, currentrenderentity->scale);
488                 }
489         }
490         // always needed, for model lighting
491         softwaretransformforentity(currentrenderentity);
492
493         frame1 = (void *)((int) m + m->ofs_frames + (m->framesize * currentrenderentity->frameblend[0].frame));
494         frame2 = (void *)((int) m + m->ofs_frames + (m->framesize * currentrenderentity->frameblend[1].frame));
495         frame3 = (void *)((int) m + m->ofs_frames + (m->framesize * currentrenderentity->frameblend[2].frame));
496         frame4 = (void *)((int) m + m->ofs_frames + (m->framesize * currentrenderentity->frameblend[3].frame));
497         R_AliasLerpVerts(m->num_xyz,
498                 currentrenderentity->frameblend[0].lerp, frame1->verts, frame1->scale, frame1->translate,
499                 currentrenderentity->frameblend[1].lerp, frame2->verts, frame2->scale, frame2->translate,
500                 currentrenderentity->frameblend[2].lerp, frame3->verts, frame3->scale, frame3->translate,
501                 currentrenderentity->frameblend[3].lerp, frame4->verts, frame4->scale, frame4->translate);
502         if (!gl_transform.value)
503                 R_AliasTransformVerts(m->num_xyz);
504
505         R_LightModel(m->num_xyz);
506
507         if (!r_render.value)
508                 return;
509
510         if (currentrenderentity->effects & EF_ADDITIVE)
511         {
512                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
513                 glEnable(GL_BLEND);
514                 glDepthMask(0);
515         }
516         else if (currentrenderentity->alpha != 1.0 || (currentrenderentity->model->flags2 & MODF_TRANSPARENT))
517         {
518                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
519                 glEnable(GL_BLEND);
520                 glDepthMask(0);
521         }
522         else
523         {
524                 glDisable(GL_BLEND);
525                 glDepthMask(1);
526         }
527
528         // LordHavoc: big mess...
529         // using vertex arrays only slightly, although it is enough to prevent duplicates
530         // (saving half the transforms)
531         glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
532         glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(byte[4]), aliasvertcolor);
533         glEnableClientState(GL_VERTEX_ARRAY);
534         glEnableClientState(GL_COLOR_ARRAY);
535
536         order = (int *)((int)m + m->ofs_glcmds);
537         while(1)
538         {
539                 if (!(count = *order++))
540                         break;
541                 if (count > 0)
542                         glBegin(GL_TRIANGLE_STRIP);
543                 else
544                 {
545                         glBegin(GL_TRIANGLE_FAN);
546                         count = -count;
547                 }
548                 do
549                 {
550                         glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
551                         glArrayElement(order[2]);
552                         order += 3;
553                 }
554                 while (count--);
555         }
556
557         glDisableClientState(GL_COLOR_ARRAY);
558         glDisableClientState(GL_VERTEX_ARRAY);
559
560         if (fogenabled)
561         {
562                 glDisable (GL_TEXTURE_2D);
563                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
564                 glEnable (GL_BLEND);
565                 glDepthMask(0); // disable zbuffer updates
566
567                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
568                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
569
570                 // LordHavoc: big mess...
571                 // using vertex arrays only slightly, although it is enough to prevent duplicates
572                 // (saving half the transforms)
573                 glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
574                 glEnableClientState(GL_VERTEX_ARRAY);
575
576                 order = (int *)((int)m + m->ofs_glcmds);
577                 while(1)
578                 {
579                         if (!(count = *order++))
580                                 break;
581                         if (count > 0)
582                                 glBegin(GL_TRIANGLE_STRIP);
583                         else
584                         {
585                                 glBegin(GL_TRIANGLE_FAN);
586                                 count = -count;
587                         }
588                         do
589                         {
590                                 glArrayElement(order[2]);
591                                 order += 3;
592                         }
593                         while (count--);
594                 }
595
596                 glDisableClientState(GL_VERTEX_ARRAY);
597
598                 glEnable (GL_TEXTURE_2D);
599                 glColor3f (1,1,1);
600         }
601
602         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
603         glEnable (GL_BLEND);
604         glDepthMask(1);
605
606         if (gl_transform.value)
607                 glPopMatrix();
608 }
609
610 void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone, float rootorigin[3], float rootangles[3], float rootscale)
611 {
612         float lerp1, lerp2, lerp3, lerp4;
613         zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
614         out = zymbonepose;
615         AngleVectors(rootangles, rootmatrix.m[0], rootmatrix.m[1], rootmatrix.m[2]);
616         VectorScale(rootmatrix.m[0], rootscale, rootmatrix.m[0]);
617         VectorScale(rootmatrix.m[1], rootscale, rootmatrix.m[1]);
618         VectorScale(rootmatrix.m[2], rootscale, rootmatrix.m[2]);
619         rootmatrix.m[0][3] = rootorigin[0];
620         rootmatrix.m[1][3] = rootorigin[1];
621         rootmatrix.m[2][3] = rootorigin[2];
622         bone1 = bonebase + blend[0].frame * count;
623         lerp1 = blend[0].lerp;
624         if (blend[1].lerp)
625         {
626                 bone2 = bonebase + blend[1].frame * count;
627                 lerp2 = blend[1].lerp;
628                 if (blend[2].lerp)
629                 {
630                         bone3 = bonebase + blend[2].frame * count;
631                         lerp3 = blend[2].lerp;
632                         if (blend[3].lerp)
633                         {
634                                 // 4 poses
635                                 bone4 = bonebase + blend[3].frame * count;
636                                 lerp4 = blend[3].lerp;
637                                 while(count--)
638                                 {
639                                         // interpolate matrices
640                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
641                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
642                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
643                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
644                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
645                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
646                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
647                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
648                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
649                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
650                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
651                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
652                                         if (bone->parent >= 0)
653                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
654                                         else
655                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
656                                         bone1++;
657                                         bone2++;
658                                         bone3++;
659                                         bone4++;
660                                         bone++;
661                                         out++;
662                                 }
663                         }
664                         else
665                         {
666                                 // 3 poses
667                                 while(count--)
668                                 {
669                                         // interpolate matrices
670                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
671                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
672                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
673                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
674                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
675                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
676                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
677                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
678                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
679                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
680                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
681                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
682                                         if (bone->parent >= 0)
683                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
684                                         else
685                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
686                                         bone1++;
687                                         bone2++;
688                                         bone3++;
689                                         bone++;
690                                         out++;
691                                 }
692                         }
693                 }
694                 else
695                 {
696                         // 2 poses
697                         while(count--)
698                         {
699                                 // interpolate matrices
700                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
701                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
702                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
703                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
704                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
705                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
706                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
707                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
708                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
709                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
710                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
711                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
712                                 if (bone->parent >= 0)
713                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
714                                 else
715                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
716                                 bone1++;
717                                 bone2++;
718                                 bone++;
719                                 out++;
720                         }
721                 }
722         }
723         else
724         {
725                 // 1 pose
726                 if (lerp1 != 1)
727                 {
728                         // lerp != 1.0
729                         while(count--)
730                         {
731                                 // interpolate matrices
732                                 m.m[0][0] = bone1->m[0][0] * lerp1;
733                                 m.m[0][1] = bone1->m[0][1] * lerp1;
734                                 m.m[0][2] = bone1->m[0][2] * lerp1;
735                                 m.m[0][3] = bone1->m[0][3] * lerp1;
736                                 m.m[1][0] = bone1->m[1][0] * lerp1;
737                                 m.m[1][1] = bone1->m[1][1] * lerp1;
738                                 m.m[1][2] = bone1->m[1][2] * lerp1;
739                                 m.m[1][3] = bone1->m[1][3] * lerp1;
740                                 m.m[2][0] = bone1->m[2][0] * lerp1;
741                                 m.m[2][1] = bone1->m[2][1] * lerp1;
742                                 m.m[2][2] = bone1->m[2][2] * lerp1;
743                                 m.m[2][3] = bone1->m[2][3] * lerp1;
744                                 if (bone->parent >= 0)
745                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
746                                 else
747                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
748                                 bone1++;
749                                 bone++;
750                                 out++;
751                         }
752                 }
753                 else
754                 {
755                         // lerp == 1.0
756                         while(count--)
757                         {
758                                 if (bone->parent >= 0)
759                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]);
760                                 else
761                                         R_ConcatTransforms(&rootmatrix.m[0], &bone1->m[0], &out->m[0]);
762                                 bone1++;
763                                 bone++;
764                                 out++;
765                         }
766                 }
767         }
768 }
769
770 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
771 {
772         int c;
773         float *out = aliasvert;
774         zymbonematrix *matrix;
775         while(vertcount--)
776         {
777                 c = *bonecounts++;
778                 // FIXME: validate bonecounts at load time (must be >= 1)
779                 if (c == 1)
780                 {
781                         matrix = &zymbonepose[vert->bonenum];
782                         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];
783                         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];
784                         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];
785                         vert++;
786                 }
787                 else
788                 {
789                         VectorClear(out);
790                         while(c--)
791                         {
792                                 matrix = &zymbonepose[vert->bonenum];
793                                 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];
794                                 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];
795                                 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];
796                                 vert++;
797                         }
798                 }
799                 out += 3;
800         }
801 }
802
803 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
804 {
805         int a, b, c, d;
806         float *out, v1[3], v2[3], normal[3];
807         int *u;
808         // clear normals
809         memset(aliasvertnorm, 0, sizeof(float[3]) * vertcount);
810         memset(aliasvertusage, 0, sizeof(int) * vertcount);
811         // parse render list and accumulate surface normals
812         while(shadercount--)
813         {
814                 d = *renderlist++;
815                 while (d--)
816                 {
817                         a = renderlist[0]*3;
818                         b = renderlist[1]*3;
819                         c = renderlist[2]*3;
820                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
821                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
822                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
823                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
824                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
825                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
826                         CrossProduct(v1, v2, normal);
827                         VectorNormalize(normal);
828                         // add surface normal to vertices
829                         aliasvertnorm[a+0] += normal[0];
830                         aliasvertnorm[a+1] += normal[1];
831                         aliasvertnorm[a+2] += normal[2];
832                         aliasvertusage[a]++;
833                         aliasvertnorm[b+0] += normal[0];
834                         aliasvertnorm[b+1] += normal[1];
835                         aliasvertnorm[b+2] += normal[2];
836                         aliasvertusage[b]++;
837                         aliasvertnorm[c+0] += normal[0];
838                         aliasvertnorm[c+1] += normal[1];
839                         aliasvertnorm[c+2] += normal[2];
840                         aliasvertusage[c]++;
841                         renderlist += 3;
842                 }
843         }
844         // average surface normals
845         out = aliasvertnorm;
846         u = aliasvertusage;
847         while(vertcount--)
848         {
849                 if (*u > 1)
850                 {
851                         a = ixtable[*u];
852                         out[0] *= a;
853                         out[1] *= a;
854                         out[2] *= a;
855                 }
856                 u++;
857                 out += 3;
858         }
859 }
860
861 void GL_DrawZymoticModelMesh(byte *colors, zymtype1header_t *m)
862 {
863         int i, c, *renderlist;
864         rtexture_t **texture;
865         if (!r_render.value)
866                 return;
867         renderlist = (int *)(m->lump_render.start + (int) m);
868         texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
869         glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
870         glEnableClientState(GL_VERTEX_ARRAY);
871
872         glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(byte[4]), colors);
873         glEnableClientState(GL_COLOR_ARRAY);
874
875         glTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), (float *)(m->lump_texcoords.start + (int) m));
876         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
877
878         for (i = 0;i < m->numshaders;i++)
879         {
880                 c = (*renderlist++) * 3;
881                 glBindTexture(GL_TEXTURE_2D, R_GetTexture(*texture));
882                 texture++;
883                 glDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
884                 renderlist += c;
885         }
886
887         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
888
889         glDisableClientState(GL_COLOR_ARRAY);
890
891         glDisableClientState(GL_VERTEX_ARRAY);
892 }
893
894 void GL_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m)
895 {
896         vec3_t diff;
897         int i, c, *renderlist;
898         if (!r_render.value)
899                 return;
900         renderlist = (int *)(m->lump_render.start + (int) m);
901         glDisable(GL_TEXTURE_2D);
902         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
903         glEnable (GL_BLEND);
904         glDepthMask(0); // disable zbuffer updates
905
906         VectorSubtract(org, r_origin, diff);
907         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
908
909         glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
910         glEnableClientState(GL_VERTEX_ARRAY);
911
912         for (i = 0;i < m->numshaders;i++)
913         {
914                 c = (*renderlist++) * 3;
915                 glDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
916                 renderlist += c;
917         }
918
919         glDisableClientState(GL_VERTEX_ARRAY);
920
921         glEnable(GL_TEXTURE_2D);
922         glColor3f (1,1,1);
923 }
924
925 /*
926 =================
927 R_DrawZymoticFrame
928 =================
929 */
930 void R_DrawZymoticFrame (void)
931 {
932         zymtype1header_t *m = Mod_Extradata(currentrenderentity->model);
933         ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m), currentrenderentity->origin, currentrenderentity->angles, currentrenderentity->scale);
934         ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
935         ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
936
937         R_LightModel(m->numverts);
938
939         if (!r_render.value)
940                 return;
941         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
942 //      glShadeModel(GL_SMOOTH);
943         if (currentrenderentity->effects & EF_ADDITIVE)
944         {
945                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
946                 glEnable(GL_BLEND);
947                 glDepthMask(0);
948         }
949         else if (currentrenderentity->alpha != 1.0)
950         {
951                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
952                 glEnable(GL_BLEND);
953                 glDepthMask(0);
954         }
955         else
956         {
957                 glDisable(GL_BLEND);
958                 glDepthMask(1);
959         }
960
961         GL_DrawZymoticModelMesh(aliasvertcolor, m);
962
963         if (fogenabled)
964                 GL_DrawZymoticModelMeshFog(currentrenderentity->origin, m);
965
966         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
967         glEnable (GL_BLEND);
968         glDepthMask(1);
969 }
970
971 /*
972 =================
973 R_DrawAliasModel
974
975 =================
976 */
977 void R_DrawAliasModel (void)
978 {
979         if (currentrenderentity->alpha < (1.0 / 64.0))
980                 return; // basically completely transparent
981
982         c_models++;
983
984         if (r_render.value)
985                 glEnable (GL_TEXTURE_2D);
986
987         c_alias_polys += currentrenderentity->model->numtris;
988         if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
989                 R_DrawZymoticFrame ();
990         else if (currentrenderentity->model->aliastype == ALIASTYPE_MD2)
991                 R_DrawQ2AliasFrame ();
992         else
993                 R_DrawAliasFrame   ();
994 }