From 67778f391a6730d9d373d6a0a4f47edfee7d08a9 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sat, 17 May 2014 14:00:26 +0200 Subject: [PATCH 1/2] Working on shadows --- assets | 2 +- src/GameWorld.cpp | 6 +++--- src/Renderer.cpp | 33 +++++++++++++++++++++++++++++++-- src/Renderer.h | 2 ++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/assets b/assets index 672e8a2..cdee701 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 672e8a2b11ecaafc95a5b0286b5ec310c62438ad +Subproject commit cdee7015793193f0e78a2b75b25e3a2c07881632 diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 19026b2..160498b 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -17,14 +17,14 @@ void GameWorld::Initialize() auto ground = CreateEntity(); auto transform = AddComponent(ground, "Transform"); transform->Position = glm::vec3(0, -5, 0); - transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); + transform->Scale = glm::vec3(800.0f, 10.0f, 800.0f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(ground, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; auto box = AddComponent(ground, "Box"); - box->Width = 200; + box->Width = 400; box->Height = 5; - box->Depth = 200; + box->Depth = 400; auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 2e781a0..d78f0b7 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -17,10 +17,10 @@ Renderer::Renderer() CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*8; + m_ShadowMapRes = 2048*16; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); - m_SunProjection = glm::ortho(-200.f, 200.f, -200.f, 200.f, -100, 200); + m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); /* Lights = 0;*/ } @@ -782,3 +782,32 @@ glm::mat4 Renderer::CreateLightMatrix(Light &_light) return model; } +void Renderer::UpdateSunProjection() +{ + glm::vec3 NDCCube[] = + { + glm::vec3(-1.f, -1.f, -1.f), + glm::vec3(1.f, -1.f, -1.f), + glm::vec3(-1.f, 1.f, -1.f), + glm::vec3(1.f, 1.f, -1.f), + glm::vec3(-1.f, -1.f, 1.f), + glm::vec3(1.f, -1.f, 1.f), + glm::vec3(-1.f, 1.f, 1.f), + glm::vec3(1.f, 1.f, 1.f) + }; + + glm::mat4 inverseProjectionViewMatrix = glm::inverse(m_Camera->ViewMatrix()) * glm::inverse(m_Camera->ProjectionMatrix()); + //Also * with world matrix for light + + for(auto corner : NDCCube) + { + //corner *= inverseProjectionViewMatrix; + } + + //Calculate the bounding box of the transformed frustum corners. This will be the view frustum for the shadow map. + + //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. +} + + + diff --git a/src/Renderer.h b/src/Renderer.h index e1a2aec..9053d92 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -141,6 +141,8 @@ private: void DrawLightScene(); void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); + void UpdateSunProjection(); + GLuint CreateQuad(); void DrawDebugShadowMap(); From 7455f4b8653e4a5cb55cd707faa8ba3f9ed1bcf3 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 19 May 2014 01:01:46 +0200 Subject: [PATCH 2/2] NormalMaps Working and started work on specular maps. --- src/Model.cpp | 118 +++++++++++++++++++++++++++++++- src/Model.h | 12 +++- src/Renderer.cpp | 17 ++++- src/Renderer.h | 2 + src/Shaders/FinalPass.frag.glsl | 2 +- src/Shaders/Fragment.glsl | 14 +++- src/Shaders/Fragment2.glsl | 1 + src/Shaders/Vertex.glsl | 6 ++ src/Texture.h | 1 + 9 files changed, 167 insertions(+), 6 deletions(-) diff --git a/src/Model.cpp b/src/Model.cpp index 547e940..ae99194 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -22,6 +22,8 @@ Model::Model(OBJ &obj, ResourceManager* rm) auto texture = std::shared_ptr(rm->Load("Texture", currentMaterial->DiffuseTexture.FileName)); // TODO: Load normal map std::shared_ptr normalMap = nullptr; + if (!currentMaterial->NormalMap.FileName.empty()) + normalMap = std::shared_ptr(rm->Load("Texture", currentMaterial->NormalMap.FileName)); // Load specular map std::shared_ptr specularMap = nullptr; if (!currentMaterial->SpecularMap.FileName.empty()) @@ -63,7 +65,9 @@ Model::Model(OBJ &obj, ResourceManager* rm) if (Vertices.size() > 0) { - CreateBuffers(Vertices, Normals, TextureCoords); + CreateTangents(); + getSimilarVertexIndex(); + CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords); } else { @@ -71,7 +75,7 @@ Model::Model(OBJ &obj, ResourceManager* rm) } } -void Model::CreateBuffers( std::vector vertices, std::vector normals, std::vectortextureCoords) +void Model::CreateBuffers( std::vector vertices, std::vector normals, std::vector tangents, std::vector biTangents, std::vectortextureCoords) { LOG_INFO("Generating VertexBuffer"); @@ -100,6 +104,32 @@ void Model::CreateBuffers( std::vector vertices, std::vector 0) + { + glBindBuffer(GL_ARRAY_BUFFER, TangentNormalsBuffer); + glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW); + GLERROR("GLEW: BufferFail, TangentNormalsBuffer"); + } + else + { + LOG_WARNING("Created empty tangent buffer!"); + } + + LOG_INFO("Generating BiTangentNormalsBuffer"); + glGenBuffers(1, &BiTangentNormalsBuffer); + if (biTangents.size() > 0) + { + glBindBuffer(GL_ARRAY_BUFFER, BiTangentNormalsBuffer); + glBufferData(GL_ARRAY_BUFFER, biTangents.size() * sizeof(glm::vec3), &biTangents[0], GL_STATIC_DRAW); + GLERROR("GLEW: BufferFail, BiTangentNormalsBuffer"); + } + else + { + LOG_WARNING("Created empty biTangent buffer!"); + } + LOG_INFO("Generating textureCoordBuffer"); glGenBuffers(1, &TextureCoordBuffer); @@ -130,10 +160,94 @@ void Model::CreateBuffers( std::vector vertices, std::vector Normals; + std::vector TangentNormals; + std::vector BiTangentNormals; std::vector TextureCoords; GLuint VertexBuffer; GLuint NormalBuffer; + GLuint TangentNormalsBuffer; + GLuint BiTangentNormalsBuffer; GLuint TextureCoordBuffer; bool Loadobj( @@ -53,9 +57,15 @@ private: void CreateBuffers( std::vector _Vertices, - std::vector _Normals, + std::vector _Normals, + std::vector _Tangents, + std::vector _BiTangents, std::vector_TextureCoords ); + void CreateTangents(); + bool IsNear(float v1, float v2); + void getSimilarVertexIndex(); + }; #endif // Model_h__ \ No newline at end of file diff --git a/src/Renderer.cpp b/src/Renderer.cpp index d78f0b7..c0878f1 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -17,7 +17,7 @@ Renderer::Renderer() CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*16; + m_ShadowMapRes = 2048*6; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); @@ -551,6 +551,15 @@ void Renderer::FrameBufferTextures() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + //Generate and bind normal texture + glGenTextures(1, &m_fSpecularTexture); + glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + /*glGenTextures(1, &m_fShadowTexture); glBindTexture(GL_TEXTURE_2D, m_fShadowTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); @@ -567,6 +576,7 @@ void Renderer::FrameBufferTextures() glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fSpecularTexture, 0); //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fShadowTexture, 0); GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); @@ -713,6 +723,11 @@ void Renderer::DrawFBOScene() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); + if (texGroup.NormalMap) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap); + } glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } } diff --git a/src/Renderer.h b/src/Renderer.h index 9053d92..29635b4 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -102,6 +102,7 @@ private: GLuint m_fDiffuseTexture; GLuint m_fPositionTexture; GLuint m_fNormalsTexture; + GLuint m_fSpecularTexture; GLuint m_fBlendTexture; GLuint m_fbLightingPass; GLuint m_fLightingTexture; @@ -142,6 +143,7 @@ private: void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); void UpdateSunProjection(); + void CreateNormalMapTangent(); GLuint CreateQuad(); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 2a6cf4c..0bdb1b1 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -24,6 +24,6 @@ void main() vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); - //FragmentColor = ShadowTexel; + //FragmentColor = DiffuseTexel; } \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index cc5370f..87a95fa 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -2,6 +2,9 @@ layout (binding=0) uniform sampler2D DiffuseTexture; layout (binding=1) uniform sampler2D ShadowTexture; +layout (binding=2) uniform sampler2D NormalMapTexture; +layout (binding=3) uniform sampler2D SpecularMapTexture; + in VertexData { @@ -9,11 +12,14 @@ in VertexData vec3 Normal; vec2 TextureCoord; vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; } Input; out vec4 frag_Diffuse; out vec4 frag_Position; out vec4 frag_Normal; +out vec4 frag_specular; float Shadow(vec4 ShadowCoord) { @@ -32,6 +38,7 @@ float Shadow(vec4 ShadowCoord) void main() { + // Diffuse Texture frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord); @@ -39,5 +46,10 @@ void main() frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal - frag_Normal = vec4(Input.Normal, 0.0); + mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.Normal)); + frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); + //frag_Normal = vec4(Input.Normal, 0.0); + + //G-buffer Specular + frag_specular = texture(SpecularMapTexture, Input.TextureCoord); } \ No newline at end of file diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index f531f76..5ded0a2 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -81,4 +81,5 @@ void main() vec4 NormalTexel = texture(NormalsTexture, TextureCoord); FragColor = phong(vec3(PositionTexel), vec3(NormalTexel)); + //FragColor = NormalTexel; } \ No newline at end of file diff --git a/src/Shaders/Vertex.glsl b/src/Shaders/Vertex.glsl index 2f799ef..4c63e0d 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -9,6 +9,8 @@ uniform mat4 DepthMVP; layout (location = 0) in vec3 Position; layout (location = 1) in vec3 Normal; layout (location = 2) in vec2 TextureCoord; +layout (location = 3) in vec3 Tangent; +layout (location = 4) in vec3 BiTangent; out VertexData { @@ -16,6 +18,8 @@ out VertexData vec3 Normal; vec2 TextureCoord; vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; } Output; void main() @@ -26,4 +30,6 @@ void main() Output.Normal = normalize(vec3(inverse(transpose(V * M)) * vec4(Normal, 0.0))); Output.TextureCoord = TextureCoord; Output.ShadowCoord = DepthMVP * vec4(Position, 1.0); + Output.Tangent = normalize(vec3(inverse(transpose(V * M)) * vec4(Tangent, 0.0))); + Output.BiTangent = normalize(vec3(inverse(transpose(V * M)) * vec4(BiTangent, 0.0))); } \ No newline at end of file diff --git a/src/Texture.h b/src/Texture.h index c958213..a8c48bb 100755 --- a/src/Texture.h +++ b/src/Texture.h @@ -24,4 +24,5 @@ private: std::unordered_map m_TextureCache; }; + #endif // Texture_h__