implemented r_render and r_upload cvar options for CPU profiling (not hardware bound...
[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         if (!r_render.value)
155                 return;
156         glBindTexture(GL_TEXTURE_2D, skin);
157         if (!colors)
158         {
159                 if (lighthalf)
160                         glColor3f(0.5f, 0.5f, 0.5f);
161                 else
162                         glColor3f(1.0f, 1.0f, 1.0f);
163         }
164         if (gl_vertexarrays.value)
165         {
166                 if (colors)
167                 {
168                         qglColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
169                         glEnableClientState(GL_COLOR_ARRAY);
170                 }
171
172                 qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr));
173                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
174
175                 qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
176
177                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
178
179                 if (colors)
180                         glDisableClientState(GL_COLOR_ARRAY);
181         }
182         else
183         {
184                 unsigned short *in, index;
185                 float *tex;
186                 in = (void *)((int) maliashdr + maliashdr->tridata);
187                 glBegin(GL_TRIANGLES);
188                 tex = (void *)((int) maliashdr + maliashdr->texdata);
189                 for (i = 0;i < maliashdr->numtris * 3;i++)
190                 {
191                         index = *in++;
192                         glTexCoord2f(tex[index*2], tex[index*2+1]);
193                         if (colors)
194                                 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));
195                         glVertex3fv(&aliasvert[index*3]);
196                 }
197                 glEnd();
198         }
199         // leave it in a state for additional passes
200         glDepthMask(0);
201         glEnable(GL_BLEND);
202         glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive
203 }
204
205 void R_TintModel(byte *in, byte *out, int verts, byte *color)
206 {
207         int i;
208         byte r = color[0];
209         byte g = color[1];
210         byte b = color[2];
211         for (i = 0;i < verts;i++)
212         {
213                 out[0] = (byte) ((in[0] * r) >> 8);
214                 out[1] = (byte) ((in[1] * g) >> 8);
215                 out[2] = (byte) ((in[2] * b) >> 8);
216                 out[3] =          in[3];
217                 in += 4;
218                 out += 4;
219         }
220 }
221
222 /*
223 =================
224 R_DrawAliasFrame
225
226 =================
227 */
228 extern vec3_t lightspot;
229 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor);
230 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)
231 {
232         int             i, pose;
233         float   lerpscale, lerp;
234         maliasframe_t *frameinfo;
235
236         softwaretransformforentity(ent);
237
238         if ((frame >= maliashdr->numframes) || (frame < 0))
239         {
240                 Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
241                 frame = 0;
242         }
243
244         frameinfo = ((maliasframe_t *)((int) maliashdr + maliashdr->framedata)) + frame;
245         pose = frameinfo->start;
246
247         if (frameinfo->length > 1)
248         {
249                 lerpscale = frameinfo->rate;
250                 pose += (int)(cl.time * frameinfo->rate) % frameinfo->length;
251         }
252         else
253                 lerpscale = 10.0f;
254
255         lerp = R_CalcAnimLerp(ent, pose, lerpscale);
256
257         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);
258
259         // prep the vertex array as early as possible
260         if (r_render.value)
261         {
262                 if (gl_vertexarrays.value)
263                 {
264                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
265                         glEnableClientState(GL_VERTEX_ARRAY);
266                 }
267         }
268
269         R_LightModel(maliashdr->numverts, org, color);
270
271         if (!r_render.value)
272                 return;
273         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
274         glShadeModel(GL_SMOOTH);
275         if (effects & EF_ADDITIVE)
276         {
277                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
278                 glEnable(GL_BLEND);
279                 glDepthMask(0);
280         }
281         else if (alpha != 1.0)
282         {
283                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
284                 glEnable(GL_BLEND);
285                 glDepthMask(0);
286         }
287         else
288         {
289                 glDisable(GL_BLEND);
290                 glDepthMask(1);
291         }
292
293         if (colormap >= 0)
294         {
295                 if (!skin[0] && !skin[1] && !skin[2] && !skin[3])
296                         GL_DrawModelMesh(0, NULL, maliashdr);
297                 else
298                 {
299                         int c;
300                         if (skin[0])
301                                 GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr);
302                         if (skin[1])
303                         {
304                                 c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
305                                 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
306                                 GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr);
307                         }
308                         if (skin[2])
309                         {
310                                 c = colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
311                                 R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c]));
312                                 GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr);
313                         }
314                         if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
315                 }
316         }
317         else
318         {
319                 if (!skin[3] && !skin[4])
320                         GL_DrawModelMesh(0, NULL, maliashdr);
321                 else
322                 {
323                         if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr);
324                         if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr);
325                 }
326         }
327
328         if (fogenabled)
329         {
330                 vec3_t diff;
331                 glDisable (GL_TEXTURE_2D);
332                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
333                 glEnable (GL_BLEND);
334                 glDepthMask(0); // disable zbuffer updates
335
336                 VectorSubtract(org, r_refdef.vieworg, diff);
337                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
338
339                 if (gl_vertexarrays.value)
340                 {
341                         qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
342                 }
343                 else
344                 {
345                         unsigned short *in;
346                         in = (void *)((int) maliashdr + maliashdr->tridata);
347                         glBegin(GL_TRIANGLES);
348                         for (i = 0;i < maliashdr->numtris * 3;i++)
349                                 glVertex3fv(&aliasvert[*in++ * 3]);
350                         glEnd();
351                 }
352
353                 glEnable (GL_TEXTURE_2D);
354                 glColor3f (1,1,1);
355         }
356         if (gl_vertexarrays.value)
357                 glDisableClientState(GL_VERTEX_ARRAY);
358
359         if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
360         {
361                 // flatten it to make a shadow
362                 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
363                 av = aliasvert + 2;
364                 for (i = 0;i < maliashdr->numverts;i++, av+=3)
365                         if (*av > l)
366                                 *av = l;
367                 glDisable (GL_TEXTURE_2D);
368                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
369                 glEnable (GL_BLEND);
370                 glDepthMask(0); // disable zbuffer updates
371                 glColor4f (0,0,0,0.5 * alpha);
372
373                 if (gl_vertexarrays.value)
374                 {
375                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
376                         glEnableClientState(GL_VERTEX_ARRAY);
377                         qglDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata));
378                         glDisableClientState(GL_VERTEX_ARRAY);
379                 }
380                 else
381                 {
382                         unsigned short *in;
383                         in = (void *)((int) maliashdr + maliashdr->tridata);
384                         glBegin(GL_TRIANGLES);
385                         for (i = 0;i < maliashdr->numtris * 3;i++)
386                                 glVertex3fv(&aliasvert[*in++ * 3]);
387                         glEnd();
388                 }
389
390                 glEnable (GL_TEXTURE_2D);
391                 glColor3f (1,1,1);
392         }
393
394         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
395         glEnable (GL_BLEND);
396         glDepthMask(1);
397 }
398
399 /*
400 =================
401 R_DrawQ2AliasFrame
402
403 =================
404 */
405 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)
406 {
407         int *order, count;
408         float lerp;
409         md2memframe_t *frame1, *frame2;
410
411         if (r_render.value)
412                 glBindTexture(GL_TEXTURE_2D, skin);
413
414         softwaretransformforentity(ent);
415
416         if ((frame >= pheader->num_frames) || (frame < 0))
417         {
418                 Con_DPrintf ("R_SetupQ2AliasFrame: no such frame %d\n", frame);
419                 frame = 0;
420         }
421
422         lerp = R_CalcAnimLerp(ent, frame, 10);
423
424         frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_lastpose));
425         frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * ent->draw_pose));
426         R_AliasLerpVerts(pheader->num_xyz, lerp, frame1->verts, frame1->scale, frame1->translate, frame2->verts, frame2->scale, frame2->translate);
427
428         R_LightModel(pheader->num_xyz, org, color);
429
430         if (!r_render.value)
431                 return;
432         if (gl_vertexarrays.value)
433         {
434                 // LordHavoc: big mess...
435                 // using arrays only slightly, although it is enough to prevent duplicates
436                 // (saving half the transforms)
437                 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
438                 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
439                 glEnableClientState(GL_VERTEX_ARRAY);
440                 glEnableClientState(GL_COLOR_ARRAY);
441
442                 order = (int *)((int)pheader + pheader->ofs_glcmds);
443                 while(1)
444                 {
445                         if (!(count = *order++))
446                                 break;
447                         if (count > 0)
448                                 glBegin(GL_TRIANGLE_STRIP);
449                         else
450                         {
451                                 glBegin(GL_TRIANGLE_FAN);
452                                 count = -count;
453                         }
454                         do
455                         {
456                                 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
457                                 qglArrayElement(order[2]);
458                                 order += 3;
459                         }
460                         while (count--);
461                 }
462
463                 glDisableClientState(GL_COLOR_ARRAY);
464                 glDisableClientState(GL_VERTEX_ARRAY);
465         }
466         else
467         {
468                 order = (int *)((int)pheader + pheader->ofs_glcmds);
469                 while(1)
470                 {
471                         if (!(count = *order++))
472                                 break;
473                         if (count > 0)
474                                 glBegin(GL_TRIANGLE_STRIP);
475                         else
476                         {
477                                 glBegin(GL_TRIANGLE_FAN);
478                                 count = -count;
479                         }
480                         do
481                         {
482                                 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
483                                 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));
484                                 glVertex3fv(&aliasvert[order[2] * 3]);
485                                 order += 3;
486                         }
487                         while (count--);
488                 }
489         }
490
491         if (fogenabled)
492         {
493                 glDisable (GL_TEXTURE_2D);
494                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
495                 glEnable (GL_BLEND);
496                 glDepthMask(0); // disable zbuffer updates
497                 {
498                         vec3_t diff;
499                         VectorSubtract(org, r_refdef.vieworg, diff);
500                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
501                 }
502
503                 if (gl_vertexarrays.value)
504                 {
505                         // LordHavoc: big mess...
506                         // using arrays only slightly, although it is enough to prevent duplicates
507                         // (saving half the transforms)
508                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
509                         glEnableClientState(GL_VERTEX_ARRAY);
510
511                         order = (int *)((int)pheader + pheader->ofs_glcmds);
512                         while(1)
513                         {
514                                 if (!(count = *order++))
515                                         break;
516                                 if (count > 0)
517                                         glBegin(GL_TRIANGLE_STRIP);
518                                 else
519                                 {
520                                         glBegin(GL_TRIANGLE_FAN);
521                                         count = -count;
522                                 }
523                                 do
524                                 {
525                                         qglArrayElement(order[2]);
526                                         order += 3;
527                                 }
528                                 while (count--);
529                         }
530
531                         glDisableClientState(GL_VERTEX_ARRAY);
532                 }
533                 else
534                 {
535                         order = (int *)((int)pheader + pheader->ofs_glcmds);
536                         while(1)
537                         {
538                                 if (!(count = *order++))
539                                         break;
540                                 if (count > 0)
541                                         glBegin(GL_TRIANGLE_STRIP);
542                                 else
543                                 {
544                                         glBegin(GL_TRIANGLE_FAN);
545                                         count = -count;
546                                 }
547                                 do
548                                 {
549                                         glVertex3fv(&aliasvert[order[2] * 3]);
550                                         order += 3;
551                                 }
552                                 while (count--);
553                         }
554                 }
555
556                 glEnable (GL_TEXTURE_2D);
557                 glColor3f (1,1,1);
558         }
559
560         if (!fogenabled && r_shadows.value && !(effects & EF_ADDITIVE) && shadow)
561         {
562                 int i;
563                 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
564                 av = aliasvert + 2;
565                 for (i = 0;i < pheader->num_xyz;i++, av+=3)
566                         if (*av > l)
567                                 *av = l;
568                 glDisable (GL_TEXTURE_2D);
569                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
570                 glEnable (GL_BLEND);
571                 glDepthMask(0); // disable zbuffer updates
572                 glColor4f (0,0,0,0.5 * alpha);
573
574                 if (gl_vertexarrays.value)
575                 {
576                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
577                         glEnableClientState(GL_VERTEX_ARRAY);
578                                                 
579                         while(1)
580                         {
581                                 if (!(count = *order++))
582                                         break;
583                                 if (count > 0)
584                                         glBegin(GL_TRIANGLE_STRIP);
585                                 else
586                                 {
587                                         glBegin(GL_TRIANGLE_FAN);
588                                         count = -count;
589                                 }
590                                 do
591                                 {
592                                         qglArrayElement(order[2]);
593                                         order += 3;
594                                 }
595                                 while (count--);
596                         }
597
598                         glDisableClientState(GL_VERTEX_ARRAY);
599                 }
600                 else
601                 {
602                         while(1)
603                         {
604                                 if (!(count = *order++))
605                                         break;
606                                 if (count > 0)
607                                         glBegin(GL_TRIANGLE_STRIP);
608                                 else
609                                 {
610                                         glBegin(GL_TRIANGLE_FAN);
611                                         count = -count;
612                                 }
613                                 do
614                                 {
615                                         glVertex3fv(&aliasvert[order[2] * 3]);
616                                         order += 3;
617                                 }
618                                 while (count--);
619                         }
620                 }
621
622                 glEnable (GL_TEXTURE_2D);
623                 glColor3f (1,1,1);
624         }
625
626         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
627         glEnable (GL_BLEND);
628         glDepthMask(1);
629 }
630
631 int modeldlightbits[8];
632 extern int r_dlightframecount;
633 extern void R_LightPoint (vec3_t color, vec3_t p);
634
635 /*
636 =================
637 R_DrawAliasModel
638
639 =================
640 */
641 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)
642 {
643         int                     i;
644         vec3_t          mins, maxs, color;
645         mleaf_t         *leaf;
646         void            *modelheader;
647         int                     *skinset;
648
649         if (alpha < (1.0 / 64.0))
650                 return; // basically completely transparent
651
652         VectorAdd (org, clmodel->mins, mins);
653         VectorAdd (org, clmodel->maxs, maxs);
654
655         if (cull && R_CullBox (mins, maxs))
656                 return;
657
658         leaf = Mod_PointInLeaf (org, cl.worldmodel);
659         if (leaf->dlightframe == r_dlightframecount)
660                 for (i = 0;i < 8;i++)
661                         modeldlightbits[i] = leaf->dlightbits[i];
662         else
663                 for (i = 0;i < 8;i++)
664                         modeldlightbits[i] = 0;
665
666         // get lighting information
667
668         if ((flags & EF_FULLBRIGHT) || (effects & EF_FULLBRIGHT))
669                 color[0] = color[1] = color[2] = 256;
670         else
671         {
672                 // HACK HACK HACK -- no fullbright colors, so make torches full light
673                 if (!strcmp (clmodel->name, "progs/flame2.mdl") || !strcmp (clmodel->name, "progs/flame.mdl") )
674                         color[0] = color[1] = color[2] = 128;
675                 else
676                         R_LightPoint (color, org);
677         }
678
679         if (r_render.value)
680                 glDisable(GL_ALPHA_TEST);
681
682         if (frame < 0 || frame >= clmodel->numframes)
683         {
684                 frame = 0;
685                 Con_DPrintf("invalid skin number %d for model %s\n", frame, clmodel->name);
686         }
687
688         if (skin < 0 || skin >= clmodel->numskins)
689         {
690                 skin = 0;
691                 Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name);
692         }
693
694         modelheader = Mod_Extradata (clmodel);
695
696         {
697 //              int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2;
698 //              int *skinanim = (int *) (clmodel->skinanim + (int) modelheader);
699                 int *skinanimrange = clmodel->skinanimrange + skin * 2;
700                 int *skinanim = clmodel->skinanim;
701                 i = skinanimrange[0];
702                 if (skinanimrange[1] > 1) // animated
703                         i += ((int) (cl.time * 10) % skinanimrange[1]);
704                 skinset = skinanim + i*5;
705         }
706
707         if (r_render.value)
708                 glEnable (GL_TEXTURE_2D);
709
710         c_alias_polys += clmodel->numtris;
711         if (clmodel->aliastype == ALIASTYPE_MD2)
712                 R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset[0], effects, flags);
713         else
714                 R_DrawAliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, frame, skinset, colormap, effects, flags);
715 }