From b6faadca0333ae3f2a16f41978fbae803026e267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Thu, 11 Feb 2016 15:04:07 +0100 Subject: [PATCH] Rewritten OpenGL 2.1 engine's two-sided lighting --- src/graphics/opengl/gl21device.cpp | 2 ++ .../shaders/fragment_shader_21_pervertex.glsl | 21 +++++++++---- .../shaders/vertex_shader_21_pervertex.glsl | 30 +++++++++++++------ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/graphics/opengl/gl21device.cpp b/src/graphics/opengl/gl21device.cpp index 8a4fcd90..7c1c9cbb 100644 --- a/src/graphics/opengl/gl21device.cpp +++ b/src/graphics/opengl/gl21device.cpp @@ -388,6 +388,8 @@ bool CGL21Device::Create() for (int i = 0; i < 8; i++) glUniform1i(uni_Light[i].Enabled, 0); + glEnable(GL_VERTEX_PROGRAM_TWO_SIDE); + // create default framebuffer object FramebufferParams framebufferParams; diff --git a/src/graphics/opengl/shaders/fragment_shader_21_pervertex.glsl b/src/graphics/opengl/shaders/fragment_shader_21_pervertex.glsl index 7d965553..5e4ba055 100644 --- a/src/graphics/opengl/shaders/fragment_shader_21_pervertex.glsl +++ b/src/graphics/opengl/shaders/fragment_shader_21_pervertex.glsl @@ -35,27 +35,36 @@ uniform vec4 uni_FogColor; uniform float uni_ShadowColor; varying float pass_Distance; +varying vec4 pass_Color; +varying vec3 pass_Normal; +varying vec2 pass_TexCoord0; +varying vec2 pass_TexCoord1; +varying vec3 pass_TexCoord2; + +const vec3 const_LightDirection = vec3(1.0f, 2.0f, -1.0f); void main() { - vec4 color = gl_Color; + vec4 color = pass_Color; if (uni_TextureEnabled[0]) { - color = color * texture2D(uni_PrimaryTexture, gl_TexCoord[0].st); + color = color * texture2D(uni_PrimaryTexture, pass_TexCoord0); } if (uni_TextureEnabled[1]) { - color = color * texture2D(uni_SecondaryTexture, gl_TexCoord[1].st); + color = color * texture2D(uni_SecondaryTexture, pass_TexCoord1); } if (uni_TextureEnabled[2]) { - if (gl_FrontFacing) - color.rgb *= mix(uni_ShadowColor, 1.0f, shadow2D(uni_ShadowTexture, gl_TexCoord[2].xyz).x); - else + vec3 normal = pass_Normal * (2.0f * gl_Color.x - 1.0f); + + if (dot(normal, const_LightDirection) < 0.0f) color.rgb *= uni_ShadowColor; + else + color.rgb *= mix(uni_ShadowColor, 1.0f, shadow2D(uni_ShadowTexture, pass_TexCoord2).x); } if (uni_FogEnabled) diff --git a/src/graphics/opengl/shaders/vertex_shader_21_pervertex.glsl b/src/graphics/opengl/shaders/vertex_shader_21_pervertex.glsl index e2b93157..b0bf8e67 100644 --- a/src/graphics/opengl/shaders/vertex_shader_21_pervertex.glsl +++ b/src/graphics/opengl/shaders/vertex_shader_21_pervertex.glsl @@ -44,18 +44,21 @@ uniform bool uni_LightingEnabled; uniform LightParams uni_Light[8]; varying float pass_Distance; +varying vec4 pass_Color; +varying vec3 pass_Normal; +varying vec2 pass_TexCoord0; +varying vec2 pass_TexCoord1; +varying vec3 pass_TexCoord2; void main() { vec4 position = uni_ModelMatrix * gl_Vertex; vec4 eyeSpace = uni_ViewMatrix * position; vec4 shadowCoord = uni_ShadowMatrix * position; - gl_Position = uni_ProjectionMatrix * eyeSpace; - gl_FrontColor = gl_Color; - gl_TexCoord[0] = gl_MultiTexCoord0; - gl_TexCoord[1] = gl_MultiTexCoord1; - gl_TexCoord[2] = vec4(shadowCoord.xyz / shadowCoord.w, 1.0f); - pass_Distance = abs(eyeSpace.z / eyeSpace.w); + + vec4 color = gl_Color; + + vec3 normal = normalize((uni_NormalMatrix * vec4(gl_Normal, 0.0f)).xyz); if (uni_LightingEnabled) { @@ -63,8 +66,6 @@ void main() vec4 diffuse = vec4(0.0f); vec4 specular = vec4(0.0f); - vec3 normal = normalize((uni_NormalMatrix * vec4(gl_Normal, 0.0f)).xyz); - for (int i = 0; i < 8; i++) { if (uni_Light[i].Enabled) @@ -97,6 +98,17 @@ void main() + uni_DiffuseColor * diffuse + uni_SpecularColor * specular; - gl_FrontColor = vec4(min(vec3(1.0f), result.rgb), 1.0f); + color = vec4(min(vec3(1.0f), result.rgb), 1.0f); } + + gl_Position = uni_ProjectionMatrix * eyeSpace; + gl_FrontColor = vec4(1.0f); + gl_BackColor = vec4(0.0f); + + pass_Distance = abs(eyeSpace.z / eyeSpace.w); + pass_Color = color; + pass_Normal = normal; + pass_TexCoord0 = gl_MultiTexCoord0.st; + pass_TexCoord1 = gl_MultiTexCoord1.st; + pass_TexCoord2 = shadowCoord.xyz / shadowCoord.w; }