From a954cbbf50768499df34505119926c6846334761 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 13:52:27 +0100 Subject: [PATCH] Normal map support --- include/Engine/Rendering/DrawFinalPass.h | 1 + resources/Shaders/ForwardPlus.frag.glsl | 31 ++++++++++-------------- resources/Shaders/ForwardPlus.vert.glsl | 4 +++ src/Engine/Rendering/DrawBloomPass.cpp | 2 +- src/Engine/Rendering/DrawFinalPass.cpp | 11 ++++++++- src/Engine/Rendering/Renderer.cpp | 2 +- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index d57770e7..acb5cd13 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -40,6 +40,7 @@ private: Texture* m_WhiteTexture; Texture* m_BlackTexture; + Texture* m_NeutralNormalTexture; FrameBuffer m_FinalPassFrameBuffer; GLuint m_BloomTexture; diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 42500b44..7d27ca04 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -9,7 +9,8 @@ uniform vec2 ScreenDimensions; uniform vec4 FillColor; uniform float FillPercentage; layout (binding = 0) uniform sampler2D DiffuseTexture; -layout (binding = 1) uniform sampler2D GlowMap; +layout (binding = 1) uniform sampler2D NormalMapTexture; +layout (binding = 2) uniform sampler2D GlowMapTexture; #define TILE_SIZE 16 @@ -49,6 +50,8 @@ layout (std430, binding = 4) buffer LightIndexBuffer in VertexData{ vec3 Position; vec3 Normal; + vec3 Tangent; + vec3 BiTangent; vec2 TextureCoordinate; vec4 ExplosionColor; }Input; @@ -102,13 +105,20 @@ LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensi return result; } +vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap) +{ + mat3 TBN = mat3(tangent, bitangent, normal); + vec3 NormalMap = texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0); + return vec4(TBN * normalize(NormalMap), 0.0); +} void main() { vec4 diffuseTexel = texture2D(DiffuseTexture, Input.TextureCoordinate); - vec4 glowTexel = texture2D(GlowMap, Input.TextureCoordinate); + vec4 glowTexel = texture2D(GlowMapTexture, Input.TextureCoordinate); vec4 position = V * M * vec4(Input.Position, 1.0); - vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); + vec4 normal = V * CalcNormalMappedValue(Input.Normal, Input.Tangent, Input.BiTangent, Input.TextureCoordinate, NormalMapTexture); + //vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); vec4 viewVec = normalize(-position); vec2 tilePos; @@ -147,25 +157,10 @@ void main() if(pos <= FillPercentage) { color_result += FillColor; } - //bloomColor = vec4(0.3, 0.8, 0.6, 1.0); sceneColor = vec4(color_result.xyz, clamp(color_result.a, 0, 1)); - //These if statements should be removed if they are slow. color_result += glowTexel; - - - bloomColor = vec4(clamp(color_result.xyz - 1.0, 0, 100), 1.0); - /* - if(color_result.x > 1 || color_result.y > 1 || color_result.z > 1) { - bloomColor = vec4(color_result.xyz, 1.0); - } else { - bloomColor = vec4(0.0, 0.0, 0.0, 1.0); - } */ - - - - //Tiled Debug Code /* diff --git a/resources/Shaders/ForwardPlus.vert.glsl b/resources/Shaders/ForwardPlus.vert.glsl index bd7df47e..1a7cca12 100644 --- a/resources/Shaders/ForwardPlus.vert.glsl +++ b/resources/Shaders/ForwardPlus.vert.glsl @@ -16,6 +16,8 @@ layout(location = 6) in vec4 BoneWeights; out VertexData{ vec3 Position; vec3 Normal; + vec3 Tangent; + vec3 BiTangent; vec2 TextureCoordinate; vec4 ExplosionColor; }Output; @@ -37,5 +39,7 @@ void main() Output.Position = (boneTransform * vec4(Position, 1.0)).xyz; Output.TextureCoordinate = TextureCoords; Output.Normal = vec3(M * vec4(Normal, 0.0)); + Output.Tangent = vec3(M * vec4(Tangent, 0.0)); + Output.BiTangent = vec3(M * vec4(BiTangent, 0.0)); Output.ExplosionColor = vec4(0.0); } \ No newline at end of file diff --git a/src/Engine/Rendering/DrawBloomPass.cpp b/src/Engine/Rendering/DrawBloomPass.cpp index 8fe1d807..5599b9e0 100644 --- a/src/Engine/Rendering/DrawBloomPass.cpp +++ b/src/Engine/Rendering/DrawBloomPass.cpp @@ -13,7 +13,7 @@ DrawBloomPass::DrawBloomPass(IRenderer* renderer) void DrawBloomPass::InitializeTextures() { - m_WhiteTexture = ResourceManager::Load("Textures/Core/Blank.png"); + m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); } void DrawBloomPass::InitializeShaderPrograms() diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index 4fd0c2aa..a284a773 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -11,8 +11,9 @@ DrawFinalPass::DrawFinalPass(IRenderer* renderer, LightCullingPass* lightCulling void DrawFinalPass::InitializeTextures() { - m_WhiteTexture = ResourceManager::Load("Textures/Core/Blank.png"); + m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); m_BlackTexture = ResourceManager::Load("Textures/Core/Black.png"); + m_NeutralNormalTexture = ResourceManager::Load("Textures/Core/NeutralNormalMap.png"); } void DrawFinalPass::InitializeFrameBuffers() @@ -230,7 +231,15 @@ void DrawFinalPass::BindModelTextures(std::shared_ptr& job) } else { glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); } + glActiveTexture(GL_TEXTURE1); + if (job->NormalTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->NormalTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_NeutralNormalTexture->m_Texture); + } + + glActiveTexture(GL_TEXTURE2); if (job->IncandescenceTexture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->IncandescenceTexture->m_Texture); } else { diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 81f1a1cc..d12a1d1d 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -143,7 +143,7 @@ PickData Renderer::Pick(glm::vec2 screenCoord) void Renderer::InitializeTextures() { m_ErrorTexture = ResourceManager::Load("Textures/Core/ErrorTexture.png"); - m_WhiteTexture = ResourceManager::Load("Textures/Core/Blank.png"); + m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); }