From ec4b6115d110390092e734ca3bcefc8c62545a45 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 24 Dec 2017 18:59:44 +0000 Subject: [PATCH] world shader: avoid matrix multiplication in the fragment shader alias shader: sync fog implementation with world shader Thanks to Axel for the suggestion. Add a FIXME where our shader differs from vkQuake in treatment of alpha transparent faces + fog. see: https://sourceforge.net/p/quakespasm/bugs/24/ https://github.com/Novum/vkQuake/blob/master/Shaders/world.vert https://github.com/Novum/vkQuake/blob/master/Shaders/world.frag git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1546 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/r_alias.c | 17 ++++++++++------- Quake/r_world.c | 15 +++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Quake/r_alias.c b/Quake/r_alias.c index 5b3b9b56..1f1f1e1b 100644 --- a/Quake/r_alias.c +++ b/Quake/r_alias.c @@ -140,6 +140,9 @@ void GLAlias_CreateShaders (void) "attribute vec3 Pose1Normal;\n" "attribute vec4 Pose2Vert;\n" "attribute vec3 Pose2Normal;\n" + "\n" + "varying float FogFragCoord;\n" + "\n" "float r_avertexnormal_dot(vec3 vertexnormal) // from MH \n" "{\n" " float dot = dot(vertexnormal, ShadeVector);\n" @@ -152,14 +155,12 @@ void GLAlias_CreateShaders (void) "void main()\n" "{\n" " gl_TexCoord[0] = TexCoords;\n" - " vec4 lerpedVert = mix(Pose1Vert, Pose2Vert, Blend);\n" + " vec4 lerpedVert = mix(vec4(Pose1Vert.xyz, 1.0), vec4(Pose2Vert.xyz, 1.0), Blend);\n" " gl_Position = gl_ModelViewProjectionMatrix * lerpedVert;\n" + " FogFragCoord = gl_Position.w;\n" " float dot1 = r_avertexnormal_dot(Pose1Normal);\n" " float dot2 = r_avertexnormal_dot(Pose2Normal);\n" " gl_FrontColor = LightColor * vec4(vec3(mix(dot1, dot2, Blend)), 1.0);\n" - " // fog\n" - " vec3 ecPosition = vec3(gl_ModelViewMatrix * lerpedVert);\n" - " gl_FogFragCoord = abs(ecPosition.z);\n" "}\n"; const GLchar *fragSource = \ @@ -170,6 +171,9 @@ void GLAlias_CreateShaders (void) "uniform bool UseFullbrightTex;\n" "uniform bool UseOverbright;\n" "uniform bool UseAlphaTest;\n" + "\n" + "varying float FogFragCoord;\n" + "\n" "void main()\n" "{\n" " vec4 result = texture2D(Tex, gl_TexCoord[0].xy);\n" @@ -181,11 +185,10 @@ void GLAlias_CreateShaders (void) " if (UseFullbrightTex)\n" " result += texture2D(FullbrightTex, gl_TexCoord[0].xy);\n" " result = clamp(result, 0.0, 1.0);\n" - " // apply GL_EXP2 fog (from the orange book)\n" - " float fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n" + " float fog = exp(-gl_Fog.density * gl_Fog.density * FogFragCoord * FogFragCoord);\n" " fog = clamp(fog, 0.0, 1.0);\n" " result = mix(gl_Fog.color, result, fog);\n" - " result.a = gl_Color.a;\n" + " result.a = gl_Color.a;\n" // FIXME: This will make almost transparent things cut holes though heavy fog " gl_FragColor = result;\n" "}\n"; diff --git a/Quake/r_world.c b/Quake/r_world.c index 4371078c..d9e4038c 100644 --- a/Quake/r_world.c +++ b/Quake/r_world.c @@ -832,14 +832,14 @@ void GLWorld_CreateShaders (void) "attribute vec2 TexCoords;\n" "attribute vec2 LMCoords;\n" "\n" - "varying vec3 VertVarying;\n" + "varying float FogFragCoord;\n" "\n" "void main()\n" "{\n" " gl_TexCoord[0] = vec4(TexCoords, 0.0, 0.0);\n" " gl_TexCoord[1] = vec4(LMCoords, 0.0, 0.0);\n" " gl_Position = gl_ModelViewProjectionMatrix * vec4(Vert, 1.0);\n" - " VertVarying = Vert;\n" + " FogFragCoord = gl_Position.w;\n" "}\n"; const GLchar *fragSource = \ @@ -853,7 +853,7 @@ void GLWorld_CreateShaders (void) "uniform bool UseAlphaTest;\n" "uniform float Alpha;\n" "\n" - "varying vec3 VertVarying;\n" + "varying float FogFragCoord;\n" "\n" "void main()\n" "{\n" @@ -866,15 +866,10 @@ void GLWorld_CreateShaders (void) " if (UseFullbrightTex)\n" " result += texture2D(FullbrightTex, gl_TexCoord[0].xy);\n" " result = clamp(result, 0.0, 1.0);\n" - " // apply GL_EXP2 fog (from the orange book)\n" - " // needs to be done fully in the fragment shader to match fixed-function's quality\n" - " // (visible on large polygons / large texture scales)\n" - " vec3 ecPosition = vec3(gl_ModelViewMatrix * vec4(VertVarying, 1.0));\n" - " float fogFragCoord = abs(ecPosition.z);\n" - " float fog = exp(-gl_Fog.density * gl_Fog.density * fogFragCoord * fogFragCoord);\n" + " float fog = exp(-gl_Fog.density * gl_Fog.density * FogFragCoord * FogFragCoord);\n" " fog = clamp(fog, 0.0, 1.0);\n" " result = mix(gl_Fog.color, result, fog);\n" - " result.a = Alpha;\n" + " result.a = Alpha;\n" // FIXME: This will make almost transparent things cut holes though heavy fog " gl_FragColor = result;\n" "}\n";