2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 entity_t r_worldentity;
26 qboolean r_cache_thrash; // compatability
28 vec3_t modelorg, r_entorigin;
29 entity_t *currententity;
31 int r_visframecount; // bumped when going to a new PVS
32 int r_framecount; // used for dlight push checking
36 int c_brush_polys, c_alias_polys, c_light_polys, c_faces, c_nodes, c_leafs, c_models, c_bmodels, c_sprites, c_particles, c_dlights;
38 qboolean envmap; // true during envmap command capture
40 // LordHavoc: moved all code related to particles into r_part.c
41 //int particletexture; // little dot for particles
42 //int playertextures; // up to 16 color translated skins
52 float r_world_matrix[16];
53 float r_base_world_matrix[16];
60 mleaf_t *r_viewleaf, *r_oldviewleaf;
62 texture_t *r_notexture_mip;
64 unsigned short d_lightstylevalue[256]; // 8.8 fraction of base light value
67 void R_MarkLeaves (void);
69 //cvar_t r_norefresh = {"r_norefresh","0"};
70 cvar_t r_drawentities = {"r_drawentities","1"};
71 cvar_t r_drawviewmodel = {"r_drawviewmodel","1"};
72 cvar_t r_speeds = {"r_speeds","0"};
73 cvar_t r_speeds2 = {"r_speeds2","0"};
74 cvar_t r_fullbright = {"r_fullbright","0"};
75 //cvar_t r_lightmap = {"r_lightmap","0"};
76 cvar_t r_shadows = {"r_shadows","0"};
77 cvar_t r_wateralpha = {"r_wateralpha","1"};
78 cvar_t r_dynamic = {"r_dynamic","1"};
79 cvar_t r_novis = {"r_novis","0"};
80 cvar_t r_waterripple = {"r_waterripple","0"};
81 cvar_t r_fullbrights = {"r_fullbrights", "1"};
83 cvar_t contrast = {"contrast", "1.0", true}; // LordHavoc: a method of operating system independent color correction
84 cvar_t brightness = {"brightness", "1.0", true}; // LordHavoc: a method of operating system independent color correction
85 cvar_t gl_lightmode = {"gl_lightmode", "1", true}; // LordHavoc: overbright lighting
86 //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...
87 cvar_t r_farclip = {"r_farclip", "6144"};
89 cvar_t gl_fogenable = {"gl_fogenable", "0"};
90 cvar_t gl_fogdensity = {"gl_fogdensity", "0.25"};
91 cvar_t gl_fogred = {"gl_fogred","0.3"};
92 cvar_t gl_foggreen = {"gl_foggreen","0.3"};
93 cvar_t gl_fogblue = {"gl_fogblue","0.3"};
94 cvar_t gl_fogstart = {"gl_fogstart", "0"};
95 cvar_t gl_fogend = {"gl_fogend","0"};
96 cvar_t glfog = {"glfog", "0"};
102 float fog_density, fog_red, fog_green, fog_blue;
104 qboolean oldgl_fogenable;
105 void FOG_framebegin()
109 // if (!Nehahrademcompatibility)
110 // gl_fogenable.value = 0;
111 if (gl_fogenable.value)
113 oldgl_fogenable = true;
114 fog_density = gl_fogdensity.value;
115 fog_red = gl_fogred.value;
116 fog_green = gl_foggreen.value;
117 fog_blue = gl_fogblue.value;
119 else if (oldgl_fogenable)
121 oldgl_fogenable = false;
130 fogcolor[0] = fog_red = bound(0.0f, fog_red , 1.0f);
131 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
132 fogcolor[2] = fog_blue = bound(0.0f, fog_blue , 1.0f);
146 // LordHavoc: Borland C++ 5.0 was choking on this line, stupid compiler...
147 //GLfloat colors[4] = {(GLfloat) gl_fogred.value, (GLfloat) gl_foggreen.value, (GLfloat) gl_fogblue.value, (GLfloat) 1};
150 colors[1] = fog_green;
151 colors[2] = fog_blue;
160 glFogi (GL_FOG_MODE, GL_EXP2);
161 glFogf (GL_FOG_DENSITY, (GLfloat) fog_density / 100);
162 glFogfv (GL_FOG_COLOR, colors);
173 fogdensity = -4000.0f / (fog_density * fog_density);
174 // fog color was already set
191 Cvar_Set("gl_fogenable", "0");
192 Cvar_Set("gl_fogdensity", "0.2");
193 Cvar_Set("gl_fogred", "0.3");
194 Cvar_Set("gl_foggreen", "0.3");
195 Cvar_Set("gl_fogblue", "0.3");
197 fog_density = fog_red = fog_green = fog_blue = 0.0f;
200 void FOG_registercvars()
202 Cvar_RegisterVariable (&glfog);
205 Cvar_RegisterVariable (&gl_fogenable);
206 Cvar_RegisterVariable (&gl_fogdensity);
207 Cvar_RegisterVariable (&gl_fogred);
208 Cvar_RegisterVariable (&gl_foggreen);
209 Cvar_RegisterVariable (&gl_fogblue);
210 Cvar_RegisterVariable (&gl_fogstart);
211 Cvar_RegisterVariable (&gl_fogend);
219 void glmain_shutdown()
226 Cvar_RegisterVariable (&r_drawentities);
227 Cvar_RegisterVariable (&r_drawviewmodel);
228 Cvar_RegisterVariable (&r_shadows);
229 Cvar_RegisterVariable (&r_speeds);
230 Cvar_RegisterVariable (&r_speeds2);
231 Cvar_RegisterVariable (&contrast);
232 Cvar_RegisterVariable (&brightness);
233 Cvar_RegisterVariable (&gl_lightmode);
234 // Cvar_RegisterVariable (&r_dynamicwater);
235 // Cvar_RegisterVariable (&r_dynamicbothsides);
236 Cvar_RegisterVariable (&r_fullbrights);
237 Cvar_RegisterVariable (&r_wateralpha);
238 Cvar_RegisterVariable (&r_dynamic);
239 Cvar_RegisterVariable (&r_novis);
240 Cvar_RegisterVariable (&r_waterripple); // LordHavoc: added waterripple
242 Cvar_SetValue("r_fullbrights", 0);
243 // if (gl_vendor && strstr(gl_vendor, "3Dfx"))
244 // gl_lightmode.value = 0;
245 Cvar_RegisterVariable (&r_fullbright);
246 R_RegisterModule("GL_Main", glmain_start, glmain_shutdown);
249 extern void GL_Draw_Init();
250 extern void GL_Main_Init();
251 extern void GL_Models_Init();
252 extern void GL_Poly_Init();
253 extern void GL_Surf_Init();
254 extern void GL_Screen_Init();
255 extern void GL_Misc_Init();
256 extern void R_Crosshairs_Init();
257 extern void R_Light_Init();
258 extern void R_Particles_Init();
259 extern void R_Explosion_Init();
283 extern char *QSG_EXTENSIONS;
286 gl_vendor = glGetString (GL_VENDOR);
287 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
288 gl_renderer = glGetString (GL_RENDERER);
289 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
291 gl_version = glGetString (GL_VERSION);
292 Con_Printf ("GL_VERSION: %s\n", gl_version);
293 gl_extensions = glGetString (GL_EXTENSIONS);
294 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
296 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
298 VID_CheckMultitexture();
299 VID_CheckVertexArrays();
301 // LordHavoc: report supported extensions
302 Con_Printf ("\nQSG extensions: %s\n", QSG_EXTENSIONS);
304 glCullFace(GL_FRONT);
305 glEnable(GL_TEXTURE_2D);
306 // glDisable(GL_ALPHA_TEST);
307 glAlphaFunc(GL_GREATER, 0.5);
309 // glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
316 void R_RotateForEntity (entity_t *e)
318 glTranslatef (e->origin[0], e->origin[1], e->origin[2]);
320 glRotatef (e->angles[1], 0, 0, 1);
321 glRotatef (-e->angles[0], 0, 1, 0);
322 glRotatef (e->angles[2], 1, 0, 0);
324 glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
328 // LordHavoc: if not for the fact BRIGHTFIELD particles require this, it would be removed...
329 #define NUMVERTEXNORMALS 162
331 float r_avertexnormals[NUMVERTEXNORMALS][3] = {
335 // LordHavoc: shading stuff
341 //==================================================================================
343 void R_DrawBrushModel (entity_t *e);
344 void R_DrawSpriteModel (entity_t *e);
351 // LordHavoc: split so bmodels are rendered before any other objects
352 void R_DrawEntitiesOnList1 (void)
356 if (!r_drawentities.value)
359 for (i=0 ; i<cl_numvisedicts ; i++)
361 if (cl_visedicts[i]->model->type != mod_brush)
363 currententity = cl_visedicts[i];
364 modelalpha = currententity->alpha;
366 R_DrawBrushModel (currententity);
370 void R_DrawEntitiesOnList2 (void)
374 if (!r_drawentities.value)
377 for (i=0 ; i<cl_numvisedicts ; i++)
379 currententity = cl_visedicts[i];
380 modelalpha = currententity->alpha;
382 switch (currententity->model->type)
385 R_DrawAliasModel (currententity, true, modelalpha, currententity->model, currententity->frame, currententity->skinnum, currententity->origin, currententity->angles, currententity->effects, currententity->model->flags, currententity->colormap);
389 R_DrawSpriteModel (currententity);
403 void R_DrawViewModel (void)
405 if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.model)
408 currententity = &cl.viewent;
409 currententity->alpha = modelalpha = cl_entities[cl.viewentity].alpha; // LordHavoc: if the player is transparent, so is his gun
410 currententity->effects = cl_entities[cl.viewentity].effects;
411 currententity->scale = 1;
412 VectorCopy(cl_entities[cl.viewentity].colormod, currententity->colormod);
414 // hack the depth range to prevent view model from poking into walls
415 glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
416 R_DrawAliasModel (currententity, false, modelalpha, currententity->model, currententity->frame, currententity->skinnum, currententity->origin, currententity->angles, currententity->effects, currententity->model->flags, currententity->colormap);
417 glDepthRange (gldepthmin, gldepthmax);
420 void R_DrawBrushModel (entity_t *e);
422 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
424 void R_SetFrustum (void)
428 if (r_refdef.fov_x == 90)
430 // front side is visible
432 VectorAdd (vpn, vright, frustum[0].normal);
433 VectorSubtract (vpn, vright, frustum[1].normal);
435 VectorAdd (vpn, vup, frustum[2].normal);
436 VectorSubtract (vpn, vup, frustum[3].normal);
440 // rotate VPN right by FOV_X/2 degrees
441 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
442 // rotate VPN left by FOV_X/2 degrees
443 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
444 // rotate VPN up by FOV_X/2 degrees
445 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
446 // rotate VPN down by FOV_X/2 degrees
447 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
450 for (i=0 ; i<4 ; i++)
452 frustum[i].type = PLANE_ANYZ;
453 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
454 // frustum[i].signbits = SignbitsForPlane (&frustum[i]);
455 BoxOnPlaneSideClassify(&frustum[i]);
459 void R_AnimateLight (void);
460 void V_CalcBlend (void);
467 void R_SetupFrame (void)
469 // don't allow cheats in multiplayer
470 if (cl.maxclients > 1)
472 Cvar_Set ("r_fullbright", "0");
473 Cvar_Set ("r_ambient", "0");
480 // build the transformation matrix for the given view angles
481 VectorCopy (r_refdef.vieworg, r_origin);
483 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
486 r_oldviewleaf = r_viewleaf;
487 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
489 V_SetContentsColor (r_viewleaf->contents);
492 r_cache_thrash = false;
508 void MYgluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
510 GLdouble xmin, xmax, ymin, ymax;
512 ymax = zNear * tan( fovy * M_PI / 360.0 );
515 xmin = ymin * aspect;
516 xmax = ymax * aspect;
518 glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
522 extern char skyname[];
529 void R_SetupGL (void)
532 extern int glwidth, glheight;
533 int x, x2, y2, y, w, h;
540 glMatrixMode(GL_PROJECTION);
542 x = r_refdef.vrect.x * glwidth/vid.width;
543 x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
544 y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
545 y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
547 // fudge around because of frac screen scale
566 glViewport (glx + x, gly + y2, w, h);
567 screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
568 // yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
569 // if (skyname[0]) // skybox enabled?
570 // MYgluPerspective (r_refdef.fov_y, screenaspect, 4, r_skyboxsize.value*1.732050807569 + 256); // this is size*sqrt(3) + 256
572 MYgluPerspective (r_refdef.fov_y, screenaspect, 4, r_farclip.value);
574 glCullFace(GL_FRONT);
576 glMatrixMode(GL_MODELVIEW);
579 glRotatef (-90, 1, 0, 0); // put Z going up
580 glRotatef (90, 0, 0, 1); // put Z going up
581 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);
582 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);
583 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);
584 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);
586 glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
591 // if (gl_cull.value)
592 glEnable(GL_CULL_FACE);
594 // glDisable(GL_CULL_FACE);
596 glEnable(GL_BLEND); // was Disable
597 glDisable(GL_ALPHA_TEST);
598 glAlphaFunc(GL_GREATER, 0.5);
599 glEnable(GL_DEPTH_TEST);
601 glShadeModel(GL_SMOOTH);
613 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
616 glDepthFunc (GL_LEQUAL);
618 glDepthRange (gldepthmin, gldepthmax);
621 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
626 glMatrixMode(GL_PROJECTION);
628 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
629 glMatrixMode(GL_MODELVIEW);
631 glDisable (GL_DEPTH_TEST);
632 glDisable (GL_CULL_FACE);
633 glDisable(GL_TEXTURE_2D);
635 glBlendFunc (GL_DST_COLOR, GL_ONE);
636 glBegin (GL_TRIANGLES);
638 glVertex2f (-5000, -5000);
639 glVertex2f (10000, -5000);
640 glVertex2f (-5000, 10000);
642 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
644 glEnable(GL_TEXTURE_2D);
645 glEnable (GL_DEPTH_TEST);
646 glEnable (GL_CULL_FACE);
649 extern cvar_t contrast;
650 extern cvar_t brightness;
651 extern cvar_t gl_lightmode;
657 glMatrixMode(GL_PROJECTION);
659 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
660 glMatrixMode(GL_MODELVIEW);
662 glDisable (GL_DEPTH_TEST);
663 glDisable (GL_CULL_FACE);
664 glDisable(GL_TEXTURE_2D);
668 glBlendFunc (GL_DST_COLOR, GL_ONE);
669 glBegin (GL_TRIANGLES);
671 glVertex2f (-5000, -5000);
672 glVertex2f (10000, -5000);
673 glVertex2f (-5000, 10000);
676 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
677 contrast.value = bound(0.2, contrast.value, 1.0);
678 if (/*gl_polyblend.value && */v_blend[3])
680 glBegin (GL_TRIANGLES);
681 glColor4fv (v_blend);
682 glVertex2f (-5000, -5000);
683 glVertex2f (10000, -5000);
684 glVertex2f (-5000, 10000);
688 glEnable (GL_CULL_FACE);
689 glEnable (GL_DEPTH_TEST);
691 glEnable(GL_TEXTURE_2D);
695 #define TIMEREPORT(DESC) \
696 if (r_speeds2.value)\
698 temptime = -currtime;\
699 currtime = Sys_FloatTime();\
700 temptime += currtime;\
701 Con_Printf(DESC " %.4fms ", temptime * 1000.0);\
704 #define TIMEREPORT(VAR) \
705 if (r_speeds2.value)\
707 temptime = currtime;\
708 currtime = Sys_FloatTime();\
709 VAR = (int) ((currtime - temptime) * 1000000.0);\
718 r_refdef must be set before the first call
721 extern qboolean intimerefresh;
722 extern qboolean skyisvisible;
724 extern void UploadLightmaps();
725 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];
726 void R_RenderView (void)
728 double starttime, currtime, temptime;
729 int time_clear, time_setup, time_world, time_bmodels, time_upload, time_sky, time_wall, time_models, time_moveparticles, time_drawparticles, time_moveexplosions, time_drawexplosions, time_transpoly, time_blend, time_total;
730 // double currtime, temptime;
731 // if (r_norefresh.value)
734 if (!r_worldentity.model || !cl.worldmodel)
735 Sys_Error ("R_RenderView: NULL worldmodel");
737 lighthalf = gl_lightmode.value;
743 starttime = currtime = Sys_FloatTime();
744 // Con_Printf("render time: ");
747 starttime = currtime = 0;
752 skyisvisible = false;
753 TIMEREPORT(time_clear)
755 // render normal view
760 TIMEREPORT(time_setup)
762 R_MarkLeaves (); // done here so we know if we're in water
763 R_DrawWorld (); // adds static entities to the list
764 TIMEREPORT(time_world)
765 R_DrawEntitiesOnList1 (); // BSP models
766 TIMEREPORT(time_bmodels)
769 TIMEREPORT(time_upload)
771 skypolyrender(); // fogged sky polys, affects depth
773 if (skyname[0] && skyisvisible && !fogenabled)
774 R_Sky(); // does not affect depth, draws over the sky polys
778 TIMEREPORT(time_wall)
780 // if (!intimerefresh)
781 // S_ExtraUpdate (); // don't let sound get messed up if going slow
783 R_DrawEntitiesOnList2 (); // other models
784 // R_RenderDlights ();
786 TIMEREPORT(time_models)
788 TIMEREPORT(time_moveparticles)
790 TIMEREPORT(time_drawparticles)
792 TIMEREPORT(time_moveexplosions)
794 TIMEREPORT(time_drawexplosions)
797 TIMEREPORT(time_transpoly)
802 TIMEREPORT(time_blend)
805 time_total = (int) ((Sys_FloatTime() - starttime) * 1000000.0);
807 sprintf(r_speeds2_string1, "%6i walls %6i dlitwalls %7i modeltris %7i transpoly\n", c_brush_polys, c_light_polys, c_alias_polys, currenttranspoly);
808 sprintf(r_speeds2_string2, "BSP: %6i faces %6i nodes %6i leafs\n", c_faces, c_nodes, c_leafs);
809 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);
810 sprintf(r_speeds2_string4, "%6ius clear %6ius setup %6ius world %6ius bmodel %6ius upload", time_clear, time_setup, time_world, time_bmodels, time_upload);
811 sprintf(r_speeds2_string5, "%6ius sky %6ius wall %6ius models %6ius mpart %6ius dpart ", time_sky, time_wall, time_models, time_moveparticles, time_drawparticles);
812 sprintf(r_speeds2_string6, "%6ius trans %6ius blend %6ius total %6ius permdl", time_transpoly, time_blend, time_total, time_models / max(c_models, 1));