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