]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rmain.c
fixed gl_combine so it is enabled by default (if present)
[divverent/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 qboolean        r_cache_thrash;         // compatability
25
26 vec3_t          modelorg, r_entorigin;
27 entity_t        *currententity;
28
29 int                     r_framecount;           // used for dlight push checking
30
31 mplane_t        frustum[4];
32
33 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;
34
35 qboolean        envmap;                         // true during envmap command capture
36
37 // LordHavoc: moved all code related to particles into r_part.c
38 //int                   particletexture;        // little dot for particles
39 //int                   playertextures;         // up to 16 color translated skins
40
41 //
42 // view origin
43 //
44 vec3_t  vup;
45 vec3_t  vpn;
46 vec3_t  vright;
47 vec3_t  r_origin;
48
49 float   r_world_matrix[16];
50 float   r_base_world_matrix[16];
51
52 //
53 // screen size info
54 //
55 refdef_t        r_refdef;
56
57 mleaf_t         *r_viewleaf, *r_oldviewleaf;
58
59 unsigned short  d_lightstylevalue[256]; // 8.8 fraction of base light value
60
61 void R_MarkLeaves (void);
62
63 //cvar_t        r_norefresh = {0, "r_norefresh","0"};
64 cvar_t  r_drawentities = {0, "r_drawentities","1"};
65 cvar_t  r_drawviewmodel = {0, "r_drawviewmodel","1"};
66 cvar_t  r_speeds = {0, "r_speeds","0"};
67 cvar_t  r_speeds2 = {0, "r_speeds2","0"};
68 cvar_t  r_fullbright = {0, "r_fullbright","0"};
69 //cvar_t        r_lightmap = {0, "r_lightmap","0"};
70 cvar_t  r_wateralpha = {CVAR_SAVE, "r_wateralpha","1"};
71 cvar_t  r_dynamic = {CVAR_SAVE, "r_dynamic","1"};
72 cvar_t  r_waterripple = {CVAR_SAVE, "r_waterripple","0"};
73 cvar_t  r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1"};
74
75 cvar_t  gl_lightmode = {CVAR_SAVE, "gl_lightmode", "1"}; // LordHavoc: overbright lighting
76 //cvar_t        r_dynamicbothsides = {CVAR_SAVE, "r_dynamicbothsides", "1"}; // LordHavoc: can disable dynamic lighting of backfaces, but quake maps are weird so it doesn't always work right...
77 cvar_t  r_farclip = {0, "r_farclip", "6144"}; // FIXME: make this go away (calculate based on farthest visible object/polygon)
78
79 cvar_t  gl_fogenable = {0, "gl_fogenable", "0"};
80 cvar_t  gl_fogdensity = {0, "gl_fogdensity", "0.25"};
81 cvar_t  gl_fogred = {0, "gl_fogred","0.3"};
82 cvar_t  gl_foggreen = {0, "gl_foggreen","0.3"};
83 cvar_t  gl_fogblue = {0, "gl_fogblue","0.3"};
84 cvar_t  gl_fogstart = {0, "gl_fogstart", "0"};
85 cvar_t  gl_fogend = {0, "gl_fogend","0"};
86 cvar_t  glfog = {0, "glfog", "0"};
87
88 cvar_t  r_ser = {CVAR_SAVE, "r_ser", "1"};
89
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         node = cl.worldmodel->nodes;
98 loc0:
99         if (node->contents < 0)
100         {
101                 if (((mleaf_t *)node)->visframe == r_framecount)
102                 {
103                         if (R_CullBox(mins, maxs))
104                                 return true;
105                         return false;
106                 }
107                 if (!stack)
108                         return true;
109                 node = nodestack[--stack];
110                 goto loc0;
111         }
112
113         sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
114
115 // recurse down the contacted sides
116         if (sides & 1)
117         {
118                 if (sides & 2) // 3
119                 {
120                         // put second child on the stack for later examination
121                         nodestack[stack++] = node->children[1];
122                         node = node->children[0];
123                         goto loc0;
124                 }
125                 else // 1
126                 {
127                         node = node->children[0];
128                         goto loc0;
129                 }
130         }
131         // 2
132         node = node->children[1];
133         goto loc0;
134 }
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(void)
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(void)
221 {
222         if (glfog.value)
223                 glDisable(GL_FOG);
224 }
225
226 void FOG_clear(void)
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(void)
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(void)
255 {
256 }
257
258 void gl_main_shutdown(void)
259 {
260 }
261
262 void gl_main_newmap(void)
263 {
264 }
265
266 void GL_Main_Init(void)
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 (&gl_lightmode);
274 //      Cvar_RegisterVariable (&r_dynamicwater);
275 //      Cvar_RegisterVariable (&r_dynamicbothsides);
276         Cvar_RegisterVariable (&r_fullbrights);
277         Cvar_RegisterVariable (&r_wateralpha);
278         Cvar_RegisterVariable (&r_dynamic);
279         Cvar_RegisterVariable (&r_waterripple);
280         Cvar_RegisterVariable (&r_farclip);
281         if (nehahra)
282                 Cvar_SetValue("r_fullbrights", 0);
283 //      if (gl_vendor && strstr(gl_vendor, "3Dfx"))
284 //              gl_lightmode.value = 0;
285         Cvar_RegisterVariable (&r_fullbright);
286         Cvar_RegisterVariable (&r_ser);
287         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
288 }
289
290 extern void GL_Draw_Init(void);
291 extern void GL_Main_Init(void);
292 extern void GL_Models_Init(void);
293 extern void GL_Poly_Init(void);
294 extern void GL_Surf_Init(void);
295 extern void GL_Screen_Init(void);
296 extern void GL_Misc_Init(void);
297 extern void R_Crosshairs_Init(void);
298 extern void R_Light_Init(void);
299 extern void R_Particles_Init(void);
300 extern void R_Explosion_Init(void);
301 extern void CL_Effects_Init(void);
302 extern void R_Clip_Init(void);
303 extern void ui_init(void);
304
305 void Render_Init(void)
306 {
307         R_Modules_Shutdown();
308         R_Clip_Init();
309         GL_Draw_Init();
310         GL_Main_Init();
311         GL_Models_Init();
312         GL_Poly_Init();
313         GL_Surf_Init();
314         GL_Screen_Init();
315         GL_Misc_Init();
316         R_Crosshairs_Init();
317         R_Light_Init();
318         R_Particles_Init();
319         R_Explosion_Init();
320         CL_Effects_Init();
321         R_Decals_Init();
322         ui_init();
323         R_Modules_Start();
324 }
325
326 /*
327 ===============
328 GL_Init
329 ===============
330 */
331 extern char *ENGINE_EXTENSIONS;
332 void GL_Init (void)
333 {
334         gl_vendor = glGetString (GL_VENDOR);
335         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
336         gl_renderer = glGetString (GL_RENDERER);
337         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
338
339         gl_version = glGetString (GL_VERSION);
340         Con_Printf ("GL_VERSION: %s\n", gl_version);
341         gl_extensions = glGetString (GL_EXTENSIONS);
342         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
343
344 //      Con_Printf ("%s %s\n", gl_renderer, gl_version);
345
346 //      VID_CheckMultitexture();
347 //      VID_CheckCVA();
348 //      VID_CheckCombine();
349         VID_CheckExtensions();
350
351         // LordHavoc: report supported extensions
352         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
353
354         glCullFace(GL_FRONT);
355         glEnable(GL_TEXTURE_2D);
356 //      glDisable(GL_ALPHA_TEST);
357         glAlphaFunc(GL_GREATER, 0.5);
358
359 //      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
360 }
361
362
363 /*
364 void R_RotateForEntity (entity_t *e)
365 {
366         glTranslatef (e->origin[0],  e->origin[1],  e->origin[2]);
367
368         glRotatef (e->angles[1],  0, 0, 1);
369         glRotatef (-e->angles[0],  0, 1, 0);
370         glRotatef (e->angles[2],  1, 0, 0);
371
372         glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
373 }
374 */
375
376 // LordHavoc: shading stuff
377 vec3_t  shadevector;
378 vec3_t  shadecolor;
379
380 float   modelalpha;
381
382 //==================================================================================
383
384 void R_LerpUpdate(entity_t *ent)
385 {
386         int frame;
387         frame = ent->render.frame;
388         if (ent->render.model && ent->render.frame >= ent->render.model->numframes)
389         {
390                 Con_Printf("R_LerpUpdate: no such frame%6i in \"%s\"\n", ent->render.frame, ent->render.model->name);
391                 frame = 0;
392         }
393
394         if (ent->render.lerp_model != ent->render.model)
395         {
396                 // reset all interpolation information
397                 ent->render.lerp_model = ent->render.model;
398                 ent->render.frame1 = ent->render.frame2 = frame;
399                 ent->render.frame1start = ent->render.frame2start = cl.time;
400                 ent->render.framelerp = 1;
401                 ent->render.lerp_starttime = 0;
402         }
403         else if (ent->render.frame2 != frame)
404         {
405                 // transition to new frame
406                 ent->render.frame1 = ent->render.frame2;
407                 ent->render.frame1start = ent->render.frame2start;
408                 ent->render.frame2 = frame;
409                 ent->render.frame2start = cl.time;
410                 ent->render.framelerp = 0;
411                 ent->render.lerp_starttime = cl.time;
412         }
413         else
414         {
415                 // lerp_starttime < 0 is used to prevent changing of framelerp
416                 if (ent->render.lerp_starttime >= 0)
417                 {
418                         // update transition
419                         ent->render.framelerp = (cl.time - ent->render.lerp_starttime) * 10;
420                         ent->render.framelerp = bound(0, ent->render.framelerp, 1);
421                 }
422         }
423 }
424
425
426 void R_PrepareEntities (void)
427 {
428         int i;
429         entity_t *ent;
430         vec3_t v;
431         // this updates entities that are supposed to be view relative
432         for (i = 0;i < cl_numvisedicts;i++)
433         {
434                 ent = cl_visedicts[i];
435
436                 if (ent->render.flags & RENDER_VIEWMODEL)
437                 {
438                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
439                         ent->render.flags -= RENDER_VIEWMODEL;
440                         // transform origin
441                         VectorCopy(ent->render.origin, v);
442                         ent->render.origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
443                         ent->render.origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
444                         ent->render.origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
445                         // adjust angles
446                         VectorAdd(ent->render.angles, r_refdef.viewangles, ent->render.angles);
447                 }
448         }
449 }
450
451 void R_Entity_Callback(void *data, void *junk)
452 {
453         ((entity_t *)data)->render.visframe = r_framecount;
454 }
455
456 void R_AddModelEntities (void)
457 {
458         int             i;
459         vec3_t  mins, maxs;
460         frameblend_t blend[4];
461
462         if (!r_drawentities.value)
463                 return;
464
465         for (i = 0;i < cl_numvisedicts;i++)
466         {
467                 currententity = cl_visedicts[i];
468                 if (currententity->render.model->type == mod_brush)
469                 {
470                         modelalpha = currententity->render.alpha;
471                         R_DrawBrushModel (currententity);
472                 }
473                 else if (currententity->render.model->type == mod_alias)
474                 {
475                         VectorAdd(currententity->render.origin, currententity->render.model->mins, mins);
476                         VectorAdd(currententity->render.origin, currententity->render.model->maxs, maxs);
477                         if (r_ser.value)
478                                 R_Clip_AddBox(mins, maxs, R_Entity_Callback, currententity, NULL);
479                         else if (R_NotCulledBox(mins, maxs))
480                                 currententity->render.visframe = r_framecount;
481                 }
482                 else if (currententity->render.model->type == mod_sprite)
483                 {
484                         R_LerpUpdate(currententity);
485                         if (r_ser.value)
486                         {
487                                 R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
488                                 R_ClipSprite(currententity, blend);
489                         }
490                         else
491                         {
492                                 VectorAdd(currententity->render.origin, currententity->render.model->mins, mins);
493                                 VectorAdd(currententity->render.origin, currententity->render.model->maxs, maxs);
494                                 if (R_NotCulledBox(mins, maxs))
495                                         currententity->render.visframe = r_framecount;
496                         }
497                 }
498         }
499 }
500
501 /*
502 =============
503 R_DrawEntitiesOnList
504 =============
505 */
506 /*
507 void R_DrawEntitiesOnList1 (void)
508 {
509         int             i;
510
511         if (!r_drawentities.value)
512                 return;
513
514         for (i = 0;i < cl_numvisedicts;i++)
515         {
516                 if (cl_visedicts[i]->render.visframe != r_framecount)
517                         continue;
518                 if (cl_visedicts[i]->render.model->type != mod_brush)
519                         continue;
520                 currententity = cl_visedicts[i];
521                 modelalpha = currententity->render.alpha;
522
523                 R_DrawBrushModel (currententity);
524         }
525 }
526 */
527
528 void R_DrawModels (void)
529 {
530         int             i;
531         frameblend_t blend[4];
532 //      vec3_t  mins, maxs;
533
534         if (!r_drawentities.value)
535                 return;
536
537         for (i = 0;i < cl_numvisedicts;i++)
538         {
539                 if (cl_visedicts[i]->render.visframe != r_framecount)
540                         continue;
541                 currententity = cl_visedicts[i];
542                 if (currententity->render.model->type != mod_alias && currententity->render.model->type != mod_sprite)
543                         continue;
544
545                 modelalpha = currententity->render.alpha;
546
547                 if (currententity->render.model->type == mod_alias)
548                 {
549                         // only lerp models here because sprites were already lerped for their clip polygon
550                         R_LerpUpdate(currententity);
551                         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
552                         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);
553                 }
554                 else //if (currententity->render.model->type == mod_sprite)
555                 {
556                         // build blend array
557                         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
558                         R_DrawSpriteModel (currententity, blend);
559                 }
560
561                 /*
562                 VectorAdd(cl_visedicts[i]->render.origin, cl_visedicts[i]->render.model->mins, mins);
563                 VectorAdd(cl_visedicts[i]->render.origin, cl_visedicts[i]->render.model->maxs, maxs);
564
565                 switch (cl_visedicts[i]->render.model->type)
566                 {
567                 case mod_alias:
568                         R_Clip_AddBox(mins, maxs, R_DrawModelCallback, cl_visedicts[i], NULL);
569                         break;
570                 case mod_sprite:
571                         R_Clip_AddBox(mins, maxs, R_DrawSpriteCallback, cl_visedicts[i], NULL);
572                         break;
573                 }
574                 */
575         }
576 }
577
578 /*
579 =============
580 R_DrawViewModel
581 =============
582 */
583 void R_DrawViewModel (void)
584 {
585         frameblend_t blend[4];
586
587         if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
588                 return;
589
590         currententity = &cl.viewent;
591         currententity->render.alpha = modelalpha = cl_entities[cl.viewentity].render.alpha; // LordHavoc: if the player is transparent, so is the gun
592         currententity->render.effects = cl_entities[cl.viewentity].render.effects;
593         currententity->render.scale = 1;
594         VectorCopy(cl_entities[cl.viewentity].render.colormod, currententity->render.colormod);
595
596         R_LerpUpdate(currententity);
597         R_LerpAnimation(currententity->render.model, currententity->render.frame1, currententity->render.frame2, currententity->render.frame1start, currententity->render.frame2start, currententity->render.framelerp, blend);
598
599         // hack the depth range to prevent view model from poking into walls
600         glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
601         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);
602         glDepthRange (gldepthmin, gldepthmax);
603 }
604
605 void R_DrawBrushModel (entity_t *e);
606
607 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
608
609 void R_SetFrustum (void)
610 {
611         int             i;
612
613         // LordHavoc: note to all quake engine coders, this code was making the
614         // view frustum taller than it should have been (it assumed the view is
615         // square; it is not square), so I disabled it
616         /*
617         if (r_refdef.fov_x == 90)
618         {
619                 // front side is visible
620
621                 VectorAdd (vpn, vright, frustum[0].normal);
622                 VectorSubtract (vpn, vright, frustum[1].normal);
623
624                 VectorAdd (vpn, vup, frustum[2].normal);
625                 VectorSubtract (vpn, vup, frustum[3].normal);
626         }
627         else
628         {
629         */
630                 // rotate VPN right by FOV_X/2 degrees
631                 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
632                 // rotate VPN left by FOV_X/2 degrees
633                 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
634                 // rotate VPN up by FOV_X/2 degrees
635                 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
636                 // rotate VPN down by FOV_X/2 degrees
637                 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
638         //}
639
640         for (i=0 ; i<4 ; i++)
641         {
642                 frustum[i].type = PLANE_ANYZ;
643                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
644 //              frustum[i].signbits = SignbitsForPlane (&frustum[i]);
645                 PlaneClassify(&frustum[i]);
646         }
647 }
648
649 void R_AnimateLight (void);
650 void V_CalcBlend (void);
651
652 /*
653 ===============
654 R_SetupFrame
655 ===============
656 */
657 void R_SetupFrame (void)
658 {
659 // don't allow cheats in multiplayer
660         if (cl.maxclients > 1)
661         {
662                 Cvar_Set ("r_fullbright", "0");
663                 Cvar_Set ("r_ambient", "0");
664         }
665
666         r_framecount++;
667
668 // build the transformation matrix for the given view angles
669         VectorCopy (r_refdef.vieworg, r_origin);
670
671         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
672
673 // current viewleaf
674         r_oldviewleaf = r_viewleaf;
675         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
676
677         V_SetContentsColor (r_viewleaf->contents);
678         V_CalcBlend ();
679
680         r_cache_thrash = false;
681
682         c_brush_polys = 0;
683         c_alias_polys = 0;
684         c_light_polys = 0;
685         c_faces = 0;
686         c_nodes = 0;
687         c_leafs = 0;
688         c_models = 0;
689         c_bmodels = 0;
690         c_sprites = 0;
691         c_particles = 0;
692 //      c_dlights = 0;
693
694         R_AnimateLight ();
695 }
696
697
698 void MYgluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
699 {
700    GLdouble xmin, xmax, ymin, ymax;
701
702    ymax = zNear * tan( fovy * M_PI / 360.0 );
703    ymin = -ymax;
704
705    xmin = ymin * aspect;
706    xmax = ymax * aspect;
707
708    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
709 }
710
711
712 /*
713 =============
714 R_SetupGL
715 =============
716 */
717 void R_SetupGL (void)
718 {
719         float   screenaspect;
720         int             x, x2, y2, y, w, h;
721
722         if (!r_render.value)
723                 return;
724         //
725         // set up viewpoint
726         //
727         glMatrixMode(GL_PROJECTION);
728     glLoadIdentity ();
729         x = r_refdef.vrect.x * glwidth/vid.width;
730         x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
731         y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
732         y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
733
734         // fudge around because of frac screen scale
735         if (x > 0)
736                 x--;
737         if (x2 < glwidth)
738                 x2++;
739         if (y2 < 0)
740                 y2--;
741         if (y < glheight)
742                 y++;
743
744         w = x2 - x;
745         h = y - y2;
746
747         if (envmap)
748         {
749                 x = y2 = 0;
750                 w = h = 256;
751         }
752
753         glViewport (glx + x, gly + y2, w, h);
754     screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
755 //      yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
756         MYgluPerspective (r_refdef.fov_y,  screenaspect,  4,  r_farclip.value);
757
758         glCullFace(GL_FRONT);
759
760         glMatrixMode(GL_MODELVIEW);
761     glLoadIdentity ();
762
763     glRotatef (-90,  1, 0, 0);      // put Z going up
764     glRotatef (90,  0, 0, 1);       // put Z going up
765     glRotatef (-r_refdef.viewangles[2],  1, 0, 0);
766     glRotatef (-r_refdef.viewangles[0],  0, 1, 0);
767     glRotatef (-r_refdef.viewangles[1],  0, 0, 1);
768     glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
769
770         glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
771
772         //
773         // set drawing parms
774         //
775 //      if (gl_cull.value)
776                 glEnable(GL_CULL_FACE);
777 //      else
778 //              glDisable(GL_CULL_FACE);
779
780         glEnable(GL_BLEND); // was Disable
781         glDisable(GL_ALPHA_TEST);
782         glAlphaFunc(GL_GREATER, 0.5);
783         glEnable(GL_DEPTH_TEST);
784         glDepthMask(1);
785         glShadeModel(GL_SMOOTH);
786 }
787
788 /*
789 =============
790 R_Clear
791 =============
792 */
793 void R_Clear (void)
794 {
795         if (!r_render.value)
796                 return;
797 //      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
798         gldepthmin = 0;
799         gldepthmax = 1;
800         glDepthFunc (GL_LEQUAL);
801
802         glDepthRange (gldepthmin, gldepthmax);
803 }
804
805 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
806 void GL_Brighten(void)
807 {
808         if (!r_render.value)
809                 return;
810         glMatrixMode(GL_PROJECTION);
811     glLoadIdentity ();
812         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
813         glMatrixMode(GL_MODELVIEW);
814     glLoadIdentity ();
815         glDisable (GL_DEPTH_TEST);
816         glDisable (GL_CULL_FACE);
817         glDisable(GL_TEXTURE_2D);
818         glEnable(GL_BLEND);
819         glBlendFunc (GL_DST_COLOR, GL_ONE);
820         glBegin (GL_TRIANGLES);
821         glColor3f (1, 1, 1);
822         glVertex2f (-5000, -5000);
823         glVertex2f (10000, -5000);
824         glVertex2f (-5000, 10000);
825         glEnd ();
826         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
827         glDisable(GL_BLEND);
828         glEnable(GL_TEXTURE_2D);
829         glEnable (GL_DEPTH_TEST);
830         glEnable (GL_CULL_FACE);
831 }
832
833 void GL_BlendView(void)
834 {
835         if (!r_render.value)
836                 return;
837
838         if (v_blend[3] < 0.01f)
839                 return;
840
841         glMatrixMode(GL_PROJECTION);
842     glLoadIdentity ();
843         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
844         glMatrixMode(GL_MODELVIEW);
845     glLoadIdentity ();
846         glDisable (GL_DEPTH_TEST);
847         glDisable (GL_CULL_FACE);
848         glDisable(GL_TEXTURE_2D);
849         glEnable(GL_BLEND);
850         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
851         glBegin (GL_TRIANGLES);
852         if (lighthalf)
853                 glColor4f (v_blend[0] * 0.5f, v_blend[1] * 0.5f, v_blend[2] * 0.5f, v_blend[3]);
854         else
855                 glColor4fv (v_blend);
856         glVertex2f (-5000, -5000);
857         glVertex2f (10000, -5000);
858         glVertex2f (-5000, 10000);
859         glEnd ();
860
861         glEnable (GL_CULL_FACE);
862         glEnable (GL_DEPTH_TEST);
863         glDisable(GL_BLEND);
864         glEnable(GL_TEXTURE_2D);
865 }
866
867 /*
868 ================
869 R_RenderView
870
871 r_refdef must be set before the first call
872 ================
873 */
874 extern void R_Sky(void);
875 extern void UploadLightmaps(void);
876 extern void R_DrawSurfaces(void);
877 extern void R_DrawPortals(void);
878 char r_speeds2_string[1024];
879 int speedstringcount;
880
881 void timestring(int t, char *desc)
882 {
883         char tempbuf[256];
884         int length;
885         if (t < 1000000)
886                 sprintf(tempbuf, " %6ius %s", t, desc);
887         else
888                 sprintf(tempbuf, " %6ims %s", t / 1000, desc);
889         length = strlen(tempbuf);
890 //      while (length < 20)
891 //              tempbuf[length++] = ' ';
892 //      tempbuf[length] = 0;
893         if (speedstringcount + length > 80)
894         {
895                 strcat(r_speeds2_string, "\n");
896                 speedstringcount = 0;
897         }
898         // skip the space at the beginning if it's the first on the line
899         if (speedstringcount == 0)
900         {
901                 strcat(r_speeds2_string, tempbuf + 1);
902                 speedstringcount = length - 1;
903         }
904         else
905         {
906                 strcat(r_speeds2_string, tempbuf);
907                 speedstringcount += length;
908         }
909 }
910
911 #define TIMEREPORT(NAME) \
912         if (r_speeds2.value)\
913         {\
914                 temptime = currtime;\
915                 currtime = Sys_DoubleTime();\
916                 timestring((int) ((currtime - temptime) * 1000000.0), NAME);\
917         }
918
919 void R_RenderView (void)
920 {
921         double starttime, currtime, temptime;
922 //      if (r_norefresh.value)
923 //              return;
924
925         if (!cl.worldmodel)
926                 Host_Error ("R_RenderView: NULL worldmodel");
927
928         if (r_speeds2.value)
929         {
930                 starttime = currtime = Sys_DoubleTime();
931
932                 speedstringcount = 0;
933                 sprintf(r_speeds2_string, "org:'%c%6.2f %c%6.2f %c%6.2f' ang:'%c%3.0f %c%3.0f %c%3.0f' dir:'%c%2.3f %c%2.3f %c%2.3f'\n%6i walls %6i dlitwalls %7i modeltris %7i transpoly\nBSP: %6i faces %6i nodes %6i leafs\n%4i models %4i bmodels %4i sprites %5i particles %3i dlights\n",
934                         r_origin[0] < 0 ? '-' : ' ', fabs(r_origin[0]), r_origin[1] < 0 ? '-' : ' ', fabs(r_origin[1]), r_origin[2] < 0 ? '-' : ' ', fabs(r_origin[2]), r_refdef.viewangles[0] < 0 ? '-' : ' ', fabs(r_refdef.viewangles[0]), r_refdef.viewangles[1] < 0 ? '-' : ' ', fabs(r_refdef.viewangles[1]), r_refdef.viewangles[2] < 0 ? '-' : ' ', fabs(r_refdef.viewangles[2]), vpn[0] < 0 ? '-' : ' ', fabs(vpn[0]), vpn[1] < 0 ? '-' : ' ', fabs(vpn[1]), vpn[2] < 0 ? '-' : ' ', fabs(vpn[2]),
935                         c_brush_polys, c_light_polys, c_alias_polys, currenttranspoly,
936                         c_faces, c_nodes, c_leafs,
937                         c_models, c_bmodels, c_sprites, c_particles, c_dlights);
938         }
939         else
940                 starttime = currtime = 0;
941
942         R_MoveParticles ();
943         R_MoveExplosions();
944
945         FOG_framebegin();
946
947         R_Clear();
948         TIMEREPORT("clear     ")
949
950         // render normal view
951
952         R_SetupFrame ();
953         R_SetFrustum ();
954         R_SetupGL ();
955         R_Clip_StartFrame();
956
957         R_PrepareEntities();
958
959         skypolyclear();
960         wallpolyclear();
961         transpolyclear();
962
963         TIMEREPORT("setup     ")
964
965         R_DrawWorld ();
966         TIMEREPORT("world     ")
967
968         R_AddModelEntities();
969         TIMEREPORT("addmodels")
970
971         R_Clip_EndFrame();
972         TIMEREPORT("scanedge  ")
973
974         // now mark the lit surfaces
975         R_PushDlights ();
976         // yes this does add the world surfaces after the brush models
977         R_DrawSurfaces ();
978         R_DrawPortals ();
979         TIMEREPORT("surfaces  ");
980
981         UploadLightmaps();
982         TIMEREPORT("uploadlmap")
983
984         // fogged sky polys, affects depth
985         skypolyrender();
986
987         // does not affect depth, draws over the sky polys
988         if (currentskypoly)
989                 R_Sky();
990         TIMEREPORT("skypoly   ")
991
992         wallpolyrender();
993         TIMEREPORT("wallpoly  ")
994
995         GL_DrawDecals();
996         TIMEREPORT("ddecal    ")
997
998         // don't let sound skip if going slow
999         if (!intimerefresh && !r_speeds2.value)
1000                 S_ExtraUpdate ();
1001
1002         R_DrawViewModel ();
1003         R_DrawModels ();
1004         TIMEREPORT("models    ")
1005
1006         R_DrawParticles ();
1007         TIMEREPORT("dparticles")
1008         R_DrawExplosions();
1009         TIMEREPORT("dexplosion")
1010
1011         transpolyrender();
1012         TIMEREPORT("transpoly ")
1013
1014         FOG_frameend();
1015
1016         GL_BlendView();
1017         TIMEREPORT("blend     ")
1018
1019         if (r_speeds2.value)
1020                 timestring((int) ((Sys_DoubleTime() - starttime) * 1000000.0), "total    ");
1021 }