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