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