some whitespace changes
[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_framecount;           // used for dlight push checking
32
33 mplane_t        frustum[4];
34
35 int                     c_brush_polys, c_alias_polys, c_light_polys, c_faces, c_nodes, c_leafs, c_models, c_bmodels, c_sprites, c_particles, c_dlights;
36
37 qboolean        envmap;                         // true during envmap command capture 
38
39 // LordHavoc: moved all code related to particles into r_part.c
40 //int                   particletexture;        // little dot for particles
41 //int                   playertextures;         // up to 16 color translated skins
42
43 //
44 // view origin
45 //
46 vec3_t  vup;
47 vec3_t  vpn;
48 vec3_t  vright;
49 vec3_t  r_origin;
50
51 float   r_world_matrix[16];
52 float   r_base_world_matrix[16];
53
54 //
55 // screen size info
56 //
57 refdef_t        r_refdef;
58
59 mleaf_t         *r_viewleaf, *r_oldviewleaf;
60
61 unsigned short  d_lightstylevalue[256]; // 8.8 fraction of base light value
62
63
64 void R_MarkLeaves (void);
65
66 //cvar_t        r_norefresh = {"r_norefresh","0"};
67 cvar_t  r_drawentities = {"r_drawentities","1"};
68 cvar_t  r_drawviewmodel = {"r_drawviewmodel","1"};
69 cvar_t  r_speeds = {"r_speeds","0"};
70 cvar_t  r_speeds2 = {"r_speeds2","0"};
71 cvar_t  r_fullbright = {"r_fullbright","0"};
72 //cvar_t        r_lightmap = {"r_lightmap","0"};
73 cvar_t  r_wateralpha = {"r_wateralpha","1"};
74 cvar_t  r_dynamic = {"r_dynamic","1"};
75 cvar_t  r_waterripple = {"r_waterripple","0"};
76 cvar_t  r_fullbrights = {"r_fullbrights", "1"};
77
78 cvar_t  gl_lightmode = {"gl_lightmode", "1", true}; // LordHavoc: overbright lighting
79 //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...
80 cvar_t  r_farclip = {"r_farclip", "6144"};
81
82 cvar_t  gl_fogenable = {"gl_fogenable", "0"};
83 cvar_t  gl_fogdensity = {"gl_fogdensity", "0.25"};
84 cvar_t  gl_fogred = {"gl_fogred","0.3"};
85 cvar_t  gl_foggreen = {"gl_foggreen","0.3"};
86 cvar_t  gl_fogblue = {"gl_fogblue","0.3"};
87 cvar_t  gl_fogstart = {"gl_fogstart", "0"};
88 cvar_t  gl_fogend = {"gl_fogend","0"};
89 cvar_t  glfog = {"glfog", "0"};
90
91 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
92 {
93         int sides;
94         mnode_t *nodestack[8192], *node;
95         int stack = 0;
96
97         if (R_CullBox(mins, maxs))
98                 return true;
99
100         node = cl.worldmodel->nodes;
101 loc0:
102         if (node->contents < 0)
103         {
104                 if (((mleaf_t *)node)->visframe == r_framecount)
105                         return false;
106                 if (!stack)
107                         return true;
108                 node = nodestack[--stack];
109                 goto loc0;
110         }
111         
112         sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
113         
114 // recurse down the contacted sides
115         if (sides & 1)
116         {
117                 if (sides & 2) // 3
118                 {
119                         // put second child on the stack for later examination
120                         nodestack[stack++] = node->children[1];
121                         node = node->children[0];
122                         goto loc0;
123                 }
124                 else // 1
125                 {
126                         node = node->children[0];
127                         goto loc0;
128                 }
129         }
130         // 2
131         node = node->children[1];
132         goto loc0;
133 }
134
135 qboolean lighthalf;
136
137 vec3_t fogcolor;
138 vec_t fogdensity;
139 float fog_density, fog_red, fog_green, fog_blue;
140 qboolean fogenabled;
141 qboolean oldgl_fogenable;
142 void FOG_framebegin()
143 {
144         if (nehahra)
145         {
146 //              if (!Nehahrademcompatibility)
147 //                      gl_fogenable.value = 0;
148                 if (gl_fogenable.value)
149                 {
150                         oldgl_fogenable = true;
151                         fog_density = gl_fogdensity.value;
152                         fog_red = gl_fogred.value;
153                         fog_green = gl_foggreen.value;
154                         fog_blue = gl_fogblue.value;
155                 }
156                 else if (oldgl_fogenable)
157                 {
158                         oldgl_fogenable = false;
159                         fog_density = 0;
160                         fog_red = 0;
161                         fog_green = 0;
162                         fog_blue = 0;
163                 }
164         }
165         if (fog_density)
166         {
167                         fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
168                         fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
169                         fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
170                         if (lighthalf)
171                         {
172                                 fogcolor[0] *= 0.5f;
173                                 fogcolor[1] *= 0.5f;
174                                 fogcolor[2] *= 0.5f;
175                         }
176         }
177         if (glfog.value)
178         {
179                 if (!r_render.value)
180                         return;
181                 if(fog_density)
182                 {
183                         // LordHavoc: Borland C++ 5.0 was choking on this line, stupid compiler...
184                         //GLfloat colors[4] = {(GLfloat) gl_fogred.value, (GLfloat) gl_foggreen.value, (GLfloat) gl_fogblue.value, (GLfloat) 1};
185                         GLfloat colors[4];
186                         colors[0] = fog_red;
187                         colors[1] = fog_green;
188                         colors[2] = fog_blue;
189                         colors[3] = 1;
190                         if (lighthalf)
191                         {
192                                 colors[0] *= 0.5f;
193                                 colors[1] *= 0.5f;
194                                 colors[2] *= 0.5f;
195                         }
196
197                         glFogi (GL_FOG_MODE, GL_EXP2);
198                         glFogf (GL_FOG_DENSITY, (GLfloat) fog_density / 100); 
199                         glFogfv (GL_FOG_COLOR, colors);
200                         glEnable (GL_FOG);
201                 }
202                 else
203                         glDisable(GL_FOG);
204         }
205         else
206         {
207                 if (fog_density)
208                 {
209                         fogenabled = true;
210                         fogdensity = -4000.0f / (fog_density * fog_density);
211                         // fog color was already set
212                 }
213                 else
214                         fogenabled = false;
215         }
216 }
217
218 void FOG_frameend()
219 {
220         if (glfog.value)
221                 glDisable(GL_FOG);
222 }
223
224 void FOG_clear()
225 {
226         if (nehahra)
227         {
228                 Cvar_Set("gl_fogenable", "0");
229                 Cvar_Set("gl_fogdensity", "0.2");
230                 Cvar_Set("gl_fogred", "0.3");
231                 Cvar_Set("gl_foggreen", "0.3");
232                 Cvar_Set("gl_fogblue", "0.3");
233         }
234         fog_density = fog_red = fog_green = fog_blue = 0.0f;
235 }
236
237 void FOG_registercvars()
238 {
239         Cvar_RegisterVariable (&glfog);
240         if (nehahra)
241         {
242                 Cvar_RegisterVariable (&gl_fogenable);
243                 Cvar_RegisterVariable (&gl_fogdensity);
244                 Cvar_RegisterVariable (&gl_fogred);
245                 Cvar_RegisterVariable (&gl_foggreen); 
246                 Cvar_RegisterVariable (&gl_fogblue);
247                 Cvar_RegisterVariable (&gl_fogstart);
248                 Cvar_RegisterVariable (&gl_fogend);
249         }
250 }
251
252 void gl_main_start()
253 {
254 }
255
256 void gl_main_shutdown()
257 {
258 }
259
260 void gl_main_newmap()
261 {
262 }
263
264 void GL_Main_Init()
265 {
266         FOG_registercvars();
267         Cvar_RegisterVariable (&r_drawentities);
268         Cvar_RegisterVariable (&r_drawviewmodel);
269         Cvar_RegisterVariable (&r_speeds);
270         Cvar_RegisterVariable (&r_speeds2);
271         Cvar_RegisterVariable (&gl_lightmode);
272 //      Cvar_RegisterVariable (&r_dynamicwater);
273 //      Cvar_RegisterVariable (&r_dynamicbothsides);
274         Cvar_RegisterVariable (&r_fullbrights);
275         Cvar_RegisterVariable (&r_wateralpha);
276         Cvar_RegisterVariable (&r_dynamic);
277         Cvar_RegisterVariable (&r_waterripple); // LordHavoc: added waterripple
278         if (nehahra)
279                 Cvar_SetValue("r_fullbrights", 0);
280 //      if (gl_vendor && strstr(gl_vendor, "3Dfx"))
281 //              gl_lightmode.value = 0;
282         Cvar_RegisterVariable (&r_fullbright);
283         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
284 }
285
286 extern void GL_Draw_Init();
287 extern void GL_Main_Init();
288 extern void GL_Models_Init();
289 extern void GL_Poly_Init();
290 extern void GL_Surf_Init();
291 extern void GL_Screen_Init();
292 extern void GL_Misc_Init();
293 extern void R_Crosshairs_Init();
294 extern void R_Light_Init();
295 extern void R_Particles_Init();
296 extern void R_Explosion_Init();
297 extern void CL_Effects_Init();
298
299 void Render_Init()
300 {
301         R_Modules_Shutdown();
302         GL_Draw_Init();
303         GL_Main_Init();
304         GL_Models_Init();
305         GL_Poly_Init();
306         GL_Surf_Init();
307         GL_Screen_Init();
308         GL_Misc_Init();
309         R_Crosshairs_Init();
310         R_Light_Init();
311         R_Particles_Init();
312         R_Explosion_Init();
313         CL_Effects_Init();
314         R_Decals_Init();
315         R_Modules_Start();
316 }
317
318 /*
319 ===============
320 GL_Init
321 ===============
322 */
323 extern char *ENGINE_EXTENSIONS;
324 void GL_Init (void)
325 {
326         gl_vendor = glGetString (GL_VENDOR);
327         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
328         gl_renderer = glGetString (GL_RENDERER);
329         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
330
331         gl_version = glGetString (GL_VERSION);
332         Con_Printf ("GL_VERSION: %s\n", gl_version);
333         gl_extensions = glGetString (GL_EXTENSIONS);
334         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
335
336 //      Con_Printf ("%s %s\n", gl_renderer, gl_version);
337
338         VID_CheckMultitexture();
339         VID_CheckCVA();
340
341         // LordHavoc: report supported extensions
342         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
343
344         glCullFace(GL_FRONT);
345         glEnable(GL_TEXTURE_2D);
346 //      glDisable(GL_ALPHA_TEST);
347         glAlphaFunc(GL_GREATER, 0.5);
348
349 //      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
350 }
351
352
353 /*
354 void R_RotateForEntity (entity_t *e)
355 {
356         glTranslatef (e->origin[0],  e->origin[1],  e->origin[2]);
357
358         glRotatef (e->angles[1],  0, 0, 1);
359         glRotatef (-e->angles[0],  0, 1, 0);
360         glRotatef (e->angles[2],  1, 0, 0);
361
362         glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
363 }
364 */
365
366 // LordHavoc: shading stuff
367 vec3_t  shadevector;
368 vec3_t  shadecolor;
369
370 float   modelalpha;
371
372 //==================================================================================
373
374 void R_DrawBrushModel (entity_t *e);
375 void R_DrawSpriteModel (entity_t *e, frameblend_t *blend);
376
377 void R_LerpUpdate(entity_t *ent)
378 {
379         int frame;
380         frame = ent->render.frame;
381         if (ent->render.model && ent->render.frame >= ent->render.model->numframes)
382         {
383                 Con_Printf("R_LerpUpdate: no such frame%6i in \"%s\"\n", ent->render.frame, ent->render.model->name);
384                 frame = 0;
385         }
386
387         if (ent->render.lerp_model != ent->render.model)
388         {
389                 // reset all interpolation information
390                 ent->render.lerp_model = ent->render.model;
391                 ent->render.frame1 = ent->render.frame2 = frame;
392                 ent->render.frame1start = ent->render.frame2start = cl.time;
393                 ent->render.framelerp = 1;
394                 ent->render.lerp_starttime = 0;
395         }
396         else if (ent->render.frame2 != frame)
397         {
398                 // transition to new frame
399                 ent->render.frame1 = ent->render.frame2;
400                 ent->render.frame1start = ent->render.frame2start;
401                 ent->render.frame2 = frame;
402                 ent->render.frame2start = cl.time;
403                 ent->render.framelerp = 0;
404                 ent->render.lerp_starttime = cl.time;
405         }
406         else
407         {
408                 // lerp_starttime < 0 is used to prevent changing of framelerp
409                 if (ent->render.lerp_starttime >= 0)
410                 {
411                         // update transition
412                         ent->render.framelerp = (cl.time - ent->render.lerp_starttime) * 10;
413                         ent->render.framelerp = bound(0, ent->render.framelerp, 1);
414                 }
415         }
416 }
417
418
419 void R_PrepareEntities (void)
420 {
421         int i;
422         entity_t *ent;
423         vec3_t v;
424         // this updates entities that are supposed to be view relative
425         for (i = 0;i < cl_numvisedicts;i++)
426         {
427                 ent = cl_visedicts[i];
428
429                 if (ent->render.flags & RENDER_VIEWMODEL)
430                 {
431                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
432                         ent->render.flags -= RENDER_VIEWMODEL;
433                         // transform origin
434                         VectorCopy(ent->render.origin, v);
435                         ent->render.origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
436                         ent->render.origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
437                         ent->render.origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
438                         // adjust angles
439                         VectorAdd(ent->render.angles, r_refdef.viewangles, ent->render.angles);
440                 }
441         }
442 }
443
444 /*
445 =============
446 R_DrawEntitiesOnList
447 =============
448 */
449 // LordHavoc: split so bmodels are rendered before any other objects
450 void R_DrawEntitiesOnList1 (void)
451 {
452         int             i;
453
454         if (!r_drawentities.value)
455                 return;
456
457         for (i = 0;i < cl_numvisedicts;i++)
458         {
459                 if (cl_visedicts[i]->render.model->type != mod_brush)
460                         continue;
461                 currententity = cl_visedicts[i];
462                 modelalpha = currententity->render.alpha;
463
464                 R_DrawBrushModel (currententity);
465         }
466 }
467
468 void R_DrawEntitiesOnList2 (void)
469 {
470         int             i;
471         frameblend_t blend[4];
472
473         if (!r_drawentities.value)
474                 return;
475
476         for (i = 0;i < cl_numvisedicts;i++)
477         {
478                 currententity = cl_visedicts[i];
479                 modelalpha = currententity->render.alpha;
480
481                 switch (currententity->render.model->type)
482                 {
483                 case mod_alias:
484                         R_LerpUpdate(currententity);
485                         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
486                         R_DrawAliasModel (currententity, true, modelalpha, currententity->render.model, blend, currententity->render.skinnum, currententity->render.origin, currententity->render.angles, currententity->render.scale, currententity->render.effects, currententity->render.model->flags, currententity->render.colormap);
487                         break;
488
489                 case mod_sprite:
490                         R_LerpUpdate(currententity);
491                         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
492                         R_DrawSpriteModel (currententity, blend);
493                         break;
494
495                 default:
496                         break;
497                 }
498         }
499 }
500
501 /*
502 =============
503 R_DrawViewModel
504 =============
505 */
506 void R_DrawViewModel (void)
507 {
508         frameblend_t blend[4];
509
510         if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
511                 return;
512
513         currententity = &cl.viewent;
514         currententity->render.alpha = modelalpha = cl_entities[cl.viewentity].render.alpha; // LordHavoc: if the player is transparent, so is the gun
515         currententity->render.effects = cl_entities[cl.viewentity].render.effects;
516         currententity->render.scale = 1;
517         VectorCopy(cl_entities[cl.viewentity].render.colormod, currententity->render.colormod);
518
519         R_LerpUpdate(currententity);
520         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
521
522         // hack the depth range to prevent view model from poking into walls
523         glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
524         R_DrawAliasModel (currententity, false, modelalpha, currententity->render.model, blend, currententity->render.skinnum, currententity->render.origin, currententity->render.angles, currententity->render.scale, currententity->render.effects, currententity->render.model->flags, currententity->render.colormap);
525         glDepthRange (gldepthmin, gldepthmax);
526 }
527
528 void R_DrawBrushModel (entity_t *e);
529
530 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
531
532 void R_SetFrustum (void)
533 {
534         int             i;
535
536         if (r_refdef.fov_x == 90) 
537         {
538                 // front side is visible
539
540                 VectorAdd (vpn, vright, frustum[0].normal);
541                 VectorSubtract (vpn, vright, frustum[1].normal);
542
543                 VectorAdd (vpn, vup, frustum[2].normal);
544                 VectorSubtract (vpn, vup, frustum[3].normal);
545         }
546         else
547         {
548                 // rotate VPN right by FOV_X/2 degrees
549                 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
550                 // rotate VPN left by FOV_X/2 degrees
551                 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
552                 // rotate VPN up by FOV_X/2 degrees
553                 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
554                 // rotate VPN down by FOV_X/2 degrees
555                 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
556         }
557
558         for (i=0 ; i<4 ; i++)
559         {
560                 frustum[i].type = PLANE_ANYZ;
561                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
562 //              frustum[i].signbits = SignbitsForPlane (&frustum[i]);
563                 BoxOnPlaneSideClassify(&frustum[i]);
564         }
565 }
566
567 void R_AnimateLight (void);
568 void V_CalcBlend (void);
569
570 /*
571 ===============
572 R_SetupFrame
573 ===============
574 */
575 void R_SetupFrame (void)
576 {
577 // don't allow cheats in multiplayer
578         if (cl.maxclients > 1)
579         {
580                 Cvar_Set ("r_fullbright", "0");
581                 Cvar_Set ("r_ambient", "0");
582         }
583
584         r_framecount++;
585
586 // build the transformation matrix for the given view angles
587         VectorCopy (r_refdef.vieworg, r_origin);
588
589         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
590
591 // current viewleaf
592         r_oldviewleaf = r_viewleaf;
593         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
594
595         V_SetContentsColor (r_viewleaf->contents);
596         V_CalcBlend ();
597
598         r_cache_thrash = false;
599
600         c_brush_polys = 0;
601         c_alias_polys = 0;
602         c_light_polys = 0;
603         c_faces = 0;
604         c_nodes = 0;
605         c_leafs = 0;
606         c_models = 0;
607         c_bmodels = 0;
608         c_sprites = 0;
609         c_particles = 0;
610 //      c_dlights = 0;
611
612         R_AnimateLight ();
613 }
614
615
616 void MYgluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
617 {
618    GLdouble xmin, xmax, ymin, ymax;
619
620    ymax = zNear * tan( fovy * M_PI / 360.0 );
621    ymin = -ymax;
622
623    xmin = ymin * aspect;
624    xmax = ymax * aspect;
625
626    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
627 }
628
629
630 extern char skyname[];
631
632 /*
633 =============
634 R_SetupGL
635 =============
636 */
637 void R_SetupGL (void)
638 {
639         float   screenaspect;
640         extern  int glwidth, glheight;
641         int             x, x2, y2, y, w, h;
642
643         if (!r_render.value)
644                 return;
645         //
646         // set up viewpoint
647         //
648         glMatrixMode(GL_PROJECTION);
649     glLoadIdentity ();
650         x = r_refdef.vrect.x * glwidth/vid.width;
651         x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
652         y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
653         y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
654
655         // fudge around because of frac screen scale
656         if (x > 0)
657                 x--;
658         if (x2 < glwidth)
659                 x2++;
660         if (y2 < 0)
661                 y2--;
662         if (y < glheight)
663                 y++;
664
665         w = x2 - x;
666         h = y - y2;
667
668         if (envmap)
669         {
670                 x = y2 = 0;
671                 w = h = 256;
672         }
673
674         glViewport (glx + x, gly + y2, w, h);
675     screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
676 //      yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
677         MYgluPerspective (r_refdef.fov_y,  screenaspect,  4,  r_farclip.value);
678
679         glCullFace(GL_FRONT);
680
681         glMatrixMode(GL_MODELVIEW);
682     glLoadIdentity ();
683
684     glRotatef (-90,  1, 0, 0);      // put Z going up
685     glRotatef (90,  0, 0, 1);       // put Z going up
686     glRotatef (-r_refdef.viewangles[2],  1, 0, 0);
687     glRotatef (-r_refdef.viewangles[0],  0, 1, 0);
688     glRotatef (-r_refdef.viewangles[1],  0, 0, 1);
689     glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
690
691         glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
692
693         //
694         // set drawing parms
695         //
696 //      if (gl_cull.value)
697                 glEnable(GL_CULL_FACE);
698 //      else
699 //              glDisable(GL_CULL_FACE);
700
701         glEnable(GL_BLEND); // was Disable
702         glDisable(GL_ALPHA_TEST);
703         glAlphaFunc(GL_GREATER, 0.5);
704         glEnable(GL_DEPTH_TEST);
705         glDepthMask(1);
706         glShadeModel(GL_SMOOTH);
707 }
708
709 /*
710 =============
711 R_Clear
712 =============
713 */
714 void R_Clear (void)
715 {
716         if (!r_render.value)
717                 return;
718 //      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
719         gldepthmin = 0;
720         gldepthmax = 1;
721         glDepthFunc (GL_LEQUAL);
722
723         glDepthRange (gldepthmin, gldepthmax);
724 }
725
726 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
727 void GL_Brighten()
728 {
729         if (!r_render.value)
730                 return;
731         glMatrixMode(GL_PROJECTION);
732     glLoadIdentity ();
733         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
734         glMatrixMode(GL_MODELVIEW);
735     glLoadIdentity ();
736         glDisable (GL_DEPTH_TEST);
737         glDisable (GL_CULL_FACE);
738         glDisable(GL_TEXTURE_2D);
739         glEnable(GL_BLEND);
740         glBlendFunc (GL_DST_COLOR, GL_ONE);
741         glBegin (GL_TRIANGLES);
742         glColor3f (1, 1, 1);
743         glVertex2f (-5000, -5000);
744         glVertex2f (10000, -5000);
745         glVertex2f (-5000, 10000);
746         glEnd ();
747         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
748         glDisable(GL_BLEND);
749         glEnable(GL_TEXTURE_2D);
750         glEnable (GL_DEPTH_TEST);
751         glEnable (GL_CULL_FACE);
752 }
753
754 void GL_BlendView()
755 {
756         if (!r_render.value)
757                 return;
758
759         if (v_blend[3] < 0.01f)
760                 return;
761
762         glMatrixMode(GL_PROJECTION);
763     glLoadIdentity ();
764         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
765         glMatrixMode(GL_MODELVIEW);
766     glLoadIdentity ();
767         glDisable (GL_DEPTH_TEST);
768         glDisable (GL_CULL_FACE);
769         glDisable(GL_TEXTURE_2D);
770         glEnable(GL_BLEND);
771         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
772         glBegin (GL_TRIANGLES);
773         if (lighthalf)
774                 glColor4f (v_blend[0] * 0.5f, v_blend[1] * 0.5f, v_blend[2] * 0.5f, v_blend[3]);
775         else
776                 glColor4fv (v_blend);
777         glVertex2f (-5000, -5000);
778         glVertex2f (10000, -5000);
779         glVertex2f (-5000, 10000);
780         glEnd ();
781
782         glEnable (GL_CULL_FACE);
783         glEnable (GL_DEPTH_TEST);
784         glDisable(GL_BLEND);
785         glEnable(GL_TEXTURE_2D);
786 }
787
788 #define TIMEREPORT(VAR) \
789         if (r_speeds2.value)\
790         {\
791                 temptime = currtime;\
792                 currtime = Sys_DoubleTime();\
793                 VAR = (int) ((currtime - temptime) * 1000000.0);\
794         }\
795         else\
796                 VAR = 0;
797
798 /*
799 ================
800 R_RenderView
801
802 r_refdef must be set before the first call
803 ================
804 */
805 extern qboolean intimerefresh;
806 extern void R_Sky();
807 extern void UploadLightmaps();
808 char r_speeds2_string1[81], r_speeds2_string2[81], r_speeds2_string3[81], r_speeds2_string4[81], r_speeds2_string5[81], r_speeds2_string6[81], r_speeds2_string7[81];
809 void R_RenderView (void)
810 {
811         double starttime, currtime, temptime;
812         int time_clear, time_setup, time_world, time_bmodels, time_upload, time_sky, time_wall, time_models, time_moveparticles, time_drawparticles, time_moveexplosions, time_drawexplosions, time_drawdecals, time_transpoly, time_blend, time_total;
813 //      if (r_norefresh.value)
814 //              return;
815
816         if (!r_worldentity.render.model || !cl.worldmodel)
817                 Host_Error ("R_RenderView: NULL worldmodel");
818
819         lighthalf = gl_lightmode.value;
820
821         FOG_framebegin();
822
823         if (r_speeds2.value)
824                 starttime = currtime = Sys_DoubleTime();
825         else
826                 starttime = currtime = 0;
827         R_Clear();
828         TIMEREPORT(time_clear)
829
830         // render normal view
831
832         R_SetupFrame ();
833         R_SetFrustum ();
834         R_SetupGL ();
835
836         R_PrepareEntities();
837
838         skypolyclear();
839         wallpolyclear();
840         transpolyclear();
841
842         TIMEREPORT(time_setup)
843
844         R_DrawWorld ();
845         TIMEREPORT(time_world)
846         R_DrawEntitiesOnList1 (); // BSP models
847         TIMEREPORT(time_bmodels)
848
849         UploadLightmaps();
850         TIMEREPORT(time_upload)
851
852         skypolyrender(); // fogged sky polys, affects depth
853
854         if (skyname[0] && currentskypoly && !fogenabled)
855                 R_Sky(); // does not affect depth, draws over the sky polys
856         TIMEREPORT(time_sky)
857
858         wallpolyrender();
859         TIMEREPORT(time_wall)
860
861         GL_DrawDecals();
862         TIMEREPORT(time_drawdecals)
863
864         if (!intimerefresh && !r_speeds2.value)
865                 S_ExtraUpdate ();       // don't let sound get messed up if going slow
866
867         R_DrawEntitiesOnList2 (); // other models
868 //      R_RenderDlights ();
869         R_DrawViewModel ();
870         TIMEREPORT(time_models)
871         R_MoveParticles ();
872         TIMEREPORT(time_moveparticles)
873         R_DrawParticles ();
874         TIMEREPORT(time_drawparticles)
875         R_MoveExplosions();
876         TIMEREPORT(time_moveexplosions)
877         R_DrawExplosions();
878         TIMEREPORT(time_drawexplosions)
879
880         transpolyrender();
881         TIMEREPORT(time_transpoly)
882
883         FOG_frameend();
884
885         GL_BlendView();
886         TIMEREPORT(time_blend)
887         if (r_speeds2.value)
888         {
889                 time_total = (int) ((Sys_DoubleTime() - starttime) * 1000000.0);
890                 sprintf(r_speeds2_string1, "%6i walls %6i dlitwalls %7i modeltris %7i transpoly\n", c_brush_polys, c_light_polys, c_alias_polys, currenttranspoly);
891                 sprintf(r_speeds2_string2, "BSP: %6i faces %6i nodes %6i leafs\n", c_faces, c_nodes, c_leafs);
892                 sprintf(r_speeds2_string3, "%4i models %4i bmodels %4i sprites %5i particles %3i dlights\n", c_models, c_bmodels, c_sprites, c_particles, c_dlights);
893                 sprintf(r_speeds2_string4, "%6ius clear  %6ius setup  %6ius world  %6ius bmodel %6ius upload", time_clear, time_setup, time_world, time_bmodels, time_upload);
894                 sprintf(r_speeds2_string5, "%6ius sky    %6ius wall   %6ius models %6ius mpart  %6ius dpart ", time_sky, time_wall, time_models, time_moveparticles, time_drawparticles);
895                 sprintf(r_speeds2_string6, "%6ius mexplo %6ius dexplo %6ius decals %6ius trans  %6ius blend ", time_moveexplosions, time_drawexplosions, time_drawdecals, time_transpoly, time_blend);
896                 sprintf(r_speeds2_string7, "%6ius permdl %6ius total ", time_models / max(c_models, 1), time_total);
897         }
898 }