From 4905c17ebf9c22e337217e75686a2633b6bde094 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 13 Mar 2014 16:29:34 +0100 Subject: [PATCH] =?UTF-8?q?I=20Can't=20Believe=20It's=20Not=20Shadows!?= =?UTF-8?q?=C2=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Escape-the-Dawn.sln | 3 -- Escape-the-Dawn/Escape-the-Dawn.vcxproj | 6 +-- Escape-the-Dawn/GameWorld.cpp | 10 ++--- Escape-the-Dawn/Renderer.cpp | 45 +++++++++++++------ Escape-the-Dawn/Renderer.h | 1 + Escape-the-Dawn/Shaders/Fragment.glsl | 14 +++--- .../Shaders/VisualizeDepth.frag.glsl | 2 +- Escape-the-Dawn/System.h | 4 +- Escape-the-Dawn/Systems/PlayerSystem.cpp | 24 +++++++--- Escape-the-Dawn/Systems/PlayerSystem.h | 4 +- Escape-the-Dawn/World.cpp | 9 +++- 11 files changed, 81 insertions(+), 41 deletions(-) diff --git a/Escape-the-Dawn.sln b/Escape-the-Dawn.sln index ea79299..a932c57 100644 --- a/Escape-the-Dawn.sln +++ b/Escape-the-Dawn.sln @@ -17,7 +17,4 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection EndGlobal diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 861bdb7..a03446f 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -54,7 +54,6 @@ $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) - true @@ -83,9 +82,10 @@ MaxSpeed true true - GLEW_STATIC;%(PreprocessorDefinitions) + GLEW_STATIC;DEBUG;%(PreprocessorDefinitions) NotUsing - None + ProgramDatabase + true true diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 2e32d6f..2416224 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -86,7 +86,7 @@ void GameWorld::Initialize() m_Player = CreateEntity(); SetProperty(m_Player, "Name", std::string("PlayerShip")); transform = AddComponent(m_Player, "Transform"); - transform->Position = glm::vec3(0.f, 4.f, -5.f); + transform->Position = glm::vec3(0.f, 2.f, -5.f); transform->Scale = glm::vec3(1.0f); model = AddComponent(m_Player, "Model"); model->ModelFile = "Models/ship.obj"; @@ -102,10 +102,10 @@ void GameWorld::Initialize() collision->Phantom = false; collision->Interested = true; bounds = AddComponent(m_Player, "Bounds"); - bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3); - bounds->VolumeVector = glm::vec3(4.f,0.7f,4); + bounds->Origin = glm::vec3(0, 0, 2.f); + bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1); - ent = CreateEntity(); + /*ent = CreateEntity(); transform = AddComponent(ent, "Transform"); transform->Position = glm::vec3(10.f, 4.f, 0.f); model = AddComponent(ent, "Model"); @@ -114,7 +114,7 @@ void GameWorld::Initialize() collision->Phantom = false; bounds = AddComponent(player2, "Bounds"); bounds->Origin = transform->Position; - bounds->VolumeVector = glm::vec3(2.f,2.f,2.f); + bounds->VolumeVector = glm::vec3(2.f,2.f,2.f);*/ } void GameWorld::Update(double dt) diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 1c68d81..d237d2d 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -6,9 +6,10 @@ Renderer::Renderer() m_DrawNormals = false; m_DrawWireframe = false; - m_SunPosition = glm::vec3(0, 10, 10); + m_ShadowMapRes = 2048*2; + m_SunPosition = glm::vec3(0, 0.3f, 10); m_SunTarget = glm::vec3(0, 0, 0); - m_SunProjection = glm::ortho(-20, 20, -20, 20, -10, 20); + m_SunProjection = glm::ortho(-200, 200, -10, 100, -800, 400); Lights = 0; } @@ -93,7 +94,7 @@ void Renderer::LoadContent() m_DebugAABB = CreateAABB(); m_ScreenQuad = CreateQuad(); - CreateShadowMap(2048*2); + CreateShadowMap(m_ShadowMapRes); } void Renderer::CreateShadowMap(int resolution) @@ -129,6 +130,7 @@ void Renderer::Draw(double dt) DrawScene(); #ifdef DEBUG + // Draw bounding boxes glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); m_ShaderProgramDebugAABB.Bind(); @@ -148,10 +150,9 @@ void Renderer::Draw(double dt) glBindVertexArray(m_DebugAABB); glDrawArrays(GL_LINES, 0, 24); } - -#endif DrawDebugShadowMap(); +#endif ClearStuff(); glfwSwapBuffers(m_Window); @@ -174,19 +175,16 @@ void Renderer::DrawScene() #endif // Draw models - 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(1, 0, 0)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; - glm::mat4 depthMVP = depthCamera; glm::mat4 biasMatrix( 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); - glm::mat4 depthBiasMVP = biasMatrix * depthMVP; m_ShaderProgram.Bind(); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthBiasMVP)); glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights); glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), Lights, Light_position.data()); glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), Lights, Light_specular.data()); @@ -200,8 +198,28 @@ void Renderer::DrawScene() } glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); - DrawModels(m_ShaderProgram); - + //DrawModels(m_ShaderProgram); + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 MVP; + for (auto tuple : ModelsToRender) + { + Model* model; + glm::mat4 modelMatrix; + std::tie(model, modelMatrix) = tuple; + MVP = cameraMatrix * modelMatrix; + glm::mat4 depthMVP = biasMatrix * depthCamera * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glBindVertexArray(model->VAO); + for (auto texGroup : model->TextureGroups) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texGroup.Texture->texture); + glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); + } + } + #ifdef DEBUG // Debug draw model normals if (m_DrawNormals) { @@ -219,12 +237,13 @@ void Renderer::DrawShadowMap() glCullFace(GL_BACK); glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); - glViewport(0, 0, 2048*2, 2048*2); + glViewport(0, 0, m_ShadowMapRes, m_ShadowMapRes); glClear(GL_DEPTH_BUFFER_BIT); //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - 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(1, 0, 0)); +// glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; //glm::mat4 cameraMatrix = depthProjectionMatrix * m_Camera->ViewMatrix(); diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index b113017..c02787d 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -78,6 +78,7 @@ private: bool m_DrawNormals; bool m_DrawWireframe; + int m_ShadowMapRes; glm::vec3 m_SunPosition; glm::vec3 m_SunTarget; glm::mat4 m_SunProjection; diff --git a/Escape-the-Dawn/Shaders/Fragment.glsl b/Escape-the-Dawn/Shaders/Fragment.glsl index 3514159..8eeca87 100644 --- a/Escape-the-Dawn/Shaders/Fragment.glsl +++ b/Escape-the-Dawn/Shaders/Fragment.glsl @@ -23,7 +23,7 @@ in VertexData { vec3 ShadowCoord; } Input; -vec3 scene_ambient = vec3(0.2); +vec3 scene_ambient = vec3(0.3); out vec4 fragmentColor; @@ -49,18 +49,20 @@ void main() { //float cosTheta = clamp(dot(Input.Normal, vec3(0, 1, 0)), 0.0, 1.0); //float bias = 0.001 * tan(acos(cosTheta)); // cosTheta is dot( n,l ), clamped between 0 and 1 //bias = clamp(bias, 0.0, 0.01); - float bias = 0.003; float visibility = 1.0; - vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy); - if (shadowMapValue.z < Input.ShadowCoord.z - bias) { - visibility = 0.5; + if (Input.ShadowCoord.x >= 0.0 && Input.ShadowCoord.x <= 1.0 && Input.ShadowCoord.y >= 0.0 && Input.ShadowCoord.y <= 1.0) { + float bias = 0.003; + vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy); + if (shadowMapValue.z < Input.ShadowCoord.z - bias) { + visibility = 0.5; + } } vec3 totalLighting = La * Ka * visibility; float attenuation; - for(int i = 0; i < numberOfLights; i++) + for(int i = 0; i < numberOfLights && i < maxNumberOfLights; i++) { // Light //vec3 lightPosition = vec3(0, 0, 2); diff --git a/Escape-the-Dawn/Shaders/VisualizeDepth.frag.glsl b/Escape-the-Dawn/Shaders/VisualizeDepth.frag.glsl index eef8e6e..42e55f3 100644 --- a/Escape-the-Dawn/Shaders/VisualizeDepth.frag.glsl +++ b/Escape-the-Dawn/Shaders/VisualizeDepth.frag.glsl @@ -12,7 +12,7 @@ out vec4 FragmentColor; float LinearizeDepth(float z) { float n = 0.1; // camera z near - float f = 10.0; // camera z far + float f = 800.0; // camera z far return (2.0 * n) / (f + n - z * (f - n)); } diff --git a/Escape-the-Dawn/System.h b/Escape-the-Dawn/System.h index f295857..f62eda9 100644 --- a/Escape-the-Dawn/System.h +++ b/Escape-the-Dawn/System.h @@ -19,7 +19,9 @@ public: virtual void UpdateEntity(double dt, EntityID entity, EntityID parent) { } // Called when a component is created - virtual void OnComponentCreated(std::string type, std:: shared_ptr component) { } + virtual void OnComponentCreated(std::string type, std::shared_ptr component) { } + // Called when a component is removed + virtual void OnComponentRemoved(std::string type, Component* component) { } protected: World* m_World; diff --git a/Escape-the-Dawn/Systems/PlayerSystem.cpp b/Escape-the-Dawn/Systems/PlayerSystem.cpp index cb29cfb..768b9c2 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.cpp +++ b/Escape-the-Dawn/Systems/PlayerSystem.cpp @@ -3,7 +3,8 @@ Systems::PlayerSystem::PlayerSystem( World* world ) : System(world) { - speed = 20; + m_PlayerSpeed = 20; + m_PlayerOriginalBounds = glm::vec3(0); } void Systems::PlayerSystem::Update(double dt) @@ -21,10 +22,10 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa auto name = m_World->GetProperty(entity, "Name"); if (name == "Camera") { - glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation); glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation); + float speed = m_PlayerSpeed; if(input->KeyState[GLFW_KEY_LEFT_SHIFT]) { speed *= 4.0f; } @@ -64,12 +65,15 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa // DO STUFF } } - - - + name = m_World->GetProperty(entity, "Name"); if (name == "PlayerShip") { + auto bounds = m_World->GetComponent(entity, "Bounds"); + if (bounds && m_PlayerOriginalBounds == glm::vec3(0)) { + m_PlayerOriginalBounds = bounds->VolumeVector; + } + glm::vec3 Ship_Right = glm::vec3(glm::vec4(1, 0, 0, 0)); glm::vec3 Ship_Forward = glm::vec3(glm::vec4(0, 0, 1, 0)); @@ -77,7 +81,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa glm::vec3 Euler = glm::eulerAngles(transform->Orientation); if(input->KeyState[GLFW_KEY_LEFT]) { - transform->Position -= Ship_Right * (float)dt * speed; + transform->Position -= Ship_Right * (float)dt * m_PlayerSpeed; if(Euler.z < 10.f) { @@ -93,7 +97,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } } else if(input->KeyState[GLFW_KEY_RIGHT]) { - transform->Position += Ship_Right * (float)dt * speed; + transform->Position += Ship_Right * (float)dt * m_PlayerSpeed; if(Euler.z > -10.f) { @@ -117,6 +121,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa else if(Euler.z > 0.f) transform->Orientation = transform->Orientation * glm::angleAxis((float)dt,glm::vec3(0,0,-1)); } + + // Update player bounds based on rotation + if (bounds) { + Euler = glm::eulerAngles(transform->Orientation); + bounds->VolumeVector.x = m_PlayerOriginalBounds.x * glm::cos(glm::radians(Euler.z)); + } } } diff --git a/Escape-the-Dawn/Systems/PlayerSystem.h b/Escape-the-Dawn/Systems/PlayerSystem.h index 56734db..0d5ccad 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.h +++ b/Escape-the-Dawn/Systems/PlayerSystem.h @@ -7,6 +7,7 @@ #include "Components/Transform.h" #include "Components/Input.h" #include "Components/Collision.h" +#include "Components/Bounds.h" #include "logging.h" @@ -23,7 +24,8 @@ namespace Systems void UpdateEntity(double dt, EntityID entity, EntityID parent) override; private: - float speed; + float m_PlayerSpeed; + glm::vec3 m_PlayerOriginalBounds; }; } diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp index e52c803..500395e 100644 --- a/Escape-the-Dawn/World.cpp +++ b/Escape-the-Dawn/World.cpp @@ -61,12 +61,19 @@ bool World::ValidEntity(EntityID entity) void World::RemoveEntity(EntityID entity) { m_EntityParents.erase(entity); - m_EntityComponents.erase(entity); + // Remove components for (auto pair : m_EntityComponents[entity]) { auto type = pair.first; auto component = pair.second; + // Trigger events + for (auto pair : m_Systems) { + auto system = pair.second; + system->OnComponentRemoved(type, component.get()); + } m_ComponentsOfType[type].remove(component); } + m_EntityComponents.erase(entity); + RecycleEntityID(entity); }