From ebbb675cb8ee1ceae3c207b1778a16679a86a8c7 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Mon, 23 Jan 2017 01:41:26 +0100 Subject: [PATCH] GL3: gamma and intensity via shader only for 2D rendering, as we don't have 3D yet; also this might need a more flexible solution later, as some textures are not supposed to have intensity applied. According to the old R_Upload32*() and R_LightScaleTexture() the ones without mipmaps didn't get intensity. Those were it_pic and it_sky (and the ones in the "scrap", but those were it_pic too) --- src/client/refresh/gl3/gl3_image.c | 5 ---- src/client/refresh/gl3/gl3_main.c | 20 +++++++++---- src/client/refresh/gl3/gl3_shaders.c | 43 +++++++++++++++++++++++---- src/client/refresh/gl3/header/local.h | 2 ++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/client/refresh/gl3/gl3_image.c b/src/client/refresh/gl3/gl3_image.c index d79f7508..65030680 100644 --- a/src/client/refresh/gl3/gl3_image.c +++ b/src/client/refresh/gl3/gl3_image.c @@ -146,11 +146,6 @@ GL3_Upload32(unsigned *data, int width, int height, qboolean mipmap) scan = ((byte *)data) + 3; samples = gl3_solid_format; comp = gl3_tex_solid_format; - //upload_width = width; // TODO: remove, probably - //upload_height = height; - - STUB_ONCE("TODO: something with gamma and intensity, somewhere"); - // R_LightScaleTexture(data, upload_width, upload_height, !mipmap); for (i = 0; i < c; i++, scan += 4) { diff --git a/src/client/refresh/gl3/gl3_main.c b/src/client/refresh/gl3/gl3_main.c index 4cd0a674..0d6ca801 100644 --- a/src/client/refresh/gl3/gl3_main.c +++ b/src/client/refresh/gl3/gl3_main.c @@ -58,6 +58,8 @@ cvar_t *gl_customheight; cvar_t *vid_gamma; cvar_t *gl_anisotropic; +cvar_t *intensity; + cvar_t *gl_norefresh; cvar_t *gl_nolerp_list; cvar_t *gl_nobind; @@ -104,6 +106,7 @@ GL3_Register(void) vid_fullscreen = ri.Cvar_Get("vid_fullscreen", "0", CVAR_ARCHIVE); vid_gamma = ri.Cvar_Get("vid_gamma", "1.0", CVAR_ARCHIVE); + intensity = ri.Cvar_Get("intensity", "1.0", CVAR_ARCHIVE); #if 0 // TODO! @@ -398,10 +401,8 @@ GL3_Init(void) return false; } - STUB("TODO: Some intensity and gamma stuff that was in R_InitImages()"); - registration_sequence = 1; // also from R_InitImages() + registration_sequence = 1; // from R_InitImages() (everything else from there shouldn't be needed anymore) - //R_InitImages(); - most of the things in R_InitImages() shouldn't be needed anymore GL3_Mod_Init(); GL3_InitParticleTexture(); @@ -461,8 +462,9 @@ GL3_SetGL2D(void) hmm_mat4 transMatr = HMM_Orthographic(0, vid.width, vid.height, 0, -99999, 99999); + glUseProgram(gl3state.si2Dcolor.shaderProgram); + glUniformMatrix4fv(gl3state.si2Dcolor.uniTransMatrix , 1, GL_FALSE, transMatr.Elements[0]); glUseProgram(gl3state.si2D.shaderProgram); - glUniformMatrix4fv(gl3state.si2D.uniTransMatrix , 1, GL_FALSE, transMatr.Elements[0]); // FIXME: change to GL3 code! @@ -476,7 +478,6 @@ GL3_SetGL2D(void) glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glDisable(GL_BLEND); - // glEnable(GL_ALPHA_TEST); TODO: do in shader https://www.khronos.org/opengl/wiki/Transparency_Sorting#Alpha_test // glColor4f(1, 1, 1, 1); } @@ -753,6 +754,15 @@ GL3_BeginFrame(float camera_separation) glClearColor(1, 0, 0.5, 0.5); GL3_SetGL2D(); + + if (vid_gamma->modified || intensity->modified) + { + vid_gamma->modified = false; + intensity->modified = false; + + GL3_SetGammaAndIntensity(); + } + #if 0 gl_state.camera_separation = camera_separation; diff --git a/src/client/refresh/gl3/gl3_shaders.c b/src/client/refresh/gl3/gl3_shaders.c index 9e1488af..f84ed43b 100644 --- a/src/client/refresh/gl3/gl3_shaders.c +++ b/src/client/refresh/gl3/gl3_shaders.c @@ -201,16 +201,21 @@ static const char* fragmentSrc2D = MULTILINE_STRING(#version 150\n in vec2 passTexCoord; uniform sampler2D tex; + uniform float gamma; // this is 1.0/vid_gamma + uniform float intensity; out vec4 outColor; void main() { - // TODO: gamma, intensity vec4 texel = texture(tex, passTexCoord); if(texel.a < 0.666) discard; - outColor = texel; + + // apply gamma correction and intensity + texel.rgb *= intensity; + outColor.rgb = pow(texel.rgb, vec3(gamma)); + outColor.a = texel.a; // I think alpha shouldn't be modified by gamma and intensity } ); @@ -233,14 +238,16 @@ static const char* vertexSrc2Dcolor = MULTILINE_STRING(#version 150\n static const char* fragmentSrc2Dcolor = MULTILINE_STRING(#version 150\n in vec4 passColor; - uniform sampler2D tex; + uniform float gamma; // this is 1.0/vid_gamma + uniform float intensity; out vec4 outColor; void main() { - // TODO: gamma, intensity? (not sure we need that here) - outColor = passColor; + vec3 col = passColor.rgb * intensity; + outColor.rgb = pow(col, vec3(gamma)); + outColor.a = passColor.a; } ); @@ -324,6 +331,8 @@ qboolean GL3_InitShaders(void) return false; } + GL3_SetGammaAndIntensity(); + return true; } @@ -337,3 +346,27 @@ void GL3_ShutdownShaders(void) glDeleteProgram(gl3state.si2Dcolor.shaderProgram); memset(&gl3state.si2Dcolor, 0, sizeof(gl3ShaderInfo_t)); } + +void GL3_SetGammaAndIntensity(void) +{ + float gamma = 1.0f/vid_gamma->value; + float intens = intensity->value; + int i=0; + GLint progs[2] = { gl3state.si2D.shaderProgram, gl3state.si2Dcolor.shaderProgram }; + + for(i=0; i<2; ++i) + { + glUseProgram(progs[i]); + GLint uni = glGetUniformLocation(progs[i], "gamma"); + if(uni != -1) + { + glUniform1f(uni, gamma); + } + + uni = glGetUniformLocation(progs[i], "intensity"); + if(uni != -1) + { + glUniform1f(uni, intens); + } + } +} diff --git a/src/client/refresh/gl3/header/local.h b/src/client/refresh/gl3/header/local.h index c1f7c1b6..a1ec5cf2 100644 --- a/src/client/refresh/gl3/header/local.h +++ b/src/client/refresh/gl3/header/local.h @@ -228,6 +228,7 @@ extern void GL3_SetSky(char *name, float rotate, vec3_t axis); // gl3_shaders.c extern qboolean GL3_InitShaders(void); extern void GL3_ShutdownShaders(void); +extern void GL3_SetGammaAndIntensity(void); // ############ Cvars ########### @@ -243,6 +244,7 @@ extern cvar_t *gl_nolerp_list; extern cvar_t *gl_nobind; extern cvar_t *vid_gamma; +extern cvar_t *intensity; extern cvar_t *gl_anisotropic; extern cvar_t *gl3_debugcontext;