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_nodes, c_leafs;
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
44 extern qboolean isG200, isRagePro; // LordHavoc: special card hacks
54 float r_world_matrix[16];
55 float r_base_world_matrix[16];
62 mleaf_t *r_viewleaf, *r_oldviewleaf;
64 texture_t *r_notexture_mip;
66 unsigned short d_lightstylevalue[256]; // 8.8 fraction of base light value
69 void R_MarkLeaves (void);
71 //cvar_t r_norefresh = {"r_norefresh","0"};
72 cvar_t r_drawentities = {"r_drawentities","1"};
73 cvar_t r_drawviewmodel = {"r_drawviewmodel","1"};
74 cvar_t r_speeds = {"r_speeds","0"};
75 cvar_t r_speeds2 = {"r_speeds2","0"};
76 cvar_t r_fullbright = {"r_fullbright","0"};
77 //cvar_t r_lightmap = {"r_lightmap","0"};
78 cvar_t r_shadows = {"r_shadows","0"};
79 cvar_t r_wateralpha = {"r_wateralpha","1"};
80 cvar_t r_dynamic = {"r_dynamic","1"};
81 cvar_t r_novis = {"r_novis","0"};
82 cvar_t r_waterripple = {"r_waterripple","0"};
83 cvar_t r_fullbrights = {"r_fullbrights", "1"};
85 cvar_t contrast = {"contrast", "1.0", TRUE}; // LordHavoc: a method of operating system independent color correction
86 cvar_t brightness = {"brightness", "1.0", TRUE}; // LordHavoc: a method of operating system independent color correction
87 cvar_t gl_lightmode = {"gl_lightmode", "1", TRUE}; // LordHavoc: overbright lighting
88 //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...
89 cvar_t r_farclip = {"r_farclip", "6144"};
91 cvar_t gl_fogenable = {"gl_fogenable", "0"};
92 cvar_t gl_fogdensity = {"gl_fogdensity", "0.25"};
93 cvar_t gl_fogred = {"gl_fogred","0.3"};
94 cvar_t gl_foggreen = {"gl_foggreen","0.3"};
95 cvar_t gl_fogblue = {"gl_fogblue","0.3"};
96 cvar_t gl_fogstart = {"gl_fogstart", "0"};
97 cvar_t gl_fogend = {"gl_fogend","0"};
98 cvar_t glfog = {"glfog", "0"};
100 extern qboolean isRagePro;
106 float fog_density, fog_red, fog_green, fog_blue;
108 qboolean oldgl_fogenable;
109 void FOG_framebegin()
113 // if (!Nehahrademcompatibility)
114 // gl_fogenable.value = 0;
115 if (gl_fogenable.value)
117 oldgl_fogenable = true;
118 fog_density = gl_fogdensity.value;
119 fog_red = gl_fogred.value;
120 fog_green = gl_foggreen.value;
121 fog_blue = gl_fogblue.value;
123 else if (oldgl_fogenable)
125 oldgl_fogenable = false;
136 // LordHavoc: Borland C++ 5.0 was choking on this line, stupid compiler...
137 //GLfloat colors[4] = {(GLfloat) gl_fogred.value, (GLfloat) gl_foggreen.value, (GLfloat) gl_fogblue.value, (GLfloat) 1};
140 colors[1] = fog_green;
141 colors[2] = fog_blue;
150 glFogi (GL_FOG_MODE, GL_EXP2);
151 glFogf (GL_FOG_DENSITY, (GLfloat) fog_density / 100);
152 glFogfv (GL_FOG_COLOR, colors);
163 fogdensity = -4000.0f / (fog_density * fog_density);
164 fogcolor[0] = fog_red = bound(0.0f, fog_red , 1.0f);
165 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
166 fogcolor[2] = fog_blue = bound(0.0f, fog_blue , 1.0f);
189 Cvar_Set("gl_fogenable", "0");
190 Cvar_Set("gl_fogdensity", "0.2");
191 Cvar_Set("gl_fogred", "0.3");
192 Cvar_Set("gl_foggreen", "0.3");
193 Cvar_Set("gl_fogblue", "0.3");
195 fog_density = fog_red = fog_green = fog_blue = 0.0f;
198 void FOG_registercvars()
200 Cvar_RegisterVariable (&glfog);
203 Cvar_RegisterVariable (&gl_fogenable);
204 Cvar_RegisterVariable (&gl_fogdensity);
205 Cvar_RegisterVariable (&gl_fogred);
206 Cvar_RegisterVariable (&gl_foggreen);
207 Cvar_RegisterVariable (&gl_fogblue);
208 Cvar_RegisterVariable (&gl_fogstart);
209 Cvar_RegisterVariable (&gl_fogend);
217 void glmain_shutdown()
224 Cvar_RegisterVariable (&r_speeds2);
225 Cvar_RegisterVariable (&contrast);
226 Cvar_RegisterVariable (&brightness);
227 Cvar_RegisterVariable (&gl_lightmode);
228 // Cvar_RegisterVariable (&r_dynamicwater);
229 // Cvar_RegisterVariable (&r_dynamicbothsides);
230 Cvar_RegisterVariable (&r_fullbrights);
232 Cvar_SetValue("r_fullbrights", 0);
233 // if (gl_vendor && strstr(gl_vendor, "3Dfx"))
234 // gl_lightmode.value = 0;
235 Cvar_RegisterVariable (&r_fullbright);
236 R_RegisterModule("GL_Main", glmain_start, glmain_shutdown);
239 extern void GL_Draw_Init();
240 extern void GL_Main_Init();
241 extern void GL_Models_Init();
242 extern void GL_Poly_Init();
243 extern void GL_Surf_Init();
244 extern void GL_Screen_Init();
245 extern void GL_Misc_Init();
246 extern void R_Crosshairs_Init();
247 extern void R_Light_Init();
248 extern void R_Particles_Init();
267 void R_RotateForEntity (entity_t *e)
269 glTranslatef (e->origin[0], e->origin[1], e->origin[2]);
271 glRotatef (e->angles[1], 0, 0, 1);
272 glRotatef (-e->angles[0], 0, 1, 0);
273 glRotatef (e->angles[2], 1, 0, 0);
275 glScalef (e->scale, e->scale, e->scale); // LordHavoc: model scale
279 // LordHavoc: if not for the fact BRIGHTFIELD particles require this, it would be removed...
280 #define NUMVERTEXNORMALS 162
282 float r_avertexnormals[NUMVERTEXNORMALS][3] = {
286 // LordHavoc: moved this shading stuff up because the sprites need shading stuff
292 extern void R_LightPoint (vec3_t color, vec3_t p);
293 extern void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
294 extern void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
297 =============================================================
301 =============================================================
309 void R_GetSpriteFrame (entity_t *currententity, mspriteframe_t **oldframe, mspriteframe_t **newframe, float *framelerp)
312 mspritegroup_t *pspritegroup;
313 int i, j, numframes, frame;
314 float *pintervals, fullinterval, targettime, time, jtime, jinterval;
316 psprite = currententity->model->cache.data;
317 frame = currententity->frame;
319 if ((frame >= psprite->numframes) || (frame < 0))
321 Con_Printf ("R_DrawSprite: no such frame %d\n", frame);
325 if (psprite->frames[frame].type == SPR_SINGLE)
327 if (currententity->draw_lastmodel == currententity->model && currententity->draw_lerpstart < cl.time)
329 if (frame != currententity->draw_pose)
331 currententity->draw_lastpose = currententity->draw_pose;
332 currententity->draw_pose = frame;
333 currententity->draw_lerpstart = cl.time;
337 *framelerp = (cl.time - currententity->draw_lerpstart) * 10.0;
339 else // uninitialized
341 currententity->draw_lastmodel = currententity->model;
342 currententity->draw_lastpose = currententity->draw_pose = frame;
343 currententity->draw_lerpstart = cl.time;
346 *oldframe = psprite->frames[currententity->draw_lastpose].frameptr;
347 *newframe = psprite->frames[frame].frameptr;
351 pspritegroup = (mspritegroup_t *)psprite->frames[frame].frameptr;
352 pintervals = pspritegroup->intervals;
353 numframes = pspritegroup->numframes;
354 fullinterval = pintervals[numframes-1];
356 time = cl.time + currententity->syncbase;
358 // when loading in Mod_LoadSpriteGroup, we guaranteed all interval values
359 // are positive, so we don't have to worry about division by 0
360 targettime = time - ((int)(time / fullinterval)) * fullinterval;
362 // LordHavoc: since I can't measure the time properly when it loops from numframes-1 to 0,
363 // I instead measure the time of the first frame, hoping it is consistent
364 j = numframes-1;jtime = 0;jinterval = pintervals[1] - pintervals[0];
365 for (i=0 ; i<(numframes-1) ; i++)
367 if (pintervals[i] > targettime)
369 j = i;jinterval = pintervals[i] - jtime;jtime = pintervals[i];
371 *framelerp = (targettime - jtime) / jinterval;
373 *oldframe = pspritegroup->frames[j];
374 *newframe = pspritegroup->frames[i];
378 void GL_DrawSpriteImage (mspriteframe_t *frame, vec3_t origin, vec3_t up, vec3_t right, int red, int green, int blue, int alpha)
380 // LordHavoc: rewrote this to use the transparent poly system
381 transpolybegin(frame->gl_texturenum, 0, frame->gl_fogtexturenum, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
382 transpolyvert(origin[0] + frame->down * up[0] + frame->left * right[0], origin[1] + frame->down * up[1] + frame->left * right[1], origin[2] + frame->down * up[2] + frame->left * right[2], 0, 1, red, green, blue, alpha);
383 transpolyvert(origin[0] + frame->up * up[0] + frame->left * right[0], origin[1] + frame->up * up[1] + frame->left * right[1], origin[2] + frame->up * up[2] + frame->left * right[2], 0, 0, red, green, blue, alpha);
384 transpolyvert(origin[0] + frame->up * up[0] + frame->right * right[0], origin[1] + frame->up * up[1] + frame->right * right[1], origin[2] + frame->up * up[2] + frame->right * right[2], 1, 0, red, green, blue, alpha);
385 transpolyvert(origin[0] + frame->down * up[0] + frame->right * right[0], origin[1] + frame->down * up[1] + frame->right * right[1], origin[2] + frame->down * up[2] + frame->right * right[2], 1, 1, red, green, blue, alpha);
389 extern qboolean isG200, isRagePro, lighthalf;
397 void R_DrawSpriteModel (entity_t *e)
399 mspriteframe_t *oldframe, *newframe;
400 float *up, *right, lerp, ilerp;
401 vec3_t v_forward, v_right, v_up, org;
404 // don't even bother culling, because it's just a single
405 // polygon without a surface cache
406 R_GetSpriteFrame (e, &oldframe, &newframe, &lerp);
407 if (lerp < 0) lerp = 0;
408 if (lerp > 1) lerp = 1;
409 if (isRagePro) // LordHavoc: no alpha scaling supported on per pixel alpha images on ATI Rage Pro... ACK!
412 psprite = e->model->cache.data;
414 if (psprite->type == SPR_ORIENTED)
415 { // bullet marks on walls
416 AngleVectors (e->angles, v_forward, v_right, v_up);
419 VectorSubtract(e->origin, vpn, org);
425 VectorCopy(e->origin, org);
429 VectorScale(up, e->scale, up);
430 VectorScale(right, e->scale, right);
433 if (e->model->flags & EF_FULLBRIGHT || e->effects & EF_FULLBRIGHT)
435 shadecolor[0] = e->colormod[0] * 255;
436 shadecolor[1] = e->colormod[1] * 255;
437 shadecolor[2] = e->colormod[2] * 255;
441 R_LightPoint (shadecolor, e->origin);
442 R_DynamicLightPointNoMask(shadecolor, e->origin);
445 // LordHavoc: interpolated sprite rendering
447 GL_DrawSpriteImage(oldframe, org, up, right, shadecolor[0],shadecolor[1],shadecolor[2],e->alpha*255*ilerp);
449 GL_DrawSpriteImage(newframe, org, up, right, shadecolor[0],shadecolor[1],shadecolor[2],e->alpha*255*lerp);
452 //==================================================================================
454 void R_DrawBrushModel (entity_t *e);
461 // LordHavoc: split so bmodels are rendered before any other objects
462 void R_DrawEntitiesOnList1 (void)
466 if (!r_drawentities.value)
469 for (i=0 ; i<cl_numvisedicts ; i++)
471 if (cl_visedicts[i]->model->type != mod_brush)
473 currententity = cl_visedicts[i];
474 modelalpha = currententity->alpha;
476 R_DrawBrushModel (currententity);
480 void R_DrawEntitiesOnList2 (void)
484 if (!r_drawentities.value)
487 for (i=0 ; i<cl_numvisedicts ; i++)
489 currententity = cl_visedicts[i];
490 modelalpha = currententity->alpha;
492 switch (currententity->model->type)
495 R_DrawAliasModel (currententity, true, modelalpha, currententity->model, currententity->frame, currententity->skinnum, currententity->origin, currententity->effects, currententity->model->flags, currententity->colormap);
499 R_DrawSpriteModel (currententity);
513 void R_DrawViewModel (void)
515 if (!r_drawviewmodel.value || chase_active.value || envmap || !r_drawentities.value || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.model)
518 currententity = &cl.viewent;
519 currententity->alpha = modelalpha = cl_entities[cl.viewentity].alpha; // LordHavoc: if the player is transparent, so is his gun
520 currententity->effects = cl_entities[cl.viewentity].effects;
521 currententity->scale = 1;
522 VectorCopy(cl_entities[cl.viewentity].colormod, currententity->colormod);
524 // hack the depth range to prevent view model from poking into walls
525 glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
526 R_DrawAliasModel (currententity, FALSE, modelalpha, currententity->model, currententity->frame, currententity->skinnum, currententity->origin, currententity->effects, currententity->model->flags, currententity->colormap);
527 glDepthRange (gldepthmin, gldepthmax);
530 void R_DrawBrushModel (entity_t *e);
532 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
534 void R_SetFrustum (void)
538 if (r_refdef.fov_x == 90)
540 // front side is visible
542 VectorAdd (vpn, vright, frustum[0].normal);
543 VectorSubtract (vpn, vright, frustum[1].normal);
545 VectorAdd (vpn, vup, frustum[2].normal);
546 VectorSubtract (vpn, vup, frustum[3].normal);
550 // rotate VPN right by FOV_X/2 degrees
551 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
552 // rotate VPN left by FOV_X/2 degrees
553 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
554 // rotate VPN up by FOV_X/2 degrees
555 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
556 // rotate VPN down by FOV_X/2 degrees
557 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
560 for (i=0 ; i<4 ; i++)
562 frustum[i].type = PLANE_ANYZ;
563 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
564 // frustum[i].signbits = SignbitsForPlane (&frustum[i]);
565 BoxOnPlaneSideClassify(&frustum[i]);
569 void R_AnimateLight (void);
570 void V_CalcBlend (void);
577 void R_SetupFrame (void)
579 // don't allow cheats in multiplayer
580 if (cl.maxclients > 1)
582 Cvar_Set ("r_fullbright", "0");
583 Cvar_Set ("r_ambient", "0");
590 // build the transformation matrix for the given view angles
591 VectorCopy (r_refdef.vieworg, r_origin);
593 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
596 r_oldviewleaf = r_viewleaf;
597 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
599 V_SetContentsColor (r_viewleaf->contents);
602 r_cache_thrash = false;
613 void MYgluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
615 GLdouble xmin, xmax, ymin, ymax;
617 ymax = zNear * tan( fovy * M_PI / 360.0 );
620 xmin = ymin * aspect;
621 xmax = ymax * aspect;
623 glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
627 extern char skyname[];
634 void R_SetupGL (void)
637 extern int glwidth, glheight;
638 int x, x2, y2, y, w, h;
643 glMatrixMode(GL_PROJECTION);
645 x = r_refdef.vrect.x * glwidth/vid.width;
646 x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
647 y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
648 y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
650 // fudge around because of frac screen scale
669 glViewport (glx + x, gly + y2, w, h);
670 screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
671 // yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
672 // if (skyname[0]) // skybox enabled?
673 // MYgluPerspective (r_refdef.fov_y, screenaspect, 4, r_skyboxsize.value*1.732050807569 + 256); // this is size*sqrt(3) + 256
675 MYgluPerspective (r_refdef.fov_y, screenaspect, 4, r_farclip.value);
677 glCullFace(GL_FRONT);
679 glMatrixMode(GL_MODELVIEW);
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]);
689 glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
694 // if (gl_cull.value)
695 glEnable(GL_CULL_FACE);
697 // glDisable(GL_CULL_FACE);
699 glEnable(GL_BLEND); // was Disable
700 glDisable(GL_ALPHA_TEST);
701 glAlphaFunc(GL_GREATER, 0.5);
702 glEnable(GL_DEPTH_TEST);
704 glShadeModel(GL_SMOOTH);
707 void R_DrawWorld (void);
708 //void R_RenderDlights (void);
709 void R_DrawParticles (void);
718 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
721 glDepthFunc (GL_LEQUAL);
723 glDepthRange (gldepthmin, gldepthmax);
726 // LordHavoc: my trick to *FIX* GLQuake lighting once and for all :)
729 glMatrixMode(GL_PROJECTION);
731 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
732 glMatrixMode(GL_MODELVIEW);
734 glDisable (GL_DEPTH_TEST);
735 glDisable (GL_CULL_FACE);
736 glDisable(GL_TEXTURE_2D);
738 glBlendFunc (GL_DST_COLOR, GL_ONE);
739 glBegin (GL_TRIANGLES);
741 glVertex2f (-5000, -5000);
742 glVertex2f (10000, -5000);
743 glVertex2f (-5000, 10000);
745 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
747 glEnable(GL_TEXTURE_2D);
748 glEnable (GL_DEPTH_TEST);
749 glEnable (GL_CULL_FACE);
752 extern cvar_t contrast;
753 extern cvar_t brightness;
754 extern cvar_t gl_lightmode;
758 glMatrixMode(GL_PROJECTION);
760 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
761 glMatrixMode(GL_MODELVIEW);
763 glDisable (GL_DEPTH_TEST);
764 glDisable (GL_CULL_FACE);
765 glDisable(GL_TEXTURE_2D);
769 glBlendFunc (GL_DST_COLOR, GL_ONE);
770 glBegin (GL_TRIANGLES);
772 glVertex2f (-5000, -5000);
773 glVertex2f (10000, -5000);
774 glVertex2f (-5000, 10000);
777 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
778 contrast.value = bound(0.2, contrast.value, 1.0);
779 if (/*gl_polyblend.value && */v_blend[3])
781 glBegin (GL_TRIANGLES);
782 glColor4fv (v_blend);
783 glVertex2f (-5000, -5000);
784 glVertex2f (10000, -5000);
785 glVertex2f (-5000, 10000);
789 glEnable (GL_CULL_FACE);
790 glEnable (GL_DEPTH_TEST);
792 glEnable(GL_TEXTURE_2D);
795 #define TIMEREPORT(DESC) \
796 if (r_speeds2.value)\
798 temptime = -currtime;\
799 currtime = Sys_FloatTime();\
800 temptime += currtime;\
801 Con_Printf(DESC " %.4fms ", temptime * 1000.0);\
808 r_refdef must be set before the first call
811 extern qboolean intimerefresh;
812 extern qboolean skyisvisible;
814 extern void UploadLightmaps();
815 void R_RenderView (void)
817 // double currtime, temptime;
818 // if (r_norefresh.value)
821 if (!r_worldentity.model || !cl.worldmodel)
822 Sys_Error ("R_RenderView: NULL worldmodel");
824 lighthalf = gl_lightmode.value;
828 // if (r_speeds2.value)
830 // currtime = Sys_FloatTime();
831 // Con_Printf("render time: ");
834 // TIMEREPORT("R_Clear")
836 // render normal view
845 skyisvisible = false;
847 R_MarkLeaves (); // done here so we know if we're in water
848 R_DrawWorld (); // adds static entities to the list
850 S_ExtraUpdate (); // don't let sound get messed up if going slow
851 R_DrawEntitiesOnList1 (); // BSP models
853 skypolyrender(); // fogged sky polys, affects depth
854 if (skyname[0] && skyisvisible && !fogenabled)
855 R_Sky(); // does not affect depth, draws over the sky polys
860 R_DrawEntitiesOnList2 (); // other models
861 // R_RenderDlights ();
869 // if (r_speeds2.value)