2010-02-15 23:26:55 +00:00
/*
Copyright ( C ) 1996 - 2001 Id Software , Inc .
Copyright ( C ) 2002 - 2009 John Fitzgibbons and others
2014-09-22 08:55:46 +00:00
Copyright ( C ) 2010 - 2014 QuakeSpasm developers
2010-02-15 23:26:55 +00:00
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
See the GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
// r_main.c
# include "quakedef.h"
2021-06-25 18:15:10 +00:00
# include "glquake.h"
2010-02-15 23:26:55 +00:00
vec3_t modelorg , r_entorigin ;
2021-09-03 07:18:27 +00:00
static entity_t r_worldentity ; //so we can make sure currententity is valid
2010-02-15 23:26:55 +00:00
entity_t * currententity ;
int r_visframecount ; // bumped when going to a new PVS
int r_framecount ; // used for dlight push checking
mplane_t frustum [ 4 ] ;
//johnfitz -- rendering statistics
2022-07-05 05:50:02 +00:00
int rs_brushpolys , rs_aliaspolys , rs_skypolys ;
2010-02-15 23:26:55 +00:00
int rs_dynamiclightmaps , rs_brushpasses , rs_aliaspasses , rs_skypasses ;
//
// view origin
//
vec3_t vup ;
vec3_t vpn ;
vec3_t vright ;
vec3_t r_origin ;
float r_fovx , r_fovy ; //johnfitz -- rendering fov may be different becuase of r_waterwarp and r_stereo
//
// screen size info
//
refdef_t r_refdef ;
mleaf_t * r_viewleaf , * r_oldviewleaf ;
2019-09-10 14:41:11 +00:00
int d_lightstylevalue [ MAX_LIGHTSTYLES ] ; // 8.8 fraction of base light value
2010-02-15 23:26:55 +00:00
2011-12-28 22:01:33 +00:00
cvar_t r_norefresh = { " r_norefresh " , " 0 " , CVAR_NONE } ;
cvar_t r_drawentities = { " r_drawentities " , " 1 " , CVAR_NONE } ;
cvar_t r_drawviewmodel = { " r_drawviewmodel " , " 1 " , CVAR_NONE } ;
cvar_t r_speeds = { " r_speeds " , " 0 " , CVAR_NONE } ;
2015-09-12 03:29:41 +00:00
cvar_t r_pos = { " r_pos " , " 0 " , CVAR_NONE } ;
2011-12-28 22:01:33 +00:00
cvar_t r_fullbright = { " r_fullbright " , " 0 " , CVAR_NONE } ;
cvar_t r_lightmap = { " r_lightmap " , " 0 " , CVAR_NONE } ;
cvar_t r_shadows = { " r_shadows " , " 0 " , CVAR_ARCHIVE } ;
cvar_t r_wateralpha = { " r_wateralpha " , " 1 " , CVAR_ARCHIVE } ;
cvar_t r_dynamic = { " r_dynamic " , " 1 " , CVAR_ARCHIVE } ;
cvar_t r_novis = { " r_novis " , " 0 " , CVAR_ARCHIVE } ;
cvar_t gl_finish = { " gl_finish " , " 0 " , CVAR_NONE } ;
2016-01-29 20:47:24 +00:00
cvar_t gl_clear = { " gl_clear " , " 1 " , CVAR_NONE } ;
2011-12-28 22:01:33 +00:00
cvar_t gl_cull = { " gl_cull " , " 1 " , CVAR_NONE } ;
cvar_t gl_smoothmodels = { " gl_smoothmodels " , " 1 " , CVAR_NONE } ;
cvar_t gl_affinemodels = { " gl_affinemodels " , " 0 " , CVAR_NONE } ;
cvar_t gl_polyblend = { " gl_polyblend " , " 1 " , CVAR_NONE } ;
cvar_t gl_flashblend = { " gl_flashblend " , " 0 " , CVAR_ARCHIVE } ;
cvar_t gl_playermip = { " gl_playermip " , " 0 " , CVAR_NONE } ;
cvar_t gl_nocolors = { " gl_nocolors " , " 0 " , CVAR_NONE } ;
2010-02-15 23:26:55 +00:00
//johnfitz -- new cvars
2011-12-28 22:01:33 +00:00
cvar_t r_stereo = { " r_stereo " , " 0 " , CVAR_NONE } ;
cvar_t r_stereodepth = { " r_stereodepth " , " 128 " , CVAR_NONE } ;
cvar_t r_clearcolor = { " r_clearcolor " , " 2 " , CVAR_ARCHIVE } ;
cvar_t r_drawflat = { " r_drawflat " , " 0 " , CVAR_NONE } ;
cvar_t r_flatlightstyles = { " r_flatlightstyles " , " 0 " , CVAR_NONE } ;
cvar_t gl_fullbrights = { " gl_fullbrights " , " 1 " , CVAR_ARCHIVE } ;
cvar_t gl_farclip = { " gl_farclip " , " 16384 " , CVAR_ARCHIVE } ;
cvar_t gl_overbright = { " gl_overbright " , " 1 " , CVAR_ARCHIVE } ;
cvar_t gl_overbright_models = { " gl_overbright_models " , " 1 " , CVAR_ARCHIVE } ;
cvar_t r_oldskyleaf = { " r_oldskyleaf " , " 0 " , CVAR_NONE } ;
cvar_t r_drawworld = { " r_drawworld " , " 1 " , CVAR_NONE } ;
cvar_t r_showtris = { " r_showtris " , " 0 " , CVAR_NONE } ;
cvar_t r_showbboxes = { " r_showbboxes " , " 0 " , CVAR_NONE } ;
2021-09-06 09:20:58 +00:00
cvar_t r_lerpmodels = { " r_lerpmodels " , " 1 " , CVAR_ARCHIVE } ;
cvar_t r_lerpmove = { " r_lerpmove " , " 1 " , CVAR_ARCHIVE } ;
2011-12-28 22:01:33 +00:00
cvar_t r_nolerp_list = { " r_nolerp_list " , " progs/flame.mdl,progs/flame2.mdl,progs/braztall.mdl,progs/brazshrt.mdl,progs/longtrch.mdl,progs/flame_pyre.mdl,progs/v_saw.mdl,progs/v_xfist.mdl,progs/h2stuff/newfire.mdl " , CVAR_NONE } ;
2015-05-18 18:00:49 +00:00
cvar_t r_noshadow_list = { " r_noshadow_list " , " progs/flame2.mdl,progs/flame.mdl,progs/bolt1.mdl,progs/bolt2.mdl,progs/bolt3.mdl,progs/laser.mdl " , CVAR_NONE } ;
chase.c, cl_input.c, cl_parse.c, client.h, common.c, common.h, console.h,
cvar.h, draw.h, gl_draw.c, gl_fog.c, gl_mesh.c, gl_model.c, gl_model.h,
gl_rmain.c, gl_rmisc.c, gl_screen.c, gl_sky.c, gl_texmgr.c, glquake.h,
host.c, keys.c, keys.h, main.c, menu.c, menu.h, pr_cmds.c, quakedef.h,
r_alias.c, r_brush.c, r_part.c, r_sprite.c, r_world.c, sbar.c, sbar.h,
screen.h, snd_dma.c, snd_mem.c, snd_mix.c, sv_main.c, sys_sdl.c, vid.h,
view.h, world.c, world.h: Loads of warning fixes about missing function
prototypes, missing parens around &, missing braces leading to ambiguous
else statements and unused and uninitialized variables. There are still a
couple of unitialised variables here and there, but not much. The warnings
about strict aliasing violations need taking care of.
git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@21 af15c1b1-3010-417e-b628-4374ebc0bcbd
2010-02-16 12:01:07 +00:00
extern cvar_t r_vfog ;
2010-02-15 23:26:55 +00:00
//johnfitz
2010-04-22 19:02:29 +00:00
2014-09-07 19:35:52 +00:00
cvar_t gl_zfix = { " gl_zfix " , " 0 " , CVAR_NONE } ; // QuakeSpasm z-fighting fix
2011-08-24 07:47:29 +00:00
new cvars: r_lavaalpha, r_slimealpha, r_telealpha for fine-tuning specific liquid opacities (from DirectQ, RMQEngine)
new worldspawn keys: _wateralpha, _lavaalpha, _slimealpha, _telealpha, _skyfog (unique to Quakespasm)
The lava/slime/telealpha cvars are non-archived, and default to 0, which means to use the value of r_wateralpha, so they have no effect by default.
The worldspawn keys allow custom maps to set these values in a way that only applies while the map is loaded, and doesn't change the cvar value. (similar to the behaviour of the "fog" worldspawn key.) They are accepted with or without the underscore, like "fog".
see also:
http://forums.insideqc.com/viewtopic.php?f=3&t=5532
http://celephais.net/board/view_thread.php?id=60452&start=937
git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1238 af15c1b1-3010-417e-b628-4374ebc0bcbd
2015-07-26 21:51:25 +00:00
cvar_t r_lavaalpha = { " r_lavaalpha " , " 0 " , CVAR_NONE } ;
cvar_t r_telealpha = { " r_telealpha " , " 0 " , CVAR_NONE } ;
cvar_t r_slimealpha = { " r_slimealpha " , " 0 " , CVAR_NONE } ;
float map_wateralpha , map_lavaalpha , map_telealpha , map_slimealpha ;
2017-09-17 02:12:53 +00:00
float map_fallbackalpha ;
new cvars: r_lavaalpha, r_slimealpha, r_telealpha for fine-tuning specific liquid opacities (from DirectQ, RMQEngine)
new worldspawn keys: _wateralpha, _lavaalpha, _slimealpha, _telealpha, _skyfog (unique to Quakespasm)
The lava/slime/telealpha cvars are non-archived, and default to 0, which means to use the value of r_wateralpha, so they have no effect by default.
The worldspawn keys allow custom maps to set these values in a way that only applies while the map is loaded, and doesn't change the cvar value. (similar to the behaviour of the "fog" worldspawn key.) They are accepted with or without the underscore, like "fog".
see also:
http://forums.insideqc.com/viewtopic.php?f=3&t=5532
http://celephais.net/board/view_thread.php?id=60452&start=937
git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1238 af15c1b1-3010-417e-b628-4374ebc0bcbd
2015-07-26 21:51:25 +00:00
2010-04-22 19:02:29 +00:00
qboolean r_drawflat_cheatsafe , r_fullbright_cheatsafe , r_lightmap_cheatsafe , r_drawworld_cheatsafe ; //johnfitz
2010-02-15 23:26:55 +00:00
2017-05-26 20:37:48 +00:00
cvar_t r_scale = { " r_scale " , " 1 " , CVAR_ARCHIVE } ;
2015-01-26 20:18:45 +00:00
//==============================================================================
//
// GLSL GAMMA CORRECTION
//
//==============================================================================
static GLuint r_gamma_texture ;
static GLuint r_gamma_program ;
2015-06-15 21:17:39 +00:00
static int r_gamma_texture_width , r_gamma_texture_height ;
2015-01-26 20:18:45 +00:00
// uniforms used in gamma shader
static GLuint gammaLoc ;
2016-02-12 23:26:36 +00:00
static GLuint contrastLoc ;
2015-01-26 20:18:45 +00:00
static GLuint textureLoc ;
/*
= = = = = = = = = = = = =
GLSLGamma_DeleteTexture
= = = = = = = = = = = = =
*/
void GLSLGamma_DeleteTexture ( void )
{
glDeleteTextures ( 1 , & r_gamma_texture ) ;
r_gamma_texture = 0 ;
r_gamma_program = 0 ; // deleted in R_DeleteShaders
}
/*
= = = = = = = = = = = = =
GLSLGamma_CreateShaders
= = = = = = = = = = = = =
*/
static void GLSLGamma_CreateShaders ( void )
{
const GLchar * vertSource = \
" #version 110 \n "
" \n "
" void main(void) { \n "
" gl_Position = vec4(gl_Vertex.xy, 0.0, 1.0); \n "
" gl_TexCoord[0] = gl_MultiTexCoord0; \n "
" } \n " ;
const GLchar * fragSource = \
" #version 110 \n "
" \n "
" uniform sampler2D GammaTexture; \n "
" uniform float GammaValue; \n "
2016-02-12 23:26:36 +00:00
" uniform float ContrastValue; \n "
2015-01-26 20:18:45 +00:00
" \n "
" void main(void) { \n "
" vec4 frag = texture2D(GammaTexture, gl_TexCoord[0].xy); \n "
2016-02-12 23:26:36 +00:00
" frag.rgb = frag.rgb * ContrastValue; \n "
2015-01-26 20:18:45 +00:00
" gl_FragColor = vec4(pow(frag.rgb, vec3(GammaValue)), 1.0); \n "
" } \n " ;
if ( ! gl_glsl_gamma_able )
return ;
r_gamma_program = GL_CreateProgram ( vertSource , fragSource , 0 , NULL ) ;
// get uniform locations
gammaLoc = GL_GetUniformLocation ( & r_gamma_program , " GammaValue " ) ;
2016-02-12 23:26:36 +00:00
contrastLoc = GL_GetUniformLocation ( & r_gamma_program , " ContrastValue " ) ;
2015-01-26 20:18:45 +00:00
textureLoc = GL_GetUniformLocation ( & r_gamma_program , " GammaTexture " ) ;
}
/*
= = = = = = = = = = = = =
GLSLGamma_GammaCorrect
= = = = = = = = = = = = =
*/
void GLSLGamma_GammaCorrect ( void )
{
2020-06-19 20:23:04 +00:00
int tw = glwidth , th = glheight ;
2015-06-15 21:17:39 +00:00
float smax , tmax ;
2015-01-26 20:18:45 +00:00
if ( ! gl_glsl_gamma_able )
return ;
2016-02-12 23:26:36 +00:00
if ( vid_gamma . value = = 1 & & vid_contrast . value = = 1 )
2015-01-26 20:18:45 +00:00
return ;
// create render-to-texture texture if needed
if ( ! r_gamma_texture )
{
glGenTextures ( 1 , & r_gamma_texture ) ;
2020-06-19 20:23:04 +00:00
r_gamma_texture_width = 0 ;
r_gamma_texture_height = 0 ;
}
GL_DisableMultitexture ( ) ;
glBindTexture ( GL_TEXTURE_2D , r_gamma_texture ) ;
2015-06-15 21:17:39 +00:00
2020-06-19 20:23:04 +00:00
if ( ! gl_texture_NPOT )
{
tw = TexMgr_Pad ( tw ) ;
th = TexMgr_Pad ( th ) ;
}
if ( r_gamma_texture_width ! = tw | | r_gamma_texture_height ! = th )
{
r_gamma_texture_width = tw ;
r_gamma_texture_height = th ;
2015-06-15 21:17:39 +00:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA8 , r_gamma_texture_width , r_gamma_texture_height , 0 , GL_BGRA , GL_UNSIGNED_INT_8_8_8_8_REV , NULL ) ;
2015-03-08 21:13:26 +00:00
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ) ;
2015-01-26 20:18:45 +00:00
}
// create shader if needed
if ( ! r_gamma_program )
{
GLSLGamma_CreateShaders ( ) ;
if ( ! r_gamma_program )
{
Sys_Error ( " GLSLGamma_CreateShaders failed " ) ;
}
}
2022-03-11 20:55:10 +00:00
2015-01-26 20:18:45 +00:00
// copy the framebuffer to the texture
glCopyTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 , glx , gly , glwidth , glheight ) ;
// draw the texture back to the framebuffer with a fragment shader
GL_UseProgramFunc ( r_gamma_program ) ;
GL_Uniform1fFunc ( gammaLoc , vid_gamma . value ) ;
2022-06-12 11:32:28 +00:00
GL_Uniform1fFunc ( contrastLoc , q_min ( 2.0f , q_max ( 1.0f , vid_contrast . value ) ) ) ;
2015-01-26 20:18:45 +00:00
GL_Uniform1iFunc ( textureLoc , 0 ) ; // use texture unit 0
glDisable ( GL_ALPHA_TEST ) ;
glDisable ( GL_DEPTH_TEST ) ;
glViewport ( glx , gly , glwidth , glheight ) ;
2015-06-15 21:17:39 +00:00
smax = glwidth / ( float ) r_gamma_texture_width ;
tmax = glheight / ( float ) r_gamma_texture_height ;
2015-01-26 20:18:45 +00:00
glBegin ( GL_QUADS ) ;
glTexCoord2f ( 0 , 0 ) ;
glVertex2f ( - 1 , - 1 ) ;
2015-06-15 21:17:39 +00:00
glTexCoord2f ( smax , 0 ) ;
2015-01-26 20:18:45 +00:00
glVertex2f ( 1 , - 1 ) ;
2015-06-15 21:17:39 +00:00
glTexCoord2f ( smax , tmax ) ;
2015-01-26 20:18:45 +00:00
glVertex2f ( 1 , 1 ) ;
2015-06-15 21:17:39 +00:00
glTexCoord2f ( 0 , tmax ) ;
2015-01-26 20:18:45 +00:00
glVertex2f ( - 1 , 1 ) ;
glEnd ( ) ;
2022-03-11 20:55:10 +00:00
2015-01-26 20:18:45 +00:00
GL_UseProgramFunc ( 0 ) ;
2022-03-11 20:55:10 +00:00
2015-01-26 20:18:45 +00:00
// clear cached binding
2015-12-13 07:23:04 +00:00
GL_ClearBindings ( ) ;
2015-01-26 20:18:45 +00:00
}
2010-02-15 23:26:55 +00:00
/*
= = = = = = = = = = = = = = = = =
R_CullBox - - johnfitz - - replaced with new function from lordhavoc
Returns true if the box is completely outside the frustum
= = = = = = = = = = = = = = = = =
*/
qboolean R_CullBox ( vec3_t emins , vec3_t emaxs )
{
int i ;
mplane_t * p ;
2021-07-07 20:55:50 +00:00
byte signbits ;
float vec [ 3 ] ;
2022-03-11 20:55:10 +00:00
2010-02-15 23:26:55 +00:00
for ( i = 0 ; i < 4 ; i + + )
{
p = frustum + i ;
2021-07-07 20:55:50 +00:00
signbits = p - > signbits ;
vec [ 0 ] = ( ( signbits % 2 ) < 1 ) ? emaxs [ 0 ] : emins [ 0 ] ;
vec [ 1 ] = ( ( signbits % 4 ) < 2 ) ? emaxs [ 1 ] : emins [ 1 ] ;
vec [ 2 ] = ( ( signbits % 8 ) < 4 ) ? emaxs [ 2 ] : emins [ 2 ] ;
if ( p - > normal [ 0 ] * vec [ 0 ] + p - > normal [ 1 ] * vec [ 1 ] + p - > normal [ 2 ] * vec [ 2 ] < p - > dist )
return true ;
2010-02-15 23:26:55 +00:00
}
return false ;
}
2021-07-07 20:55:50 +00:00
2010-02-15 23:26:55 +00:00
/*
= = = = = = = = = = = = = = =
R_CullModelForEntity - - johnfitz - - uses correct bounds based on rotation
= = = = = = = = = = = = = = =
*/
qboolean R_CullModelForEntity ( entity_t * e )
{
vec3_t mins , maxs ;
2022-08-02 00:39:04 +00:00
float scale = e - > netstate . scale / 16.0 ;
2010-02-15 23:26:55 +00:00
if ( e - > angles [ 0 ] | | e - > angles [ 2 ] ) //pitch or roll
{
2022-08-02 00:39:04 +00:00
VectorMA ( e - > origin , scale , e - > model - > rmins , mins ) ;
VectorMA ( e - > origin , scale , e - > model - > rmaxs , maxs ) ;
2010-02-15 23:26:55 +00:00
}
else if ( e - > angles [ 1 ] ) //yaw
{
2022-08-02 00:39:04 +00:00
VectorMA ( e - > origin , scale , e - > model - > ymins , mins ) ;
VectorMA ( e - > origin , scale , e - > model - > ymaxs , maxs ) ;
2010-02-15 23:26:55 +00:00
}
else //no rotation
{
2022-08-02 00:39:04 +00:00
VectorMA ( e - > origin , scale , e - > model - > mins , mins ) ;
VectorMA ( e - > origin , scale , e - > model - > maxs , maxs ) ;
2010-02-15 23:26:55 +00:00
}
return R_CullBox ( mins , maxs ) ;
}
/*
= = = = = = = = = = = = = = =
R_RotateForEntity - - johnfitz - - modified to take origin and angles instead of pointer to entity
= = = = = = = = = = = = = = =
*/
2017-09-17 02:12:53 +00:00
void R_RotateForEntity ( vec3_t origin , vec3_t angles , unsigned char scale )
2010-02-15 23:26:55 +00:00
{
glTranslatef ( origin [ 0 ] , origin [ 1 ] , origin [ 2 ] ) ;
glRotatef ( angles [ 1 ] , 0 , 0 , 1 ) ;
glRotatef ( - angles [ 0 ] , 0 , 1 , 0 ) ;
glRotatef ( angles [ 2 ] , 1 , 0 , 0 ) ;
2017-09-17 02:12:53 +00:00
if ( scale ! = 16 )
glScalef ( scale / 16.0 , scale / 16.0 , scale / 16.0 ) ;
2010-02-15 23:26:55 +00:00
}
/*
= = = = = = = = = = = = =
GL_PolygonOffset - - johnfitz
negative offset moves polygon closer to camera
= = = = = = = = = = = = =
*/
void GL_PolygonOffset ( int offset )
{
if ( offset > 0 )
{
glEnable ( GL_POLYGON_OFFSET_FILL ) ;
glEnable ( GL_POLYGON_OFFSET_LINE ) ;
glPolygonOffset ( 1 , offset ) ;
}
else if ( offset < 0 )
{
glEnable ( GL_POLYGON_OFFSET_FILL ) ;
glEnable ( GL_POLYGON_OFFSET_LINE ) ;
glPolygonOffset ( - 1 , offset ) ;
}
else
{
glDisable ( GL_POLYGON_OFFSET_FILL ) ;
glDisable ( GL_POLYGON_OFFSET_LINE ) ;
}
}
//==============================================================================
//
// SETUP FRAME
//
//==============================================================================
int SignbitsForPlane ( mplane_t * out )
{
int bits , j ;
// for fast box on planeside test
bits = 0 ;
for ( j = 0 ; j < 3 ; j + + )
{
if ( out - > normal [ j ] < 0 )
bits | = 1 < < j ;
}
return bits ;
}
/*
= = = = = = = = = = = = = = =
TurnVector - - johnfitz
turn forward towards side on the plane defined by forward and side
if angle = 90 , the result will be equal to side
assumes side and forward are perpendicular , and normalized
to turn away from side , use a negative angle
= = = = = = = = = = = = = = =
*/
void TurnVector ( vec3_t out , const vec3_t forward , const vec3_t side , float angle )
{
float scale_forward , scale_side ;
scale_forward = cos ( DEG2RAD ( angle ) ) ;
scale_side = sin ( DEG2RAD ( angle ) ) ;
out [ 0 ] = scale_forward * forward [ 0 ] + scale_side * side [ 0 ] ;
out [ 1 ] = scale_forward * forward [ 1 ] + scale_side * side [ 1 ] ;
out [ 2 ] = scale_forward * forward [ 2 ] + scale_side * side [ 2 ] ;
}
/*
= = = = = = = = = = = = = = =
R_SetFrustum - - johnfitz - - rewritten
= = = = = = = = = = = = = = =
*/
void R_SetFrustum ( float fovx , float fovy )
{
int i ;
if ( r_stereo . value )
fovx + = 10 ; //silly hack so that polygons don't drop out becuase of stereo skew
TurnVector ( frustum [ 0 ] . normal , vpn , vright , fovx / 2 - 90 ) ; //left plane
TurnVector ( frustum [ 1 ] . normal , vpn , vright , 90 - fovx / 2 ) ; //right plane
TurnVector ( frustum [ 2 ] . normal , vpn , vup , 90 - fovy / 2 ) ; //bottom plane
TurnVector ( frustum [ 3 ] . normal , vpn , vup , fovy / 2 - 90 ) ; //top plane
for ( i = 0 ; i < 4 ; i + + )
{
frustum [ i ] . type = PLANE_ANYZ ;
frustum [ i ] . dist = DotProduct ( r_origin , frustum [ i ] . normal ) ; //FIXME: shouldn't this always be zero?
frustum [ i ] . signbits = SignbitsForPlane ( & frustum [ i ] ) ;
}
}
/*
= = = = = = = = = = = = =
GL_SetFrustum - - johnfitz - - written to replace MYgluPerspective
= = = = = = = = = = = = =
*/
# define NEARCLIP 4
float frustum_skew = 0.0 ; //used by r_stereo
2021-03-18 09:06:29 +00:00
/*void GL_SetFrustum(float fovx, float fovy)
2010-02-15 23:26:55 +00:00
{
float xmax , ymax ;
xmax = NEARCLIP * tan ( fovx * M_PI / 360.0 ) ;
ymax = NEARCLIP * tan ( fovy * M_PI / 360.0 ) ;
glFrustum ( - xmax + frustum_skew , xmax + frustum_skew , - ymax , ymax , NEARCLIP , gl_farclip . value ) ;
2021-03-18 09:06:29 +00:00
} */
2010-02-15 23:26:55 +00:00
/*
= = = = = = = = = = = = =
R_SetupGL
= = = = = = = = = = = = =
*/
void R_SetupGL ( void )
{
2017-05-26 20:37:48 +00:00
int scale ;
2010-02-15 23:26:55 +00:00
//johnfitz -- rewrote this section
2021-05-10 23:53:16 +00:00
if ( ! r_refdef . drawworld )
scale = 1 ; //don't rescale. we can't handle rescaling transparent parts.
else
scale = CLAMP ( 1 , ( int ) r_scale . value , 4 ) ; // ericw -- see R_ScaleView
2010-02-15 23:26:55 +00:00
glViewport ( glx + r_refdef . vrect . x ,
gly + glheight - r_refdef . vrect . y - r_refdef . vrect . height ,
2017-05-26 20:37:48 +00:00
r_refdef . vrect . width / scale ,
r_refdef . vrect . height / scale ) ;
2010-02-15 23:26:55 +00:00
//johnfitz
2021-03-18 12:33:44 +00:00
# if 1 //Spike: these should be equivelent. gpus tend not to use doubles in favour of speed, so no loss there.
2021-03-18 09:06:29 +00:00
{
mat4_t mat ;
Matrix4_ProjectionMatrix ( r_fovx , r_fovy , NEARCLIP , gl_farclip . value , false , frustum_skew , 0 , mat ) ;
2021-03-18 12:33:44 +00:00
glMatrixMode ( GL_PROJECTION ) ;
2021-03-18 09:06:29 +00:00
glLoadMatrixf ( mat ) ;
Matrix4_ViewMatrix ( r_refdef . viewangles , r_refdef . vieworg , mat ) ;
glMatrixMode ( GL_MODELVIEW ) ;
glLoadMatrixf ( mat ) ;
}
# else
2021-03-18 12:33:44 +00:00
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ( ) ;
2021-03-18 09:06:29 +00:00
GL_SetFrustum ( r_fovx , r_fovy ) ; //johnfitz -- use r_fov* vars
2010-02-15 23:26:55 +00:00
// glCullFace(GL_BACK); //johnfitz -- glquake used CCW with backwards culling -- let's do it right
glMatrixMode ( GL_MODELVIEW ) ;
glLoadIdentity ( ) ;
glRotatef ( - 90 , 1 , 0 , 0 ) ; // put Z going up
glRotatef ( 90 , 0 , 0 , 1 ) ; // put Z going up
glRotatef ( - r_refdef . viewangles [ 2 ] , 1 , 0 , 0 ) ;
glRotatef ( - r_refdef . viewangles [ 0 ] , 0 , 1 , 0 ) ;
glRotatef ( - r_refdef . viewangles [ 1 ] , 0 , 0 , 1 ) ;
glTranslatef ( - r_refdef . vieworg [ 0 ] , - r_refdef . vieworg [ 1 ] , - r_refdef . vieworg [ 2 ] ) ;
2021-03-18 09:06:29 +00:00
# endif
2010-02-15 23:26:55 +00:00
//
// set drawing parms
//
if ( gl_cull . value )
glEnable ( GL_CULL_FACE ) ;
else
glDisable ( GL_CULL_FACE ) ;
glDisable ( GL_BLEND ) ;
glDisable ( GL_ALPHA_TEST ) ;
glEnable ( GL_DEPTH_TEST ) ;
}
/*
= = = = = = = = = = = = =
R_Clear - - johnfitz - - rewritten and gutted
= = = = = = = = = = = = =
*/
void R_Clear ( void )
{
unsigned int clearbits ;
clearbits = GL_DEPTH_BUFFER_BIT ;
2015-04-16 23:57:45 +00:00
// from mh -- if we get a stencil buffer, we should clear it, even though we don't use it
if ( gl_stencilbits )
clearbits | = GL_STENCIL_BUFFER_BIT ;
2020-07-02 19:05:51 +00:00
if ( gl_clear . value & & ! skyroom_drawn & & r_refdef . drawworld )
2013-03-02 12:31:09 +00:00
clearbits | = GL_COLOR_BUFFER_BIT ;
2010-02-15 23:26:55 +00:00
glClear ( clearbits ) ;
}
/*
= = = = = = = = = = = = = = =
R_SetupScene - - johnfitz - - this is the stuff that needs to be done once per eye in stereo mode
= = = = = = = = = = = = = = =
*/
void R_SetupScene ( void )
{
R_SetupGL ( ) ;
}
/*
= = = = = = = = = = = = = = =
R_SetupView - - johnfitz - - this is the stuff that needs to be done once per frame , even in stereo mode
= = = = = = = = = = = = = = =
*/
void R_SetupView ( void )
{
2019-09-13 17:51:16 +00:00
int viewcontents ; //spike -- rewrote this a little
int i ;
2021-09-11 03:49:40 +00:00
2021-09-04 17:28:24 +00:00
// Need to do those early because we now update dynamic light maps during R_MarkSurfaces
R_PushDlights ( ) ;
R_AnimateLight ( ) ;
r_framecount + + ;
2010-02-15 23:26:55 +00:00
Fog_SetupFrame ( ) ; //johnfitz
// build the transformation matrix for the given view angles
VectorCopy ( r_refdef . vieworg , r_origin ) ;
AngleVectors ( r_refdef . viewangles , vpn , vright , vup ) ;
2020-07-02 19:05:51 +00:00
if ( r_refdef . drawworld )
{
2010-02-15 23:26:55 +00:00
// current viewleaf
2020-07-02 19:05:51 +00:00
r_oldviewleaf = r_viewleaf ;
r_viewleaf = Mod_PointInLeaf ( r_origin , cl . worldmodel ) ;
viewcontents = r_viewleaf - > contents ;
2019-09-13 17:51:16 +00:00
2020-07-02 19:05:51 +00:00
//spike -- FTE_ENT_SKIN_CONTENTS -- added this loop for moving water volumes, to avoid v_cshift etc hacks.
for ( i = 0 ; i < cl . num_entities & & viewcontents = = CONTENTS_EMPTY ; i + + )
2019-09-13 17:51:16 +00:00
{
2020-07-02 19:05:51 +00:00
mleaf_t * subleaf ;
vec3_t relpos ;
if ( cl . entities [ i ] . model & & cl . entities [ i ] . model - > type = = mod_brush )
{
VectorSubtract ( r_origin , cl . entities [ i ] . origin , relpos ) ;
if ( cl . entities [ i ] . angles [ 0 ] | | cl . entities [ i ] . angles [ 1 ] | | cl . entities [ i ] . angles [ 2 ] )
{ //rotate the point, just in case.
vec3_t axis [ 3 ] , t ;
AngleVectors ( cl . entities [ i ] . angles , axis [ 0 ] , axis [ 1 ] , axis [ 2 ] ) ;
VectorCopy ( relpos , t ) ;
relpos [ 0 ] = DotProduct ( t , axis [ 0 ] ) ;
relpos [ 0 ] = - DotProduct ( t , axis [ 1 ] ) ;
relpos [ 0 ] = DotProduct ( t , axis [ 2 ] ) ;
}
subleaf = Mod_PointInLeaf ( relpos , cl . entities [ i ] . model ) ;
if ( ( char ) cl . entities [ i ] . skinnum < 0 )
viewcontents = ( ( subleaf - > contents = = CONTENTS_SOLID ) ? ( char ) cl . entities [ i ] . skinnum : CONTENTS_EMPTY ) ;
else
viewcontents = subleaf - > contents ;
2019-09-13 18:04:49 +00:00
}
2019-09-13 17:51:16 +00:00
}
}
2020-07-02 19:05:51 +00:00
else
viewcontents = CONTENTS_EMPTY ;
2010-02-15 23:26:55 +00:00
2019-09-13 17:51:16 +00:00
V_SetContentsColor ( viewcontents ) ;
2010-02-15 23:26:55 +00:00
V_CalcBlend ( ) ;
//johnfitz -- calculate r_fovx and r_fovy here
r_fovx = r_refdef . fov_x ;
r_fovy = r_refdef . fov_y ;
if ( r_waterwarp . value )
{
2019-09-13 17:51:16 +00:00
if ( viewcontents = = CONTENTS_WATER | | viewcontents = = CONTENTS_SLIME | | viewcontents = = CONTENTS_LAVA )
2010-02-15 23:26:55 +00:00
{
//variance is a percentage of width, where width = 2 * tan(fov / 2) otherwise the effect is too dramatic at high FOV and too subtle at low FOV. what a mess!
r_fovx = atan ( tan ( DEG2RAD ( r_refdef . fov_x ) / 2 ) * ( 0.97 + sin ( cl . time * 1.5 ) * 0.03 ) ) * 2 / M_PI_DIV_180 ;
r_fovy = atan ( tan ( DEG2RAD ( r_refdef . fov_y ) / 2 ) * ( 1.03 - sin ( cl . time * 1.5 ) * 0.03 ) ) * 2 / M_PI_DIV_180 ;
}
}
//johnfitz
R_SetFrustum ( r_fovx , r_fovy ) ; //johnfitz -- use r_fov* vars
2020-07-02 19:05:51 +00:00
if ( r_refdef . drawworld )
{
2021-09-03 07:18:27 +00:00
currententity = & r_worldentity ;
2020-07-02 19:05:51 +00:00
R_MarkSurfaces ( ) ; //johnfitz -- create texture chains from PVS
2021-09-03 07:18:27 +00:00
currententity = NULL ;
2020-07-02 19:05:51 +00:00
}
2010-02-15 23:26:55 +00:00
R_Clear ( ) ;
//johnfitz -- cheat-protect some draw modes
r_drawflat_cheatsafe = r_fullbright_cheatsafe = r_lightmap_cheatsafe = false ;
2020-07-02 19:05:51 +00:00
r_drawworld_cheatsafe = r_refdef . drawworld ;
if ( cl . maxclients = = 1 & & r_refdef . drawworld )
2010-02-15 23:26:55 +00:00
{
if ( ! r_drawworld . value ) r_drawworld_cheatsafe = false ;
if ( r_drawflat . value ) r_drawflat_cheatsafe = true ;
else if ( r_fullbright . value | | ! cl . worldmodel - > lightdata ) r_fullbright_cheatsafe = true ;
else if ( r_lightmap . value ) r_lightmap_cheatsafe = true ;
}
//johnfitz
}
//==============================================================================
//
// RENDER VIEW
//
//==============================================================================
/*
= = = = = = = = = = = = =
R_DrawEntitiesOnList
= = = = = = = = = = = = =
*/
void R_DrawEntitiesOnList ( qboolean alphapass ) //johnfitz -- added parameter
{
int i ;
if ( ! r_drawentities . value )
return ;
//johnfitz -- sprites are not a special case
for ( i = 0 ; i < cl_numvisedicts ; i + + )
{
2015-06-24 18:22:56 +00:00
currententity = cl_visedicts [ i ] ;
2010-02-15 23:26:55 +00:00
//johnfitz -- if alphapass is true, draw only alpha entites this time
//if alphapass is false, draw only nonalpha entities this time
if ( ( ENTALPHA_DECODE ( currententity - > alpha ) < 1 & & ! alphapass ) | |
( ENTALPHA_DECODE ( currententity - > alpha ) = = 1 & & alphapass ) )
continue ;
//johnfitz -- chasecam
2017-09-17 02:12:53 +00:00
if ( currententity = = & cl . entities [ cl . viewentity ] )
2010-02-15 23:26:55 +00:00
currententity - > angles [ 0 ] * = 0.3 ;
//johnfitz
2019-01-30 01:53:15 +00:00
//spike -- this would be more efficient elsewhere, but its more correct here.
if ( currententity - > eflags & EFLAGS_EXTERIORMODEL )
continue ;
2010-02-15 23:26:55 +00:00
switch ( currententity - > model - > type )
{
case mod_alias :
R_DrawAliasModel ( currententity ) ;
break ;
case mod_brush :
R_DrawBrushModel ( currententity ) ;
break ;
case mod_sprite :
R_DrawSpriteModel ( currententity ) ;
break ;
2017-09-17 02:12:53 +00:00
case mod_ext_invalid :
//nothing. could draw a blob instead.
break ;
2010-02-15 23:26:55 +00:00
}
}
}
/*
= = = = = = = = = = = = =
R_DrawViewModel - - johnfitz - - gutted
= = = = = = = = = = = = =
*/
void R_DrawViewModel ( void )
{
2021-06-25 18:15:10 +00:00
if ( ! r_drawviewmodel . value | | ! r_drawentities . value | | chase_active . value | | skyroom_drawing /*silly depthrange*/ )
2010-02-15 23:26:55 +00:00
return ;
if ( cl . items & IT_INVISIBILITY | | cl . stats [ STAT_HEALTH ] < = 0 )
return ;
currententity = & cl . viewent ;
if ( ! currententity - > model )
return ;
//johnfitz -- this fixes a crash
if ( currententity - > model - > type ! = mod_alias )
return ;
//johnfitz
// hack the depth range to prevent view model from poking into walls
glDepthRange ( 0 , 0.3 ) ;
R_DrawAliasModel ( currententity ) ;
glDepthRange ( 0 , 1 ) ;
}
/*
= = = = = = = = = = = = = = = =
R_EmitWirePoint - - johnfitz - - draws a wireframe cross shape for point entities
= = = = = = = = = = = = = = = =
*/
void R_EmitWirePoint ( vec3_t origin )
{
2022-06-12 11:32:28 +00:00
const int size = 8 ;
2010-02-15 23:26:55 +00:00
glBegin ( GL_LINES ) ;
glVertex3f ( origin [ 0 ] - size , origin [ 1 ] , origin [ 2 ] ) ;
glVertex3f ( origin [ 0 ] + size , origin [ 1 ] , origin [ 2 ] ) ;
glVertex3f ( origin [ 0 ] , origin [ 1 ] - size , origin [ 2 ] ) ;
glVertex3f ( origin [ 0 ] , origin [ 1 ] + size , origin [ 2 ] ) ;
glVertex3f ( origin [ 0 ] , origin [ 1 ] , origin [ 2 ] - size ) ;
glVertex3f ( origin [ 0 ] , origin [ 1 ] , origin [ 2 ] + size ) ;
glEnd ( ) ;
}
/*
= = = = = = = = = = = = = = = =
R_EmitWireBox - - johnfitz - - draws one axis aligned bounding box
= = = = = = = = = = = = = = = =
*/
void R_EmitWireBox ( vec3_t mins , vec3_t maxs )
{
glBegin ( GL_QUAD_STRIP ) ;
glVertex3f ( mins [ 0 ] , mins [ 1 ] , mins [ 2 ] ) ;
glVertex3f ( mins [ 0 ] , mins [ 1 ] , maxs [ 2 ] ) ;
glVertex3f ( maxs [ 0 ] , mins [ 1 ] , mins [ 2 ] ) ;
glVertex3f ( maxs [ 0 ] , mins [ 1 ] , maxs [ 2 ] ) ;
glVertex3f ( maxs [ 0 ] , maxs [ 1 ] , mins [ 2 ] ) ;
glVertex3f ( maxs [ 0 ] , maxs [ 1 ] , maxs [ 2 ] ) ;
glVertex3f ( mins [ 0 ] , maxs [ 1 ] , mins [ 2 ] ) ;
glVertex3f ( mins [ 0 ] , maxs [ 1 ] , maxs [ 2 ] ) ;
glVertex3f ( mins [ 0 ] , mins [ 1 ] , mins [ 2 ] ) ;
glVertex3f ( mins [ 0 ] , mins [ 1 ] , maxs [ 2 ] ) ;
glEnd ( ) ;
}
/*
= = = = = = = = = = = = = = = =
R_ShowBoundingBoxes - - johnfitz
draw bounding boxes - - the server - side boxes , not the renderer cullboxes
= = = = = = = = = = = = = = = =
*/
void R_ShowBoundingBoxes ( void )
{
extern edict_t * sv_player ;
vec3_t mins , maxs ;
edict_t * ed ;
int i ;
2019-01-27 16:50:19 +00:00
qcvm_t * oldvm ; //in case we ever draw a scene from within csqc.
2010-02-15 23:26:55 +00:00
if ( ! r_showbboxes . value | | cl . maxclients > 1 | | ! r_drawentities . value | | ! sv . active )
return ;
glDisable ( GL_DEPTH_TEST ) ;
glPolygonMode ( GL_FRONT_AND_BACK , GL_LINE ) ;
GL_PolygonOffset ( OFFSET_SHOWTRIS ) ;
glDisable ( GL_TEXTURE_2D ) ;
glDisable ( GL_CULL_FACE ) ;
glColor3f ( 1 , 1 , 1 ) ;
2019-01-27 16:50:19 +00:00
oldvm = qcvm ;
2020-10-16 00:31:37 +00:00
PR_SwitchQCVM ( NULL ) ;
2019-01-27 16:50:19 +00:00
PR_SwitchQCVM ( & sv . qcvm ) ;
2018-05-01 00:35:14 +00:00
for ( i = 0 , ed = NEXT_EDICT ( qcvm - > edicts ) ; i < qcvm - > num_edicts ; i + + , ed = NEXT_EDICT ( ed ) )
2010-02-15 23:26:55 +00:00
{
if ( ed = = sv_player )
continue ; //don't draw player's own bbox
// if (r_showbboxes.value != 2)
// if (!SV_VisibleToClient (sv_player, ed, sv.worldmodel))
// continue; //don't draw if not in pvs
if ( ed - > v . mins [ 0 ] = = ed - > v . maxs [ 0 ] & & ed - > v . mins [ 1 ] = = ed - > v . maxs [ 1 ] & & ed - > v . mins [ 2 ] = = ed - > v . maxs [ 2 ] )
{
//point entity
R_EmitWirePoint ( ed - > v . origin ) ;
}
else
{
//box entity
2021-09-08 01:44:57 +00:00
if ( ( ed - > v . solid = = SOLID_BSP | | ed - > v . solid = = SOLID_EXT_BSPTRIGGER ) & & ( ed - > v . angles [ 0 ] | | ed - > v . angles [ 1 ] | | ed - > v . angles [ 2 ] ) & & pr_checkextension . value )
2020-04-06 23:56:15 +00:00
R_EmitWireBox ( ed - > v . absmin , ed - > v . absmax ) ;
else
{
VectorAdd ( ed - > v . mins , ed - > v . origin , mins ) ;
VectorAdd ( ed - > v . maxs , ed - > v . origin , maxs ) ;
R_EmitWireBox ( mins , maxs ) ;
}
2010-02-15 23:26:55 +00:00
}
}
2020-10-16 00:31:37 +00:00
PR_SwitchQCVM ( NULL ) ;
2019-01-27 16:50:19 +00:00
PR_SwitchQCVM ( oldvm ) ;
2010-02-15 23:26:55 +00:00
glColor3f ( 1 , 1 , 1 ) ;
glEnable ( GL_TEXTURE_2D ) ;
glEnable ( GL_CULL_FACE ) ;
glPolygonMode ( GL_FRONT_AND_BACK , GL_FILL ) ;
GL_PolygonOffset ( OFFSET_NONE ) ;
glEnable ( GL_DEPTH_TEST ) ;
Sbar_Changed ( ) ; //so we don't get dots collecting on the statusbar
}
/*
= = = = = = = = = = = = = = = =
R_ShowTris - - johnfitz
= = = = = = = = = = = = = = = =
*/
void R_ShowTris ( void )
{
extern cvar_t r_particles ;
int i ;
if ( r_showtris . value < 1 | | r_showtris . value > 2 | | cl . maxclients > 1 )
return ;
if ( r_showtris . value = = 1 )
glDisable ( GL_DEPTH_TEST ) ;
glPolygonMode ( GL_FRONT_AND_BACK , GL_LINE ) ;
GL_PolygonOffset ( OFFSET_SHOWTRIS ) ;
glDisable ( GL_TEXTURE_2D ) ;
glColor3f ( 1 , 1 , 1 ) ;
// glEnable (GL_BLEND);
// glBlendFunc (GL_ONE, GL_ONE);
if ( r_drawworld . value )
{
2014-08-29 08:27:22 +00:00
R_DrawWorld_ShowTris ( ) ;
2010-02-15 23:26:55 +00:00
}
if ( r_drawentities . value )
{
for ( i = 0 ; i < cl_numvisedicts ; i + + )
{
currententity = cl_visedicts [ i ] ;
2017-09-17 02:12:53 +00:00
if ( currententity = = & cl . entities [ cl . viewentity ] ) // chasecam
2010-02-15 23:26:55 +00:00
currententity - > angles [ 0 ] * = 0.3 ;
switch ( currententity - > model - > type )
{
case mod_brush :
R_DrawBrushModel_ShowTris ( currententity ) ;
break ;
case mod_alias :
R_DrawAliasModel_ShowTris ( currententity ) ;
break ;
case mod_sprite :
R_DrawSpriteModel ( currententity ) ;
break ;
default :
break ;
}
}
// viewmodel
currententity = & cl . viewent ;
if ( r_drawviewmodel . value
& & ! chase_active . value
& & cl . stats [ STAT_HEALTH ] > 0
& & ! ( cl . items & IT_INVISIBILITY )
& & currententity - > model
& & currententity - > model - > type = = mod_alias )
{
glDepthRange ( 0 , 0.3 ) ;
R_DrawAliasModel_ShowTris ( currententity ) ;
glDepthRange ( 0 , 1 ) ;
}
}
if ( r_particles . value )
{
R_DrawParticles_ShowTris ( ) ;
}
// glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glDisable (GL_BLEND);
glColor3f ( 1 , 1 , 1 ) ;
glEnable ( GL_TEXTURE_2D ) ;
glPolygonMode ( GL_FRONT_AND_BACK , GL_FILL ) ;
GL_PolygonOffset ( OFFSET_NONE ) ;
if ( r_showtris . value = = 1 )
glEnable ( GL_DEPTH_TEST ) ;
Sbar_Changed ( ) ; //so we don't get dots collecting on the statusbar
}
/*
= = = = = = = = = = = = = = = =
R_DrawShadows
= = = = = = = = = = = = = = = =
*/
void R_DrawShadows ( void )
{
int i ;
if ( ! r_shadows . value | | ! r_drawentities . value | | r_drawflat_cheatsafe | | r_lightmap_cheatsafe )
return ;
2015-05-18 06:11:56 +00:00
// Use stencil buffer to prevent self-intersecting shadows, from Baker (MarkV)
if ( gl_stencilbits )
{
glClear ( GL_STENCIL_BUFFER_BIT ) ;
glStencilFunc ( GL_EQUAL , 0 , ~ 0 ) ;
glStencilOp ( GL_KEEP , GL_KEEP , GL_INCR ) ;
glEnable ( GL_STENCIL_TEST ) ;
}
2010-02-15 23:26:55 +00:00
for ( i = 0 ; i < cl_numvisedicts ; i + + )
{
currententity = cl_visedicts [ i ] ;
if ( currententity - > model - > type ! = mod_alias )
continue ;
if ( currententity = = & cl . viewent )
return ;
GL_DrawAliasShadow ( currententity ) ;
}
2015-05-18 06:11:56 +00:00
if ( gl_stencilbits )
{
glDisable ( GL_STENCIL_TEST ) ;
}
2010-02-15 23:26:55 +00:00
}
/*
= = = = = = = = = = = = = = = =
R_RenderScene
= = = = = = = = = = = = = = = =
*/
void R_RenderScene ( void )
{
2018-05-25 10:54:33 +00:00
currententity = & r_worldentity ;
2010-02-15 23:26:55 +00:00
R_SetupScene ( ) ; //johnfitz -- this does everything that should be done once per call to RenderScene
Fog_EnableGFog ( ) ; //johnfitz
Sky_DrawSky ( ) ; //johnfitz
R_DrawWorld ( ) ;
2018-05-25 10:54:33 +00:00
currententity = NULL ;
2010-02-15 23:26:55 +00:00
S_ExtraUpdate ( ) ; // don't let sound get messed up if going slow
2020-07-22 01:37:57 +00:00
if ( r_refdef . drawworld )
R_DrawShadows ( ) ; //johnfitz -- render entity shadows
2010-02-15 23:26:55 +00:00
R_DrawEntitiesOnList ( false ) ; //johnfitz -- false means this is the pass for nonalpha entities
2014-08-29 08:27:22 +00:00
R_DrawWorld_Water ( ) ; //johnfitz -- drawn here since they might have transparency
2010-02-15 23:26:55 +00:00
R_DrawEntitiesOnList ( true ) ; //johnfitz -- true means this is the pass for alpha entities
R_RenderDlights ( ) ; //triangle fan dlights -- johnfitz -- moved after water
2020-07-02 19:05:51 +00:00
if ( r_refdef . drawworld )
{
R_DrawParticles ( ) ;
2017-09-17 02:12:53 +00:00
# ifdef PSET_SCRIPT
2020-07-02 19:05:51 +00:00
PScript_DrawParticles ( ) ;
2017-09-17 02:12:53 +00:00
# endif
2020-07-02 19:05:51 +00:00
}
2010-02-15 23:26:55 +00:00
Fog_DisableGFog ( ) ; //johnfitz
R_DrawViewModel ( ) ; //johnfitz -- moved here from R_RenderView
R_ShowTris ( ) ; //johnfitz
R_ShowBoundingBoxes ( ) ; //johnfitz
}
2017-05-26 20:37:48 +00:00
static GLuint r_scaleview_texture ;
static int r_scaleview_texture_width , r_scaleview_texture_height ;
/*
= = = = = = = = = = = = =
R_ScaleView_DeleteTexture
= = = = = = = = = = = = =
*/
void R_ScaleView_DeleteTexture ( void )
{
glDeleteTextures ( 1 , & r_scaleview_texture ) ;
r_scaleview_texture = 0 ;
}
/*
= = = = = = = = = = = = = = = =
R_ScaleView
The r_scale cvar allows rendering the 3 D view at 1 / 2 , 1 / 3 , or 1 / 4 resolution .
This function scales the reduced resolution 3 D view back up to fill
r_refdef . vrect . This is for emulating a low - resolution pixellated look ,
or possibly as a perforance boost on slow graphics cards .
= = = = = = = = = = = = = = = =
*/
void R_ScaleView ( void )
{
float smax , tmax ;
int scale ;
int srcx , srcy , srcw , srch ;
// copied from R_SetupGL()
scale = CLAMP ( 1 , ( int ) r_scale . value , 4 ) ;
srcx = glx + r_refdef . vrect . x ;
srcy = gly + glheight - r_refdef . vrect . y - r_refdef . vrect . height ;
srcw = r_refdef . vrect . width / scale ;
srch = r_refdef . vrect . height / scale ;
2021-05-10 23:53:16 +00:00
if ( scale = = 1 | | ! r_refdef . drawworld )
2017-05-26 20:37:48 +00:00
return ;
// make sure texture unit 0 is selected
GL_DisableMultitexture ( ) ;
// create (if needed) and bind the render-to-texture texture
if ( ! r_scaleview_texture )
{
glGenTextures ( 1 , & r_scaleview_texture ) ;
r_scaleview_texture_width = 0 ;
r_scaleview_texture_height = 0 ;
}
glBindTexture ( GL_TEXTURE_2D , r_scaleview_texture ) ;
// resize render-to-texture texture if needed
if ( r_scaleview_texture_width < srcw
| | r_scaleview_texture_height < srch )
{
r_scaleview_texture_width = srcw ;
r_scaleview_texture_height = srch ;
if ( ! gl_texture_NPOT )
{
r_scaleview_texture_width = TexMgr_Pad ( r_scaleview_texture_width ) ;
r_scaleview_texture_height = TexMgr_Pad ( r_scaleview_texture_height ) ;
}
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , r_scaleview_texture_width , r_scaleview_texture_height , 0 , GL_RGBA , GL_UNSIGNED_BYTE , NULL ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ) ;
}
// copy the framebuffer to the texture
glBindTexture ( GL_TEXTURE_2D , r_scaleview_texture ) ;
glCopyTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 , srcx , srcy , srcw , srch ) ;
// draw the texture back to the framebuffer
glDisable ( GL_ALPHA_TEST ) ;
glDisable ( GL_DEPTH_TEST ) ;
glDisable ( GL_CULL_FACE ) ;
glDisable ( GL_BLEND ) ;
glViewport ( srcx , srcy , r_refdef . vrect . width , r_refdef . vrect . height ) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ( ) ;
glMatrixMode ( GL_MODELVIEW ) ;
glLoadIdentity ( ) ;
// correction factor if we lack NPOT textures, normally these are 1.0f
smax = srcw / ( float ) r_scaleview_texture_width ;
tmax = srch / ( float ) r_scaleview_texture_height ;
glBegin ( GL_QUADS ) ;
glTexCoord2f ( 0 , 0 ) ;
glVertex2f ( - 1 , - 1 ) ;
glTexCoord2f ( smax , 0 ) ;
glVertex2f ( 1 , - 1 ) ;
glTexCoord2f ( smax , tmax ) ;
glVertex2f ( 1 , 1 ) ;
glTexCoord2f ( 0 , tmax ) ;
glVertex2f ( - 1 , 1 ) ;
glEnd ( ) ;
// clear cached binding
GL_ClearBindings ( ) ;
}
2019-09-10 17:58:03 +00:00
static qboolean R_SkyroomWasVisible ( void )
{
qmodel_t * model = cl . worldmodel ;
texture_t * t ;
size_t i ;
2020-08-22 03:35:40 +00:00
extern cvar_t r_fastsky ;
if ( ! skyroom_enabled | | r_fastsky . value )
2019-09-10 17:58:03 +00:00
return false ;
for ( i = 0 ; i < model - > numtextures ; i + + )
{
t = model - > textures [ i ] ;
if ( t & & t - > texturechains [ chain_world ] & & t - > texturechains [ chain_world ] - > flags & SURF_DRAWSKY )
return true ;
}
return false ;
}
2010-02-15 23:26:55 +00:00
/*
= = = = = = = = = = = = = = = =
R_RenderView
= = = = = = = = = = = = = = = =
*/
void R_RenderView ( void )
{
2019-09-10 17:58:03 +00:00
static qboolean skyroom_visible ;
2010-02-15 23:26:55 +00:00
double time1 , time2 ;
if ( r_norefresh . value )
return ;
2021-09-03 07:18:27 +00:00
if ( r_refdef . drawworld & & ! cl . worldmodel )
2010-02-15 23:26:55 +00:00
Sys_Error ( " R_RenderView: NULL worldmodel " ) ;
2011-01-01 14:05:20 +00:00
time1 = 0 ; /* avoid compiler warning */
2010-02-15 23:26:55 +00:00
if ( r_speeds . value )
{
glFinish ( ) ;
2011-12-12 16:01:01 +00:00
time1 = Sys_DoubleTime ( ) ;
2010-02-15 23:26:55 +00:00
//johnfitz -- rendering statistics
2022-07-05 05:50:02 +00:00
rs_brushpolys = rs_aliaspolys = rs_skypolys =
2010-02-15 23:26:55 +00:00
rs_dynamiclightmaps = rs_aliaspasses = rs_skypasses = rs_brushpasses = 0 ;
}
else if ( gl_finish . value )
glFinish ( ) ;
2021-10-05 05:13:27 +00:00
if ( lightmaps_latecached )
{
GL_BuildLightmaps ( ) ;
GL_BuildBModelVertexBuffer ( ) ;
lightmaps_latecached = false ;
}
2019-09-10 17:58:03 +00:00
//Spike -- quickly draw the world from the skyroom camera's point of view.
skyroom_drawn = false ;
2021-09-03 07:18:27 +00:00
if ( r_refdef . drawworld & & skyroom_enabled & & skyroom_visible )
2019-09-10 17:58:03 +00:00
{
vec3_t vieworg ;
2019-11-02 20:22:54 +00:00
vec3_t viewang ;
2019-09-10 17:58:03 +00:00
VectorCopy ( r_refdef . vieworg , vieworg ) ;
2019-11-02 20:22:54 +00:00
VectorCopy ( r_refdef . viewangles , viewang ) ;
VectorMA ( skyroom_origin , skyroom_origin [ 3 ] , vieworg , r_refdef . vieworg ) ; //allow a little paralax
if ( skyroom_orientation [ 3 ] )
{
vec3_t axis [ 3 ] ;
float ang = skyroom_orientation [ 3 ] * cl . time ;
if ( ! skyroom_orientation [ 0 ] & & ! skyroom_orientation [ 1 ] & & ! skyroom_orientation [ 2 ] )
{
skyroom_orientation [ 0 ] = 0 ;
skyroom_orientation [ 1 ] = 0 ;
skyroom_orientation [ 2 ] = 1 ;
}
VectorNormalize ( skyroom_orientation ) ;
RotatePointAroundVector ( axis [ 0 ] , skyroom_orientation , vpn , ang ) ;
RotatePointAroundVector ( axis [ 1 ] , skyroom_orientation , vright , ang ) ;
RotatePointAroundVector ( axis [ 2 ] , skyroom_orientation , vup , ang ) ;
VectorAngles ( axis [ 0 ] , axis [ 2 ] , r_refdef . viewangles ) ;
}
2021-06-25 18:15:10 +00:00
skyroom_drawing = true ;
2019-09-10 17:58:03 +00:00
R_SetupView ( ) ;
//note: sky boxes are generally considered an 'infinite' distance away such that you'd not see paralax.
//that's my excuse for not handling r_stereo here, and I'm sticking to it.
R_RenderScene ( ) ;
VectorCopy ( vieworg , r_refdef . vieworg ) ;
2019-11-02 20:22:54 +00:00
VectorCopy ( viewang , r_refdef . viewangles ) ;
2019-09-10 17:58:03 +00:00
skyroom_drawn = true ; //disable glClear(GL_COLOR_BUFFER_BIT)
}
2021-06-25 18:15:10 +00:00
skyroom_drawing = false ;
2019-09-10 17:58:03 +00:00
//skyroom end
2010-02-15 23:26:55 +00:00
R_SetupView ( ) ; //johnfitz -- this does everything that should be done once per frame
//johnfitz -- stereo rendering -- full of hacky goodness
if ( r_stereo . value )
{
float eyesep = CLAMP ( - 8.0f , r_stereo . value , 8.0f ) ;
float fdepth = CLAMP ( 32.0f , r_stereodepth . value , 1024.0f ) ;
AngleVectors ( r_refdef . viewangles , vpn , vright , vup ) ;
//render left eye (red)
glColorMask ( 1 , 0 , 0 , 1 ) ;
VectorMA ( r_refdef . vieworg , - 0.5f * eyesep , vright , r_refdef . vieworg ) ;
frustum_skew = 0.5 * eyesep * NEARCLIP / fdepth ;
srand ( ( int ) ( cl . time * 1000 ) ) ; //sync random stuff between eyes
R_RenderScene ( ) ;
//render right eye (cyan)
glClear ( GL_DEPTH_BUFFER_BIT ) ;
glColorMask ( 0 , 1 , 1 , 1 ) ;
VectorMA ( r_refdef . vieworg , 1.0f * eyesep , vright , r_refdef . vieworg ) ;
frustum_skew = - frustum_skew ;
srand ( ( int ) ( cl . time * 1000 ) ) ; //sync random stuff between eyes
R_RenderScene ( ) ;
//restore
glColorMask ( 1 , 1 , 1 , 1 ) ;
VectorMA ( r_refdef . vieworg , - 0.5f * eyesep , vright , r_refdef . vieworg ) ;
frustum_skew = 0.0f ;
}
else
{
R_RenderScene ( ) ;
}
//johnfitz
2019-09-10 17:58:03 +00:00
//Spike: flag whether the skyroom was actually visible, so we don't needlessly draw it when its not (1 frame's lag, hopefully not too noticable)
2021-09-03 07:18:27 +00:00
if ( r_refdef . drawworld )
{
if ( r_viewleaf - > contents = = CONTENTS_SOLID | | r_drawflat_cheatsafe | | r_lightmap_cheatsafe )
skyroom_visible = false ; //don't do skyrooms when the view is in the void, for framerate reasons while debugging.
else
skyroom_visible = R_SkyroomWasVisible ( ) ;
skyroom_drawn = false ;
}
2019-09-10 17:58:03 +00:00
//skyroom end
2017-05-26 20:37:48 +00:00
R_ScaleView ( ) ;
2010-02-15 23:26:55 +00:00
//johnfitz -- modified r_speeds output
2011-12-12 16:01:01 +00:00
time2 = Sys_DoubleTime ( ) ;
2015-09-12 03:29:41 +00:00
if ( r_pos . value )
Con_Printf ( " x %i y %i z %i (pitch %i yaw %i roll %i) \n " ,
2022-08-04 06:04:30 +00:00
( int ) cl . entities [ cl . viewentity ] . origin [ 0 ] ,
( int ) cl . entities [ cl . viewentity ] . origin [ 1 ] ,
( int ) cl . entities [ cl . viewentity ] . origin [ 2 ] ,
2022-07-05 05:50:02 +00:00
( int ) cl . viewangles [ PITCH ] ,
( int ) cl . viewangles [ YAW ] ,
( int ) cl . viewangles [ ROLL ] ) ;
2015-09-12 03:29:41 +00:00
else if ( r_speeds . value = = 2 )
2010-02-15 23:26:55 +00:00
Con_Printf ( " %3i ms %4i/%4i wpoly %4i/%4i epoly %3i lmap %4i/%4i sky %1.1f mtex \n " ,
( int ) ( ( time2 - time1 ) * 1000 ) ,
rs_brushpolys ,
rs_brushpasses ,
rs_aliaspolys ,
rs_aliaspasses ,
rs_dynamiclightmaps ,
rs_skypolys ,
rs_skypasses ,
TexMgr_FrameUsage ( ) ) ;
else if ( r_speeds . value )
Con_Printf ( " %3i ms %4i wpoly %4i epoly %3i lmap \n " ,
( int ) ( ( time2 - time1 ) * 1000 ) ,
rs_brushpolys ,
rs_aliaspolys ,
rs_dynamiclightmaps ) ;
//johnfitz
}