removed cruft
[divverent/darkplaces.git] / gl_models.c
1
2 #include "quakedef.h"
3
4 // LordHavoc: vertex array
5 float *aliasvert;
6 float *aliasvertnorm;
7 byte *aliasvertcolor;
8 byte *aliasvertcolor2;
9
10 int chrometexture;
11
12 void makechrometexture()
13 {
14         int i;
15         byte noise[64*64];
16         byte data[64*64][4];
17
18         fractalnoise(noise, 64);
19
20         // convert to RGBA data
21         for (i = 0;i < 64*64;i++)
22         {
23                 data[i][0] = data[i][1] = data[i][2] = noise[i];
24                 data[i][3] = 255;
25         }
26
27         chrometexture = GL_LoadTexture ("chrometexture", 64, 64, &data[0][0], true, false, 4);
28 }
29
30 void gl_models_start()
31 {
32         // allocate vertex processing arrays
33         aliasvert = malloc(sizeof(float[MD2MAX_VERTS][3]));
34         aliasvertnorm = malloc(sizeof(float[MD2MAX_VERTS][3]));
35         aliasvertcolor = malloc(sizeof(byte[MD2MAX_VERTS][4]));
36         aliasvertcolor2 = malloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
37         makechrometexture();
38 }
39
40 void gl_models_shutdown()
41 {
42         free(aliasvert);
43         free(aliasvertnorm);
44         free(aliasvertcolor);
45         free(aliasvertcolor2);
46 }
47
48 void GL_Models_Init()
49 {
50         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown);
51 }
52
53 extern vec3_t softwaretransform_x;
54 extern vec3_t softwaretransform_y;
55 extern vec3_t softwaretransform_z;
56 extern vec_t softwaretransform_scale;
57 extern vec3_t softwaretransform_offset;
58 void R_AliasLerpVerts(int vertcount, float lerp, trivert2 *verts1, vec3_t scale1, vec3_t translate1, trivert2 *verts2, vec3_t scale2, vec3_t translate2)
59 {
60         int i;
61         vec3_t point, matrix_x, matrix_y, matrix_z;
62         float *av, *avn;
63         av = aliasvert;
64         avn = aliasvertnorm;
65         VectorScale(softwaretransform_x, softwaretransform_scale, matrix_x);
66         VectorScale(softwaretransform_y, softwaretransform_scale, matrix_y);
67         VectorScale(softwaretransform_z, softwaretransform_scale, matrix_z);
68         if (lerp < 0) lerp = 0;
69         if (lerp > 1) lerp = 1;
70         if (lerp != 0)
71         {
72                 float ilerp, ilerp127, lerp127, scalex1, scalex2, translatex, scaley1, scaley2, translatey, scalez1, scalez2, translatez;
73                 if (lerp < 0) lerp = 0;
74                 if (lerp > 1) lerp = 1;
75                 ilerp = 1 - lerp;
76                 ilerp127 = ilerp * (1.0 / 127.0);
77                 lerp127 = lerp * (1.0 / 127.0);
78                 // calculate combined interpolation variables
79                 scalex1 = scale1[0] * ilerp;scalex2 = scale2[0] *  lerp;translatex = translate1[0] * ilerp + translate2[0] *  lerp;
80                 scaley1 = scale1[1] * ilerp;scaley2 = scale2[1] *  lerp;translatey = translate1[1] * ilerp + translate2[1] *  lerp;
81                 scalez1 = scale1[2] * ilerp;scalez2 = scale2[2] *  lerp;translatez = translate1[2] * ilerp + translate2[2] *  lerp;
82                 // generate vertices
83                 for (i = 0;i < vertcount;i++)
84                 {
85                         // rotate, scale, and translate the vertex locations
86                         point[0] = verts1->v[0] * scalex1 + verts2->v[0] * scalex2 + translatex;
87                         point[1] = verts1->v[1] * scaley1 + verts2->v[1] * scaley2 + translatey;
88                         point[2] = verts1->v[2] * scalez1 + verts2->v[2] * scalez2 + translatez;
89                         *av++ = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
90                         *av++ = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
91                         *av++ = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
92                         // rotate the normals
93                         point[0] = verts1->n[0] * ilerp127 + verts2->n[0] * lerp127;
94                         point[1] = verts1->n[1] * ilerp127 + verts2->n[1] * lerp127;
95                         point[2] = verts1->n[2] * ilerp127 + verts2->n[2] * lerp127;
96                         *avn++ = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
97                         *avn++ = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
98                         *avn++ = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
99                         verts1++;verts2++;
100                 }
101         }
102         else
103         {
104                 // generate vertices
105                 for (i = 0;i < vertcount;i++)
106                 {
107                         // rotate, scale, and translate the vertex locations
108                         point[0] = verts1->v[0] * scale1[0] + translate1[0];
109                         point[1] = verts1->v[1] * scale1[1] + translate1[1];
110                         point[2] = verts1->v[2] * scale1[2] + translate1[2];
111                         *av++ = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
112                         *av++ = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
113                         *av++ = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
114                         // rotate the normals
115                         point[0] = verts1->n[0] * (1.0f / 127.0f);
116                         point[1] = verts1->n[1] * (1.0f / 127.0f);
117                         point[2] = verts1->n[2] * (1.0f / 127.0f);
118                         *avn++ = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
119                         *avn++ = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
120                         *avn++ = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
121                         verts1++;
122                 }
123         }
124 }
125
126 float R_CalcAnimLerp(entity_t *ent, int pose, float lerpscale)
127 {
128         if (ent->draw_lastmodel == ent->model && ent->draw_lerpstart <= cl.time)
129         {
130                 if (pose != ent->draw_pose)
131                 {
132                         ent->draw_lastpose = ent->draw_pose;
133                         ent->draw_pose = pose;
134                         ent->draw_lerpstart = cl.time;
135                         return 0;
136                 }
137                 else
138                         return ((cl.time - ent->draw_lerpstart) * lerpscale);
139         }
140         else // uninitialized
141         {
142                 ent->draw_lastmodel = ent->model;
143                 ent->draw_lastpose = ent->draw_pose = pose;
144                 ent->draw_lerpstart = cl.time;
145                 return 0;
146         }
147 }
148
149 extern cvar_t gl_vertexarrays;
150 extern qboolean lighthalf;
151 void GL_DrawModelMesh(int skin, byte *colors, maliashdr_t *maliashdr)
152 {
153         int i;
154         glBindTexture(GL_TEXTURE_2D, skin);
155         if (!colors)
156         {
157                 if (lighthalf)
158                         glColor3f(0.5f, 0.5f, 0.5f);
159                 else
160                         glColor3f(1.0f, 1.0f, 1.0f);
161         }
162         if (gl_vertexarrays.value)
163         {
164                 if (colors)
165                 {
166                         qglColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
167                         glEnableClientState(GL_COLOR_ARRAY);
168                 }
169
170                 qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr));
171                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
172
173                 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
174
175                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
176
177                 if (colors)
178                         glDisableClientState(GL_COLOR_ARRAY);
179         }
180         else
181         {
182                 unsigned short *in, index;
183                 float *tex;
184                 in = (void *)((int) maliashdr + maliashdr->tridata);
185                 glBegin(GL_TRIANGLES);
186                 tex = (void *)((int) maliashdr + maliashdr->texdata);
187                 for (i = 0;i < maliashdr->numtris * 3;i++)
188                 {
189                         index = *in++;
190                         glTexCoord2f(tex[index*2], tex[index*2+1]);
191                         if (colors)
192                                 glColor4f(colors[index*4] * (1.0f / 255.0f), colors[index*4+1] * (1.0f / 255.0f), colors[index*4+2] * (1.0f / 255.0f), colors[index*4+3] * (1.0f / 255.0f));
193                         glVertex3fv(&aliasvert[index*3]);
194                 }
195                 glEnd();
196         }
197         // leave it in a state for additional passes
198         glDepthMask(0);
199         glEnable(GL_BLEND);
200         glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive
201 }
202
203 void R_TintModel(byte *in, byte *out, int verts, byte *color)
204 {
205         int i;
206         byte r = color[0];
207         byte g = color[1];
208         byte b = color[2];
209         for (i = 0;i < verts;i++)
210         {
211                 out[0] = (byte) ((in[0] * r) >> 8);
212                 out[1] = (byte) ((in[1] * g) >> 8);
213                 out[2] = (byte) ((in[2] * b) >> 8);
214                 out[3] =          in[3];
215                 in += 4;
216                 out += 4;
217         }
218 }
219
220 /*
221 =================
222 R_DrawAliasFrame
223
224 =================
225 */
226 extern vec3_t lightspot;
227 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor);
228 void R_DrawAliasFrame (maliashdr_t *maliashdr, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, int frame, int *skin, int colormap, int effects, int flags)
229 {
230         int             i, pose;
231         float   lerpscale, lerp;
232         maliasframe_t *frameinfo;
233
234         softwaretransformforentity(ent);
235
236         if ((frame >= maliashdr->numframes) || (frame < 0))
237         {
238                 Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
239                 frame = 0;
240         }
241
242         frameinfo = ((maliasframe_t *)((int) maliashdr + maliashdr->framedata)) + frame;
243         pose = frameinfo->start;
244
245         if (frameinfo->length > 1)
246         {
247                 lerpscale = frameinfo->rate;
248                 pose += (int)(cl.time * frameinfo->rate) % frameinfo->length;
249         }
250         else
251                 lerpscale = 10.0f;
252
253         lerp = R_CalcAnimLerp(ent, pose, lerpscale);
254
255         R_AliasLerpVerts(maliashdr->numverts, lerp, (trivert2 *)((int) maliashdr + maliashdr->posedata) + ent->draw_lastpose * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin, (trivert2 *)((int) maliashdr + maliashdr->posedata) + ent->draw_pose * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin);
256
257         // prep the vertex array as early as possible
258         if (gl_vertexarrays.value)
259         {
260                 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
261                 glEnableClientState(GL_VERTEX_ARRAY);
262         }
263
264         R_LightModel(maliashdr->numverts, org, color);
265
266         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
267         glShadeModel(GL_SMOOTH);
268         if (effects & EF_ADDITIVE)
269         {
270                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
271                 glEnable(GL_BLEND);
272                 glDepthMask(0);
273         }
274         else if (alpha != 1.0)
275         {
276                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
277                 glEnable(GL_BLEND);
278                 glDepthMask(0);
279         }
280         else
281         {
282                 glDisable(GL_BLEND);
283                 glDepthMask(1);
284         }
285
286         if (colormap >= 0)
287         {
288                 if (!skin[0] && !skin[1] && !skin[2] && !skin[3])
289                         GL_DrawModelMesh(0, NULL, maliashdr);
290                 else
291                 {
292                         int c;
293                         if (skin[0])
294                                 GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
295                         if (skin[1])
296                         {
297                                 c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
298                                 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
299                                 GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr);
300                         }
301                         if (skin[2])
302                         {
303                                 c = colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
304                                 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
305                                 GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr);
306                         }
307                         if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
308                 }
309         }
310         else
311         {
312                 if (!skin[3] && !skin[4])
313                         GL_DrawModelMesh(0, NULL, maliashdr);
314                 else
315                 {
316                         if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr);
317                         if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
318                 }
319         }
320
321         if (fogenabled)
322         {
323                 vec3_t diff;
324                 glDisable (GL_TEXTURE_2D);
325                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
326                 glEnable (GL_BLEND);
327                 glDepthMask(0); // disable zbuffer updates
328
329                 VectorSubtract(org, r_refdef.vieworg, diff);
330                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
331
332                 if (gl_vertexarrays.value)
333                 {
334                         qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
335                 }
336                 else
337                 {
338                         unsigned short *in;
339                         in = (void *)((int) maliashdr + maliashdr->tridata);
340                         glBegin(GL_TRIANGLES);
341                         for (i = 0;i < maliashdr->numtris * 3;i++)
342                                 glVertex3fv(&aliasvert[*in++ * 3]);
343                         glEnd();
344                 }
345
346                 glEnable (GL_TEXTURE_2D);
347                 glColor3f (1,1,1);
348         }
349         if (gl_vertexarrays.value)
350                 glDisableClientState(GL_VERTEX_ARRAY);
351
352         if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
353         {
354                 // flatten it to make a shadow
355                 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
356                 av = aliasvert + 2;
357                 for (i = 0;i < maliashdr->numverts;i++, av+=3)
358                         if (*av > l)
359                                 *av = l;
360                 glDisable (GL_TEXTURE_2D);
361                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
362                 glEnable (GL_BLEND);
363                 glDepthMask(0); // disable zbuffer updates
364                 glColor4f (0,0,0,0.5 * alpha);
365
366                 if (gl_vertexarrays.value)
367                 {
368                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
369                         glEnableClientState(GL_VERTEX_ARRAY);
370                         qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
371                         glDisableClientState(GL_VERTEX_ARRAY);
372                 }
373                 else
374                 {
375                         unsigned short *in;
376                         in = (void *)((int) maliashdr + maliashdr->tridata);
377                         glBegin(GL_TRIANGLES);
378                         for (i = 0;i < maliashdr->numtris * 3;i++)
379                                 glVertex3fv(&aliasvert[*in++ * 3]);
380                         glEnd();
381                 }
382
383                 glEnable (GL_TEXTURE_2D);
384                 glColor3f (1,1,1);
385         }
386
387         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
388         glEnable (GL_BLEND);
389         glDepthMask(1);
390 }
391
392 /*
393 =================
394 R_DrawQ2AliasFrame
395
396 =================
397 */
398 void R_DrawQ2AliasFrame (md2mem_t *pheader, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, int frame, int skin, int effects, int flags)
399 {
400         int *order, count;
401         float lerp;
402         md2memframe_t *frame1, *frame2;
403
404         glBindTexture(GL_TEXTURE_2D, skin);
405
406         softwaretransformforentity(ent);
407
408         if ((frame >= pheader->num_frames) || (frame < 0))
409         {
410                 Con_DPrintf ("R_SetupQ2AliasFrame: no such frame %d\n", frame);
411                 frame = 0;
412         }
413
414         lerp = R_CalcAnimLerp(ent, frame, 10);
415
416         frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_lastpose));
417         frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_pose));
418         R_AliasLerpVerts(pheader->num_xyz, lerp, frame1->verts, frame1->scale, frame1->translate, frame2->verts, frame2->scale, frame2->translate);
419
420         R_LightModel(pheader->num_xyz, org, color);
421
422         if (gl_vertexarrays.value)
423         {
424                 // LordHavoc: big mess...
425                 // using arrays only slightly, although it is enough to prevent duplicates
426                 // (saving half the transforms)
427                 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
428                 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
429                 glEnableClientState(GL_VERTEX_ARRAY);
430                 glEnableClientState(GL_COLOR_ARRAY);
431
432                 order = (int *)((int)pheader + pheader->ofs_glcmds);
433                 while(1)
434                 {
435                         if (!(count = *order++))
436                                 break;
437                         if (count > 0)
438                                 glBegin(GL_TRIANGLE_STRIP);
439                         else
440                         {
441                                 glBegin(GL_TRIANGLE_FAN);
442                                 count = -count;
443                         }
444                         do
445                         {
446                                 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
447                                 qglArrayElement(order[2]);
448                                 order += 3;
449                         }
450                         while (count--);
451                 }
452
453                 glDisableClientState(GL_COLOR_ARRAY);
454                 glDisableClientState(GL_VERTEX_ARRAY);
455         }
456         else
457         {
458                 order = (int *)((int)pheader + pheader->ofs_glcmds);
459                 while(1)
460                 {
461                         if (!(count = *order++))
462                                 break;
463                         if (count > 0)
464                                 glBegin(GL_TRIANGLE_STRIP);
465                         else
466                         {
467                                 glBegin(GL_TRIANGLE_FAN);
468                                 count = -count;
469                         }
470                         do
471                         {
472                                 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
473                                 glColor4f(aliasvertcolor[order[2] * 4] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 1] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 2] * (1.0f / 255.0f), aliasvertcolor[order[2] * 4 + 3] * (1.0f / 255.0f));
474                                 glVertex3fv(&aliasvert[order[2] * 3]);
475                                 order += 3;
476                         }
477                         while (count--);
478                 }
479         }
480
481         if (fogenabled)
482         {
483                 glDisable (GL_TEXTURE_2D);
484                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
485                 glEnable (GL_BLEND);
486                 glDepthMask(0); // disable zbuffer updates
487                 {
488                         vec3_t diff;
489                         VectorSubtract(org, r_refdef.vieworg, diff);
490                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
491                 }
492
493                 if (gl_vertexarrays.value)
494                 {
495                         // LordHavoc: big mess...
496                         // using arrays only slightly, although it is enough to prevent duplicates
497                         // (saving half the transforms)
498                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
499                         glEnableClientState(GL_VERTEX_ARRAY);
500
501                         order = (int *)((int)pheader + pheader->ofs_glcmds);
502                         while(1)
503                         {
504                                 if (!(count = *order++))
505                                         break;
506                                 if (count > 0)
507                                         glBegin(GL_TRIANGLE_STRIP);
508                                 else
509                                 {
510                                         glBegin(GL_TRIANGLE_FAN);
511                                         count = -count;
512                                 }
513                                 do
514                                 {
515                                         qglArrayElement(order[2]);
516                                         order += 3;
517                                 }
518                                 while (count--);
519                         }
520
521                         glDisableClientState(GL_VERTEX_ARRAY);
522                 }
523                 else
524                 {
525                         order = (int *)((int)pheader + pheader->ofs_glcmds);
526                         while(1)
527                         {
528                                 if (!(count = *order++))
529                                         break;
530                                 if (count > 0)
531                                         glBegin(GL_TRIANGLE_STRIP);
532                                 else
533                                 {
534                                         glBegin(GL_TRIANGLE_FAN);
535                                         count = -count;
536                                 }
537                                 do
538                                 {
539                                         glVertex3fv(&aliasvert[order[2] * 3]);
540                                         order += 3;
541                                 }
542                                 while (count--);
543                         }
544                 }
545
546                 glEnable (GL_TEXTURE_2D);
547                 glColor3f (1,1,1);
548         }
549
550         if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
551         {
552                 int i;
553                 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
554                 av = aliasvert + 2;
555                 for (i = 0;i < pheader->num_xyz;i++, av+=3)
556                         if (*av > l)
557                                 *av = l;
558                 glDisable (GL_TEXTURE_2D);
559                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
560                 glEnable (GL_BLEND);
561                 glDepthMask(0); // disable zbuffer updates
562                 glColor4f (0,0,0,0.5 * alpha);
563
564                 if (gl_vertexarrays.value)
565                 {
566                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
567                         glEnableClientState(GL_VERTEX_ARRAY);
568                                                 
569                         while(1)
570                         {
571                                 if (!(count = *order++))
572                                         break;
573                                 if (count > 0)
574                                         glBegin(GL_TRIANGLE_STRIP);
575                                 else
576                                 {
577                                         glBegin(GL_TRIANGLE_FAN);
578                                         count = -count;
579                                 }
580                                 do
581                                 {
582                                         qglArrayElement(order[2]);
583                                         order += 3;
584                                 }
585                                 while (count--);
586                         }
587
588                         glDisableClientState(GL_VERTEX_ARRAY);
589                 }
590                 else
591                 {
592                         while(1)
593                         {
594                                 if (!(count = *order++))
595                                         break;
596                                 if (count > 0)
597                                         glBegin(GL_TRIANGLE_STRIP);
598                                 else
599                                 {
600                                         glBegin(GL_TRIANGLE_FAN);
601                                         count = -count;
602                                 }
603                                 do
604                                 {
605                                         glVertex3fv(&aliasvert[order[2] * 3]);
606                                         order += 3;
607                                 }
608                                 while (count--);
609                         }
610                 }
611
612                 glEnable (GL_TEXTURE_2D);
613                 glColor3f (1,1,1);
614         }
615
616         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
617         glEnable (GL_BLEND);
618         glDepthMask(1);
619 }
620
621 int modeldlightbits[8];
622 extern int r_dlightframecount;
623 extern void R_LightPoint (vec3_t color, vec3_t p);
624
625 /*
626 =================
627 R_DrawAliasModel
628
629 =================
630 */
631 void R_DrawAliasModel (entity_t *ent, int cull, float alpha, model_t *clmodel, int frame, int skin, vec3_t org, int effects, int flags, int colormap)
632 {
633         int                     i;
634         vec3_t          mins, maxs, color;
635         mleaf_t         *leaf;
636         void            *modelheader;
637         int                     *skinset;
638
639         if (alpha < (1.0 / 64.0))
640                 return; // basically completely transparent
641
642         VectorAdd (org, clmodel->mins, mins);
643         VectorAdd (org, clmodel->maxs, maxs);
644
645         if (cull && R_CullBox (mins, maxs))
646                 return;
647
648         leaf = Mod_PointInLeaf (org, cl.worldmodel);
649         if (leaf->dlightframe == r_dlightframecount)
650                 for (i = 0;i < 8;i++)
651                         modeldlightbits[i] = leaf->dlightbits[i];
652         else
653                 for (i = 0;i < 8;i++)
654                         modeldlightbits[i] = 0;
655
656         // get lighting information
657
658         if ((flags & EF_FULLBRIGHT) || (effects & EF_FULLBRIGHT))
659                 color[0] = color[1] = color[2] = 256;
660         else
661         {
662                 // HACK HACK HACK -- no fullbright colors, so make torches full light
663                 if (!strcmp (clmodel->name, "progs/flame2.mdl") || !strcmp (clmodel->name, "progs/flame.mdl") )
664                         color[0] = color[1] = color[2] = 128;
665                 else
666                         R_LightPoint (color, org);
667         }
668
669         glDisable(GL_ALPHA_TEST);
670
671         if (frame < 0 || frame >= clmodel->numframes)
672         {
673                 frame = 0;
674                 Con_DPrintf("invalid skin number %d for model %s\n", frame, clmodel->name);
675         }
676
677         if (skin < 0 || skin >= clmodel->numskins)
678         {
679                 skin = 0;
680                 Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name);
681         }
682
683         modelheader = Mod_Extradata (clmodel);
684
685         {
686 //              int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2;
687 //              int *skinanim = (int *) (clmodel->skinanim + (int) modelheader);
688                 int *skinanimrange = clmodel->skinanimrange + skin * 2;
689                 int *skinanim = clmodel->skinanim;
690                 i = skinanimrange[0];
691                 if (skinanimrange[1] > 1) // animated
692                         i += ((int) (cl.time * 10) % skinanimrange[1]);
693                 skinset = skinanim + i*5;
694         }
695
696         glEnable (GL_TEXTURE_2D);
697
698         c_alias_polys += clmodel->numtris;
699         if (clmodel->aliastype == ALIASTYPE_MD2)
700                 R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset[0], effects, flags);
701         else
702                 R_DrawAliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset, colormap, effects, flags);
703 }