optimized AngleVectors calls (pass NULL for vectors that should not be generated)
[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_Init()
96 {
97         Cvar_RegisterVariable(&gl_transform);
98         Cvar_RegisterVariable(&gl_lockarrays);
99
100         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown);
101 }
102
103 extern vec3_t softwaretransform_x;
104 extern vec3_t softwaretransform_y;
105 extern vec3_t softwaretransform_z;
106 extern vec_t softwaretransform_scale;
107 extern vec3_t softwaretransform_offset;
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         VectorScaleQuick(fscale1, lerp1, scale1);
154         if (lerp2)
155         {
156                 VectorScaleQuick(fscale2, lerp2, scale2);
157                 if (lerp3)
158                 {
159                         VectorScaleQuick(fscale3, lerp3, scale3);
160                         if (lerp4)
161                         {
162                                 VectorScaleQuick(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 *maliashdr)
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, 0, colors);
284                 glEnableClientState(GL_COLOR_ARRAY);
285         }
286
287         glDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->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 extern vec3_t lightspot;
321 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor);
322 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)
323 {
324         if (gl_transform.value)
325         {
326                 if (r_render.value)
327                 {
328                         glPushMatrix();
329                         GL_SetupModelTransform(org, angles, scale);
330                 }
331         }
332         // always needed, for model lighting
333         softwaretransformforentity(ent);
334
335         R_AliasLerpVerts(maliashdr->numverts,
336                 blend[0].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[0].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin,
337                 blend[1].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[1].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin,
338                 blend[2].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[2].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin,
339                 blend[3].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[3].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin);
340         if (!gl_transform.value)
341                 R_AliasTransformVerts(maliashdr->numverts);
342
343         // prep the vertex array as early as possible
344         if (r_render.value)
345         {
346                 glVertexPointer(3, GL_FLOAT, 0, aliasvert);
347                 glEnableClientState(GL_VERTEX_ARRAY);
348                 glTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr));
349                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
350                 GL_LockArray(0, maliashdr->numverts);
351         }
352
353         R_LightModel(maliashdr->numverts, org, color);
354
355         if (!r_render.value)
356                 return;
357         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
358         glShadeModel(GL_SMOOTH);
359         if (effects & EF_ADDITIVE)
360         {
361                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
362                 glEnable(GL_BLEND);
363                 glDepthMask(0);
364         }
365         else if (alpha != 1.0)
366         {
367                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
368                 glEnable(GL_BLEND);
369                 glDepthMask(0);
370         }
371         else
372         {
373                 glDisable(GL_BLEND);
374                 glDepthMask(1);
375         }
376
377         if (skin[0] || skin[1] || skin[2] || skin[3] || skin[4])
378         {
379                 if (colormap >= 0 && (skin[0] || skin[1] || skin[2]))
380                 {
381                         int c;
382                         if (skin[0])
383                                 GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
384                         if (skin[1])
385                         {
386                                 c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
387                                 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
388                                 GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr);
389                         }
390                         if (skin[2])
391                         {
392                                 c = colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
393                                 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
394                                 GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr);
395                         }
396                 }
397                 else
398                 {
399                         if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr);
400                         else
401                         {
402                                 if (skin[0]) GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
403                                 if (skin[1]) GL_DrawModelMesh(skin[1], aliasvertcolor, maliashdr);
404                                 if (skin[2]) GL_DrawModelMesh(skin[2], aliasvertcolor, maliashdr);
405                         }
406                 }
407                 if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
408         }
409         else
410                 GL_DrawModelMesh(0, NULL, maliashdr);
411
412         if (fogenabled)
413         {
414                 vec3_t diff;
415                 glDisable (GL_TEXTURE_2D);
416                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
417                 glEnable (GL_BLEND);
418                 glDepthMask(0); // disable zbuffer updates
419
420                 VectorSubtract(org, r_refdef.vieworg, diff);
421                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
422
423                 glDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
424
425                 glEnable (GL_TEXTURE_2D);
426                 glColor3f (1,1,1);
427         }
428         GL_UnlockArray();
429         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
430         glDisableClientState(GL_VERTEX_ARRAY);
431
432         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
433         glEnable (GL_BLEND);
434         glDepthMask(1);
435
436         glPopMatrix();
437 }
438
439 /*
440 =================
441 R_DrawQ2AliasFrame
442
443 =================
444 */
445 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)
446 {
447         int *order, count;
448         md2frame_t *frame1, *frame2, *frame3, *frame4;
449
450         if (r_render.value)
451                 glBindTexture(GL_TEXTURE_2D, R_GetTexture(skin));
452
453         if (gl_transform.value)
454         {
455                 if (r_render.value)
456                 {
457                         glPushMatrix();
458                         GL_SetupModelTransform(org, angles, scale);
459                 }
460         }
461         // always needed, for model lighting
462         softwaretransformforentity(ent);
463
464         frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[0].frame));
465         frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[1].frame));
466         frame3 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[2].frame));
467         frame4 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[3].frame));
468         R_AliasLerpVerts(pheader->num_xyz,
469                 blend[0].lerp, frame1->verts, frame1->scale, frame1->translate,
470                 blend[1].lerp, frame2->verts, frame2->scale, frame2->translate,
471                 blend[2].lerp, frame3->verts, frame3->scale, frame3->translate,
472                 blend[3].lerp, frame4->verts, frame4->scale, frame4->translate);
473         if (!gl_transform.value)
474                 R_AliasTransformVerts(pheader->num_xyz);
475
476         R_LightModel(pheader->num_xyz, org, color);
477
478         if (!r_render.value)
479                 return;
480         // LordHavoc: big mess...
481         // using vertex arrays only slightly, although it is enough to prevent duplicates
482         // (saving half the transforms)
483         glVertexPointer(3, GL_FLOAT, 0, aliasvert);
484         glColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
485         glEnableClientState(GL_VERTEX_ARRAY);
486         glEnableClientState(GL_COLOR_ARRAY);
487
488         order = (int *)((int)pheader + pheader->ofs_glcmds);
489         while(1)
490         {
491                 if (!(count = *order++))
492                         break;
493                 if (count > 0)
494                         glBegin(GL_TRIANGLE_STRIP);
495                 else
496                 {
497                         glBegin(GL_TRIANGLE_FAN);
498                         count = -count;
499                 }
500                 do
501                 {
502                         glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
503                         glArrayElement(order[2]);
504                         order += 3;
505                 }
506                 while (count--);
507         }
508
509         glDisableClientState(GL_COLOR_ARRAY);
510         glDisableClientState(GL_VERTEX_ARRAY);
511
512         if (fogenabled)
513         {
514                 glDisable (GL_TEXTURE_2D);
515                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
516                 glEnable (GL_BLEND);
517                 glDepthMask(0); // disable zbuffer updates
518                 {
519                         vec3_t diff;
520                         VectorSubtract(org, r_refdef.vieworg, diff);
521                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
522                 }
523
524                 // LordHavoc: big mess...
525                 // using vertex arrays only slightly, although it is enough to prevent duplicates
526                 // (saving half the transforms)
527                 glVertexPointer(3, GL_FLOAT, 0, aliasvert);
528                 glEnableClientState(GL_VERTEX_ARRAY);
529
530                 order = (int *)((int)pheader + pheader->ofs_glcmds);
531                 while(1)
532                 {
533                         if (!(count = *order++))
534                                 break;
535                         if (count > 0)
536                                 glBegin(GL_TRIANGLE_STRIP);
537                         else
538                         {
539                                 glBegin(GL_TRIANGLE_FAN);
540                                 count = -count;
541                         }
542                         do
543                         {
544                                 glArrayElement(order[2]);
545                                 order += 3;
546                         }
547                         while (count--);
548                 }
549
550                 glDisableClientState(GL_VERTEX_ARRAY);
551
552                 glEnable (GL_TEXTURE_2D);
553                 glColor3f (1,1,1);
554         }
555
556         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
557         glEnable (GL_BLEND);
558         glDepthMask(1);
559
560         if (gl_transform.value)
561                 glPopMatrix();
562 }
563
564 void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone, float rootorigin[3], float rootangles[3], float rootscale)
565 {
566         float lerp1, lerp2, lerp3, lerp4;
567         zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
568         lerp1 = 1 - lerp2;
569         out = zymbonepose;
570         AngleVectors(rootangles, rootmatrix.m[0], rootmatrix.m[1], rootmatrix.m[2]);
571         VectorScale(rootmatrix.m[0], rootscale, rootmatrix.m[0]);
572         VectorScale(rootmatrix.m[1], rootscale, rootmatrix.m[1]);
573         VectorScale(rootmatrix.m[2], rootscale, rootmatrix.m[2]);
574         rootmatrix.m[0][3] = rootorigin[0];
575         rootmatrix.m[1][3] = rootorigin[1];
576         rootmatrix.m[2][3] = rootorigin[2];
577         bone1 = bonebase + blend[0].frame * count;
578         lerp1 = blend[0].lerp;
579         if (blend[1].lerp)
580         {
581                 bone2 = bonebase + blend[1].frame * count;
582                 lerp2 = blend[1].lerp;
583                 if (blend[2].lerp)
584                 {
585                         bone3 = bonebase + blend[2].frame * count;
586                         lerp3 = blend[2].lerp;
587                         if (blend[3].lerp)
588                         {
589                                 // 4 poses
590                                 bone4 = bonebase + blend[3].frame * count;
591                                 lerp4 = blend[3].lerp;
592                                 while(count--)
593                                 {
594                                         // interpolate matrices
595                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
596                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
597                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
598                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
599                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
600                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
601                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
602                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
603                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
604                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
605                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
606                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
607                                         if (bone->parent >= 0)
608                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
609                                         else
610                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
611                                         bone1++;
612                                         bone2++;
613                                         bone3++;
614                                         bone4++;
615                                         bone++;
616                                         out++;
617                                 }
618                         }
619                         else
620                         {
621                                 // 3 poses
622                                 while(count--)
623                                 {
624                                         // interpolate matrices
625                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
626                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
627                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
628                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
629                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
630                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
631                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
632                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
633                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
634                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
635                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
636                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
637                                         if (bone->parent >= 0)
638                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
639                                         else
640                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
641                                         bone1++;
642                                         bone2++;
643                                         bone3++;
644                                         bone++;
645                                         out++;
646                                 }
647                         }
648                 }
649                 else
650                 {
651                         // 2 poses
652                         while(count--)
653                         {
654                                 // interpolate matrices
655                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
656                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
657                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
658                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
659                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
660                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
661                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
662                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
663                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
664                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
665                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
666                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
667                                 if (bone->parent >= 0)
668                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
669                                 else
670                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
671                                 bone1++;
672                                 bone2++;
673                                 bone++;
674                                 out++;
675                         }
676                 }
677         }
678         else
679         {
680                 // 1 pose
681                 if (lerp1 != 1)
682                 {
683                         // lerp != 1.0
684                         while(count--)
685                         {
686                                 // interpolate matrices
687                                 m.m[0][0] = bone1->m[0][0] * lerp1;
688                                 m.m[0][1] = bone1->m[0][1] * lerp1;
689                                 m.m[0][2] = bone1->m[0][2] * lerp1;
690                                 m.m[0][3] = bone1->m[0][3] * lerp1;
691                                 m.m[1][0] = bone1->m[1][0] * lerp1;
692                                 m.m[1][1] = bone1->m[1][1] * lerp1;
693                                 m.m[1][2] = bone1->m[1][2] * lerp1;
694                                 m.m[1][3] = bone1->m[1][3] * lerp1;
695                                 m.m[2][0] = bone1->m[2][0] * lerp1;
696                                 m.m[2][1] = bone1->m[2][1] * lerp1;
697                                 m.m[2][2] = bone1->m[2][2] * lerp1;
698                                 m.m[2][3] = bone1->m[2][3] * lerp1;
699                                 if (bone->parent >= 0)
700                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
701                                 else
702                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
703                                 bone1++;
704                                 bone++;
705                                 out++;
706                         }
707                 }
708                 else
709                 {
710                         // lerp == 1.0
711                         while(count--)
712                         {
713                                 if (bone->parent >= 0)
714                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]);
715                                 else
716                                         R_ConcatTransforms(&rootmatrix.m[0], &bone1->m[0], &out->m[0]);
717                                 bone1++;
718                                 bone++;
719                                 out++;
720                         }
721                 }
722         }
723 }
724
725 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
726 {
727         int c;
728         float *out = aliasvert;
729         zymbonematrix *matrix;
730         while(vertcount--)
731         {
732                 c = *bonecounts++;
733                 if (c == 1)
734                 {
735                         matrix = &zymbonepose[vert->bonenum];
736                         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];
737                         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];
738                         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];
739                         vert++;
740                 }
741                 else
742                 {
743                         VectorClear(out);
744                         while(c--)
745                         {
746                                 matrix = &zymbonepose[vert->bonenum];
747                                 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];
748                                 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];
749                                 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];
750                                 vert++;
751                         }
752                 }
753                 out += 3;
754         }
755 }
756
757 float ixtable[4096];
758
759 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
760 {
761         int a, b, c, d;
762         float *out, v1[3], v2[3], normal[3];
763         int *u;
764         if (!ixtable[1])
765         {
766                 ixtable[0] = 0;
767                 for (a = 1;a < 4096;a++)
768                         ixtable[a] = 1.0f / a;
769         }
770         // clear normals
771         memset(aliasvertnorm, 0, sizeof(float[3]) * vertcount);
772         memset(aliasvertusage, 0, sizeof(int) * vertcount);
773         // parse render list and accumulate surface normals
774         while(shadercount--)
775         {
776                 d = *renderlist++;
777                 while (d--)
778                 {
779                         a = renderlist[0]*3;
780                         b = renderlist[1]*3;
781                         c = renderlist[2]*3;
782                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
783                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
784                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
785                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
786                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
787                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
788                         CrossProduct(v1, v2, normal);
789                         VectorNormalize(normal);
790                         // add surface normal to vertices
791                         aliasvertnorm[a+0] += normal[0];
792                         aliasvertnorm[a+1] += normal[1];
793                         aliasvertnorm[a+2] += normal[2];
794                         aliasvertusage[a]++;
795                         aliasvertnorm[b+0] += normal[0];
796                         aliasvertnorm[b+1] += normal[1];
797                         aliasvertnorm[b+2] += normal[2];
798                         aliasvertusage[b]++;
799                         aliasvertnorm[c+0] += normal[0];
800                         aliasvertnorm[c+1] += normal[1];
801                         aliasvertnorm[c+2] += normal[2];
802                         aliasvertusage[c]++;
803                         renderlist += 3;
804                 }
805         }
806         // average surface normals
807         out = aliasvertnorm;
808         u = aliasvertusage;
809         while(vertcount--)
810         {
811                 if (*u > 1)
812                 {
813                         a = ixtable[*u];
814                         out[0] *= a;
815                         out[1] *= a;
816                         out[2] *= a;
817                 }
818                 u++;
819                 out += 3;
820         }
821 }
822
823 void GL_DrawZymoticModelMesh(byte *colors, zymtype1header_t *m)
824 {
825         int i, c, *renderlist;
826         rtexture_t **texture;
827         if (!r_render.value)
828                 return;
829         renderlist = (int *)(m->lump_render.start + (int) m);
830         texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
831         glVertexPointer(3, GL_FLOAT, 0, aliasvert);
832         glEnableClientState(GL_VERTEX_ARRAY);
833
834         glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
835         glEnableClientState(GL_COLOR_ARRAY);
836
837         glTexCoordPointer(2, GL_FLOAT, 0, (float *)(m->lump_texcoords.start + (int) m));
838         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
839
840         for (i = 0;i < m->numshaders;i++)
841         {
842                 c = (*renderlist++) * 3;
843                 glBindTexture(GL_TEXTURE_2D, R_GetTexture(*texture));
844                 texture++;
845                 glDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
846                 renderlist += c;
847         }
848
849         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
850
851         glDisableClientState(GL_COLOR_ARRAY);
852
853         glDisableClientState(GL_VERTEX_ARRAY);
854 }
855
856 void GL_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m)
857 {
858         vec3_t diff;
859         int i, c, *renderlist;
860         if (!r_render.value)
861                 return;
862         renderlist = (int *)(m->lump_render.start + (int) m);
863         glDisable(GL_TEXTURE_2D);
864         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
865         glEnable (GL_BLEND);
866         glDepthMask(0); // disable zbuffer updates
867
868         VectorSubtract(org, r_refdef.vieworg, diff);
869         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
870
871         glVertexPointer(3, GL_FLOAT, 0, aliasvert);
872         glEnableClientState(GL_VERTEX_ARRAY);
873
874         for (i = 0;i < m->numshaders;i++)
875         {
876                 c = (*renderlist++) * 3;
877                 glDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist);
878                 renderlist += c;
879         }
880
881         glDisableClientState(GL_VERTEX_ARRAY);
882
883         glEnable(GL_TEXTURE_2D);
884         glColor3f (1,1,1);
885 }
886
887 /*
888 =================
889 R_DrawZymoticFrame
890 =================
891 */
892 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)
893 {
894         ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), blend, (zymbone_t *)(m->lump_bones.start + (int) m), org, angles, scale);
895         ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
896         ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
897
898         R_LightModel(m->numverts, org, color);
899
900         if (!r_render.value)
901                 return;
902         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
903         glShadeModel(GL_SMOOTH);
904         if (effects & EF_ADDITIVE)
905         {
906                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
907                 glEnable(GL_BLEND);
908                 glDepthMask(0);
909         }
910         else if (alpha != 1.0)
911         {
912                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
913                 glEnable(GL_BLEND);
914                 glDepthMask(0);
915         }
916         else
917         {
918                 glDisable(GL_BLEND);
919                 glDepthMask(1);
920         }
921
922         GL_DrawZymoticModelMesh(aliasvertcolor, m);
923
924         if (fogenabled)
925                 GL_DrawZymoticModelMeshFog(org, m);
926
927         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
928         glEnable (GL_BLEND);
929         glDepthMask(1);
930 }
931
932 extern int r_dlightframecount;
933
934 /*
935 =================
936 R_DrawAliasModel
937
938 =================
939 */
940 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)
941 {
942         int                     i;
943         vec3_t          mins, maxs, color;
944         void            *modelheader;
945         rtexture_t      **skinset;
946
947         if (alpha < (1.0 / 64.0))
948                 return; // basically completely transparent
949
950         VectorAdd (org, clmodel->mins, mins);
951         VectorAdd (org, clmodel->maxs, maxs);
952
953         if (cull && R_CullBox (mins, maxs))
954                 return;
955
956         c_models++;
957
958         if (skin < 0 || skin >= clmodel->numskins)
959         {
960                 skin = 0;
961                 Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name);
962         }
963
964         modelheader = Mod_Extradata (clmodel);
965
966         {
967 //              int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2;
968 //              int *skinanim = (int *) (clmodel->skinanim + (int) modelheader);
969                 int *skinanimrange = clmodel->skinanimrange + skin * 2;
970                 rtexture_t **skinanim = clmodel->skinanim;
971                 i = skinanimrange[0];
972                 if (skinanimrange[1] > 1) // animated
973                         i += ((int) (cl.time * 10) % skinanimrange[1]);
974                 skinset = skinanim + i*5;
975         }
976
977         if (r_render.value)
978                 glEnable (GL_TEXTURE_2D);
979
980         c_alias_polys += clmodel->numtris;
981         if (clmodel->aliastype == ALIASTYPE_ZYM)
982                 R_DrawZymoticFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, scale, blend, 0                   , effects, flags);
983         else if (clmodel->aliastype == ALIASTYPE_MD2)
984                 R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, scale, blend, skinset[0]          , effects, flags);
985         else
986                 R_DrawAliasFrame   (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, scale, blend, skinset   , colormap, effects, flags);
987 }