From f64478c970bea981d87aeefe0c03b47d5a72e910 Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Mon, 18 May 2020 15:48:37 +0000 Subject: [PATCH] Implement chromatic aberration, configurable with r_colorfringe cvar. Do note that it is disabled by default and values higher than 0.025 will distort the image. Taken from a merge request by cjayho. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12538 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_rmain.c | 5 +++++ shader_glsl.h | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/gl_rmain.c b/gl_rmain.c index 6400d65b..2897c78f 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -190,6 +190,7 @@ cvar_t r_glsl_postprocess_uservec1_enable = {CVAR_CLIENT | CVAR_SAVE, "r_glsl_po cvar_t r_glsl_postprocess_uservec2_enable = {CVAR_CLIENT | CVAR_SAVE, "r_glsl_postprocess_uservec2_enable", "1", "enables postprocessing uservec2 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"}; cvar_t r_glsl_postprocess_uservec3_enable = {CVAR_CLIENT | CVAR_SAVE, "r_glsl_postprocess_uservec3_enable", "1", "enables postprocessing uservec3 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"}; cvar_t r_glsl_postprocess_uservec4_enable = {CVAR_CLIENT | CVAR_SAVE, "r_glsl_postprocess_uservec4_enable", "1", "enables postprocessing uservec4 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"}; +cvar_t r_colorfringe = {CVAR_CLIENT | CVAR_SAVE, "r_colorfringe", "0", "Chromatic aberration. Values higher than 0.025 will noticeably distort the image"}; cvar_t r_water = {CVAR_CLIENT | CVAR_SAVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"}; cvar_t r_water_cameraentitiesonly = {CVAR_CLIENT | CVAR_SAVE, "r_water_cameraentitiesonly", "0", "whether to only show QC-defined reflections/refractions (typically used for camera- or portal-like effects)"}; @@ -817,6 +818,7 @@ typedef struct r_glsl_permutation_s int loc_UserVec2; int loc_UserVec3; int loc_UserVec4; + int loc_ColorFringe; int loc_ViewTintColor; int loc_ViewToLight; int loc_ModelToLight; @@ -1247,6 +1249,7 @@ static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode p->loc_UserVec2 = qglGetUniformLocation(p->program, "UserVec2"); p->loc_UserVec3 = qglGetUniformLocation(p->program, "UserVec3"); p->loc_UserVec4 = qglGetUniformLocation(p->program, "UserVec4"); + p->loc_ColorFringe = qglGetUniformLocation(p->program, "ColorFringe"); p->loc_ViewTintColor = qglGetUniformLocation(p->program, "ViewTintColor"); p->loc_ViewToLight = qglGetUniformLocation(p->program, "ViewToLight"); p->loc_ModelToLight = qglGetUniformLocation(p->program, "ModelToLight"); @@ -3317,6 +3320,7 @@ void GL_Main_Init(void) Cvar_RegisterVariable(&r_lerplightstyles); Cvar_RegisterVariable(&r_waterscroll); Cvar_RegisterVariable(&r_bloom); + Cvar_RegisterVariable(&r_colorfringe); Cvar_RegisterVariable(&r_bloom_colorscale); Cvar_RegisterVariable(&r_bloom_brighten); Cvar_RegisterVariable(&r_bloom_blur); @@ -5367,6 +5371,7 @@ static void R_BlendView(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *v if (r_glsl_permutation->loc_Saturation >= 0) qglUniform1f(r_glsl_permutation->loc_Saturation , r_glsl_saturation.value); if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height); if (r_glsl_permutation->loc_BloomColorSubtract >= 0) qglUniform4f(r_glsl_permutation->loc_BloomColorSubtract , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f); + if (r_glsl_permutation->loc_ColorFringe >= 0) qglUniform1f(r_glsl_permutation->loc_ColorFringe, r_colorfringe.value ); break; } R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0); diff --git a/shader_glsl.h b/shader_glsl.h index d51cd672..608e42d7 100644 --- a/shader_glsl.h +++ b/shader_glsl.h @@ -246,6 +246,7 @@ "uniform mediump vec4 UserVec2;\n", "// uniform mediump vec4 UserVec3;\n", "// uniform mediump vec4 UserVec4;\n", +"uniform mediump float ColorFringe;\n", "// uniform highp float ClientTime;\n", "uniform mediump vec2 PixelSize;\n", "\n", @@ -298,7 +299,11 @@ "\n", "void main(void)\n", "{\n", -" dp_FragColor = dp_texture2D(Texture_First, TexCoord1);\n", +" float fringe = ColorFringe;//.0033f;\n", +" float amount = distance(TexCoord1, vec2(.5f,.5f));\n", +" vec2 offset = vec2(amount*fringe,amount*fringe);\n", +" dp_FragColor.xy = texture(Texture_First, TexCoord1-offset).xy;\n", +" dp_FragColor.z = texture(Texture_First, TexCoord1+offset).z;\n", "\n", "#ifdef USEFXAA\n", " dp_FragColor = fxaa(dp_FragColor, 8.0); // 8.0 can be changed for larger span\n", -- 2.39.2