From 80e1c797c1362abb4b5944786ce232914027ed58 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Wed, 24 May 2017 12:35:17 -0600 Subject: [PATCH] scaling v2 --- quakespasm/Quake/gl_rmain.c | 110 ++++++++++++++++++++++++++++++++++- quakespasm/Quake/gl_screen.c | 10 +++- quakespasm/Quake/gl_vidsdl.c | 1 + quakespasm/Quake/glquake.h | 2 + 4 files changed, 119 insertions(+), 4 deletions(-) diff --git a/quakespasm/Quake/gl_rmain.c b/quakespasm/Quake/gl_rmain.c index 78d6828a..34e13fa8 100644 --- a/quakespasm/Quake/gl_rmain.c +++ b/quakespasm/Quake/gl_rmain.c @@ -112,6 +112,8 @@ float map_wateralpha, map_lavaalpha, map_telealpha, map_slimealpha; qboolean r_drawflat_cheatsafe, r_fullbright_cheatsafe, r_lightmap_cheatsafe, r_drawworld_cheatsafe; //johnfitz +extern cvar_t scr_vidscale; + //============================================================================== // // GLSL GAMMA CORRECTION @@ -472,13 +474,15 @@ R_SetupGL */ void R_SetupGL (void) { + int vidscale = CLAMP(1, (int)scr_vidscale.value, 4); + //johnfitz -- rewrote this section glMatrixMode(GL_PROJECTION); glLoadIdentity (); glViewport (glx + r_refdef.vrect.x, gly + glheight - r_refdef.vrect.y - r_refdef.vrect.height, - r_refdef.vrect.width, - r_refdef.vrect.height); + r_refdef.vrect.width / vidscale, + r_refdef.vrect.height / vidscale); //johnfitz GL_SetFrustum (r_fovx, r_fovy); //johnfitz -- use r_fov* vars @@ -936,6 +940,106 @@ void R_RenderScene (void) R_ShowBoundingBoxes (); //johnfitz } +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 +================ +*/ +void R_ScaleView (void) +{ + float smax, tmax; + int vidscale; + int srcx, srcy, srcw, srch; + + if (scr_vidscale.value == 1) + return; + + // copied from R_SetupGL() + vidscale = CLAMP(1, (int)scr_vidscale.value, 4); + srcx = glx + r_refdef.vrect.x; + srcy = gly + glheight - r_refdef.vrect.y - r_refdef.vrect.height; + srcw = r_refdef.vrect.width / vidscale; + srch = r_refdef.vrect.height / vidscale; + + // create render-to-texture texture if needed + if (!r_scaleview_texture) + { + glGenTextures (1, &r_scaleview_texture); + glBindTexture (GL_TEXTURE_2D, r_scaleview_texture); + + r_scaleview_texture_width = 0; + r_scaleview_texture_height = 0; + } + + // 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 + GL_DisableMultitexture(); + 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 (); +} + /* ================ R_RenderView @@ -1001,6 +1105,8 @@ void R_RenderView (void) R_RenderScene (); } //johnfitz + + R_ScaleView (); //johnfitz -- modified r_speeds output time2 = Sys_DoubleTime (); diff --git a/quakespasm/Quake/gl_screen.c b/quakespasm/Quake/gl_screen.c index 0d560471..5fb74853 100644 --- a/quakespasm/Quake/gl_screen.c +++ b/quakespasm/Quake/gl_screen.c @@ -88,6 +88,10 @@ cvar_t scr_showfps = {"scr_showfps", "0", CVAR_NONE}; cvar_t scr_clock = {"scr_clock", "0", CVAR_NONE}; //johnfitz +//ericw -- scaling +cvar_t scr_vidscale = {"scr_vidscale", "1", CVAR_ARCHIVE}; +//ericw + cvar_t scr_viewsize = {"viewsize","100", CVAR_ARCHIVE}; cvar_t scr_fov = {"fov","90",CVAR_NONE}; // 10 - 170 cvar_t scr_fov_adapt = {"fov_adapt","1",CVAR_ARCHIVE}; @@ -322,7 +326,7 @@ static void SCR_CalcRefdef (void) r_refdef.vrect.x = (glwidth - r_refdef.vrect.width)/2; r_refdef.vrect.y = (glheight - sb_lines - r_refdef.vrect.height)/2; //johnfitz - + r_refdef.fov_x = AdaptFovx(scr_fov.value, vid.width, vid.height); r_refdef.fov_y = CalcFovy (r_refdef.fov_x, r_refdef.vrect.width, r_refdef.vrect.height); @@ -421,7 +425,9 @@ void SCR_Init (void) Cvar_RegisterVariable (&scr_centertime); Cvar_RegisterVariable (&scr_printspeed); Cvar_RegisterVariable (&gl_triplebuffer); - + Cvar_RegisterVariable (&scr_vidscale); + Cvar_SetCallback (&scr_vidscale, SCR_Callback_refdef); + Cmd_AddCommand ("screenshot",SCR_ScreenShot_f); Cmd_AddCommand ("sizeup",SCR_SizeUp_f); Cmd_AddCommand ("sizedown",SCR_SizeDown_f); diff --git a/quakespasm/Quake/gl_vidsdl.c b/quakespasm/Quake/gl_vidsdl.c index 00a62a94..6e9c4617 100644 --- a/quakespasm/Quake/gl_vidsdl.c +++ b/quakespasm/Quake/gl_vidsdl.c @@ -756,6 +756,7 @@ static void VID_Restart (void) TexMgr_DeleteTextureObjects (); GLSLGamma_DeleteTexture (); + R_ScaleView_DeleteTexture (); R_DeleteShaders (); GL_DeleteBModelVertexBuffer (); GLMesh_DeleteVertexBuffers (); diff --git a/quakespasm/Quake/glquake.h b/quakespasm/Quake/glquake.h index e85dfb6b..f30b909a 100644 --- a/quakespasm/Quake/glquake.h +++ b/quakespasm/Quake/glquake.h @@ -395,6 +395,8 @@ void GL_ClearBufferBindings (); void GLSLGamma_DeleteTexture (void); void GLSLGamma_GammaCorrect (void); +void R_ScaleView_DeleteTexture (void); + float GL_WaterAlphaForSurface (msurface_t *fa); #endif /* __GLQUAKE_H */