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