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