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