a big change with a little description...
[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 gl_main_start()
215 {
216 }
217
218 void gl_main_shutdown()
219 {
220 }
221
222 void gl_main_newmap()
223 {
224 }
225
226 void GL_Main_Init()
227 {
228         FOG_registercvars();
229         Cvar_RegisterVariable (&r_drawentities);
230         Cvar_RegisterVariable (&r_drawviewmodel);
231         Cvar_RegisterVariable (&r_speeds);
232         Cvar_RegisterVariable (&r_speeds2);
233         Cvar_RegisterVariable (&contrast);
234         Cvar_RegisterVariable (&brightness);
235         Cvar_RegisterVariable (&gl_lightmode);
236 //      Cvar_RegisterVariable (&r_dynamicwater);
237 //      Cvar_RegisterVariable (&r_dynamicbothsides);
238         Cvar_RegisterVariable (&r_fullbrights);
239         Cvar_RegisterVariable (&r_wateralpha);
240         Cvar_RegisterVariable (&r_dynamic);
241         Cvar_RegisterVariable (&r_novis);
242         Cvar_RegisterVariable (&r_waterripple); // LordHavoc: added waterripple
243         if (nehahra)
244                 Cvar_SetValue("r_fullbrights", 0);
245 //      if (gl_vendor && strstr(gl_vendor, "3Dfx"))
246 //              gl_lightmode.value = 0;
247         Cvar_RegisterVariable (&r_fullbright);
248         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
249 }
250
251 extern void GL_Draw_Init();
252 extern void GL_Main_Init();
253 extern void GL_Models_Init();
254 extern void GL_Poly_Init();
255 extern void GL_Surf_Init();
256 extern void GL_Screen_Init();
257 extern void GL_Misc_Init();
258 extern void R_Crosshairs_Init();
259 extern void R_Light_Init();
260 extern void R_Particles_Init();
261 extern void R_Explosion_Init();
262 extern void CL_Effects_Init();
263
264 void Render_Init()
265 {
266         R_Modules_Shutdown();
267         GL_Draw_Init();
268         GL_Main_Init();
269         GL_Models_Init();
270         GL_Poly_Init();
271         GL_Surf_Init();
272         GL_Screen_Init();
273         GL_Misc_Init();
274         R_Crosshairs_Init();
275         R_Light_Init();
276         R_Particles_Init();
277         R_Explosion_Init();
278         CL_Effects_Init();
279         R_Decals_Init();
280         R_Modules_Start();
281 }
282
283 /*
284 ===============
285 GL_Init
286 ===============
287 */
288 extern char *ENGINE_EXTENSIONS;
289 void GL_Init (void)
290 {
291         gl_vendor = glGetString (GL_VENDOR);
292         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
293         gl_renderer = glGetString (GL_RENDERER);
294         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
295
296         gl_version = glGetString (GL_VERSION);
297         Con_Printf ("GL_VERSION: %s\n", gl_version);
298         gl_extensions = glGetString (GL_EXTENSIONS);
299         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
300
301 //      Con_Printf ("%s %s\n", gl_renderer, gl_version);
302
303         VID_CheckMultitexture();
304         VID_CheckCVA();
305
306         // LordHavoc: report supported extensions
307         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
308
309         glCullFace(GL_FRONT);
310         glEnable(GL_TEXTURE_2D);
311 //      glDisable(GL_ALPHA_TEST);
312         glAlphaFunc(GL_GREATER, 0.5);
313
314 //      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
315
316         Palette_Init();
317 }
318
319
320 /*
321 void R_RotateForEntity (entity_t *e)
322 {
323         glTranslatef (e->origin[0],  e->origin[1],  e->origin[2]);
324
325         glRotatef (e->angles[1],  0, 0, 1);
326         glRotatef (-e->angles[0],  0, 1, 0);
327         glRotatef (e->angles[2],  1, 0, 0);
328
329         glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
330 }
331 */
332
333 // LordHavoc: shading stuff
334 vec3_t  shadevector;
335 vec3_t  shadecolor;
336
337 float   modelalpha;
338
339 //==================================================================================
340
341 void R_DrawBrushModel (entity_t *e);
342 void R_DrawSpriteModel (entity_t *e, frameblend_t *blend);
343
344 void R_LerpUpdate(entity_t *ent)
345 {
346         int frame;
347         frame = ent->render.frame;
348         if (ent->render.model && ent->render.frame >= ent->render.model->numframes)
349         {
350                 Con_Printf("R_LerpUpdate: no such frame%6i in \"%s\"\n", ent->render.frame, ent->render.model->name);
351                 frame = 0;
352         }
353
354         if (ent->render.lerp_model != ent->render.model)
355         {
356                 // reset all interpolation information
357                 ent->render.lerp_model = ent->render.model;
358                 ent->render.frame1 = ent->render.frame2 = frame;
359                 ent->render.frame1start = ent->render.frame2start = cl.time;
360                 ent->render.framelerp = 1;
361                 ent->render.lerp_starttime = 0;
362         }
363         else if (ent->render.frame2 != frame)
364         {
365                 // transition to new frame
366                 ent->render.frame1 = ent->render.frame2;
367                 ent->render.frame1start = ent->render.frame2start;
368                 ent->render.frame2 = frame;
369                 ent->render.frame2start = cl.time;
370                 ent->render.framelerp = 0;
371                 ent->render.lerp_starttime = cl.time;
372         }
373         else
374         {
375                 // lerp_starttime < 0 is used to prevent changing of framelerp
376                 if (ent->render.lerp_starttime >= 0)
377                 {
378                         // update transition
379                         ent->render.framelerp = (cl.time - ent->render.lerp_starttime) * 10;
380                         ent->render.framelerp = bound(0, ent->render.framelerp, 1);
381                 }
382         }
383 }
384
385
386 void R_PrepareEntities (void)
387 {
388         int i;
389         entity_t *ent;
390         vec3_t v;
391         // this updates entities that are supposed to be view relative
392         for (i = 0;i < cl_numvisedicts;i++)
393         {
394                 ent = cl_visedicts[i];
395
396                 if (ent->render.flags & RENDER_VIEWMODEL)
397                 {
398                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
399                         ent->render.flags -= RENDER_VIEWMODEL;
400                         // transform origin
401                         VectorCopy(ent->render.origin, v);
402                         ent->render.origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_refdef.vieworg[0];
403                         ent->render.origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_refdef.vieworg[1];
404                         ent->render.origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_refdef.vieworg[2];
405                         // adjust angles
406                         VectorAdd(ent->render.angles, r_refdef.viewangles, ent->render.angles);
407                 }
408         }
409 }
410
411 /*
412 =============
413 R_DrawEntitiesOnList
414 =============
415 */
416 // LordHavoc: split so bmodels are rendered before any other objects
417 void R_DrawEntitiesOnList1 (void)
418 {
419         int             i;
420
421         if (!r_drawentities.value)
422                 return;
423
424         for (i = 0;i < cl_numvisedicts;i++)
425         {
426                 if (cl_visedicts[i]->render.model->type != mod_brush)
427                         continue;
428                 currententity = cl_visedicts[i];
429                 modelalpha = currententity->render.alpha;
430
431                 R_DrawBrushModel (currententity);
432         }
433 }
434
435 void R_DrawEntitiesOnList2 (void)
436 {
437         int             i;
438         frameblend_t blend[4];
439
440         if (!r_drawentities.value)
441                 return;
442
443         for (i = 0;i < cl_numvisedicts;i++)
444         {
445                 currententity = cl_visedicts[i];
446                 modelalpha = currententity->render.alpha;
447
448                 switch (currententity->render.model->type)
449                 {
450                 case mod_alias:
451                         R_LerpUpdate(currententity);
452                         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
453                         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);
454                         break;
455
456                 case mod_sprite:
457                         R_LerpUpdate(currententity);
458                         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
459                         R_DrawSpriteModel (currententity, blend);
460                         break;
461
462                 default:
463                         break;
464                 }
465         }
466 }
467
468 /*
469 =============
470 R_DrawViewModel
471 =============
472 */
473 void R_DrawViewModel (void)
474 {
475         frameblend_t blend[4];
476
477         if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
478                 return;
479
480         currententity = &cl.viewent;
481         currententity->render.alpha = modelalpha = cl_entities[cl.viewentity].render.alpha; // LordHavoc: if the player is transparent, so is the gun
482         currententity->render.effects = cl_entities[cl.viewentity].render.effects;
483         currententity->render.scale = 1;
484         VectorCopy(cl_entities[cl.viewentity].render.colormod, currententity->render.colormod);
485
486         R_LerpUpdate(currententity);
487         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
488
489         // hack the depth range to prevent view model from poking into walls
490         glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
491         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);
492         glDepthRange (gldepthmin, gldepthmax);
493 }
494
495 void R_DrawBrushModel (entity_t *e);
496
497 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
498
499 void R_SetFrustum (void)
500 {
501         int             i;
502
503         if (r_refdef.fov_x == 90) 
504         {
505                 // front side is visible
506
507                 VectorAdd (vpn, vright, frustum[0].normal);
508                 VectorSubtract (vpn, vright, frustum[1].normal);
509
510                 VectorAdd (vpn, vup, frustum[2].normal);
511                 VectorSubtract (vpn, vup, frustum[3].normal);
512         }
513         else
514         {
515                 // rotate VPN right by FOV_X/2 degrees
516                 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
517                 // rotate VPN left by FOV_X/2 degrees
518                 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
519                 // rotate VPN up by FOV_X/2 degrees
520                 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
521                 // rotate VPN down by FOV_X/2 degrees
522                 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
523         }
524
525         for (i=0 ; i<4 ; i++)
526         {
527                 frustum[i].type = PLANE_ANYZ;
528                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
529 //              frustum[i].signbits = SignbitsForPlane (&frustum[i]);
530                 BoxOnPlaneSideClassify(&frustum[i]);
531         }
532 }
533
534 void R_AnimateLight (void);
535 void V_CalcBlend (void);
536
537 /*
538 ===============
539 R_SetupFrame
540 ===============
541 */
542 void R_SetupFrame (void)
543 {
544 // don't allow cheats in multiplayer
545         if (cl.maxclients > 1)
546         {
547                 Cvar_Set ("r_fullbright", "0");
548                 Cvar_Set ("r_ambient", "0");
549         }
550
551         R_AnimateLight ();
552
553         r_framecount++;
554
555 // build the transformation matrix for the given view angles
556         VectorCopy (r_refdef.vieworg, r_origin);
557
558         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
559
560 // current viewleaf
561         r_oldviewleaf = r_viewleaf;
562         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
563
564         V_SetContentsColor (r_viewleaf->contents);
565         V_CalcBlend ();
566
567         r_cache_thrash = false;
568
569         c_brush_polys = 0;
570         c_alias_polys = 0;
571         c_light_polys = 0;
572         c_faces = 0;
573         c_nodes = 0;
574         c_leafs = 0;
575         c_models = 0;
576         c_bmodels = 0;
577         c_sprites = 0;
578         c_particles = 0;
579 //      c_dlights = 0;
580 }
581
582
583 void MYgluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
584 {
585    GLdouble xmin, xmax, ymin, ymax;
586
587    ymax = zNear * tan( fovy * M_PI / 360.0 );
588    ymin = -ymax;
589
590    xmin = ymin * aspect;
591    xmax = ymax * aspect;
592
593    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
594 }
595
596
597 extern char skyname[];
598
599 /*
600 =============
601 R_SetupGL
602 =============
603 */
604 void R_SetupGL (void)
605 {
606         float   screenaspect;
607         extern  int glwidth, glheight;
608         int             x, x2, y2, y, w, h;
609
610         if (!r_render.value)
611                 return;
612         //
613         // set up viewpoint
614         //
615         glMatrixMode(GL_PROJECTION);
616     glLoadIdentity ();
617         x = r_refdef.vrect.x * glwidth/vid.width;
618         x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
619         y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
620         y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
621
622         // fudge around because of frac screen scale
623         if (x > 0)
624                 x--;
625         if (x2 < glwidth)
626                 x2++;
627         if (y2 < 0)
628                 y2--;
629         if (y < glheight)
630                 y++;
631
632         w = x2 - x;
633         h = y - y2;
634
635         if (envmap)
636         {
637                 x = y2 = 0;
638                 w = h = 256;
639         }
640
641         glViewport (glx + x, gly + y2, w, h);
642     screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
643 //      yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
644 //      if (skyname[0]) // skybox enabled?
645 //              MYgluPerspective (r_refdef.fov_y,  screenaspect,  4,  r_skyboxsize.value*1.732050807569 + 256); // this is size*sqrt(3) + 256
646 //      else
647                 MYgluPerspective (r_refdef.fov_y,  screenaspect,  4,  r_farclip.value);
648
649         glCullFace(GL_FRONT);
650
651         glMatrixMode(GL_MODELVIEW);
652     glLoadIdentity ();
653
654     glRotatef (-90,  1, 0, 0);      // put Z going up
655     glRotatef (90,  0, 0, 1);       // put Z going up
656     glRotatef (-r_refdef.viewangles[2],  1, 0, 0);
657     glRotatef (-r_refdef.viewangles[0],  0, 1, 0);
658     glRotatef (-r_refdef.viewangles[1],  0, 0, 1);
659     glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
660
661         glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
662
663         //
664         // set drawing parms
665         //
666 //      if (gl_cull.value)
667                 glEnable(GL_CULL_FACE);
668 //      else
669 //              glDisable(GL_CULL_FACE);
670
671         glEnable(GL_BLEND); // was Disable
672         glDisable(GL_ALPHA_TEST);
673         glAlphaFunc(GL_GREATER, 0.5);
674         glEnable(GL_DEPTH_TEST);
675         glDepthMask(1);
676         glShadeModel(GL_SMOOTH);
677 }
678
679 /*
680 =============
681 R_Clear
682 =============
683 */
684 void R_Clear (void)
685 {
686         if (!r_render.value)
687                 return;
688 //      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
689         gldepthmin = 0;
690         gldepthmax = 1;
691         glDepthFunc (GL_LEQUAL);
692
693         glDepthRange (gldepthmin, gldepthmax);
694 }
695
696 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
697 void GL_Brighten()
698 {
699         if (!r_render.value)
700                 return;
701         glMatrixMode(GL_PROJECTION);
702     glLoadIdentity ();
703         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
704         glMatrixMode(GL_MODELVIEW);
705     glLoadIdentity ();
706         glDisable (GL_DEPTH_TEST);
707         glDisable (GL_CULL_FACE);
708         glDisable(GL_TEXTURE_2D);
709         glEnable(GL_BLEND);
710         glBlendFunc (GL_DST_COLOR, GL_ONE);
711         glBegin (GL_TRIANGLES);
712         glColor3f (1, 1, 1);
713         glVertex2f (-5000, -5000);
714         glVertex2f (10000, -5000);
715         glVertex2f (-5000, 10000);
716         glEnd ();
717         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
718         glDisable(GL_BLEND);
719         glEnable(GL_TEXTURE_2D);
720         glEnable (GL_DEPTH_TEST);
721         glEnable (GL_CULL_FACE);
722 }
723
724 extern cvar_t contrast;
725 extern cvar_t brightness;
726 extern cvar_t gl_lightmode;
727
728 void GL_BlendView()
729 {
730         if (!r_render.value)
731                 return;
732         glMatrixMode(GL_PROJECTION);
733     glLoadIdentity ();
734         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
735         glMatrixMode(GL_MODELVIEW);
736     glLoadIdentity ();
737         glDisable (GL_DEPTH_TEST);
738         glDisable (GL_CULL_FACE);
739         glDisable(GL_TEXTURE_2D);
740         glEnable(GL_BLEND);
741         if (lighthalf)
742         {
743                 glBlendFunc (GL_DST_COLOR, GL_ONE);
744                 glBegin (GL_TRIANGLES);
745                 glColor3f (1, 1, 1);
746                 glVertex2f (-5000, -5000);
747                 glVertex2f (10000, -5000);
748                 glVertex2f (-5000, 10000);
749                 glEnd ();
750         }
751         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
752         contrast.value = bound(0.2, contrast.value, 1.0);
753         if (/*gl_polyblend.value && */v_blend[3])
754         {
755                 glBegin (GL_TRIANGLES);
756                 glColor4fv (v_blend);
757                 glVertex2f (-5000, -5000);
758                 glVertex2f (10000, -5000);
759                 glVertex2f (-5000, 10000);
760                 glEnd ();
761         }
762
763         glEnable (GL_CULL_FACE);
764         glEnable (GL_DEPTH_TEST);
765         glDisable(GL_BLEND);
766         glEnable(GL_TEXTURE_2D);
767 }
768
769 #define TIMEREPORT(VAR) \
770         if (r_speeds2.value)\
771         {\
772                 temptime = currtime;\
773                 currtime = Sys_FloatTime();\
774                 VAR = (int) ((currtime - temptime) * 1000000.0);\
775         }\
776         else\
777                 VAR = 0;
778
779 /*
780 ================
781 R_RenderView
782
783 r_refdef must be set before the first call
784 ================
785 */
786 extern qboolean intimerefresh;
787 extern qboolean skyisvisible;
788 extern void R_Sky();
789 extern void UploadLightmaps();
790 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];
791 void R_RenderView (void)
792 {
793         double starttime, currtime, temptime;
794         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;
795 //      if (r_norefresh.value)
796 //              return;
797
798         if (!r_worldentity.render.model || !cl.worldmodel)
799                 Host_Error ("R_RenderView: NULL worldmodel");
800
801         lighthalf = gl_lightmode.value;
802
803         FOG_framebegin();
804
805         if (r_speeds2.value)
806         {
807                 starttime = currtime = Sys_FloatTime();
808         }
809         else
810                 starttime = currtime = 0;
811         R_Clear();
812         skyisvisible = false;
813         TIMEREPORT(time_clear)
814
815         // render normal view
816
817         R_SetupFrame ();
818         R_SetFrustum ();
819         R_SetupGL ();
820
821         R_PrepareEntities();
822
823         skypolyclear();
824         wallpolyclear();
825         transpolyclear();
826
827         TIMEREPORT(time_setup)
828
829         R_MarkLeaves ();        // done here so we know if we're in water
830         R_DrawWorld ();         // adds static entities to the list
831         TIMEREPORT(time_world)
832         R_DrawEntitiesOnList1 (); // BSP models
833         TIMEREPORT(time_bmodels)
834
835         UploadLightmaps();
836         TIMEREPORT(time_upload)
837
838         skypolyrender(); // fogged sky polys, affects depth
839
840         if (skyname[0] && skyisvisible && !fogenabled)
841                 R_Sky(); // does not affect depth, draws over the sky polys
842         TIMEREPORT(time_sky)
843
844         wallpolyrender();
845         TIMEREPORT(time_wall)
846
847         if (!intimerefresh && !r_speeds2.value)
848                 S_ExtraUpdate ();       // don't let sound get messed up if going slow
849
850         R_DrawEntitiesOnList2 (); // other models
851 //      R_RenderDlights ();
852         R_DrawViewModel ();
853         TIMEREPORT(time_models)
854         R_MoveParticles ();
855         TIMEREPORT(time_moveparticles)
856         R_DrawParticles ();
857         TIMEREPORT(time_drawparticles)
858         R_MoveExplosions();
859         TIMEREPORT(time_moveexplosions)
860         R_DrawExplosions();
861         TIMEREPORT(time_drawexplosions)
862         R_DrawDecals();
863         TIMEREPORT(time_drawdecals)
864
865         transpolyrender();
866         TIMEREPORT(time_transpoly)
867
868         FOG_frameend();
869
870         GL_BlendView();
871         TIMEREPORT(time_blend)
872         if (r_speeds2.value)
873         {
874                 time_total = (int) ((Sys_FloatTime() - starttime) * 1000000.0);
875                 sprintf(r_speeds2_string1, "%6i walls %6i dlitwalls %7i modeltris %7i transpoly\n", c_brush_polys, c_light_polys, c_alias_polys, currenttranspoly);
876                 sprintf(r_speeds2_string2, "BSP: %6i faces %6i nodes %6i leafs\n", c_faces, c_nodes, c_leafs);
877                 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);
878                 sprintf(r_speeds2_string4, "%6ius clear  %6ius setup  %6ius world  %6ius bmodel %6ius upload", time_clear, time_setup, time_world, time_bmodels, time_upload);
879                 sprintf(r_speeds2_string5, "%6ius sky    %6ius wall   %6ius models %6ius mpart  %6ius dpart ", time_sky, time_wall, time_models, time_moveparticles, time_drawparticles);
880                 sprintf(r_speeds2_string6, "%6ius mexplo %6ius dexplo %6ius decals %6ius trans  %6ius blend ", time_moveexplosions, time_drawexplosions, time_drawdecals, time_transpoly, time_blend);
881                 sprintf(r_speeds2_string7, "%6ius permdl %6ius total ", time_models / max(c_models, 1), time_total);
882         }
883 }