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_wateralpha = {"r_wateralpha","1"};
77 cvar_t r_dynamic = {"r_dynamic","1"};
78 cvar_t r_novis = {"r_novis","0"};
79 cvar_t r_waterripple = {"r_waterripple","0"};
80 cvar_t r_fullbrights = {"r_fullbrights", "1"};
82 cvar_t contrast = {"contrast", "1.0", true}; // LordHavoc: a method of operating system independent color correction
83 cvar_t brightness = {"brightness", "1.0", true}; // LordHavoc: a method of operating system independent color correction
84 cvar_t gl_lightmode = {"gl_lightmode", "1", true}; // LordHavoc: overbright lighting
85 //cvar_t r_dynamicbothsides = {"r_dynamicbothsides", "1"}; // LordHavoc: can disable dynamic lighting of backfaces, but quake maps are weird so it doesn't always work right...
86 cvar_t r_farclip = {"r_farclip", "6144"};
88 cvar_t gl_fogenable = {"gl_fogenable", "0"};
89 cvar_t gl_fogdensity = {"gl_fogdensity", "0.25"};
90 cvar_t gl_fogred = {"gl_fogred","0.3"};
91 cvar_t gl_foggreen = {"gl_foggreen","0.3"};
92 cvar_t gl_fogblue = {"gl_fogblue","0.3"};
93 cvar_t gl_fogstart = {"gl_fogstart", "0"};
94 cvar_t gl_fogend = {"gl_fogend","0"};
95 cvar_t glfog = {"glfog", "0"};
101 float fog_density, fog_red, fog_green, fog_blue;
103 qboolean oldgl_fogenable;
104 void FOG_framebegin()
108 // if (!Nehahrademcompatibility)
109 // gl_fogenable.value = 0;
110 if (gl_fogenable.value)
112 oldgl_fogenable = true;
113 fog_density = gl_fogdensity.value;
114 fog_red = gl_fogred.value;
115 fog_green = gl_foggreen.value;
116 fog_blue = gl_fogblue.value;
118 else if (oldgl_fogenable)
120 oldgl_fogenable = false;
129 fogcolor[0] = fog_red = bound(0.0f, fog_red , 1.0f);
130 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
131 fogcolor[2] = fog_blue = bound(0.0f, fog_blue , 1.0f);
145 // LordHavoc: Borland C++ 5.0 was choking on this line, stupid compiler...
146 //GLfloat colors[4] = {(GLfloat) gl_fogred.value, (GLfloat) gl_foggreen.value, (GLfloat) gl_fogblue.value, (GLfloat) 1};
149 colors[1] = fog_green;
150 colors[2] = fog_blue;
159 glFogi (GL_FOG_MODE, GL_EXP2);
160 glFogf (GL_FOG_DENSITY, (GLfloat) fog_density / 100);
161 glFogfv (GL_FOG_COLOR, colors);
172 fogdensity = -4000.0f / (fog_density * fog_density);
173 // fog color was already set
190 Cvar_Set("gl_fogenable", "0");
191 Cvar_Set("gl_fogdensity", "0.2");
192 Cvar_Set("gl_fogred", "0.3");
193 Cvar_Set("gl_foggreen", "0.3");
194 Cvar_Set("gl_fogblue", "0.3");
196 fog_density = fog_red = fog_green = fog_blue = 0.0f;
199 void FOG_registercvars()
201 Cvar_RegisterVariable (&glfog);
204 Cvar_RegisterVariable (&gl_fogenable);
205 Cvar_RegisterVariable (&gl_fogdensity);
206 Cvar_RegisterVariable (&gl_fogred);
207 Cvar_RegisterVariable (&gl_foggreen);
208 Cvar_RegisterVariable (&gl_fogblue);
209 Cvar_RegisterVariable (&gl_fogstart);
210 Cvar_RegisterVariable (&gl_fogend);
218 void gl_main_shutdown()
222 void gl_main_newmap()
229 Cvar_RegisterVariable (&r_drawentities);
230 Cvar_RegisterVariable (&r_drawviewmodel);
231 Cvar_RegisterVariable (&r_speeds);
232 Cvar_RegisterVariable (&r_speeds2);
233 Cvar_RegisterVariable (&contrast);
234 Cvar_RegisterVariable (&brightness);
235 Cvar_RegisterVariable (&gl_lightmode);
236 // Cvar_RegisterVariable (&r_dynamicwater);
237 // Cvar_RegisterVariable (&r_dynamicbothsides);
238 Cvar_RegisterVariable (&r_fullbrights);
239 Cvar_RegisterVariable (&r_wateralpha);
240 Cvar_RegisterVariable (&r_dynamic);
241 Cvar_RegisterVariable (&r_novis);
242 Cvar_RegisterVariable (&r_waterripple); // LordHavoc: added waterripple
244 Cvar_SetValue("r_fullbrights", 0);
245 // if (gl_vendor && strstr(gl_vendor, "3Dfx"))
246 // gl_lightmode.value = 0;
247 Cvar_RegisterVariable (&r_fullbright);
248 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
251 extern void GL_Draw_Init();
252 extern void GL_Main_Init();
253 extern void GL_Models_Init();
254 extern void GL_Poly_Init();
255 extern void GL_Surf_Init();
256 extern void GL_Screen_Init();
257 extern void GL_Misc_Init();
258 extern void R_Crosshairs_Init();
259 extern void R_Light_Init();
260 extern void R_Particles_Init();
261 extern void R_Explosion_Init();
262 extern void CL_Effects_Init();
266 R_Modules_Shutdown();
288 extern char *ENGINE_EXTENSIONS;
291 gl_vendor = glGetString (GL_VENDOR);
292 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
293 gl_renderer = glGetString (GL_RENDERER);
294 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
296 gl_version = glGetString (GL_VERSION);
297 Con_Printf ("GL_VERSION: %s\n", gl_version);
298 gl_extensions = glGetString (GL_EXTENSIONS);
299 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
301 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
303 VID_CheckMultitexture();
306 // LordHavoc: report supported extensions
307 Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
309 glCullFace(GL_FRONT);
310 glEnable(GL_TEXTURE_2D);
311 // glDisable(GL_ALPHA_TEST);
312 glAlphaFunc(GL_GREATER, 0.5);
314 // glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
321 void R_RotateForEntity (entity_t *e)
323 glTranslatef (e->origin[0], e->origin[1], e->origin[2]);
325 glRotatef (e->angles[1], 0, 0, 1);
326 glRotatef (-e->angles[0], 0, 1, 0);
327 glRotatef (e->angles[2], 1, 0, 0);
329 glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
333 // LordHavoc: shading stuff
339 //==================================================================================
341 void R_DrawBrushModel (entity_t *e);
342 void R_DrawSpriteModel (entity_t *e, frameblend_t *blend);
344 void R_LerpUpdate(entity_t *ent)
348 if (ent->model && ent->frame >= ent->model->numframes)
350 Con_Printf("R_LerpUpdate: no such frame%6i in \"%s\"\n", ent->frame, ent->model->name);
354 if (ent->lerp_model != ent->model)
356 // reset all interpolation information
357 ent->lerp_model = ent->model;
358 ent->frame1 = ent->frame2 = frame;
359 ent->frame1start = ent->frame2start = cl.time;
361 ent->lerp_starttime = 0;
363 else if (ent->frame2 != frame)
365 // transition to new frame
366 ent->frame1 = ent->frame2;
367 ent->frame1start = ent->frame2start;
369 ent->frame2start = cl.time;
371 ent->lerp_starttime = cl.time;
375 // lerp_starttime < 0 is used to prevent changing of framelerp
376 if (ent->lerp_starttime >= 0)
379 ent->framelerp = (cl.time - ent->lerp_starttime) * 10;
380 ent->framelerp = bound(0, ent->framelerp, 1);
391 // LordHavoc: split so bmodels are rendered before any other objects
392 void R_DrawEntitiesOnList1 (void)
396 if (!r_drawentities.value)
399 for (i=0 ; i<cl_numvisedicts ; i++)
401 if (cl_visedicts[i]->model->type != mod_brush)
403 currententity = cl_visedicts[i];
404 modelalpha = currententity->alpha;
406 R_DrawBrushModel (currententity);
410 void R_DrawEntitiesOnList2 (void)
413 frameblend_t blend[4];
415 if (!r_drawentities.value)
418 for (i=0 ; i<cl_numvisedicts ; i++)
420 currententity = cl_visedicts[i];
421 modelalpha = currententity->alpha;
423 switch (currententity->model->type)
426 if (!strcmp(currententity->model->name, "progs/flame2.mdl"))
429 R_LerpUpdate(currententity);
430 R_LerpAnimation(currententity->model, currententity->frame1, currententity->frame2, currententity->frame1start, currententity->frame2start, currententity->framelerp, blend);
431 R_DrawAliasModel (currententity, true, modelalpha, currententity->model, blend, currententity->skinnum, currententity->origin, currententity->angles, currententity->scale, currententity->effects, currententity->model->flags, currententity->colormap);
435 R_LerpUpdate(currententity);
436 R_LerpAnimation(currententity->model, currententity->frame1, currententity->frame2, currententity->frame1start, currententity->frame2start, currententity->framelerp, blend);
437 R_DrawSpriteModel (currententity, blend);
451 void R_DrawViewModel (void)
453 frameblend_t blend[4];
455 if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.model)
458 currententity = &cl.viewent;
459 currententity->alpha = modelalpha = cl_entities[cl.viewentity].alpha; // LordHavoc: if the player is transparent, so is his gun
460 currententity->effects = cl_entities[cl.viewentity].effects;
461 currententity->scale = 1;
462 VectorCopy(cl_entities[cl.viewentity].colormod, currententity->colormod);
464 R_LerpUpdate(currententity);
465 R_LerpAnimation(currententity->model, currententity->frame1, currententity->frame2, currententity->frame1start, currententity->frame2start, currententity->framelerp, blend);
467 // hack the depth range to prevent view model from poking into walls
468 glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
469 R_DrawAliasModel (currententity, false, modelalpha, currententity->model, blend, currententity->skinnum, currententity->origin, currententity->angles, currententity->scale, currententity->effects, currententity->model->flags, currententity->colormap);
470 glDepthRange (gldepthmin, gldepthmax);
473 void R_DrawBrushModel (entity_t *e);
475 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
477 void R_SetFrustum (void)
481 if (r_refdef.fov_x == 90)
483 // front side is visible
485 VectorAdd (vpn, vright, frustum[0].normal);
486 VectorSubtract (vpn, vright, frustum[1].normal);
488 VectorAdd (vpn, vup, frustum[2].normal);
489 VectorSubtract (vpn, vup, frustum[3].normal);
493 // rotate VPN right by FOV_X/2 degrees
494 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
495 // rotate VPN left by FOV_X/2 degrees
496 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
497 // rotate VPN up by FOV_X/2 degrees
498 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
499 // rotate VPN down by FOV_X/2 degrees
500 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
503 for (i=0 ; i<4 ; i++)
505 frustum[i].type = PLANE_ANYZ;
506 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
507 // frustum[i].signbits = SignbitsForPlane (&frustum[i]);
508 BoxOnPlaneSideClassify(&frustum[i]);
512 void R_AnimateLight (void);
513 void V_CalcBlend (void);
520 void R_SetupFrame (void)
522 // don't allow cheats in multiplayer
523 if (cl.maxclients > 1)
525 Cvar_Set ("r_fullbright", "0");
526 Cvar_Set ("r_ambient", "0");
533 // build the transformation matrix for the given view angles
534 VectorCopy (r_refdef.vieworg, r_origin);
536 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
539 r_oldviewleaf = r_viewleaf;
540 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
542 V_SetContentsColor (r_viewleaf->contents);
545 r_cache_thrash = false;
561 void MYgluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
563 GLdouble xmin, xmax, ymin, ymax;
565 ymax = zNear * tan( fovy * M_PI / 360.0 );
568 xmin = ymin * aspect;
569 xmax = ymax * aspect;
571 glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
575 extern char skyname[];
582 void R_SetupGL (void)
585 extern int glwidth, glheight;
586 int x, x2, y2, y, w, h;
593 glMatrixMode(GL_PROJECTION);
595 x = r_refdef.vrect.x * glwidth/vid.width;
596 x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
597 y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
598 y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
600 // fudge around because of frac screen scale
619 glViewport (glx + x, gly + y2, w, h);
620 screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
621 // yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
622 // if (skyname[0]) // skybox enabled?
623 // MYgluPerspective (r_refdef.fov_y, screenaspect, 4, r_skyboxsize.value*1.732050807569 + 256); // this is size*sqrt(3) + 256
625 MYgluPerspective (r_refdef.fov_y, screenaspect, 4, r_farclip.value);
627 glCullFace(GL_FRONT);
629 glMatrixMode(GL_MODELVIEW);
632 glRotatef (-90, 1, 0, 0); // put Z going up
633 glRotatef (90, 0, 0, 1); // put Z going up
634 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);
635 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);
636 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);
637 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);
639 glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
644 // if (gl_cull.value)
645 glEnable(GL_CULL_FACE);
647 // glDisable(GL_CULL_FACE);
649 glEnable(GL_BLEND); // was Disable
650 glDisable(GL_ALPHA_TEST);
651 glAlphaFunc(GL_GREATER, 0.5);
652 glEnable(GL_DEPTH_TEST);
654 glShadeModel(GL_SMOOTH);
666 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
669 glDepthFunc (GL_LEQUAL);
671 glDepthRange (gldepthmin, gldepthmax);
674 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
679 glMatrixMode(GL_PROJECTION);
681 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
682 glMatrixMode(GL_MODELVIEW);
684 glDisable (GL_DEPTH_TEST);
685 glDisable (GL_CULL_FACE);
686 glDisable(GL_TEXTURE_2D);
688 glBlendFunc (GL_DST_COLOR, GL_ONE);
689 glBegin (GL_TRIANGLES);
691 glVertex2f (-5000, -5000);
692 glVertex2f (10000, -5000);
693 glVertex2f (-5000, 10000);
695 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
697 glEnable(GL_TEXTURE_2D);
698 glEnable (GL_DEPTH_TEST);
699 glEnable (GL_CULL_FACE);
702 extern cvar_t contrast;
703 extern cvar_t brightness;
704 extern cvar_t gl_lightmode;
710 glMatrixMode(GL_PROJECTION);
712 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
713 glMatrixMode(GL_MODELVIEW);
715 glDisable (GL_DEPTH_TEST);
716 glDisable (GL_CULL_FACE);
717 glDisable(GL_TEXTURE_2D);
721 glBlendFunc (GL_DST_COLOR, GL_ONE);
722 glBegin (GL_TRIANGLES);
724 glVertex2f (-5000, -5000);
725 glVertex2f (10000, -5000);
726 glVertex2f (-5000, 10000);
729 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
730 contrast.value = bound(0.2, contrast.value, 1.0);
731 if (/*gl_polyblend.value && */v_blend[3])
733 glBegin (GL_TRIANGLES);
734 glColor4fv (v_blend);
735 glVertex2f (-5000, -5000);
736 glVertex2f (10000, -5000);
737 glVertex2f (-5000, 10000);
741 glEnable (GL_CULL_FACE);
742 glEnable (GL_DEPTH_TEST);
744 glEnable(GL_TEXTURE_2D);
747 #define TIMEREPORT(VAR) \
748 if (r_speeds2.value)\
750 temptime = currtime;\
751 currtime = Sys_FloatTime();\
752 VAR = (int) ((currtime - temptime) * 1000000.0);\
761 r_refdef must be set before the first call
764 extern qboolean intimerefresh;
765 extern qboolean skyisvisible;
767 extern void UploadLightmaps();
768 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];
769 void R_RenderView (void)
771 double starttime, currtime, temptime;
772 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;
773 // if (r_norefresh.value)
776 if (!r_worldentity.model || !cl.worldmodel)
777 Sys_Error ("R_RenderView: NULL worldmodel");
779 lighthalf = gl_lightmode.value;
785 starttime = currtime = Sys_FloatTime();
788 starttime = currtime = 0;
790 skyisvisible = false;
791 TIMEREPORT(time_clear)
793 // render normal view
803 TIMEREPORT(time_setup)
805 R_MarkLeaves (); // done here so we know if we're in water
806 R_DrawWorld (); // adds static entities to the list
807 TIMEREPORT(time_world)
808 R_DrawEntitiesOnList1 (); // BSP models
809 TIMEREPORT(time_bmodels)
812 TIMEREPORT(time_upload)
814 skypolyrender(); // fogged sky polys, affects depth
816 if (skyname[0] && skyisvisible && !fogenabled)
817 R_Sky(); // does not affect depth, draws over the sky polys
821 TIMEREPORT(time_wall)
823 if (!intimerefresh && !r_speeds2.value)
824 S_ExtraUpdate (); // don't let sound get messed up if going slow
826 R_DrawEntitiesOnList2 (); // other models
827 // R_RenderDlights ();
829 TIMEREPORT(time_models)
831 TIMEREPORT(time_moveparticles)
833 TIMEREPORT(time_drawparticles)
835 TIMEREPORT(time_moveexplosions)
837 TIMEREPORT(time_drawexplosions)
839 TIMEREPORT(time_drawdecals)
842 TIMEREPORT(time_transpoly)
847 TIMEREPORT(time_blend)
850 time_total = (int) ((Sys_FloatTime() - starttime) * 1000000.0);
851 sprintf(r_speeds2_string1, "%6i walls %6i dlitwalls %7i modeltris %7i transpoly\n", c_brush_polys, c_light_polys, c_alias_polys, currenttranspoly);
852 sprintf(r_speeds2_string2, "BSP: %6i faces %6i nodes %6i leafs\n", c_faces, c_nodes, c_leafs);
853 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);
854 sprintf(r_speeds2_string4, "%6ius clear %6ius setup %6ius world %6ius bmodel %6ius upload", time_clear, time_setup, time_world, time_bmodels, time_upload);
855 sprintf(r_speeds2_string5, "%6ius sky %6ius wall %6ius models %6ius mpart %6ius dpart ", time_sky, time_wall, time_models, time_moveparticles, time_drawparticles);
856 sprintf(r_speeds2_string6, "%6ius mexplo %6ius dexplo %6ius decals %6ius trans %6ius blend ", time_moveexplosions, time_drawexplosions, time_drawdecals, time_transpoly, time_blend);
857 sprintf(r_speeds2_string7, "%6ius permdl %6ius total ", time_models / max(c_models, 1), time_total);