diff --git a/src/Camera.cpp b/src/Camera.cpp index 8fba3e2..6e8bc0d 100755 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -12,10 +12,10 @@ Camera::Camera(float yFOV, float nearClip, float farClip) UpdateViewMatrix(); } -//glm::vec3 Camera::Forward() -//{ -// return glm::rotate(glm::vec3(0.f, 0.f, -1.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); -//} +glm::vec3 Camera::Forward() +{ + return m_Orientation * glm::vec3(0, 0, -1); +} // //glm::vec3 Camera::Right() //{ diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index 9037b89..1f74165 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -16,9 +16,11 @@ struct PointLight : Component , ConstantAttenuation(1.0f) , LinearAttenuation(0.f) , QuadraticAttenuation(3.f) + , Radius(5.f) { } float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + float Radius; Color color; glm::vec3 Specular; diff --git a/src/Model.cpp b/src/Model.cpp index d938bd6..1186671 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -23,11 +23,23 @@ Model::Model(std::shared_ptr rm, OBJ &obj) // TODO: Load normal map std::shared_ptr normalMap = nullptr; if (!currentMaterial->NormalMap.FileName.empty()) + { normalMap = std::shared_ptr(rm->Load("Texture", currentMaterial->NormalMap.FileName)); + } + else + { + normalMap = std::shared_ptr(rm->Load("Texture", "Textures/NeutralNormalMap.png")); + } // Load specular map std::shared_ptr specularMap = nullptr; if (!currentMaterial->SpecularMap.FileName.empty()) + { specularMap = std::shared_ptr(rm->Load("Texture", currentMaterial->SpecularMap.FileName)); + } + else + { + specularMap = std::shared_ptr(rm->Load("Texture", "Textures/NeutralSpecularMap.png")); + } // TODO: Load material parameters // Create new texture group (start index of new group is upcoming index) @@ -36,6 +48,21 @@ Model::Model(std::shared_ptr rm, OBJ &obj) currentTexGroup = &TextureGroups.back(); } + /*std::unordered_map similarNormals; + std::unordered_map normalCount; + for (auto &faceDef : face.Definitions) + { + if (faceDef.NormalIndex == 0) + continue; + + + similarNormals[faceDef.VertexIndex - 1] += normal; + normalCount[faceDef.VertexIndex - 1]++; + int index = pair.first; + glm::vec3 averagedNormal = ; + Normals[] + }*/ + // Face definitions for (auto faceDef : face.Definitions) { @@ -178,7 +205,7 @@ void Model::CreateBuffers( std::vector vertices, std::vector(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); + m_SunProjection_height = glm::vec2(-40.f, 40.f); + m_SunProjection_width = glm::vec2(-40.f, 40.f); + m_SunProjection_length = glm::vec2(-50.f, 50.f); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); /* Lights = 0;*/ } @@ -108,6 +111,11 @@ void Renderer::LoadContent() m_ShaderProgramSkybox.Compile(); m_ShaderProgramSkybox.Link();*/ + m_ForwardRendering.AddShader(std::shared_ptr(new VertexShader("Shaders/ForwardRendering.vert.glsl"))); + m_ForwardRendering.AddShader(std::shared_ptr(new FragmentShader("Shaders/ForwardRendering.frag.glsl"))); + m_ForwardRendering.Compile(); + m_ForwardRendering.Link(); + m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); m_ShaderProgramShadows.AddShader(std::shared_ptr(new FragmentShader("Shaders/ShadowMap.frag.glsl"))); m_ShaderProgramShadows.Compile(); @@ -152,17 +160,44 @@ void Renderer::Draw(double dt) m_QuadView = true; } - if(glfwGetKey(m_Window, GLFW_KEY_KP_1)) + //if(glfwGetKey(m_Window, GLFW_KEY_KP_1)) + //{ + // Gamma -= 0.3f * dt; + // LOG_INFO("Gamma_UP: %f", Gamma); + //} + //if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) + //{ + // Gamma += 0.3f * dt; + // LOG_INFO("Gamma_DOWN: %f", Gamma); + //} + + if(glfwGetKey(m_Window, GLFW_KEY_KP_7)) { - Gamma -= 0.3f * dt; - LOG_INFO("Gamma_UP: %f", Gamma); + m_SunProjection_height.x += 10.f * dt; + LOG_INFO("Heightx+: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); } - if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) + else if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) { - Gamma += 0.3f * dt; - LOG_INFO("Gamma_DOWN: %f", Gamma); + m_SunProjection_height.x -= 10.f * dt; + LOG_INFO("Heightx-: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); } + if(glfwGetKey(m_Window, GLFW_KEY_KP_8)) + { + m_SunProjection_height.y += 10.f * dt; + LOG_INFO("Heightx+: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); + } + else if(glfwGetKey(m_Window, GLFW_KEY_KP_5)) + { + m_SunProjection_height.y -= 10.f * dt; + LOG_INFO("Heightx-: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); + } + + if(glfwGetKey(m_Window, GLFW_KEY_1)) { if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) @@ -240,8 +275,8 @@ void Renderer::CreateShadowMap(int resolution) glGenTextures(1, &m_ShadowDepthTexture); glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, resolution, resolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); @@ -262,7 +297,7 @@ void Renderer::DrawShadowMap() { glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object - glCullFace(GL_BACK); //Make it so that only the back faces are rendered + glCullFace(GL_FRONT); //Make it so that only the back faces are rendered //Binds the FBO and sets the veiwport, witch in effect is how large the shadowmap is and what resolution it has. glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); @@ -272,7 +307,7 @@ void Renderer::DrawShadowMap() //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Creates the "camera" for the shadowmap from the direction of the sun. - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1,0,1)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; glm::mat4 MVP; @@ -299,7 +334,6 @@ void Renderer::DrawShadowMap() } } - } void Renderer::DrawDebugShadowMap() @@ -383,7 +417,8 @@ void Renderer::AddPointLightToDraw( float _specularExponent, float _ConstantAttenuation, float _LinearAttenuation, - float _QuadraticAttenuation + float _QuadraticAttenuation, + float _radius ) { Light light; @@ -394,6 +429,7 @@ void Renderer::AddPointLightToDraw( light.ConstantAttenuation = _ConstantAttenuation; light.LinearAttenuation = _LinearAttenuation; light.QuadraticAttenuation = _QuadraticAttenuation; + light.Radius = _radius; light.SphereModelMatrix = CreateLightMatrix(light); Lights.push_back(light); } @@ -553,7 +589,7 @@ void Renderer::FrameBufferTextures() //Generate and bind diffuse texture glGenTextures(1, &m_fDiffuseTexture); glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_Width, m_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); @@ -562,7 +598,7 @@ void Renderer::FrameBufferTextures() //Generate and bind position texture glGenTextures(1, &m_fPositionTexture); glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, 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); @@ -571,7 +607,7 @@ void Renderer::FrameBufferTextures() //Generate and bind normal texture glGenTextures(1, &m_fNormalsTexture); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, 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); @@ -580,7 +616,7 @@ void Renderer::FrameBufferTextures() //Generate and bind normal texture glGenTextures(1, &m_fSpecularTexture); glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, m_Width, m_Height, 0, GL_RED, 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); @@ -617,7 +653,7 @@ void Renderer::FrameBufferTextures() glGenTextures(1, &m_fLightingTexture); glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_Width, m_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); @@ -659,15 +695,15 @@ void Renderer::DrawFBO() glViewport(0, 0, m_Width, m_Height); // Clear G-buffer - GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; - glDrawBuffers(3, windowBuffClear); - glClearColor(0.f, 0.f, 0.f, 0.f); + GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, windowBuffClear); + glClearColor(0.0f, 0.3f, 0.7f, 0.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Execute the first render stage which will fill out the internal buffers with data(??) m_FirstPassProgram.Bind(); - GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; - glDrawBuffers(3, windowBuffOpaque); + GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, windowBuffOpaque); glCullFace(GL_BACK); @@ -688,6 +724,8 @@ void Renderer::DrawFBO() glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); glCullFace(GL_FRONT); DrawLightScene(viewport); @@ -702,7 +740,7 @@ void Renderer::DrawFBO() m_FinalPassProgram.Bind(); // Ambient light - glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); + glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f))); glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); glActiveTexture(GL_TEXTURE0); @@ -717,6 +755,11 @@ void Renderer::DrawFBO() } } +void Renderer::DrawFBO2() +{ + ForwardRendering(); +} + void Renderer::DrawFBOScene(Viewport &viewport) { // glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly @@ -733,11 +776,14 @@ void Renderer::DrawFBOScene(Viewport &viewport) 0.5, 0.5, 0.5, 1.0 ); - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1,0,1)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; glm::mat4 depthMVP; + glm::vec3 sunDirection = m_SunTarget - m_SunPosition; + glm::vec3 sunDirection_cameraview = glm::vec3(m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix() * glm::vec4(sunDirection, 1.0)); + glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); @@ -757,6 +803,8 @@ void Renderer::DrawFBOScene(Viewport &viewport) glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix((float)m_Width / m_Height))); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview)); + glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { @@ -767,6 +815,11 @@ void Renderer::DrawFBOScene(Viewport &viewport) glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap); } + if (texGroup.SpecularMap) + { + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, *texGroup.SpecularMap); + } glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } } @@ -830,6 +883,7 @@ void Renderer::DrawLightScene(Viewport &viewport) glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), light.Radius); glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; @@ -848,14 +902,14 @@ glm::mat4 Renderer::CreateLightMatrix(Light &_light) // float c = _light.ConstantAttenuation; // float l = _light.LinearAttenuation; // float q = _light.QuadraticAttenuation; - float c = CAtt; - float l = LAtt; - float q = QAtt; - float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); + //float c = CAtt; + //float l = LAtt; + //float q = QAtt; + //float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); glm::mat4 model; model *= glm::translate(_light.Position); - model *= glm::scale(glm::vec3(cutOffRadius)); + model *= glm::scale(glm::vec3(_light.Radius*2)); return model; } @@ -886,15 +940,46 @@ void Renderer::UpdateSunProjection() //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. } -void Renderer::RegisterViewport(int identifier, float left, float top, float right, float bottom) +void Renderer::ForwardRendering() { - /*Viewport v; - v.Left = left; - v.Top = top; - v.Right = right; - v.Bottom = bottom; - v.Camera = nullptr; - m_Viewports[identifier] = v;*/ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, m_Width, m_Height); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClearColor(0.0f, 0.5f, 0.0f, 1.0f); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix(); + glm::mat4 MVP; + + m_ForwardRendering.Bind(); + + for (auto tuple : ModelsToRender) //// Todo: Add so it's TransparentModelsToRender + { + Model* model; + glm::mat4 modelMatrix; + bool visible; + std::tie(model, modelMatrix, visible, std::ignore) = tuple; + if (!visible) + continue; + + MVP = cameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height))); + + glBindVertexArray(model->VAO); + for (auto texGroup : model->TextureGroups) + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); + glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); + } + } } void Renderer::RegisterCamera(int identifier, float FOV, float nearClip, float farClip) @@ -917,4 +1002,4 @@ void Renderer::UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat m_Cameras[cameraIdentifier]->FOV(FOV); m_Cameras[cameraIdentifier]->NearClip(nearClip); m_Cameras[cameraIdentifier]->FarClip(farClip);*/ -} +} \ No newline at end of file diff --git a/src/Renderer.h b/src/Renderer.h index cd47c0d..5f2d013 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -67,7 +67,8 @@ public: float _specularExponent, float _ConstantAttenuation, float _LinearAttenuation, - float _QuadraticAttenuation + float _QuadraticAttenuation, + float _radius ); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); @@ -111,7 +112,7 @@ private: glm::vec3 Diffuse; float SpecularExponent; glm::mat4 SphereModelMatrix; - float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation, Radius; }; float Gamma; @@ -133,6 +134,10 @@ private: glm::vec3 m_SunPosition; glm::vec3 m_SunTarget; glm::mat4 m_SunProjection; + glm::vec2 m_SunProjection_width; + glm::vec2 m_SunProjection_height; + glm::vec2 m_SunProjection_length; + GLuint m_DebugAABB; GLuint m_ShadowFrameBuffer; @@ -160,6 +165,7 @@ private: ShaderProgram m_SecondPassProgram; ShaderProgram m_SecondPassProgram_Debug; ShaderProgram m_FinalPassProgram; + ShaderProgram m_ForwardRendering; ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; @@ -176,12 +182,14 @@ private: void CreateShadowMap(int resolution); void FrameBufferTextures(); void DrawFBO(); + void DrawFBO2(); void DrawFBOScene(Viewport &viewport); void DrawLightScene(Viewport &viewport); void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); void UpdateSunProjection(); void CreateNormalMapTangent(); + void ForwardRendering(); GLuint CreateQuad(); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 0bdb1b1..b4dcc13 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -21,9 +21,11 @@ void main() vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); - - vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; - FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); //FragmentColor = DiffuseTexel; + FragmentColor = DiffuseTexel * (vec4(La, 0.0) + vec4(LightingTexel.rgb, 0.0)) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0); + //FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); + + + } \ No newline at end of file diff --git a/src/Shaders/ForwardRendering.frag.glsl b/src/Shaders/ForwardRendering.frag.glsl new file mode 100644 index 0000000..4db1d79 --- /dev/null +++ b/src/Shaders/ForwardRendering.frag.glsl @@ -0,0 +1,18 @@ +#version 430 + +layout(binding=0) uniform sampler2D texture0; + +in VertexData { + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Input; + +out vec4 fragmentColor; + +void main() { + // Texture + vec4 texel = texture(texture0, Input.TextureCoord); + + fragmentColor = texel; +} \ No newline at end of file diff --git a/src/Shaders/ForwardRendering.vert.glsl b/src/Shaders/ForwardRendering.vert.glsl new file mode 100644 index 0000000..3a876d3 --- /dev/null +++ b/src/Shaders/ForwardRendering.vert.glsl @@ -0,0 +1,25 @@ +#version 430 + +uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; + +layout (location = 0) in vec3 Position; +layout (location = 1) in vec3 Normal; +layout (location = 2) in vec2 TextureCoord; + +out VertexData { + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = MVP * vec4(Position, 1.0); + + Output.Position = Position; + Output.Normal = Normal; + Output.TextureCoord = TextureCoord; +} \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 87a95fa..725bb17 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -5,6 +5,8 @@ layout (binding=1) uniform sampler2D ShadowTexture; layout (binding=2) uniform sampler2D NormalMapTexture; layout (binding=3) uniform sampler2D SpecularMapTexture; +uniform vec3 SunDirection_cameraspace; +uniform mat4 V; in VertexData { @@ -19,16 +21,26 @@ in VertexData out vec4 frag_Diffuse; out vec4 frag_Position; out vec4 frag_Normal; -out vec4 frag_specular; +out vec4 frag_Specular; float Shadow(vec4 ShadowCoord) { - //float cosTheta = clamp(dot(Input.Normal, 1.0), 0.0, 1.0); - float bias = 0.0005; // cosTheta is dot( n,l ), clamped between 0 and 1 - bias = clamp(bias, 0.0, 0.01); - if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z - bias) + if (Input.ShadowCoord.x < 0.0 || Input.ShadowCoord.x > 1.0 || Input.ShadowCoord.y < 0.0 || Input.ShadowCoord.y > 1.0) + return 0.9; + + //Fixed bias + //float bias = 0.00005; + + //Variable bias + vec3 n = normalize(Input.Normal); + vec3 l = normalize(SunDirection_cameraspace); + float cosTheta = clamp(dot(n, l), 0.0, 1.0); + float bias = tan(acos(cosTheta)); + bias = clamp(bias, 0.0, 0.00005); + + if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z + bias) { - return 0.3; + return 0.6; } else { @@ -46,10 +58,11 @@ void main() frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal - mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.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 = vec4(Input.Normal, 0.0); //G-buffer Specular - frag_specular = texture(SpecularMapTexture, Input.TextureCoord); + 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 5ded0a2..265cf2a 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -2,6 +2,7 @@ layout (binding=0) uniform sampler2D PositionTexture; layout (binding=1) uniform sampler2D NormalsTexture; +layout (binding=2) uniform sampler2D SpecularTexture; uniform vec2 ViewportSize; uniform mat4 MVP; @@ -17,12 +18,14 @@ uniform vec3 CameraPosition; uniform float ConstantAttenuation; uniform float LinearAttenuation; uniform float QuadraticAttenuation; +uniform float LightRadius; const vec3 ks = vec3(1.0, 1.0, 1.0); const vec3 kd = vec3(1.0, 1.0, 1.0); const vec3 ka = vec3(1.0, 1.0, 1.0); const float kshine = 1.0; + in VertexData { vec3 Position; @@ -31,7 +34,7 @@ in VertexData out vec4 FragColor; -vec4 phong(vec3 position, vec3 normal) +vec4 phong(vec3 position, vec3 normal, vec3 specular) { // Diffuse vec3 lightPos = vec3(V * vec4(lp, 1.0)); @@ -47,31 +50,14 @@ vec4 phong(vec3 position, vec3 normal) vec3 halfWay = normalize(surfaceToViewer + directionToLight); float dotSpecular = max(dot(halfWay, normal), 0.0); float specularFactor = pow(dotSpecular, specularExponent * 2.0); - vec3 Is = ks * ls * specularFactor; + vec3 Is = specular.r * ls * specularFactor; //Attenuation float dist = distance(lightPos, position); - //float attenuation = -log(min(1.0, dist / LightRadius)); - float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist)); + float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2); - //float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2)); - - //float attenuation = clamp(0.0, 1.0, 1.0 / (0.001 + (0.001 * dist) + (0.001 * dist * dist))); - - //float attenuation = 1.0 / dot(directionToLight, directionToLight); - - //float att_s = 5; - //float attenuation = pow(dist, 2) / pow(5.0, 2); - //attenuation = 1.0 / (1.0 + attenuation * att_s); - //att_s = 1.0 / (1.0 + att_s); - //attenuation = attenuation / (1.0 - att_s); - - //float radius = 5.0; - //float alpha = dist / radius; - //float dampingFactor = 1.0 - pow(alpha, 3); - - return vec4((Id + Is) * attenuation, 1.0); + return vec4((Id) * attenuation, Is.r * attenuation); } void main() @@ -79,7 +65,8 @@ void main() vec2 TextureCoord = gl_FragCoord.xy / ViewportSize; vec4 PositionTexel = texture(PositionTexture, TextureCoord); vec4 NormalTexel = texture(NormalsTexture, TextureCoord); + vec4 SpecularTexel = texture(SpecularTexture, TextureCoord); - FragColor = phong(vec3(PositionTexel), vec3(NormalTexel)); + FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel)); //FragColor = NormalTexel; } \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 9406be6..fb99cf9 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -218,6 +218,8 @@ + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index cbbcf4f..75bcb9d 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -462,5 +462,11 @@ Shaders + + Shaders + + + Shaders + \ No newline at end of file diff --git a/vs11/Returngeance1.psess b/vs11/Returngeance1.psess new file mode 100644 index 0000000..3dbec77 --- /dev/null +++ b/vs11/Returngeance1.psess @@ -0,0 +1,89 @@ + + + + Returngeance.sln + Sampling + None + true + true + Timestamp + Cycles + 10000000 + 10 + 10 + + false + + + + false + 500 + + \Memory\Pages/sec + \PhysicalDisk(_Total)\Avg. Disk Queue Length + \Processor(_Total)\% Processor Time + + + + true + false + false + + false + + + false + + + + bin\Debug\Returngeance.exe + 01/01/0001 00:00:00 + true + true + false + false + false + false + false + true + false + Executable + bin\Debug\Returngeance.exe + ..\bin\Debug + + + IIS + InternetExplorer + true + false + + false + + + false + + {E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + Returngeance\Returngeance.vcxproj + Returngeance + + + + + Returngeance140519.vsp + + + Returngeance140519(1).vsp + + + Returngeance140519(2).vsp + + + Returngeance140519(3).vsp + + + + + :PB:{E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + + + \ No newline at end of file diff --git a/vs11/Returngeance140519(1).vsp b/vs11/Returngeance140519(1).vsp new file mode 100644 index 0000000..256cb35 Binary files /dev/null and b/vs11/Returngeance140519(1).vsp differ diff --git a/vs11/Returngeance140519(2).vsp b/vs11/Returngeance140519(2).vsp new file mode 100644 index 0000000..90776c5 Binary files /dev/null and b/vs11/Returngeance140519(2).vsp differ diff --git a/vs11/Returngeance140519(3).vsp b/vs11/Returngeance140519(3).vsp new file mode 100644 index 0000000..f70580d Binary files /dev/null and b/vs11/Returngeance140519(3).vsp differ diff --git a/vs11/Returngeance140519.vsp b/vs11/Returngeance140519.vsp new file mode 100644 index 0000000..bd782b0 Binary files /dev/null and b/vs11/Returngeance140519.vsp differ