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