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