]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rmain.c
Fog variables redesigned slightly. (no longer cvars)
[divverent/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23
24 entity_t        r_worldentity;
25
26 qboolean        r_cache_thrash;         // compatability
27
28 vec3_t          modelorg, r_entorigin;
29 entity_t        *currententity;
30
31 int                     r_visframecount;        // bumped when going to a new PVS
32 int                     r_framecount;           // used for dlight push checking
33
34 mplane_t        frustum[4];
35
36 int                     c_brush_polys, c_alias_polys;
37
38 qboolean        envmap;                         // true during envmap command capture 
39
40 // LordHavoc: moved all code related to particles into r_part.c
41 //int                   particletexture;        // little dot for particles
42 int                     playertextures;         // up to 16 color translated skins
43
44 extern qboolean isG200, isRagePro; // LordHavoc: special card hacks
45
46 //
47 // view origin
48 //
49 vec3_t  vup;
50 vec3_t  vpn;
51 vec3_t  vright;
52 vec3_t  r_origin;
53
54 float   r_world_matrix[16];
55 float   r_base_world_matrix[16];
56
57 //
58 // screen size info
59 //
60 refdef_t        r_refdef;
61
62 mleaf_t         *r_viewleaf, *r_oldviewleaf;
63
64 texture_t       *r_notexture_mip;
65
66 int             d_lightstylevalue[256]; // 8.8 fraction of base light value
67
68
69 void R_MarkLeaves (void);
70
71 //cvar_t        r_norefresh = {"r_norefresh","0"};
72 cvar_t  r_drawentities = {"r_drawentities","1"};
73 cvar_t  r_drawviewmodel = {"r_drawviewmodel","1"};
74 cvar_t  r_speeds = {"r_speeds","0"};
75 cvar_t  r_speeds2 = {"r_speeds2","0"};
76 cvar_t  r_fullbright = {"r_fullbright","0"};
77 //cvar_t        r_lightmap = {"r_lightmap","0"};
78 //cvar_t        r_shadows = {"r_shadows","0"};
79 cvar_t  r_wateralpha = {"r_wateralpha","1"};
80 //cvar_t        r_dynamic = {"r_dynamic","1"};
81 cvar_t  r_novis = {"r_novis","0"};
82 cvar_t  r_waterripple = {"r_waterripple","0"};
83 cvar_t  r_fullbrights = {"r_fullbrights", "1"};
84
85 //cvar_t        gl_cull = {"gl_cull","1"};
86 //cvar_t        gl_affinemodels = {"gl_affinemodels","0"};
87 //cvar_t        gl_polyblend = {"gl_polyblend","1"};
88 //cvar_t        gl_flashblend = {"gl_flashblend","0"};
89 cvar_t  gl_playermip = {"gl_playermip","0"};
90 //cvar_t        gl_nocolors = {"gl_nocolors","0"};
91 //cvar_t        gl_keeptjunctions = {"gl_keeptjunctions","1"};
92 //cvar_t        gl_reporttjunctions = {"gl_reporttjunctions","0"};
93 cvar_t  contrast = {"contrast", "1.0", TRUE}; // LordHavoc: a method of operating system independent color correction
94 cvar_t  brightness = {"brightness", "1.0", TRUE}; // LordHavoc: a method of operating system independent color correction
95 cvar_t  gl_lightmode = {"gl_lightmode", "1", TRUE}; // LordHavoc: overbright lighting
96 //cvar_t        r_particles = {"r_particles", "1"};
97 //cvar_t        r_dynamicwater = {"r_dynamicwater", "1"};
98 //cvar_t        r_smokealpha = {"r_smokealpha", "0.25"};
99 //cvar_t        r_dynamicbothsides = {"r_dynamicbothsides", "1"}; // LordHavoc: can disable dynamic lighting of backfaces, but quake maps are weird so it doesn't always work right...
100
101 cvar_t  gl_fogenable = {"gl_fogenable", "0"};
102 cvar_t  gl_fogdensity = {"gl_fogdensity", "0.25"};
103 cvar_t  gl_fogred = {"gl_fogred","0.3"};
104 cvar_t  gl_foggreen = {"gl_foggreen","0.3"};
105 cvar_t  gl_fogblue = {"gl_fogblue","0.3"};
106 cvar_t  gl_fogstart = {"gl_fogstart", "0"};
107 cvar_t  gl_fogend = {"gl_fogend","0"};
108 cvar_t  glfog = {"glfog", "0"};
109
110 int chrometexture;
111
112 void makechrometextures()
113 {
114         int x, y, g, g2, amplitude, noise[64][64], min, max;
115         byte data[64][64][4];
116         //
117         // particle texture
118         //
119         chrometexture = texture_extension_number++;
120     glBindTexture(GL_TEXTURE_2D, chrometexture);
121
122 #define n(x,y) noise[(y)&63][(x)&63]
123
124         amplitude = 16777215;
125         g2 = 64;
126         noise[0][0] = 0;
127         for (;(g = g2 >> 1) >= 1;g2 >>= 1)
128         {
129                 // subdivide, diamond-square algorythm (really this has little to do with squares)
130                 // diamond
131                 for (y = 0;y < 64;y += g2)
132                         for (x = 0;x < 64;x += g2)
133                                 n(x+g,y+g) = (n(x,y) + n(x+g2,y) + n(x,y+g2) + n(x+g2,y+g2)) >> 2;
134                 // square
135                 for (y = 0;y < 64;y += g2)
136                         for (x = 0;x < 64;x += g2)
137                         {
138                                 n(x+g,y) = (n(x,y) + n(x+g2,y) + n(x+g,y-g) + n(x+g,y+g)) >> 2;
139                                 n(x,y+g) = (n(x,y) + n(x,y+g2) + n(x-g,y+g) + n(x+g,y+g)) >> 2;
140                         }
141                 // brownian motion theory
142                 amplitude >>= 1;
143                 for (y = 0;y < 64;y += g)
144                         for (x = 0;x < 64;x += g)
145                                 noise[y][x] += rand()&amplitude;
146         }
147         // normalize the noise range
148         min = max = 0;
149         for (y = 0;y < 64;y++)
150                 for (x = 0;x < 64;x++)
151                 {
152                         if (n(x,y) < min) min = n(x,y);
153                         if (n(x,y) > max) max = n(x,y);
154                 }
155         max -= min;
156         for (y = 0;y < 64;y++)
157                 for (x = 0;x < 64;x++)
158                         n(x,y) = (n(x,y) - min) * 255 / max;
159
160 #undef n
161
162         // convert to RGBA data
163         for (y = 0;y < 64;y++)
164                 for (x = 0;x < 64;x++)
165                 {
166                         data[y][x][0] = data[y][x][1] = data[y][x][2] = (byte) noise[y][x];
167                         data[y][x][3] = 255;
168                 }
169
170         glTexImage2D (GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
171
172         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
173
174         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
175         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
176 }
177
178 extern qboolean isRagePro;
179
180 qboolean lighthalf;
181
182 vec3_t fogcolor;
183 vec_t fogdensity;
184 float fog_density, fog_red, fog_green, fog_blue;
185 qboolean fogenabled;
186 qboolean oldgl_fogenable;
187 void FOG_framebegin()
188 {
189         if (nehahra)
190         {
191 //              if (!Nehahrademcompatibility)
192 //                      gl_fogenable.value = 0;
193                 if (gl_fogenable.value)
194                 {
195                         oldgl_fogenable = true;
196                         fog_density = gl_fogdensity.value;
197                         fog_red = gl_fogred.value;
198                         fog_green = gl_foggreen.value;
199                         fog_blue = gl_fogblue.value;
200                 }
201                 else if (oldgl_fogenable)
202                 {
203                         oldgl_fogenable = false;
204                         fog_density = 0;
205                         fog_red = 0;
206                         fog_green = 0;
207                         fog_blue = 0;
208                 }
209         }
210         if (glfog.value)
211         {
212                 if(fog_density)
213                 {
214                         // LordHavoc: Borland C++ 5.0 was choking on this line, stupid compiler...
215                         //GLfloat colors[4] = {(GLfloat) gl_fogred.value, (GLfloat) gl_foggreen.value, (GLfloat) gl_fogblue.value, (GLfloat) 1};
216                         GLfloat colors[4];
217                         colors[0] = fog_red;
218                         colors[1] = fog_green;
219                         colors[2] = fog_blue;
220                         colors[3] = 1;
221                         if (lighthalf)
222                         {
223                                 colors[0] *= 0.5f;
224                                 colors[1] *= 0.5f;
225                                 colors[2] *= 0.5f;
226                         }
227
228                         glFogi (GL_FOG_MODE, GL_EXP2);
229                         glFogf (GL_FOG_DENSITY, (GLfloat) fog_density / 100); 
230                         glFogfv (GL_FOG_COLOR, colors);
231                         glEnable (GL_FOG);
232                 }
233                 else
234                         glDisable(GL_FOG);
235         }
236         else
237         {
238                 if (fog_density)
239                 {
240                         fogenabled = true;
241                         fogdensity = -4096.0f / (fog_density * fog_density);
242                         fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
243                         fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
244                         fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
245                         if (lighthalf)
246                         {
247                                 fogcolor[0] *= 0.5f;
248                                 fogcolor[1] *= 0.5f;
249                                 fogcolor[2] *= 0.5f;
250                         }
251                 }
252                 else
253                         fogenabled = false;
254         }
255 }
256
257 void FOG_frameend()
258 {
259         if (glfog.value)
260                 glDisable(GL_FOG);
261 }
262
263 void FOG_clear()
264 {
265         if (nehahra)
266         {
267                 Cvar_Set("gl_fogenable", "0");
268                 Cvar_Set("gl_fogdensity", "0.2");
269                 Cvar_Set("gl_fogred", "0.3");
270                 Cvar_Set("gl_foggreen", "0.3");
271                 Cvar_Set("gl_fogblue", "0.3");
272         }
273         fog_density = fog_red = fog_green = fog_blue = 0.0f;
274 }
275
276 void FOG_registercvars()
277 {
278         Cvar_RegisterVariable (&glfog);
279         if (nehahra)
280         {
281                 Cvar_RegisterVariable (&gl_fogenable);
282                 Cvar_RegisterVariable (&gl_fogdensity);
283                 Cvar_RegisterVariable (&gl_fogred);
284                 Cvar_RegisterVariable (&gl_foggreen); 
285                 Cvar_RegisterVariable (&gl_fogblue);
286                 Cvar_RegisterVariable (&gl_fogstart);
287                 Cvar_RegisterVariable (&gl_fogend);
288         }
289 }
290
291 void glpoly_init();
292 void glrsurf_init();
293 void rlight_init();
294
295 // LordHavoc: vertex array
296 float *aliasvert;
297 float *aliasvertnorm;
298 byte *aliasvertcolor;
299
300 void rmain_registercvars()
301 {
302         // allocate vertex processing arrays
303         aliasvert = malloc(sizeof(float[MD2MAX_VERTS][3]));
304         aliasvertnorm = malloc(sizeof(float[MD2MAX_VERTS][3]));
305         aliasvertcolor = malloc(sizeof(byte[MD2MAX_VERTS][4]));
306
307         FOG_registercvars();
308         Cvar_RegisterVariable (&r_speeds2);
309         Cvar_RegisterVariable (&contrast);
310         Cvar_RegisterVariable (&brightness);
311         Cvar_RegisterVariable (&gl_lightmode);
312 //      Cvar_RegisterVariable (&r_dynamicwater);
313 //      Cvar_RegisterVariable (&r_particles);
314 //      Cvar_RegisterVariable (&r_smokealpha);
315 //      Cvar_RegisterVariable (&r_dynamicbothsides);
316         Cvar_RegisterVariable (&r_fullbrights);
317         if (nehahra)
318                 Cvar_SetValue("r_fullbrights", 0);
319 //      if (gl_vendor && strstr(gl_vendor, "3Dfx"))
320 //              gl_lightmode.value = 0;
321         Cvar_RegisterVariable (&r_fullbright);
322         makechrometextures();
323         glpoly_init();
324         glrsurf_init();
325         rlight_init();
326 }
327
328 /*
329 void R_RotateForEntity (entity_t *e)
330 {
331         glTranslatef (e->origin[0],  e->origin[1],  e->origin[2]);
332
333         glRotatef (e->angles[1],  0, 0, 1);
334         glRotatef (-e->angles[0],  0, 1, 0);
335         glRotatef (e->angles[2],  1, 0, 0);
336
337         glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
338 }
339 */
340
341 // LordHavoc: if not for the fact BRIGHTFIELD particles require this, it would be removed...
342 #define NUMVERTEXNORMALS        162
343
344 float   r_avertexnormals[NUMVERTEXNORMALS][3] = {
345 #include "anorms.h"
346 };
347
348 // LordHavoc: moved this shading stuff up because the sprites need shading stuff
349 vec3_t  shadevector;
350 vec3_t  shadecolor;
351
352 float   modelalpha;
353
354 void R_LightPoint (vec3_t color, vec3_t p);
355 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
356 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
357
358 float R_CalcAnimLerp(int pose, float lerpscale)
359 {
360         if (currententity->draw_lastmodel == currententity->model && currententity->draw_lerpstart <= cl.time)
361         {
362                 if (pose != currententity->draw_pose)
363                 {
364                         currententity->draw_lastpose = currententity->draw_pose;
365                         currententity->draw_pose = pose;
366                         currententity->draw_lerpstart = cl.time;
367                         return 0;
368                 }
369                 else
370                         return ((cl.time - currententity->draw_lerpstart) * lerpscale);
371         }
372         else // uninitialized
373         {
374                 currententity->draw_lastmodel = currententity->model;
375                 currententity->draw_lastpose = currententity->draw_pose = pose;
376                 currententity->draw_lerpstart = cl.time;
377                 return 0;
378         }
379 }
380
381 /*
382 =============================================================
383
384   SPRITE MODELS
385
386 =============================================================
387 */
388
389 /*
390 ================
391 R_GetSpriteFrame
392 ================
393 */
394 void R_GetSpriteFrame (entity_t *currententity, mspriteframe_t **oldframe, mspriteframe_t **newframe, float *framelerp)
395 {
396         msprite_t               *psprite;
397         mspritegroup_t  *pspritegroup;
398         int                             i, j, numframes, frame;
399         float                   *pintervals, fullinterval, targettime, time, jtime, jinterval;
400
401         psprite = currententity->model->cache.data;
402         frame = currententity->frame;
403
404         if ((frame >= psprite->numframes) || (frame < 0))
405         {
406                 Con_Printf ("R_DrawSprite: no such frame %d\n", frame);
407                 frame = 0;
408         }
409
410         if (psprite->frames[frame].type == SPR_SINGLE)
411         {
412                 if (currententity->draw_lastmodel == currententity->model && currententity->draw_lerpstart < cl.time)
413                 {
414                         if (frame != currententity->draw_pose)
415                         {
416                                 currententity->draw_lastpose = currententity->draw_pose;
417                                 currententity->draw_pose = frame;
418                                 currententity->draw_lerpstart = cl.time;
419                                 *framelerp = 0;
420                         }
421                         else
422                                 *framelerp = (cl.time - currententity->draw_lerpstart) * 10.0;
423                 }
424                 else // uninitialized
425                 {
426                         currententity->draw_lastmodel = currententity->model;
427                         currententity->draw_lastpose = currententity->draw_pose = frame;
428                         currententity->draw_lerpstart = cl.time;
429                         *framelerp = 0;
430                 }
431                 *oldframe = psprite->frames[currententity->draw_lastpose].frameptr;
432                 *newframe = psprite->frames[frame].frameptr;
433         }
434         else
435         {
436                 pspritegroup = (mspritegroup_t *)psprite->frames[frame].frameptr;
437                 pintervals = pspritegroup->intervals;
438                 numframes = pspritegroup->numframes;
439                 fullinterval = pintervals[numframes-1];
440
441                 time = cl.time + currententity->syncbase;
442
443         // when loading in Mod_LoadSpriteGroup, we guaranteed all interval values
444         // are positive, so we don't have to worry about division by 0
445                 targettime = time - ((int)(time / fullinterval)) * fullinterval;
446
447                 // LordHavoc: since I can't measure the time properly when it loops from numframes-1 to 0,
448                 //            I instead measure the time of the first frame, hoping it is consistent
449                 j = numframes-1;jtime = 0;jinterval = pintervals[1] - pintervals[0];
450                 for (i=0 ; i<(numframes-1) ; i++)
451                 {
452                         if (pintervals[i] > targettime)
453                                 break;
454                         j = i;jinterval = pintervals[i] - jtime;jtime = pintervals[i];
455                 }
456                 *framelerp = (targettime - jtime) / jinterval;
457
458                 *oldframe = pspritegroup->frames[j];
459                 *newframe = pspritegroup->frames[i];
460         }
461 }
462
463 void GL_DrawSpriteImage (mspriteframe_t *frame, vec3_t origin, vec3_t up, vec3_t right, int red, int green, int blue, int alpha)
464 {
465         // LordHavoc: rewrote this to use the transparent poly system
466         transpolybegin(frame->gl_texturenum, 0, frame->gl_fogtexturenum, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
467         transpolyvert(origin[0] + frame->down * up[0] + frame->left * right[0], origin[1] + frame->down * up[1] + frame->left * right[1], origin[2] + frame->down * up[2] + frame->left * right[2], 0, 1, red, green, blue, alpha);
468         transpolyvert(origin[0] + frame->up * up[0] + frame->left * right[0], origin[1] + frame->up * up[1] + frame->left * right[1], origin[2] + frame->up * up[2] + frame->left * right[2], 0, 0, red, green, blue, alpha);
469         transpolyvert(origin[0] + frame->up * up[0] + frame->right * right[0], origin[1] + frame->up * up[1] + frame->right * right[1], origin[2] + frame->up * up[2] + frame->right * right[2], 1, 0, red, green, blue, alpha);
470         transpolyvert(origin[0] + frame->down * up[0] + frame->right * right[0], origin[1] + frame->down * up[1] + frame->right * right[1], origin[2] + frame->down * up[2] + frame->right * right[2], 1, 1, red, green, blue, alpha);
471         transpolyend();
472 }
473
474 extern qboolean isG200, isRagePro, lighthalf;
475
476 /*
477 =================
478 R_DrawSpriteModel
479
480 =================
481 */
482 void R_DrawSpriteModel (entity_t *e)
483 {
484         mspriteframe_t  *oldframe, *newframe;
485         float           *up, *right, lerp, ilerp;
486         vec3_t          v_forward, v_right, v_up, org;
487         msprite_t               *psprite;
488
489         // don't even bother culling, because it's just a single
490         // polygon without a surface cache
491         R_GetSpriteFrame (e, &oldframe, &newframe, &lerp);
492         if (lerp < 0) lerp = 0;
493         if (lerp > 1) lerp = 1;
494         if (isRagePro) // LordHavoc: no alpha scaling supported on per pixel alpha images on ATI Rage Pro... ACK!
495                 lerp = 1;
496         ilerp = 1.0 - lerp;
497         psprite = e->model->cache.data;
498
499         if (psprite->type == SPR_ORIENTED)
500         {       // bullet marks on walls
501                 AngleVectors (e->angles, v_forward, v_right, v_up);
502                 up = v_up;
503                 right = v_right;
504                 VectorSubtract(e->origin, vpn, org);
505         }
506         else
507         {       // normal sprite
508                 up = vup;
509                 right = vright;
510                 VectorCopy(e->origin, org);
511         }
512         if (e->scale != 1)
513         {
514                 VectorScale(up, e->scale, up);
515                 VectorScale(right, e->scale, right);
516         }
517
518         if (e->model->flags & EF_FULLBRIGHT || e->effects & EF_FULLBRIGHT)
519         {
520                 if (lighthalf)
521                 {
522                         shadecolor[0] = e->colormod[0] * 128;
523                         shadecolor[1] = e->colormod[1] * 128;
524                         shadecolor[2] = e->colormod[2] * 128;
525                 }
526                 else
527                 {
528                         shadecolor[0] = e->colormod[0] * 255;
529                         shadecolor[1] = e->colormod[1] * 255;
530                         shadecolor[2] = e->colormod[2] * 255;
531                 }
532         }
533         else
534         {
535                 R_LightPoint (shadecolor, e->origin);
536                 R_DynamicLightPointNoMask(shadecolor, e->origin);
537                 if (lighthalf)
538                 {
539                         shadecolor[0] *= e->colormod[0] * 0.5;
540                         shadecolor[1] *= e->colormod[1] * 0.5;
541                         shadecolor[2] *= e->colormod[2] * 0.5;
542                 }
543         }
544
545         // LordHavoc: interpolated sprite rendering
546         if (ilerp != 0)
547                 GL_DrawSpriteImage(oldframe, org, up, right, shadecolor[0],shadecolor[1],shadecolor[2],e->alpha*255*ilerp);
548         if (lerp != 0)
549                 GL_DrawSpriteImage(newframe, org, up, right, shadecolor[0],shadecolor[1],shadecolor[2],e->alpha*255*lerp);
550 }
551
552 /*
553 =============================================================
554
555   ALIAS MODELS
556
557 =============================================================
558 */
559
560 extern vec3_t softwaretransform_x;
561 extern vec3_t softwaretransform_y;
562 extern vec3_t softwaretransform_z;
563 extern vec_t softwaretransform_scale;
564 extern vec3_t softwaretransform_offset;
565 void R_AliasLerpVerts(int vertcount, float lerp, trivert2 *verts1, vec3_t scale1, vec3_t translate1, trivert2 *verts2, vec3_t scale2, vec3_t translate2)
566 {
567         int i;
568         vec3_t point, matrix_x, matrix_y, matrix_z;
569         float *av, *avn;
570         av = aliasvert;
571         avn = aliasvertnorm;
572         if (lerp < 0) lerp = 0;
573         if (lerp > 1) lerp = 1;
574         if (lerp != 0)
575         {
576                 float ilerp, ilerp127, lerp127, scalex1, scalex2, translatex, scaley1, scaley2, translatey, scalez1, scalez2, translatez;
577                 if (lerp < 0) lerp = 0;
578                 if (lerp > 1) lerp = 1;
579                 ilerp = 1 - lerp;
580                 ilerp127 = ilerp * (1.0 / 127.0);
581                 lerp127 = lerp * (1.0 / 127.0);
582                 VectorScale(softwaretransform_x, softwaretransform_scale, matrix_x);
583                 VectorScale(softwaretransform_y, softwaretransform_scale, matrix_y);
584                 VectorScale(softwaretransform_z, softwaretransform_scale, matrix_z);
585                 // calculate combined interpolation variables
586                 scalex1 = scale1[0] * ilerp;scalex2 = scale2[0] *  lerp;translatex = translate1[0] * ilerp + translate2[0] *  lerp;
587                 scaley1 = scale1[1] * ilerp;scaley2 = scale2[1] *  lerp;translatey = translate1[1] * ilerp + translate2[1] *  lerp;
588                 scalez1 = scale1[2] * ilerp;scalez2 = scale2[2] *  lerp;translatez = translate1[2] * ilerp + translate2[2] *  lerp;
589                 // generate vertices
590                 for (i = 0;i < vertcount;i++)
591                 {
592                         // rotate, scale, and translate the vertex locations
593                         point[0] = verts1->v[0] * scalex1 + verts2->v[0] * scalex2 + translatex;
594                         point[1] = verts1->v[1] * scaley1 + verts2->v[1] * scaley2 + translatey;
595                         point[2] = verts1->v[2] * scalez1 + verts2->v[2] * scalez2 + translatez;
596                         *av++ = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
597                         *av++ = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
598                         *av++ = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
599                         // rotate the normals
600                         point[0] = verts1->n[0] * ilerp127 + verts2->n[0] * lerp127;
601                         point[1] = verts1->n[1] * ilerp127 + verts2->n[1] * lerp127;
602                         point[2] = verts1->n[2] * ilerp127 + verts2->n[2] * lerp127;
603                         *avn++ = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
604                         *avn++ = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
605                         *avn++ = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
606                         verts1++;verts2++;
607                 }
608         }
609         else
610         {
611                 float i127;
612                 i127 = 1.0f / 127.0f;
613                 VectorScale(softwaretransform_x, softwaretransform_scale, matrix_x);
614                 VectorScale(softwaretransform_y, softwaretransform_scale, matrix_y);
615                 VectorScale(softwaretransform_z, softwaretransform_scale, matrix_z);
616                 // generate vertices
617                 for (i = 0;i < vertcount;i++)
618                 {
619                         // rotate, scale, and translate the vertex locations
620                         point[0] = verts1->v[0] * scale1[0] + translate1[0];
621                         point[1] = verts1->v[1] * scale1[1] + translate1[1];
622                         point[2] = verts1->v[2] * scale1[2] + translate1[2];
623                         *av++ = point[0] * matrix_x[0] + point[1] * matrix_y[0] + point[2] * matrix_z[0] + softwaretransform_offset[0];
624                         *av++ = point[0] * matrix_x[1] + point[1] * matrix_y[1] + point[2] * matrix_z[1] + softwaretransform_offset[1];
625                         *av++ = point[0] * matrix_x[2] + point[1] * matrix_y[2] + point[2] * matrix_z[2] + softwaretransform_offset[2];
626                         // rotate the normals
627                         point[0] = verts1->n[0] * i127;
628                         point[1] = verts1->n[1] * i127;
629                         point[2] = verts1->n[2] * i127;
630                         *avn++ = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0];
631                         *avn++ = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1];
632                         *avn++ = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2];
633                         verts1++;
634                 }
635         }
636 }
637
638 /*
639 =================
640 R_DrawAliasFrame
641
642 =================
643 */
644 extern vec3_t lightspot;
645 void R_LightModel(int numverts, vec3_t center);
646 extern cvar_t gl_vertexarrays;
647 void R_DrawAliasFrame (aliashdr_t *paliashdr)
648 {
649         int                             i, pose, frame = currententity->frame;
650         float                   lerpscale, lerp;
651
652         softwaretransformforentity(currententity);
653
654         if ((frame >= paliashdr->numframes) || (frame < 0))
655         {
656                 Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
657                 frame = 0;
658         }
659
660         pose = paliashdr->frames[frame].firstpose;
661
662         if (paliashdr->frames[frame].numposes > 1)
663         {
664                 lerpscale = 1.0 / paliashdr->frames[frame].interval;
665                 pose += (int)(cl.time * lerpscale) % paliashdr->frames[frame].numposes;
666         }
667         else
668                 lerpscale = 10.0;
669
670         lerp = R_CalcAnimLerp(pose, lerpscale);
671
672         R_AliasLerpVerts(paliashdr->numverts, lerp, (trivert2 *)((byte *)paliashdr + paliashdr->posedata) + currententity->draw_lastpose * paliashdr->numverts, paliashdr->scale, paliashdr->scale_origin, (trivert2 *)((byte *)paliashdr + paliashdr->posedata) + currententity->draw_pose * paliashdr->numverts, paliashdr->scale, paliashdr->scale_origin);
673
674         R_LightModel(paliashdr->numverts, currententity->origin);
675
676         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
677         glShadeModel(GL_SMOOTH);
678         if (currententity->effects & EF_ADDITIVE)
679         {
680                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
681                 glEnable(GL_BLEND);
682                 glDepthMask(0);
683         }
684         else if (modelalpha != 1.0)
685         {
686                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
687                 glEnable(GL_BLEND);
688                 glDepthMask(0);
689         }
690         else
691         {
692                 glDisable(GL_BLEND);
693                 glDepthMask(1);
694         }
695
696         if (gl_vertexarrays.value)
697         {
698                 // LordHavoc: I would use InterleavedArrays here,
699                 // but the texture coordinates are a seperate array,
700                 // and it would be wasteful to copy them into the main array...
701         //      glColor4f(shadecolor[0], shadecolor[1], shadecolor[2], modelalpha);
702                 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
703                 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
704                 glEnableClientState(GL_VERTEX_ARRAY);
705                 glEnableClientState(GL_COLOR_ARRAY);
706
707                 // draw the front faces
708                 qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) paliashdr->texcoords + (int) paliashdr));
709                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
710                 qglDrawElements(GL_TRIANGLES, paliashdr->frontfaces * 3, GL_UNSIGNED_SHORT, (void *)((int) paliashdr->vertindices + (int) paliashdr));
711                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
712
713                 // draw the back faces
714                 qglTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) paliashdr->texcoords + sizeof(float[2]) * paliashdr->numverts + (int) paliashdr));
715                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
716                 qglDrawElements(GL_TRIANGLES, paliashdr->backfaces * 3, GL_UNSIGNED_SHORT, (void *)((int) paliashdr->vertindices + sizeof(unsigned short[3]) * paliashdr->frontfaces + (int) paliashdr));
717                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
718
719                 glDisableClientState(GL_COLOR_ARRAY);
720                 glDisableClientState(GL_VERTEX_ARRAY);
721         }
722         else
723         {
724                 unsigned short *in, index;
725                 float *tex;
726                 in = (void *)((int) paliashdr->vertindices + (int) paliashdr);
727                 glBegin(GL_TRIANGLES);
728                 // draw the front faces
729                 tex = (void *)((int) paliashdr->texcoords + (int) paliashdr);
730                 //if (isG200)
731                 //{
732                         for (i = 0;i < paliashdr->frontfaces * 3;i++)
733                         {
734                                 index = *in++;
735                                 glTexCoord2f(tex[index*2], tex[index*2+1]);
736                                 glColor4f(aliasvertcolor[index*4] * (1.0f / 255.0f), aliasvertcolor[index*4+1] * (1.0f / 255.0f), aliasvertcolor[index*4+2] * (1.0f / 255.0f), aliasvertcolor[index*4+3] * (1.0f / 255.0f));
737                                 glVertex3fv(&aliasvert[index*3]);
738                         }
739                 /*
740                 }
741                 else
742                 {
743                         for (i = 0;i < paliashdr->frontfaces * 3;i++)
744                         {
745                                 index = *in++;
746                                 glTexCoord2f(tex[index*2], tex[index*2+1]);
747                                 glColor4ub(aliasvertcolor[index*4], aliasvertcolor[index*4+1], aliasvertcolor[index*4+2], aliasvertcolor[index*4+3]);
748                                 glVertex3fv(&aliasvert[index*3]);
749                         }
750                 }
751                 */
752                 // draw the back faces
753                 tex += 2 * paliashdr->numverts;
754                 //if (isG200)
755                 //{
756                         for (i = 0;i < paliashdr->backfaces * 3;i++)
757                         {
758                                 index = *in++;
759                                 glTexCoord2f(tex[index*2], tex[index*2+1]);
760                                 glColor4f(aliasvertcolor[index*4] * (1.0f / 255.0f), aliasvertcolor[index*4+1] * (1.0f / 255.0f), aliasvertcolor[index*4+2] * (1.0f / 255.0f), aliasvertcolor[index*4+3] * (1.0f / 255.0f));
761                                 glVertex3fv(&aliasvert[index*3]);
762                         }
763                 /*
764                 }
765                 else
766                 {
767                         for (i = 0;i < paliashdr->backfaces * 3;i++)
768                         {
769                                 index = *in++;
770                                 glTexCoord2f(tex[index*2], tex[index*2+1]);
771                                 glColor4ub(aliasvertcolor[index*4], aliasvertcolor[index*4+1], aliasvertcolor[index*4+2], aliasvertcolor[index*4+3]);
772                                 glVertex3fv(&aliasvert[index*3]);
773                         }
774                 }
775                 */
776                 glEnd();
777         }
778
779         if (fogenabled)
780         {
781                 vec3_t diff;
782                 glDisable (GL_TEXTURE_2D);
783                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
784                 glEnable (GL_BLEND);
785                 glDepthMask(0); // disable zbuffer updates
786
787                 VectorSubtract(currententity->origin, r_refdef.vieworg, diff);
788                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
789
790                 if (gl_vertexarrays.value)
791                 {
792                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
793                         glEnableClientState(GL_VERTEX_ARRAY);
794                         qglDrawElements(GL_TRIANGLES, paliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) paliashdr->vertindices + (int) paliashdr));
795                         glDisableClientState(GL_VERTEX_ARRAY);
796                 }
797                 else
798                 {
799                         unsigned short *in;
800                         in = (void *)((int) paliashdr->vertindices + (int) paliashdr);
801                         glBegin(GL_TRIANGLES);
802                         for (i = 0;i < paliashdr->numtris * 3;i++)
803                                 glVertex3fv(&aliasvert[*in++ * 3]);
804                         glEnd();
805                 }
806
807                 glEnable (GL_TEXTURE_2D);
808                 glColor3f (1,1,1);
809         }
810
811         /*
812         if (r_shadows.value && !(currententity->effects & EF_ADDITIVE) && currententity != &cl.viewent)
813         {
814                 // flatten it to make a shadow
815                 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
816                 av = aliasvert + 2;
817                 for (i = 0;i < paliashdr->numverts;i++, av+=3)
818                         if (*av > l)
819                                 *av = l;
820                 glDisable (GL_TEXTURE_2D);
821                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
822                 glEnable (GL_BLEND);
823                 glDepthMask(0); // disable zbuffer updates
824                 glColor4f (0,0,0,0.5 * modelalpha);
825
826                 if (gl_vertexarrays.value)
827                 {
828                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
829                         glEnableClientState(GL_VERTEX_ARRAY);
830                         qglDrawElements(GL_TRIANGLES, paliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) paliashdr->vertindices + (int) paliashdr));
831                         glDisableClientState(GL_VERTEX_ARRAY);
832                 }
833                 else
834                 {
835                         unsigned short *in;
836                         in = (void *)((int) paliashdr->vertindices + (int) paliashdr);
837                         glBegin(GL_TRIANGLES);
838                         for (i = 0;i < paliashdr->numtris * 3;i++)
839                                 glVertex3fv(&aliasvert[*in++ * 3]);
840                         glEnd();
841                 }
842
843                 glEnable (GL_TEXTURE_2D);
844                 glColor3f (1,1,1);
845         }
846         */
847
848         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
849         glEnable (GL_BLEND);
850         glDepthMask(1);
851 }
852
853 /*
854 =================
855 R_DrawQ2AliasFrame
856
857 =================
858 */
859 void R_DrawQ2AliasFrame (md2mem_t *pheader)
860 {
861         int *order, count, frame = currententity->frame;
862         float lerp;
863         md2memframe_t *frame1, *frame2;
864
865         softwaretransformforentity(currententity);
866
867         if ((frame >= pheader->num_frames) || (frame < 0))
868         {
869                 Con_DPrintf ("R_SetupQ2AliasFrame: no such frame %d\n", frame);
870                 frame = 0;
871         }
872
873         lerp = R_CalcAnimLerp(frame, 10);
874
875         frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * currententity->draw_lastpose));
876         frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * currententity->draw_pose));
877         R_AliasLerpVerts(pheader->num_xyz, lerp, frame1->verts, frame1->scale, frame1->translate, frame2->verts, frame2->scale, frame2->translate);
878
879         R_LightModel(pheader->num_xyz, currententity->origin);
880
881         if (gl_vertexarrays.value)
882         {
883                 // LordHavoc: big mess...
884                 // using arrays only slightly, although it is enough to prevent duplicates
885                 // (saving half the transforms)
886                 //glColor4f(shadecolor[0], shadecolor[1], shadecolor[2], modelalpha);
887                 qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
888                 qglColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor);
889                 glEnableClientState(GL_VERTEX_ARRAY);
890                 glEnableClientState(GL_COLOR_ARRAY);
891
892                 order = (int *)((int)pheader + pheader->ofs_glcmds);
893                 while(1)
894                 {
895                         if (!(count = *order++))
896                                 break;
897                         if (count > 0)
898                                 glBegin(GL_TRIANGLE_STRIP);
899                         else
900                         {
901                                 glBegin(GL_TRIANGLE_FAN);
902                                 count = -count;
903                         }
904                         do
905                         {
906                                 glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
907                                 qglArrayElement(order[2]);
908                                 order += 3;
909                         }
910                         while (count--);
911                 }
912
913                 glDisableClientState(GL_COLOR_ARRAY);
914                 glDisableClientState(GL_VERTEX_ARRAY);
915         }
916         else
917         {
918                 order = (int *)((int)pheader + pheader->ofs_glcmds);
919                 while(1)
920                 {
921                         if (!(count = *order++))
922                                 break;
923                         if (count > 0)
924                                 glBegin(GL_TRIANGLE_STRIP);
925                         else
926                         {
927                                 glBegin(GL_TRIANGLE_FAN);
928                                 count = -count;
929                         }
930                         //if (isG200)
931                         //{
932                                 do
933                                 {
934                                         glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
935                                         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));
936                                         glVertex3fv(&aliasvert[order[2] * 3]);
937                                         order += 3;
938                                 }
939                                 while (count--);
940                         /*
941                         }
942                         else
943                         {
944                                 do
945                                 {
946                                         glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
947                                         glColor4ub(aliasvertcolor[order[2] * 4], aliasvertcolor[order[2] * 4 + 1], aliasvertcolor[order[2] * 4 + 2], aliasvertcolor[order[2] * 4 + 3]);
948                                         glVertex3fv(&aliasvert[order[2] * 3]);
949                                         order += 3;
950                                 }
951                                 while (count--);
952                         }
953                         */
954                 }
955         }
956
957         if (fogenabled)
958         {
959                 glDisable (GL_TEXTURE_2D);
960                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
961                 glEnable (GL_BLEND);
962                 glDepthMask(0); // disable zbuffer updates
963                 {
964                         vec3_t diff;
965                         VectorSubtract(currententity->origin, r_refdef.vieworg, diff);
966                         glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff)));
967                 }
968
969                 if (gl_vertexarrays.value)
970                 {
971                         // LordHavoc: big mess...
972                         // using arrays only slightly, although it is enough to prevent duplicates
973                         // (saving half the transforms)
974                         //glColor4f(shadecolor[0], shadecolor[1], shadecolor[2], modelalpha);
975                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
976                         glEnableClientState(GL_VERTEX_ARRAY);
977
978                         order = (int *)((int)pheader + pheader->ofs_glcmds);
979                         while(1)
980                         {
981                                 if (!(count = *order++))
982                                         break;
983                                 if (count > 0)
984                                         glBegin(GL_TRIANGLE_STRIP);
985                                 else
986                                 {
987                                         glBegin(GL_TRIANGLE_FAN);
988                                         count = -count;
989                                 }
990                                 do
991                                 {
992                                         qglArrayElement(order[2]);
993                                         order += 3;
994                                 }
995                                 while (count--);
996                         }
997
998                         glDisableClientState(GL_VERTEX_ARRAY);
999                 }
1000                 else
1001                 {
1002                         order = (int *)((int)pheader + pheader->ofs_glcmds);
1003                         while(1)
1004                         {
1005                                 if (!(count = *order++))
1006                                         break;
1007                                 if (count > 0)
1008                                         glBegin(GL_TRIANGLE_STRIP);
1009                                 else
1010                                 {
1011                                         glBegin(GL_TRIANGLE_FAN);
1012                                         count = -count;
1013                                 }
1014                                 do
1015                                 {
1016                                         glVertex3fv(&aliasvert[order[2] * 3]);
1017                                         order += 3;
1018                                 }
1019                                 while (count--);
1020                         }
1021                 }
1022
1023                 glEnable (GL_TEXTURE_2D);
1024                 glColor3f (1,1,1);
1025         }
1026
1027         /*
1028         if (r_shadows.value && !(currententity->effects & EF_ADDITIVE) && currententity != &cl.viewent)
1029         {
1030                 int i;
1031                 float *av = aliasvert + 2, l = lightspot[2] + 0.125;
1032                 av = aliasvert + 2;
1033                 for (i = 0;i < pheader->num_xyz;i++, av+=3)
1034                         if (*av > l)
1035                                 *av = l;
1036                 glDisable (GL_TEXTURE_2D);
1037                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1038                 glEnable (GL_BLEND);
1039                 glDepthMask(0); // disable zbuffer updates
1040                 glColor4f (0,0,0,0.5 * modelalpha);
1041
1042                 if (gl_vertexarrays.value)
1043                 {
1044                         qglVertexPointer(3, GL_FLOAT, 0, aliasvert);
1045                         glEnableClientState(GL_VERTEX_ARRAY);
1046                                                 
1047                         while(1)
1048                         {
1049                                 if (!(count = *order++))
1050                                         break;
1051                                 if (count > 0)
1052                                         glBegin(GL_TRIANGLE_STRIP);
1053                                 else
1054                                 {
1055                                         glBegin(GL_TRIANGLE_FAN);
1056                                         count = -count;
1057                                 }
1058                                 do
1059                                 {
1060                                         qglArrayElement(order[2]);
1061                                         order += 3;
1062                                 }
1063                                 while (count--);
1064                         }
1065
1066                         glDisableClientState(GL_VERTEX_ARRAY);
1067                 }
1068                 else
1069                 {
1070                         while(1)
1071                         {
1072                                 if (!(count = *order++))
1073                                         break;
1074                                 if (count > 0)
1075                                         glBegin(GL_TRIANGLE_STRIP);
1076                                 else
1077                                 {
1078                                         glBegin(GL_TRIANGLE_FAN);
1079                                         count = -count;
1080                                 }
1081                                 do
1082                                 {
1083                                         glVertex3fv(&aliasvert[order[2] * 3]);
1084                                         order += 3;
1085                                 }
1086                                 while (count--);
1087                         }
1088                 }
1089
1090                 glEnable (GL_TEXTURE_2D);
1091                 glColor3f (1,1,1);
1092         }
1093         */
1094
1095         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1096         glEnable (GL_BLEND);
1097         glDepthMask(1);
1098 }
1099
1100 /*
1101 =================
1102 R_DrawAliasModel
1103
1104 =================
1105 */
1106 void R_DrawAliasModel (entity_t *e, int cull)
1107 {
1108         int                     i;
1109         model_t         *clmodel;
1110         vec3_t          mins, maxs;
1111         aliashdr_t      *paliashdr;
1112         md2mem_t                *pheader;
1113         int                     anim;
1114
1115         if (modelalpha < (1.0 / 64.0))
1116                 return; // basically completely transparent
1117
1118         clmodel = currententity->model;
1119
1120         VectorAdd (currententity->origin, clmodel->mins, mins);
1121         VectorAdd (currententity->origin, clmodel->maxs, maxs);
1122
1123         if (cull && R_CullBox (mins, maxs))
1124                 return;
1125
1126         VectorCopy (currententity->origin, r_entorigin);
1127         VectorSubtract (r_origin, r_entorigin, modelorg);
1128
1129         // get lighting information
1130
1131         if (currententity->model->flags & EF_FULLBRIGHT || currententity->effects & EF_FULLBRIGHT)
1132         {
1133                 shadecolor[0] = currententity->colormod[0] * 256;
1134                 shadecolor[1] = currententity->colormod[1] * 256;
1135                 shadecolor[2] = currententity->colormod[2] * 256;
1136         }
1137         else
1138         {
1139                 R_LightPoint (shadecolor, currententity->origin);
1140
1141                 // HACK HACK HACK -- no fullbright colors, so make torches full light
1142                 if (!strcmp (currententity->model->name, "progs/flame2.mdl") || !strcmp (currententity->model->name, "progs/flame.mdl") )
1143                         shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
1144
1145                 shadecolor[0] *= currententity->colormod[0];
1146                 shadecolor[1] *= currententity->colormod[1];
1147                 shadecolor[2] *= currententity->colormod[2];
1148         }
1149
1150         // locate the proper data
1151         if (clmodel->aliastype == ALIASTYPE_MD2)
1152         {
1153                 pheader = (void *)Mod_Extradata (currententity->model);
1154                 c_alias_polys += pheader->num_tris;
1155         }
1156         else
1157         {
1158                 paliashdr = (void *)Mod_Extradata (currententity->model);
1159                 c_alias_polys += paliashdr->numtris;
1160         }
1161
1162         // draw all the triangles
1163
1164         if (clmodel->aliastype == ALIASTYPE_MD2)
1165         {
1166                 if (currententity->skinnum < 0 || currententity->skinnum >= pheader->num_skins)
1167                 {
1168                         currententity->skinnum = 0;
1169                         Con_DPrintf("invalid skin number %d for model %s\n", currententity->skinnum, clmodel->name);
1170                 }
1171                 glBindTexture(GL_TEXTURE_2D, pheader->gl_texturenum[currententity->skinnum]);
1172         }
1173         else
1174         {
1175                 if (currententity->skinnum < 0 || currententity->skinnum >= paliashdr->numskins)
1176                 {
1177                         currententity->skinnum = 0;
1178                         Con_DPrintf("invalid skin number %d for model %s\n", currententity->skinnum, clmodel->name);
1179                 }
1180                 anim = (int)(cl.time*10) & 3;
1181             glBindTexture(GL_TEXTURE_2D, paliashdr->gl_texturenum[currententity->skinnum][anim]);
1182         }
1183         glDisable(GL_ALPHA_TEST);
1184         glEnable (GL_TEXTURE_2D);
1185
1186         // we can't dynamically colormap textures, so they are cached
1187         // seperately for the players.  Heads are just uncolored.
1188         if (currententity->colormap != vid.colormap/* && !gl_nocolors.value*/)
1189         {
1190                 i = currententity - cl_entities;
1191                 if (i >= 1 && i<=cl.maxclients /* && !strcmp (currententity->model->name, "progs/player.mdl") */)
1192                     glBindTexture(GL_TEXTURE_2D, playertextures - 1 + i);
1193         }
1194
1195 //      if (gl_affinemodels.value)
1196 //              glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
1197         if (clmodel->aliastype == ALIASTYPE_MD2)
1198                 R_DrawQ2AliasFrame (pheader);
1199         else
1200                 R_DrawAliasFrame (paliashdr);
1201
1202         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1203 }
1204
1205 //==================================================================================
1206
1207 void R_DrawBrushModel (entity_t *e);
1208
1209 /*
1210 =============
1211 R_DrawEntitiesOnList
1212 =============
1213 */
1214 // LordHavoc: split so bmodels are rendered before any other objects
1215 void R_DrawEntitiesOnList1 (void)
1216 {
1217         int             i;
1218
1219         if (!r_drawentities.value)
1220                 return;
1221
1222         for (i=0 ; i<cl_numvisedicts ; i++)
1223         {
1224                 if (cl_visedicts[i]->model->type != mod_brush)
1225                         continue;
1226                 currententity = cl_visedicts[i];
1227                 modelalpha = currententity->alpha;
1228
1229                 R_DrawBrushModel (currententity);
1230         }
1231 }
1232
1233 void R_DrawEntitiesOnList2 (void)
1234 {
1235         int             i;
1236
1237         if (!r_drawentities.value)
1238                 return;
1239
1240         for (i=0 ; i<cl_numvisedicts ; i++)
1241         {
1242                 currententity = cl_visedicts[i];
1243                 modelalpha = currententity->alpha;
1244
1245                 switch (currententity->model->type)
1246                 {
1247                 case mod_alias:
1248                         R_DrawAliasModel (currententity, true);
1249                         break;
1250
1251                 case mod_sprite:
1252                         R_DrawSpriteModel (currententity);
1253                         break;
1254
1255                 default:
1256                         break;
1257                 }
1258         }
1259 }
1260
1261 /*
1262 =============
1263 R_DrawViewModel
1264 =============
1265 */
1266 void R_DrawViewModel (void)
1267 {
1268         if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.model)
1269                 return;
1270
1271         currententity = &cl.viewent;
1272         currententity->alpha = modelalpha = cl_entities[cl.viewentity].alpha; // LordHavoc: if the player is transparent, so is his gun
1273         currententity->effects = cl_entities[cl.viewentity].effects;
1274         currententity->scale = 1;
1275         VectorCopy(cl_entities[cl.viewentity].colormod, currententity->colormod);
1276
1277         // hack the depth range to prevent view model from poking into walls
1278         glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
1279         R_DrawAliasModel (currententity, FALSE);
1280         glDepthRange (gldepthmin, gldepthmax);
1281 }
1282
1283 void R_DrawBrushModel (entity_t *e);
1284
1285 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
1286
1287 void R_SetFrustum (void)
1288 {
1289         int             i;
1290
1291         if (r_refdef.fov_x == 90) 
1292         {
1293                 // front side is visible
1294
1295                 VectorAdd (vpn, vright, frustum[0].normal);
1296                 VectorSubtract (vpn, vright, frustum[1].normal);
1297
1298                 VectorAdd (vpn, vup, frustum[2].normal);
1299                 VectorSubtract (vpn, vup, frustum[3].normal);
1300         }
1301         else
1302         {
1303                 // rotate VPN right by FOV_X/2 degrees
1304                 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
1305                 // rotate VPN left by FOV_X/2 degrees
1306                 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
1307                 // rotate VPN up by FOV_X/2 degrees
1308                 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
1309                 // rotate VPN down by FOV_X/2 degrees
1310                 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
1311         }
1312
1313         for (i=0 ; i<4 ; i++)
1314         {
1315                 frustum[i].type = PLANE_ANYZ;
1316                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
1317 //              frustum[i].signbits = SignbitsForPlane (&frustum[i]);
1318                 BoxOnPlaneSideClassify(&frustum[i]);
1319         }
1320 }
1321
1322 void R_AnimateLight (void);
1323 void V_CalcBlend (void);
1324
1325 /*
1326 ===============
1327 R_SetupFrame
1328 ===============
1329 */
1330 void R_SetupFrame (void)
1331 {
1332 // don't allow cheats in multiplayer
1333         if (cl.maxclients > 1)
1334                 Cvar_Set ("r_fullbright", "0");
1335
1336         R_AnimateLight ();
1337
1338         r_framecount++;
1339
1340 // build the transformation matrix for the given view angles
1341         VectorCopy (r_refdef.vieworg, r_origin);
1342
1343         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
1344
1345 // current viewleaf
1346         r_oldviewleaf = r_viewleaf;
1347         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
1348
1349         V_SetContentsColor (r_viewleaf->contents);
1350         V_CalcBlend ();
1351
1352         r_cache_thrash = false;
1353
1354         c_brush_polys = 0;
1355         c_alias_polys = 0;
1356
1357 }
1358
1359
1360 void MYgluPerspective( GLdouble fovy, GLdouble aspect,
1361                      GLdouble zNear, GLdouble zFar )
1362 {
1363    GLdouble xmin, xmax, ymin, ymax;
1364
1365    ymax = zNear * tan( fovy * M_PI / 360.0 );
1366    ymin = -ymax;
1367
1368    xmin = ymin * aspect;
1369    xmax = ymax * aspect;
1370
1371    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
1372 }
1373
1374
1375 extern char skyname[];
1376
1377 /*
1378 =============
1379 R_SetupGL
1380 =============
1381 */
1382 void R_SetupGL (void)
1383 {
1384         float   screenaspect;
1385         extern  int glwidth, glheight;
1386         int             x, x2, y2, y, w, h;
1387
1388         //
1389         // set up viewpoint
1390         //
1391         glMatrixMode(GL_PROJECTION);
1392     glLoadIdentity ();
1393         x = r_refdef.vrect.x * glwidth/vid.width;
1394         x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
1395         y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
1396         y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
1397
1398         // fudge around because of frac screen scale
1399         if (x > 0)
1400                 x--;
1401         if (x2 < glwidth)
1402                 x2++;
1403         if (y2 < 0)
1404                 y2--;
1405         if (y < glheight)
1406                 y++;
1407
1408         w = x2 - x;
1409         h = y - y2;
1410
1411         if (envmap)
1412         {
1413                 x = y2 = 0;
1414                 w = h = 256;
1415         }
1416
1417         glViewport (glx + x, gly + y2, w, h);
1418     screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
1419 //      yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
1420 //      if (skyname[0]) // skybox enabled?
1421 //              MYgluPerspective (r_refdef.fov_y,  screenaspect,  4,  r_skyboxsize.value*1.732050807569 + 256); // this is size*sqrt(3) + 256
1422 //      else
1423                 MYgluPerspective (r_refdef.fov_y,  screenaspect,  4,  6144);
1424
1425         glCullFace(GL_FRONT);
1426
1427         glMatrixMode(GL_MODELVIEW);
1428     glLoadIdentity ();
1429
1430     glRotatef (-90,  1, 0, 0);      // put Z going up
1431     glRotatef (90,  0, 0, 1);       // put Z going up
1432     glRotatef (-r_refdef.viewangles[2],  1, 0, 0);
1433     glRotatef (-r_refdef.viewangles[0],  0, 1, 0);
1434     glRotatef (-r_refdef.viewangles[1],  0, 0, 1);
1435     glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
1436
1437         glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
1438
1439         //
1440         // set drawing parms
1441         //
1442 //      if (gl_cull.value)
1443                 glEnable(GL_CULL_FACE);
1444 //      else
1445 //              glDisable(GL_CULL_FACE);
1446
1447         glEnable(GL_BLEND); // was Disable
1448         glDisable(GL_ALPHA_TEST);
1449         glAlphaFunc(GL_GREATER, 0.5);
1450         glEnable(GL_DEPTH_TEST);
1451         glDepthMask(1);
1452         glShadeModel(GL_SMOOTH);
1453 }
1454
1455 void R_DrawWorld (void);
1456 //void R_RenderDlights (void);
1457 void R_DrawParticles (void);
1458
1459 /*
1460 =============
1461 R_Clear
1462 =============
1463 */
1464 void R_Clear (void)
1465 {
1466 //      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
1467         gldepthmin = 0;
1468         gldepthmax = 1;
1469         glDepthFunc (GL_LEQUAL);
1470
1471         glDepthRange (gldepthmin, gldepthmax);
1472 }
1473
1474 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
1475 void GL_Brighten()
1476 {
1477         glMatrixMode(GL_PROJECTION);
1478     glLoadIdentity ();
1479         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
1480         glMatrixMode(GL_MODELVIEW);
1481     glLoadIdentity ();
1482         glDisable (GL_DEPTH_TEST);
1483         glDisable (GL_CULL_FACE);
1484         glDisable(GL_TEXTURE_2D);
1485         glEnable(GL_BLEND);
1486         glBlendFunc (GL_DST_COLOR, GL_ONE);
1487         glBegin (GL_TRIANGLES);
1488         glColor3f (1, 1, 1);
1489         glVertex2f (-5000, -5000);
1490         glVertex2f (10000, -5000);
1491         glVertex2f (-5000, 10000);
1492         glEnd ();
1493         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1494         glDisable(GL_BLEND);
1495         glEnable(GL_TEXTURE_2D);
1496         glEnable (GL_DEPTH_TEST);
1497         glEnable (GL_CULL_FACE);
1498 }
1499
1500 extern cvar_t contrast;
1501 extern cvar_t brightness;
1502 extern cvar_t gl_lightmode;
1503
1504 void GL_BlendView()
1505 {
1506         glMatrixMode(GL_PROJECTION);
1507     glLoadIdentity ();
1508         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
1509         glMatrixMode(GL_MODELVIEW);
1510     glLoadIdentity ();
1511         glDisable (GL_DEPTH_TEST);
1512         glDisable (GL_CULL_FACE);
1513         glDisable(GL_TEXTURE_2D);
1514         glEnable(GL_BLEND);
1515         if (lighthalf)
1516         {
1517                 glBlendFunc (GL_DST_COLOR, GL_ONE);
1518                 glBegin (GL_TRIANGLES);
1519                 glColor3f (1, 1, 1);
1520                 glVertex2f (-5000, -5000);
1521                 glVertex2f (10000, -5000);
1522                 glVertex2f (-5000, 10000);
1523                 glEnd ();
1524         }
1525         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1526         contrast.value = bound(0.2, contrast.value, 1.0);
1527         if (/*gl_polyblend.value && */v_blend[3])
1528         {
1529                 glBegin (GL_TRIANGLES);
1530                 glColor4fv (v_blend);
1531                 glVertex2f (-5000, -5000);
1532                 glVertex2f (10000, -5000);
1533                 glVertex2f (-5000, 10000);
1534                 glEnd ();
1535         }
1536
1537         glEnable (GL_CULL_FACE);
1538         glEnable (GL_DEPTH_TEST);
1539         glDisable(GL_BLEND);
1540         glEnable(GL_TEXTURE_2D);
1541 }
1542
1543 #define TIMEREPORT(DESC) \
1544         if (r_speeds2.value)\
1545         {\
1546                 temptime = -currtime;\
1547                 currtime = Sys_FloatTime();\
1548                 temptime += currtime;\
1549                 Con_Printf(DESC " %.4fms ", temptime * 1000.0);\
1550         }
1551
1552 /*
1553 ================
1554 R_RenderView
1555
1556 r_refdef must be set before the first call
1557 ================
1558 */
1559 void R_RenderView (void)
1560 {
1561         double currtime, temptime;
1562 //      if (r_norefresh.value)
1563 //              return;
1564
1565         if (!r_worldentity.model || !cl.worldmodel)
1566                 Sys_Error ("R_RenderView: NULL worldmodel");
1567
1568         lighthalf = gl_lightmode.value;
1569
1570         FOG_framebegin();
1571         transpolyclear();
1572         wallpolyclear();
1573
1574         if (r_speeds2.value)
1575         {
1576                 currtime = Sys_FloatTime();
1577                 Con_Printf("render time: ");
1578         }
1579         R_Clear();
1580         TIMEREPORT("R_Clear")
1581
1582         // render normal view
1583
1584         R_SetupFrame ();
1585         TIMEREPORT("R_SetupFrame")
1586         R_SetFrustum ();
1587         TIMEREPORT("R_SetFrustum")
1588         R_SetupGL ();
1589         TIMEREPORT("R_SetupGL")
1590         R_MarkLeaves ();        // done here so we know if we're in water
1591         TIMEREPORT("R_MarkLeaves")
1592         R_DrawWorld ();         // adds static entities to the list
1593         TIMEREPORT("R_DrawWorld")
1594         S_ExtraUpdate ();       // don't let sound get messed up if going slow
1595         TIMEREPORT("S_ExtraUpdate")
1596         R_DrawEntitiesOnList1 (); // BSP models
1597         TIMEREPORT("R_DrawEntitiesOnList1")
1598         wallpolyrender();
1599         TIMEREPORT("wallpolyrender")
1600         R_DrawEntitiesOnList2 (); // other models
1601         TIMEREPORT("R_DrawEntitiesOnList2")
1602 //      R_RenderDlights ();
1603         R_DrawViewModel ();
1604         TIMEREPORT("R_DrawViewModel")
1605         R_DrawParticles ();
1606         TIMEREPORT("R_DrawParticles")
1607         transpolyrender();
1608         TIMEREPORT("transpolyrender")
1609
1610         FOG_frameend();
1611         GL_BlendView();
1612         TIMEREPORT("GL_BlendView")
1613         if (r_speeds2.value)
1614                 Con_Printf("\n");
1615 }