From 9ccf8ecc428aae328fa20549c415e0002b61f30a Mon Sep 17 00:00:00 2001 From: Tleety Date: Fri, 30 May 2014 19:53:00 +0200 Subject: [PATCH] Fixed being able to draw sprites and normal/specular maps for terrain --- assets | 2 +- src/Components/BlendMap.h | 12 +++++++ src/GUI/WorldFrame.h | 43 +++++++++++++++++++++----- src/GameWorld.cpp | 9 ++++-- src/Model.cpp | 2 +- src/RenderQueue.h | 6 ++++ src/Renderer.cpp | 32 ++++++++++++++++++- src/Shaders/BlendMap.frag.glsl | 30 +++++++++++++++--- src/Shaders/ForwardRendering.frag.glsl | 2 +- 9 files changed, 119 insertions(+), 19 deletions(-) diff --git a/assets b/assets index 20edf79..5de9bdc 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 20edf7934ae395a169ab175d17c7da28b7628f29 +Subproject commit 5de9bdcff878ab06a589101cf718fb74c3344e14 diff --git a/src/Components/BlendMap.h b/src/Components/BlendMap.h index 3106e04..aba9f7c 100644 --- a/src/Components/BlendMap.h +++ b/src/Components/BlendMap.h @@ -10,13 +10,25 @@ namespace Components { BlendMap() : TextureRed("Textures/ErrorTextureRed.png") + , TextureRedNormal("Textures/NeutralNormalMap.png") + , TextureRedSpecular("Textures/NeutralSpecularMap.png") , TextureGreen("Textures/ErrorTextureGreen.png") + , TextureGreenNormal("Textures/NeutralNormalMap.png") + , TextureGreenSpecular("Textures/NeutralSpecularMap.png") , TextureBlue("Textures/ErrorTextureBlue.png") + , TextureBlueNormal("Textures/NeutralNormalMap.png") + , TextureBlueSpecular("Textures/NeutralSpecularMap.png") , TextureRepeats(100.f) { } std::string TextureRed; + std::string TextureRedNormal; + std::string TextureRedSpecular; std::string TextureGreen; + std::string TextureGreenNormal; + std::string TextureGreenSpecular; std::string TextureBlue; + std::string TextureBlueNormal; + std::string TextureBlueSpecular; float TextureRepeats; virtual BlendMap* Clone() const override { return new BlendMap(*this); } diff --git a/src/GUI/WorldFrame.h b/src/GUI/WorldFrame.h index 89f2ec0..a0c76e8 100644 --- a/src/GUI/WorldFrame.h +++ b/src/GUI/WorldFrame.h @@ -61,11 +61,24 @@ public: auto blendmapComponent = m_World->GetComponent(entity); if (blendmapComponent) { - auto textureRed = ResourceManager->Load("Texture", blendmapComponent->TextureRed); - auto textureGreen = ResourceManager->Load("Texture", blendmapComponent->TextureGreen); - auto textureBlue = ResourceManager->Load("Texture", blendmapComponent->TextureBlue); + BlendMapTexture RedTexture; + RedTexture.Diffuse = *ResourceManager->Load("Texture", blendmapComponent->TextureRed); + RedTexture.Normal = *ResourceManager->Load("Texture", blendmapComponent->TextureRedNormal); + RedTexture.Specular = *ResourceManager->Load("Texture", blendmapComponent->TextureRedSpecular); + + BlendMapTexture GreenTexture; + GreenTexture.Diffuse = *ResourceManager->Load("Texture", blendmapComponent->TextureGreen); + GreenTexture.Normal = *ResourceManager->Load("Texture", blendmapComponent->TextureGreenNormal); + GreenTexture.Specular = *ResourceManager->Load("Texture", blendmapComponent->TextureGreenSpecular); + + BlendMapTexture BlueTexture; + BlueTexture.Diffuse = *ResourceManager->Load("Texture", blendmapComponent->TextureBlue); + BlueTexture.Normal = *ResourceManager->Load("Texture", blendmapComponent->TextureBlueNormal); + BlueTexture.Specular = *ResourceManager->Load("Texture", blendmapComponent->TextureBlueSpecular); + float textureRepeat = blendmapComponent->TextureRepeats; - EnqueueBlendMapModel(modelAsset, textureRed, textureGreen, textureBlue, textureRepeat, modelMatrix); + + EnqueueBlendMapModel(modelAsset, RedTexture, GreenTexture, BlueTexture, textureRepeat, modelMatrix); } else { @@ -113,6 +126,13 @@ protected: std::shared_ptr m_World; private: + struct BlendMapTexture + { + GLuint Diffuse; + GLuint Normal; + GLuint Specular; + }; + EventRelay m_ESetViewportCamera; bool OnSetViewportCamera(const Events::SetViewportCamera &event) { @@ -157,7 +177,7 @@ private: } } - void EnqueueBlendMapModel(Model* model, Texture* textureRed, Texture* textureGreen, Texture* textureBlue, float textureRepeat, glm::mat4 modelMatrix) + void EnqueueBlendMapModel(Model* model, BlendMapTexture textureRed, BlendMapTexture textureGreen, BlendMapTexture textureBlue, float textureRepeat, glm::mat4 modelMatrix) { for (auto texGroup : model->TextureGroups) { @@ -166,9 +186,15 @@ private: job.DiffuseTexture = *texGroup.Texture; job.NormalTexture = (texGroup.NormalMap) ? *texGroup.NormalMap : 0; job.SpecularTexture = (texGroup.SpecularMap) ? *texGroup.SpecularMap : 0; - job.BlendMapTextureRed = (*textureRed); - job.BlendMapTextureGreen = (*textureGreen); - job.BlendMapTextureBlue = (*textureBlue); + job.BlendMapTextureRed = textureRed.Diffuse; + job.BlendMapTextureRedNormal = textureRed.Normal; + job.BlendMapTextureRedSpecular = textureRed.Specular; + job.BlendMapTextureGreen = textureGreen.Diffuse; + job.BlendMapTextureGreenNormal = textureGreen.Normal; + job.BlendMapTextureGreenSpecular = textureGreen.Specular; + job.BlendMapTextureBlue = textureBlue.Diffuse; + job.BlendMapTextureBlueNormal = textureBlue.Normal; + job.BlendMapTextureBlueSpecular = textureBlue.Specular; job.TextureRepeat = textureRepeat; job.VAO = model->VAO; job.StartIndex = texGroup.StartIndex; @@ -188,6 +214,7 @@ private: RenderQueue.Forward.Add(job); } + }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index d5d4a1d..3f2d2b9 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -106,9 +106,12 @@ void GameWorld::Initialize() auto model = AddComponent(ground_middle); model->ModelFile = "Models/TerrainFiveIstles/Middle.obj"; auto blendmap = AddComponent(ground_middle); - blendmap->TextureRed = "Textures/Ground/Sand.png"; - blendmap->TextureGreen = "Textures/Ground/Grass.png"; - blendmap->TextureBlue = "Textures/Ground/Rock.png"; + blendmap->TextureRed = "Textures/Ground/SoilBeach0087_11_S.jpg"; + blendmap->TextureBlueNormal = "Textures/Ground/SoilBeach0087_11_SNM.png"; + blendmap->TextureGreen = "Textures/Ground/Grass0126_2_S.jpg"; + blendmap->TextureBlueNormal = "Textures/Ground/Grass0126_2_SNM.png"; + blendmap->TextureBlue = "Textures/Ground/Cliffs2.png"; + blendmap->TextureBlueNormal = "Textures/Ground/Cliffs2NM.png"; blendmap->TextureRepeats = 30.f; auto physics = AddComponent(ground_middle); diff --git a/src/Model.cpp b/src/Model.cpp index 1186671..dd5757c 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -93,7 +93,7 @@ Model::Model(std::shared_ptr rm, OBJ &obj) if (Vertices.size() > 0) { CreateTangents(); - //getSimilarVertexIndex(); + getSimilarVertexIndex(); CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords); } else diff --git a/src/RenderQueue.h b/src/RenderQueue.h index a38de8a..69eed26 100644 --- a/src/RenderQueue.h +++ b/src/RenderQueue.h @@ -49,8 +49,14 @@ struct ModelJob : RenderJob struct BlendMapModelJob : ModelJob { GLuint BlendMapTextureRed; + GLuint BlendMapTextureRedNormal; + GLuint BlendMapTextureRedSpecular; GLuint BlendMapTextureGreen; + GLuint BlendMapTextureGreenNormal; + GLuint BlendMapTextureGreenSpecular; GLuint BlendMapTextureBlue; + GLuint BlendMapTextureBlueNormal; + GLuint BlendMapTextureBlueSpecular; float TextureRepeat; }; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 99f7c61..1476306 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -490,6 +490,7 @@ void Renderer::ForwardRendering(RenderQueue &rq) glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection)); + glUniform4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "Color"), 1, glm::value_ptr(modelJob->Color)); glBindVertexArray(modelJob->VAO); @@ -509,6 +510,23 @@ void Renderer::ForwardRendering(RenderQueue &rq) continue; } + + auto spriteJob = std::dynamic_pointer_cast(job); + if (spriteJob) + { + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniform4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "Color"), 1, glm::value_ptr(spriteJob->Color)); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, spriteJob->Texture); + glBindVertexArray(m_ScreenQuad); + glDrawArrays(GL_TRIANGLES, 0, 6); + + continue; + } } //glDepthMask (GL_TRUE); //glDisable (GL_BLEND); @@ -1142,9 +1160,21 @@ void Renderer::DrawFBOScene(RenderQueue &rq) glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureRed); glActiveTexture(GL_TEXTURE5); - glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreen); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureRedNormal); glActiveTexture(GL_TEXTURE6); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureRedSpecular); + glActiveTexture(GL_TEXTURE7); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreen); + glActiveTexture(GL_TEXTURE8); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreenNormal); + glActiveTexture(GL_TEXTURE9); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreenSpecular); + glActiveTexture(GL_TEXTURE10); glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureBlue); + glActiveTexture(GL_TEXTURE11); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureBlueNormal); + glActiveTexture(GL_TEXTURE12); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureBlueSpecular); glDrawArrays(GL_TRIANGLES, blendMapJob->StartIndex, blendMapJob->EndIndex - blendMapJob->StartIndex + 1); diff --git a/src/Shaders/BlendMap.frag.glsl b/src/Shaders/BlendMap.frag.glsl index 038894b..2e128be 100644 --- a/src/Shaders/BlendMap.frag.glsl +++ b/src/Shaders/BlendMap.frag.glsl @@ -7,8 +7,14 @@ layout (binding=3) uniform sampler2D SpecularMapTexture; //TerrainTextures layout (binding=4) uniform sampler2D TextureRed; -layout (binding=5) uniform sampler2D TextureGreen; -layout (binding=6) uniform sampler2D TextureBlue; +layout (binding=5) uniform sampler2D TextureRedNormal; +layout (binding=6) uniform sampler2D TextureRedSpecular; +layout (binding=7) uniform sampler2D TextureGreen; +layout (binding=8) uniform sampler2D TextureGreenNormal; +layout (binding=9) uniform sampler2D TextureGreenSpecular; +layout (binding=10) uniform sampler2D TextureBlue; +layout (binding=11) uniform sampler2D TextureBlueNormal; +layout (binding=12) uniform sampler2D TextureBlueSpecular; uniform float TextureRepeats; //Determines how many times the textures will loop over the terrain uniform vec3 SunDirection_cameraspace; @@ -61,21 +67,37 @@ void main() vec4 BlendMap = texture(DiffuseTexture, Input.TextureCoord); vec4 TextureRedTexel = texture(TextureRed, Input.TextureCoord * TextureRepeats); + vec4 TextureRedTexelNormal = texture(TextureRedNormal, Input.TextureCoord * TextureRepeats); + //vec4 TextureRedTexelSpecular = texture(TextureRedSpecular, Input.TextureCoord * TextureRepeats); + vec4 TextureGreenTexel = texture(TextureGreen, Input.TextureCoord * TextureRepeats); + vec4 TextureGreenTexelNormal = texture(TextureGreenNormal, Input.TextureCoord * TextureRepeats); + //vec4 TextureGreenTexelSpecular = texture(TextureGreenSpecular, Input.TextureCoord * TextureRepeats); + vec4 TextureBlueTexel = texture(TextureBlue, Input.TextureCoord * TextureRepeats); + vec4 TextureBlueTexelNormal = texture(TextureBlueNormal, Input.TextureCoord * TextureRepeats); + //vec4 TextureBlueTexelSpecular = texture(TextureBlueSpecular, Input.TextureCoord * TextureRepeats); //Mix the Terrain-textures together TextureRedTexel *= BlendMap.r; TextureGreenTexel = mix(TextureRedTexel, TextureGreenTexel, BlendMap.g); vec4 finalBlendTexel = mix(TextureGreenTexel, TextureBlueTexel, BlendMap.b); + + TextureRedTexelNormal *= BlendMap.r; + TextureGreenTexelNormal = mix(TextureRedTexelNormal, TextureGreenTexelNormal, BlendMap.g); + vec4 finalBlendTexelNormal = mix(TextureGreenTexelNormal, TextureBlueTexelNormal, BlendMap.b); + + //TextureRedTexelSpecular *= BlendMap.r; + //TextureGreenTexelSpecular = mix(TextureRedTexelSpecular, TextureGreenTexelSpecular, BlendMap.g); + //vec4 finalBlendTexelSpecular = mix(TextureGreenTexelSpecular, TextureBlueTexelSpecular, BlendMap.b); // G-buffer Position frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal); - frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); - //frag_Diffuse = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); + frag_Normal = normalize(vec4(TBN * vec3(finalBlendTexelNormal), 0.0)); + //frag_Diffuse = normalize(vec4(TBN * vec3(finalBlendTexelNormal), 0.0)); //frag_Normal = vec4(Input.Normal, 0.0); // Diffuse Texture diff --git a/src/Shaders/ForwardRendering.frag.glsl b/src/Shaders/ForwardRendering.frag.glsl index 8a56b40..0d3a7c8 100644 --- a/src/Shaders/ForwardRendering.frag.glsl +++ b/src/Shaders/ForwardRendering.frag.glsl @@ -16,5 +16,5 @@ void main() { // Texture vec4 texel = texture(texture0, Input.TextureCoord); - frag_Diffuse = texel; + frag_Diffuse = texel * Color; } \ No newline at end of file