f848defad3109e647fa9533251708d0c209c51ed
[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 //static qboolean       r_cache_thrash;         // compatability
25
26 vec3_t          modelorg;
27 entity_render_t *currentrenderentity;
28
29 int                     r_framecount;           // used for dlight push checking
30
31 mplane_t        frustum[4];
32
33 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;
34
35 qboolean        envmap;                         // true during envmap command capture
36
37 // LordHavoc: moved all code related to particles into r_part.c
38 //int                   particletexture;        // little dot for particles
39 //int                   playertextures;         // up to 16 color translated skins
40
41 //
42 // view origin
43 //
44 vec3_t  vup;
45 vec3_t  vpn;
46 vec3_t  vright;
47 vec3_t  r_origin;
48
49 //float r_world_matrix[16];
50 //float r_base_world_matrix[16];
51
52 //
53 // screen size info
54 //
55 refdef_t        r_refdef;
56
57 mleaf_t         *r_viewleaf, *r_oldviewleaf;
58
59 unsigned short  d_lightstylevalue[256]; // 8.8 fraction of base light value
60
61 //cvar_t        r_norefresh = {0, "r_norefresh","0"};
62 cvar_t  r_drawentities = {0, "r_drawentities","1"};
63 cvar_t  r_drawviewmodel = {0, "r_drawviewmodel","1"};
64 cvar_t  r_speeds = {0, "r_speeds","0"};
65 cvar_t  r_speeds2 = {0, "r_speeds2","0"};
66 cvar_t  r_fullbright = {0, "r_fullbright","0"};
67 //cvar_t        r_lightmap = {0, "r_lightmap","0"};
68 cvar_t  r_wateralpha = {CVAR_SAVE, "r_wateralpha","1"};
69 cvar_t  r_dynamic = {CVAR_SAVE, "r_dynamic","1"};
70 cvar_t  r_waterripple = {CVAR_SAVE, "r_waterripple","0"};
71 cvar_t  r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1"};
72
73 cvar_t  gl_lightmode = {CVAR_SAVE, "gl_lightmode", "1"}; // LordHavoc: overbright lighting
74 //cvar_t        r_dynamicbothsides = {CVAR_SAVE, "r_dynamicbothsides", "1"}; // LordHavoc: can disable dynamic lighting of backfaces, but quake maps are weird so it doesn't always work right...
75 cvar_t  r_farclip = {0, "r_farclip", "6144"}; // FIXME: make this go away (calculate based on farthest visible object/polygon)
76
77 cvar_t  gl_fogenable = {0, "gl_fogenable", "0"};
78 cvar_t  gl_fogdensity = {0, "gl_fogdensity", "0.25"};
79 cvar_t  gl_fogred = {0, "gl_fogred","0.3"};
80 cvar_t  gl_foggreen = {0, "gl_foggreen","0.3"};
81 cvar_t  gl_fogblue = {0, "gl_fogblue","0.3"};
82 cvar_t  gl_fogstart = {0, "gl_fogstart", "0"};
83 cvar_t  gl_fogend = {0, "gl_fogend","0"};
84 cvar_t  glfog = {0, "glfog", "0"};
85
86 cvar_t  r_ser = {CVAR_SAVE, "r_ser", "1"};
87 cvar_t  gl_viewmodeldepthhack = {0, "gl_viewmodeldepthhack", "1"};
88
89 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
90 {
91         int sides;
92         mnode_t *nodestack[8192], *node;
93         int stack = 0;
94
95         if (R_CullBox(mins, maxs))
96                 return true;
97
98         node = cl.worldmodel->nodes;
99 loc0:
100         if (node->contents < 0)
101         {
102                 if (((mleaf_t *)node)->visframe == r_framecount)
103                         return false;
104                 if (!stack)
105                         return true;
106                 node = nodestack[--stack];
107                 goto loc0;
108         }
109
110         sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
111
112 // recurse down the contacted sides
113         if (sides & 1)
114         {
115                 if (sides & 2) // 3
116                 {
117                         // put second child on the stack for later examination
118                         nodestack[stack++] = node->children[1];
119                         node = node->children[0];
120                         goto loc0;
121                 }
122                 else // 1
123                 {
124                         node = node->children[0];
125                         goto loc0;
126                 }
127         }
128         // 2
129         node = node->children[1];
130         goto loc0;
131 }
132
133 qboolean lighthalf;
134
135 vec3_t fogcolor;
136 vec_t fogdensity;
137 float fog_density, fog_red, fog_green, fog_blue;
138 qboolean fogenabled;
139 qboolean oldgl_fogenable;
140 void FOG_framebegin(void)
141 {
142         if (gamemode == GAME_NEHAHRA)
143         {
144                 if (gl_fogenable.value)
145                 {
146                         oldgl_fogenable = true;
147                         fog_density = gl_fogdensity.value;
148                         fog_red = gl_fogred.value;
149                         fog_green = gl_foggreen.value;
150                         fog_blue = gl_fogblue.value;
151                 }
152                 else if (oldgl_fogenable)
153                 {
154                         oldgl_fogenable = false;
155                         fog_density = 0;
156                         fog_red = 0;
157                         fog_green = 0;
158                         fog_blue = 0;
159                 }
160         }
161         if (fog_density)
162         {
163                         fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
164                         fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
165                         fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
166                         if (lighthalf)
167                         {
168                                 fogcolor[0] *= 0.5f;
169                                 fogcolor[1] *= 0.5f;
170                                 fogcolor[2] *= 0.5f;
171                         }
172         }
173         if (glfog.value)
174         {
175                 if (!r_render.value)
176                         return;
177                 if(fog_density)
178                 {
179                         // LordHavoc: Borland C++ 5.0 was choking on this line...
180                         //GLfloat colors[4] = {(GLfloat) gl_fogred.value, (GLfloat) gl_foggreen.value, (GLfloat) gl_fogblue.value, (GLfloat) 1};
181                         GLfloat colors[4];
182                         colors[0] = fog_red;
183                         colors[1] = fog_green;
184                         colors[2] = fog_blue;
185                         colors[3] = 1;
186                         if (lighthalf)
187                         {
188                                 colors[0] *= 0.5f;
189                                 colors[1] *= 0.5f;
190                                 colors[2] *= 0.5f;
191                         }
192
193                         glFogi (GL_FOG_MODE, GL_EXP2);
194                         glFogf (GL_FOG_DENSITY, (GLfloat) fog_density / 100);
195                         glFogfv (GL_FOG_COLOR, colors);
196                         glEnable (GL_FOG);
197                 }
198                 else
199                         glDisable(GL_FOG);
200         }
201         else
202         {
203                 if (fog_density)
204                 {
205                         fogenabled = true;
206                         fogdensity = -4000.0f / (fog_density * fog_density);
207                         // fog color was already set
208                 }
209                 else
210                         fogenabled = false;
211         }
212 }
213
214 void FOG_frameend(void)
215 {
216         if (glfog.value)
217                 glDisable(GL_FOG);
218 }
219
220 void FOG_clear(void)
221 {
222         if (gamemode == GAME_NEHAHRA)
223         {
224                 Cvar_Set("gl_fogenable", "0");
225                 Cvar_Set("gl_fogdensity", "0.2");
226                 Cvar_Set("gl_fogred", "0.3");
227                 Cvar_Set("gl_foggreen", "0.3");
228                 Cvar_Set("gl_fogblue", "0.3");
229         }
230         fog_density = fog_red = fog_green = fog_blue = 0.0f;
231 }
232
233 void FOG_registercvars(void)
234 {
235         Cvar_RegisterVariable (&glfog);
236         if (gamemode == GAME_NEHAHRA)
237         {
238                 Cvar_RegisterVariable (&gl_fogenable);
239                 Cvar_RegisterVariable (&gl_fogdensity);
240                 Cvar_RegisterVariable (&gl_fogred);
241                 Cvar_RegisterVariable (&gl_foggreen);
242                 Cvar_RegisterVariable (&gl_fogblue);
243                 Cvar_RegisterVariable (&gl_fogstart);
244                 Cvar_RegisterVariable (&gl_fogend);
245         }
246 }
247
248 void gl_main_start(void)
249 {
250 }
251
252 void gl_main_shutdown(void)
253 {
254 }
255
256 void gl_main_newmap(void)
257 {
258 }
259
260 void GL_Main_Init(void)
261 {
262         FOG_registercvars();
263         Cvar_RegisterVariable (&r_drawentities);
264         Cvar_RegisterVariable (&r_drawviewmodel);
265         Cvar_RegisterVariable (&r_speeds);
266         Cvar_RegisterVariable (&r_speeds2);
267         Cvar_RegisterVariable (&gl_lightmode);
268 //      Cvar_RegisterVariable (&r_dynamicwater);
269 //      Cvar_RegisterVariable (&r_dynamicbothsides);
270         Cvar_RegisterVariable (&r_fullbrights);
271         Cvar_RegisterVariable (&r_wateralpha);
272         Cvar_RegisterVariable (&r_dynamic);
273         Cvar_RegisterVariable (&r_waterripple);
274         Cvar_RegisterVariable (&r_farclip);
275         Cvar_RegisterVariable (&r_fullbright);
276         Cvar_RegisterVariable (&r_ser);
277         Cvar_RegisterVariable (&gl_viewmodeldepthhack);
278         if (gamemode == GAME_NEHAHRA)
279                 Cvar_SetValue("r_fullbrights", 0);
280         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
281 }
282
283 extern void GL_Draw_Init(void);
284 extern void GL_Main_Init(void);
285 extern void GL_Models_Init(void);
286 extern void GL_Poly_Init(void);
287 extern void GL_Surf_Init(void);
288 extern void GL_Screen_Init(void);
289 extern void GL_Misc_Init(void);
290 extern void R_Crosshairs_Init(void);
291 extern void R_Light_Init(void);
292 extern void R_Particles_Init(void);
293 extern void R_Explosion_Init(void);
294 extern void CL_Effects_Init(void);
295 extern void R_Clip_Init(void);
296 extern void ui_init(void);
297
298 void Render_Init(void)
299 {
300         R_Modules_Shutdown();
301         R_Clip_Init();
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         ui_init();
316         R_Modules_Start();
317 }
318
319 /*
320 ===============
321 GL_Init
322 ===============
323 */
324 extern char *ENGINE_EXTENSIONS;
325 void GL_Init (void)
326 {
327         gl_vendor = glGetString (GL_VENDOR);
328         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
329         gl_renderer = glGetString (GL_RENDERER);
330         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
331
332         gl_version = glGetString (GL_VERSION);
333         Con_Printf ("GL_VERSION: %s\n", gl_version);
334         gl_extensions = glGetString (GL_EXTENSIONS);
335         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
336
337 //      Con_Printf ("%s %s\n", gl_renderer, gl_version);
338
339 //      VID_CheckMultitexture();
340 //      VID_CheckCVA();
341 //      VID_CheckCombine();
342         VID_CheckExtensions();
343
344         // LordHavoc: report supported extensions
345         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
346
347         glCullFace(GL_FRONT);
348         glEnable(GL_TEXTURE_2D);
349 //      glDisable(GL_ALPHA_TEST);
350         glAlphaFunc(GL_GREATER, 0.5);
351
352 //      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
353 }
354
355
356 //==================================================================================
357
358 void R_Entity_Callback(void *data, void *junk)
359 {
360         ((entity_render_t *)data)->visframe = r_framecount;
361 }
362
363 static void R_AddModelEntities (void)
364 {
365         int             i;
366         vec3_t  v;
367
368         if (!r_drawentities.value)
369                 return;
370
371         for (i = 0;i < cl_numvisedicts;i++)
372         {
373                 currentrenderentity = &cl_visedicts[i]->render;
374
375                 // move view-relative models to where they should be
376                 if (currentrenderentity->flags & RENDER_VIEWMODEL)
377                 {
378                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
379                         currentrenderentity->flags -= RENDER_VIEWMODEL;
380                         // transform origin
381                         VectorCopy(currentrenderentity->origin, v);
382                         currentrenderentity->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
383                         currentrenderentity->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
384                         currentrenderentity->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
385                         // adjust angles
386                         VectorAdd(currentrenderentity->angles, r_refdef.viewangles, currentrenderentity->angles);
387                 }
388
389                 if (currentrenderentity->angles[0] || currentrenderentity->angles[2])
390                 {
391                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmins, currentrenderentity->mins);
392                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmaxs, currentrenderentity->maxs);
393                 }
394                 else if (currentrenderentity->angles[1])
395                 {
396                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmins, currentrenderentity->mins);
397                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmaxs, currentrenderentity->maxs);
398                 }
399                 else
400                 {
401                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmins, currentrenderentity->mins);
402                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmaxs, currentrenderentity->maxs);
403                 }
404                 if (R_VisibleCullBox(currentrenderentity->mins, currentrenderentity->maxs))
405                         continue;
406
407                 R_LerpAnimation(currentrenderentity);
408                 if (r_ser.value)
409                         currentrenderentity->model->SERAddEntity();
410                 else
411                         currentrenderentity->visframe = r_framecount;
412         }
413 }
414
415 void R_DrawModels1 (void)
416 {
417         int             i;
418
419         if (!r_drawentities.value)
420                 return;
421
422         for (i = 0;i < cl_numvisedicts;i++)
423         {
424                 currentrenderentity = &cl_visedicts[i]->render;
425                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawEarly)
426                         currentrenderentity->model->DrawEarly();
427         }
428 }
429
430 void R_DrawModels2 (void)
431 {
432         int             i;
433
434         if (!r_drawentities.value)
435                 return;
436
437         for (i = 0;i < cl_numvisedicts;i++)
438         {
439                 currentrenderentity = &cl_visedicts[i]->render;
440                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawLate)
441                         currentrenderentity->model->DrawLate();
442         }
443 }
444
445 /*
446 =============
447 R_DrawViewModel
448 =============
449 */
450 void R_DrawViewModel (void)
451 {
452         if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
453                 return;
454
455         currentrenderentity = &cl.viewent.render;
456
457         R_LerpAnimation(currentrenderentity);
458
459         // hack the depth range to prevent view model from poking into walls
460         if (gl_viewmodeldepthhack.value)
461                 glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
462         currentrenderentity->model->DrawLate();
463         if (gl_viewmodeldepthhack.value)
464                 glDepthRange (gldepthmin, gldepthmax);
465 }
466
467 static void R_SetFrustum (void)
468 {
469         int             i;
470
471         // LordHavoc: note to all quake engine coders, this code was making the
472         // view frustum taller than it should have been (it assumed the view is
473         // square; it is not square), so I disabled it
474         /*
475         if (r_refdef.fov_x == 90)
476         {
477                 // front side is visible
478
479                 VectorAdd (vpn, vright, frustum[0].normal);
480                 VectorSubtract (vpn, vright, frustum[1].normal);
481
482                 VectorAdd (vpn, vup, frustum[2].normal);
483                 VectorSubtract (vpn, vup, frustum[3].normal);
484         }
485         else
486         {
487         */
488                 // rotate VPN right by FOV_X/2 degrees
489                 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
490                 // rotate VPN left by FOV_X/2 degrees
491                 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
492                 // rotate VPN up by FOV_X/2 degrees
493                 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
494                 // rotate VPN down by FOV_X/2 degrees
495                 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
496         //}
497
498         for (i=0 ; i<4 ; i++)
499         {
500                 frustum[i].type = PLANE_ANYZ;
501                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
502 //              frustum[i].signbits = SignbitsForPlane (&frustum[i]);
503                 PlaneClassify(&frustum[i]);
504         }
505 }
506
507 /*
508 ===============
509 R_SetupFrame
510 ===============
511 */
512 static void R_SetupFrame (void)
513 {
514 // don't allow cheats in multiplayer
515         if (cl.maxclients > 1)
516         {
517                 if (r_fullbright.value != 0)
518                         Cvar_Set ("r_fullbright", "0");
519                 if (r_ambient.value != 0)
520                         Cvar_Set ("r_ambient", "0");
521         }
522
523         r_framecount++;
524
525 // build the transformation matrix for the given view angles
526         VectorCopy (r_refdef.vieworg, r_origin);
527
528         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
529
530 // current viewleaf
531         r_oldviewleaf = r_viewleaf;
532         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
533
534         V_SetContentsColor (r_viewleaf->contents);
535         V_CalcBlend ();
536
537 //      r_cache_thrash = false;
538
539         c_brush_polys = 0;
540         c_alias_polys = 0;
541         c_light_polys = 0;
542         c_faces = 0;
543         c_nodes = 0;
544         c_leafs = 0;
545         c_models = 0;
546         c_bmodels = 0;
547         c_sprites = 0;
548         c_particles = 0;
549 //      c_dlights = 0;
550
551         R_AnimateLight ();
552 }
553
554
555 static void MYgluPerspective(GLdouble fovx, GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
556 {
557         GLdouble xmax, ymax;
558
559         xmax = zNear * tan( fovx * M_PI / 360.0 ) * aspect;
560         ymax = zNear * tan( fovy * M_PI / 360.0 );
561
562         if (r_viewleaf->contents != CONTENTS_EMPTY && r_viewleaf->contents != CONTENTS_SOLID)
563         {
564                 xmax *= (sin(cl.time * 4.7) * 0.03 + 0.97);
565                 ymax *= (sin(cl.time * 3) * 0.03 + 0.97);
566         }
567
568         glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar );
569 }
570
571
572 /*
573 =============
574 R_SetupGL
575 =============
576 */
577 static void R_SetupGL (void)
578 {
579         if (!r_render.value)
580                 return;
581
582         // set up viewpoint
583         glMatrixMode(GL_PROJECTION);
584         glLoadIdentity ();
585
586         // y is weird beause OpenGL is bottom to top, we use top to bottom
587         glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);
588 //      yfov = 2*atan((float)r_refdef.height/r_refdef.width)*180/M_PI;
589         MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4, r_farclip.value);
590
591         glCullFace(GL_FRONT);
592
593         glMatrixMode(GL_MODELVIEW);
594         glLoadIdentity ();
595
596         glRotatef (-90,  1, 0, 0);          // put Z going up
597         glRotatef (90,  0, 0, 1);           // put Z going up
598         glRotatef (-r_refdef.viewangles[2],  1, 0, 0);
599         glRotatef (-r_refdef.viewangles[0],  0, 1, 0);
600         glRotatef (-r_refdef.viewangles[1],  0, 0, 1);
601         glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
602
603 //      glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
604
605         //
606         // set drawing parms
607         //
608 //      if (gl_cull.value)
609                 glEnable(GL_CULL_FACE);
610 //      else
611 //              glDisable(GL_CULL_FACE);
612
613         glEnable(GL_BLEND); // was Disable
614         glDisable(GL_ALPHA_TEST);
615         glAlphaFunc(GL_GREATER, 0.5);
616         glEnable(GL_DEPTH_TEST);
617         glDepthMask(1);
618         glShadeModel(GL_SMOOTH);
619 }
620
621 /*
622 =============
623 R_Clear
624 =============
625 */
626 static void R_Clear (void)
627 {
628         if (!r_render.value)
629                 return;
630 //      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
631         gldepthmin = 0;
632         gldepthmax = 1;
633         glDepthFunc (GL_LEQUAL);
634
635         glDepthRange (gldepthmin, gldepthmax);
636 }
637
638 static void GL_BlendView(void)
639 {
640         if (!r_render.value)
641                 return;
642
643         if (v_blend[3] < 0.01f)
644                 return;
645
646         glMatrixMode(GL_PROJECTION);
647     glLoadIdentity ();
648         glOrtho  (0, 256, 256, 0, -99999, 99999);
649         glMatrixMode(GL_MODELVIEW);
650     glLoadIdentity ();
651         glDisable (GL_DEPTH_TEST);
652         glDisable (GL_CULL_FACE);
653         glDisable(GL_TEXTURE_2D);
654         glEnable(GL_BLEND);
655         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
656         glBegin (GL_TRIANGLES);
657         if (lighthalf)
658                 glColor4f (v_blend[0] * 0.5f, v_blend[1] * 0.5f, v_blend[2] * 0.5f, v_blend[3]);
659         else
660                 glColor4fv (v_blend);
661         glVertex2f (-5000, -5000);
662         glVertex2f (10000, -5000);
663         glVertex2f (-5000, 10000);
664         glEnd ();
665
666         glEnable (GL_CULL_FACE);
667         glEnable (GL_DEPTH_TEST);
668         glDisable(GL_BLEND);
669         glEnable(GL_TEXTURE_2D);
670 }
671
672 /*
673 ================
674 R_RenderView
675
676 r_refdef must be set before the first call
677 ================
678 */
679 extern void UploadLightmaps(void);
680 extern void R_DrawSurfaces(void);
681 extern void R_DrawPortals(void);
682 char r_speeds2_string[1024];
683 int speedstringcount;
684
685 void timestring(int t, char *desc)
686 {
687         char tempbuf[256];
688         int length;
689         if (t < 1000000)
690                 sprintf(tempbuf, " %6ius %s", t, desc);
691         else
692                 sprintf(tempbuf, " %6ims %s", t / 1000, desc);
693         length = strlen(tempbuf);
694 //      while (length < 20)
695 //              tempbuf[length++] = ' ';
696 //      tempbuf[length] = 0;
697         if (speedstringcount + length > 80)
698         {
699                 strcat(r_speeds2_string, "\n");
700                 speedstringcount = 0;
701         }
702         // skip the space at the beginning if it's the first on the line
703         if (speedstringcount == 0)
704         {
705                 strcat(r_speeds2_string, tempbuf + 1);
706                 speedstringcount = length - 1;
707         }
708         else
709         {
710                 strcat(r_speeds2_string, tempbuf);
711                 speedstringcount += length;
712         }
713 }
714
715 #define TIMEREPORT(NAME) \
716         if (r_speeds2.value)\
717         {\
718                 temptime = currtime;\
719                 currtime = Sys_DoubleTime();\
720                 timestring((int) ((currtime - temptime) * 1000000.0), NAME);\
721         }
722
723 void R_RenderView (void)
724 {
725         double starttime, currtime, temptime;
726
727         if (!cl.worldmodel)
728                 Host_Error ("R_RenderView: NULL worldmodel");
729
730         if (r_speeds2.value)
731         {
732                 speedstringcount = 0;
733                 sprintf(r_speeds2_string, "org:'%c%6.2f %c%6.2f %c%6.2f' ang:'%c%3.0f %c%3.0f %c%3.0f' dir:'%c%2.3f %c%2.3f %c%2.3f'\n%6i walls %6i dlitwalls %7i modeltris %7i transpoly\nBSP: %6i faces %6i nodes %6i leafs\n%4i models %4i bmodels %4i sprites %5i particles %3i dlights\n",
734                         r_origin[0] < 0 ? '-' : ' ', fabs(r_origin[0]), r_origin[1] < 0 ? '-' : ' ', fabs(r_origin[1]), r_origin[2] < 0 ? '-' : ' ', fabs(r_origin[2]), r_refdef.viewangles[0] < 0 ? '-' : ' ', fabs(r_refdef.viewangles[0]), r_refdef.viewangles[1] < 0 ? '-' : ' ', fabs(r_refdef.viewangles[1]), r_refdef.viewangles[2] < 0 ? '-' : ' ', fabs(r_refdef.viewangles[2]), vpn[0] < 0 ? '-' : ' ', fabs(vpn[0]), vpn[1] < 0 ? '-' : ' ', fabs(vpn[1]), vpn[2] < 0 ? '-' : ' ', fabs(vpn[2]),
735                         c_brush_polys, c_light_polys, c_alias_polys, currenttranspoly,
736                         c_faces, c_nodes, c_leafs,
737                         c_models, c_bmodels, c_sprites, c_particles, c_dlights);
738
739                 starttime = currtime = Sys_DoubleTime();
740         }
741         else
742                 starttime = currtime = 0;
743
744         R_MoveParticles ();
745         TIMEREPORT("mparticles")
746         R_MoveExplosions();
747         TIMEREPORT("mexplosion")
748
749         FOG_framebegin();
750
751         R_Clear();
752         TIMEREPORT("clear     ")
753
754         // render normal view
755
756         R_SetupFrame ();
757         R_SetFrustum ();
758         R_SetupGL ();
759         R_Clip_StartFrame();
760
761         skypolyclear();
762         wallpolyclear();
763         transpolyclear();
764
765         TIMEREPORT("setup     ")
766
767         R_DrawWorld ();
768         TIMEREPORT("addworld  ")
769
770         R_AddModelEntities();
771         TIMEREPORT("addmodels ")
772
773         R_Clip_EndFrame();
774         TIMEREPORT("scanedge  ")
775
776         // now mark the lit surfaces
777         R_PushDlights ();
778         TIMEREPORT("marklights")
779
780         R_DrawModels1 ();
781
782         // yes this does add the world after the brush models when using the SER
783         R_DrawSurfaces ();
784         R_DrawPortals ();
785         TIMEREPORT("surfaces  ");
786
787         UploadLightmaps();
788         TIMEREPORT("uploadlmap")
789
790         skypolyrender();
791         TIMEREPORT("skypoly   ")
792
793         wallpolyrender1();
794         TIMEREPORT("wallpoly1 ")
795
796         GL_DrawDecals();
797         TIMEREPORT("ddecal    ")
798
799         wallpolyrender2();
800         TIMEREPORT("wallpoly2 ")
801
802         // don't let sound skip if going slow
803         if (!intimerefresh && !r_speeds2.value)
804                 S_ExtraUpdate ();
805
806         R_DrawViewModel ();
807         R_DrawModels2 ();
808         TIMEREPORT("models    ")
809
810         R_DrawParticles ();
811         TIMEREPORT("dparticles")
812
813         R_DrawExplosions();
814         TIMEREPORT("dexplosion")
815
816         transpolyrender();
817         TIMEREPORT("transpoly ")
818
819         FOG_frameend();
820
821         GL_BlendView();
822         TIMEREPORT("blend     ")
823
824         if (r_speeds2.value)
825                 timestring((int) ((Sys_DoubleTime() - starttime) * 1000000.0), "total    ");
826 }