From 0382708b85eb1e19226f193faa54ab337de76b6f Mon Sep 17 00:00:00 2001 From: Teejoon Date: Thu, 10 Mar 2016 19:57:01 +0100 Subject: [PATCH] Fixed so that we could use DTX5nm as normal map --- assets | 2 +- include/Engine/Rendering/Texture.h | 4 ++- include/Engine/Rendering/TextureSprite.h | 8 +---- resources/Shaders/ForwardPlus.frag.glsl | 13 +++----- .../Shaders/ForwardPlusSplatMapRGB.frag.glsl | 18 ++++++++--- resources/Shaders/Util/CommonNormalFunc.glsl | 21 +++++++++++++ src/Engine/Rendering/DrawFinalPass.cpp | 31 +++++++++++-------- src/Engine/Rendering/Texture.cpp | 3 ++ 8 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 resources/Shaders/Util/CommonNormalFunc.glsl diff --git a/assets b/assets index e8d98c2d..1944662a 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit e8d98c2daeb51c7d84d1407593e70e439cd91638 +Subproject commit 1944662a1ca7db56610bb3215fe1ea6e15285fe1 diff --git a/include/Engine/Rendering/Texture.h b/include/Engine/Rendering/Texture.h index 5720fe1e..78f3cfc2 100644 --- a/include/Engine/Rendering/Texture.h +++ b/include/Engine/Rendering/Texture.h @@ -20,7 +20,9 @@ public: void Bind(GLenum textureUnit = GL_TEXTURE0); GLuint m_Texture = 0; - + + enum TextureType : GLint { Invalid = 0, DDS = 1, PNG = 2}; + TextureType m_Type; }; #endif diff --git a/include/Engine/Rendering/TextureSprite.h b/include/Engine/Rendering/TextureSprite.h index f8527664..312c58e9 100644 --- a/include/Engine/Rendering/TextureSprite.h +++ b/include/Engine/Rendering/TextureSprite.h @@ -14,13 +14,7 @@ protected: TextureSprite(std::string path); public: - ~TextureSprite(); - - void Bind(GLenum textureUnit = GL_TEXTURE0); - - GLuint m_Texture = 0; - unsigned char* Data = nullptr; - + ~TextureSprite() {}; }; #endif diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 112d8de5..3dd69a28 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -1,8 +1,11 @@ #version 430 +#include "Shaders/Util/CommonNormalFunc.glsl" + #define MIN_AMBIENT_LIGHT 0.3 #define MAX_SPLITS 4 + uniform mat4 VM; uniform mat4 M; uniform mat4 V; @@ -18,6 +21,7 @@ uniform vec3 CameraPosition; uniform int SSAOQuality; uniform float FarDistance[MAX_SPLITS]; +uniform int NormalTextureType; uniform vec2 DiffuseUVRepeat; uniform vec2 NormalUVRepeat; uniform vec2 SpecularUVRepeat; @@ -140,13 +144,6 @@ 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); -} - // Returns a "random" value. float Random(vec3 seed, int i) { @@ -303,7 +300,7 @@ void main() vec4 glowTexel = texture2D(GlowMapTexture, Input.TextureCoordinate * GlowUVRepeat); vec4 specularTexel = texture2D(SpecularMapTexture, Input.TextureCoordinate * SpecularUVRepeat); vec4 position = VM * vec4(Input.Position, 1.0); - vec4 normal = V * CalcNormalMappedValue(Input.Normal, Input.Tangent, Input.BiTangent, Input.TextureCoordinate * NormalUVRepeat, NormalMapTexture); + vec4 normal = V * CalcNormalMappedValue(Input.Normal, Input.Tangent, Input.BiTangent, Input.TextureCoordinate * NormalUVRepeat, NormalMapTexture, NormalTextureType); normal = normalize(normal); //vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); vec4 viewVec = normalize(-position); diff --git a/resources/Shaders/ForwardPlusSplatMapRGB.frag.glsl b/resources/Shaders/ForwardPlusSplatMapRGB.frag.glsl index d20a0c1c..fb9081b2 100644 --- a/resources/Shaders/ForwardPlusSplatMapRGB.frag.glsl +++ b/resources/Shaders/ForwardPlusSplatMapRGB.frag.glsl @@ -1,5 +1,7 @@ #version 430 +#include "Shaders/Util/CommonNormalFunc.glsl" + #define MIN_AMBIENT_LIGHT 0.3 #define MAX_SPLITS 4 @@ -20,6 +22,10 @@ layout (binding = 0) uniform sampler2D AOTexture; layout (binding = 30) uniform sampler2DArrayShadow DepthMap; //Get bineded at the same time as the textures +uniform int NormalTextureType1; +uniform int NormalTextureType2; +uniform int NormalTextureType3; + uniform vec2 DiffuseUVRepeat1; uniform vec2 DiffuseUVRepeat2; uniform vec2 DiffuseUVRepeat3; @@ -182,11 +188,12 @@ vec4 CalcBlendedTexel(vec4 blendValue, sampler2D R, sampler2D G, sampler2D B, } vec4 CalcBlendedNormal(vec4 blendValue, sampler2D R, sampler2D G, sampler2D B, - vec2 R_TileValues, vec2 G_TileValues, vec2 B_TileValues){ + vec2 R_TileValues, vec2 G_TileValues, vec2 B_TileValues + int R_TextureType, int G_TextureType, int B_TextureType) { mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal); - vec3 R_Channel = texture(R, Input.TextureCoordinate * R_TileValues).xyz * 2.0 - vec3(1.0); - vec3 G_Channel = texture(G, Input.TextureCoordinate * G_TileValues).xyz * 2.0 - vec3(1.0); - vec3 B_Channel = texture(B, Input.TextureCoordinate * B_TileValues).xyz * 2.0 - vec3(1.0); + vec3 R_Channel = NormalMapValue(Input.TextureCoordinate * R_TileValues, R, R_TextureType); + vec3 G_Channel = NormalMapValue(Input.TextureCoordinate * G_TileValues, G, G_TextureType); + vec3 B_Channel = NormalMapValue(Input.TextureCoordinate * B_TileValues, B, B_TextureType); float total = blendValue.r + blendValue.g + blendValue.b + blendValue.a; float totalDiv = 1 / total; @@ -365,7 +372,8 @@ void main() vec4 position = VM * vec4(Input.Position, 1.0); //vec4 normal = V * CalcNormalMappedValue(Input.Normal, Input.Tangent, Input.BiTangent, Input.TextureCoordinate, SplatMapTexture); vec4 normal = V * CalcBlendedNormal(splatTexel, NormalMapTexture1, NormalMapTexture2, NormalMapTexture3, - NormalUVRepeat1, NormalUVRepeat2, NormalUVRepeat3); + NormalUVRepeat1, NormalUVRepeat2, NormalUVRepeat3, + NormalTextureType1, NormalTextureType2, NormalTextureType3); normal = normalize(normal); //vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); vec4 viewVec = normalize(-position); diff --git a/resources/Shaders/Util/CommonNormalFunc.glsl b/resources/Shaders/Util/CommonNormalFunc.glsl new file mode 100644 index 00000000..aa748acc --- /dev/null +++ b/resources/Shaders/Util/CommonNormalFunc.glsl @@ -0,0 +1,21 @@ +#define NORMAL_DDS 1 +#define NORMAL_PNG 2 + +vec3 NormalMapValue(vec2 textureCoordinate, sampler2D normalMap, int normalTextureType){ + if(normalTextureType == NORMAL_DDS){ + vec3 normal; + normal.xy = texture(normalMap, textureCoordinate).wy * 2.0 - vec2(1.0); + normal.z = sqrt(1.0 - clamp(dot(normal.xy, normal.xy), 0.0, 1.0)); + return normal; + } else { + //should be PNG or anything that doesn't need special support + return texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0); + } +} + +vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap, int normalTextureType) +{ + mat3 TBN = mat3(tangent, bitangent, normal); + vec3 NormalMap = NormalMapValue(textureCoordinate, normalMap, normalTextureType); + return vec4(TBN * normalize(NormalMap), 0.0); +} \ No newline at end of file diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index 67a67351..17a7e51e 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -1527,10 +1527,12 @@ void DrawFinalPass::BindModelTextures(GLuint shaderHandle, std::shared_ptrNormalTexture.size() > 0 && job->NormalTexture[0]->Texture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->NormalTexture[0]->Texture->m_Texture); glUniform2fv(glGetUniformLocation(shaderHandle, "NormalUVRepeat"), 1, glm::value_ptr(job->NormalTexture[0]->UVRepeat)); + glUniform1i(glGetUniformLocation(shaderHandle, "NormalTextureType"), job->NormalTexture[0]->Texture->m_Type); } else { glBindTexture(GL_TEXTURE_2D, m_NeutralNormalTexture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, "NormalUVRepeat"), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + //glUniform2fv(glGetUniformLocation(shaderHandle, "NormalUVRepeat"), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + glUniform1i(glGetUniformLocation(shaderHandle, "NormalTextureType"), m_NeutralNormalTexture->m_Type); } glActiveTexture(GL_TEXTURE3); @@ -1562,57 +1564,60 @@ void DrawFinalPass::BindModelTextures(GLuint shaderHandle, std::shared_ptrDiffuseTexture.size() > i && job->DiffuseTexture[i]->Texture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->DiffuseTexture[i]->Texture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->DiffuseTexture[i]->UVRepeat)); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->DiffuseTexture[i]->UVRepeat)); } else { glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); } texturePosition++; } //Bind 5 Normal textures - UniformName = "NormalUVRepeat"; + std::string textureType = "NormalTextureType"; + UVRepeatName = "NormalUVRepeat"; for (unsigned int i = 0; i < 3; i++) { glActiveTexture(texturePosition); if (job->NormalTexture.size() > i && job->NormalTexture[i]->Texture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->NormalTexture[i]->Texture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->NormalTexture[i]->UVRepeat)); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->NormalTexture[i]->UVRepeat)); + glUniform1i(glGetUniformLocation(shaderHandle, std::string(textureType + ((char)(i + '1'))).c_str()), job->NormalTexture[i]->Texture->m_Type); } else { glBindTexture(GL_TEXTURE_2D, m_NeutralNormalTexture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + glUniform1i(glGetUniformLocation(shaderHandle, std::string(textureType + ((char)(i + '1'))).c_str()), m_NeutralNormalTexture->m_Type); } texturePosition++; } //Bind 5 Specular textures - UniformName = "SpecularUVRepeat"; + UVRepeatName = "SpecularUVRepeat"; for (unsigned int i = 0; i < 3; i++) { glActiveTexture(texturePosition); if (job->SpecularTexture.size() > i && job->SpecularTexture[i]->Texture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->SpecularTexture[i]->Texture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->SpecularTexture[i]->UVRepeat)); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->SpecularTexture[i]->UVRepeat)); } else { glBindTexture(GL_TEXTURE_2D, m_GreyTexture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); } texturePosition++; } //Bind 5 Incandescence textures - UniformName = "GlowUVRepeat"; + UVRepeatName = "GlowUVRepeat"; for (unsigned int i = 0; i < 3; i++) { glActiveTexture(texturePosition); if (job->IncandescenceTexture.size() > i && job->IncandescenceTexture[i]->Texture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->IncandescenceTexture[i]->Texture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->IncandescenceTexture[i]->UVRepeat)); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(job->IncandescenceTexture[i]->UVRepeat)); } else { glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); - glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UniformName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); + glUniform2fv(glGetUniformLocation(shaderHandle, std::string(UVRepeatName + ((char)(i + '1'))).c_str()), 1, glm::value_ptr(glm::vec2(1.0f, 1.0f))); } texturePosition++; } diff --git a/src/Engine/Rendering/Texture.cpp b/src/Engine/Rendering/Texture.cpp index 9a81b472..3b68d818 100644 --- a/src/Engine/Rendering/Texture.cpp +++ b/src/Engine/Rendering/Texture.cpp @@ -6,9 +6,12 @@ Texture::Texture(std::string path) Image* img; if (ext == ".png") { img = ResourceManager::Load(path); + m_Type = TextureType::PNG; } else if (ext == ".dds") { img = ResourceManager::Load(path); + m_Type = TextureType::DDS; } else { + m_Type = TextureType::Invalid; throw Resource::FailedLoadingException("Texture extension is not .png nor .dds"); }