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