diff --git a/Escape-the-Dawn/Components/Collision.h b/Escape-the-Dawn/Components/Collision.h index b9f44a5..83450a4 100644 --- a/Escape-the-Dawn/Components/Collision.h +++ b/Escape-the-Dawn/Components/Collision.h @@ -10,7 +10,10 @@ namespace Components struct Collision : Component { + Collision() : Phantom(false), Interested(false) { } + bool Phantom; + bool Interested; std::vector CollidingEntities; }; diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index 8e471d5..d325dac 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -11,7 +11,7 @@ namespace Components struct SoundEmitter : Component { float Gain; - float MaxDistance; + //float MaxDistance; float ReferenceDistance; float Pitch; bool Loop; diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index cb14255..a03446f 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -82,12 +82,12 @@ MaxSpeed true true - GLEW_STATIC;%(PreprocessorDefinitions) + GLEW_STATIC;DEBUG;%(PreprocessorDefinitions) NotUsing - None + ProgramDatabase + true - false true true OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies) diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index ba47e23..31c2855 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -21,31 +21,16 @@ void GameWorld::Initialize() std::shared_ptr collision; EntityID ent; - // Camera - entcamera = CreateEntity(); - SetProperty(entcamera, "Name", std::string("Camera")); - transform = AddComponent(entcamera, "Transform"); - AddComponent(entcamera, "Input"); - transform->Position = glm::vec3(0.f, 10.f, 14.f); - camera = AddComponent(entcamera, "Camera"); - camera->FOV = 45.f; - camera->FarClip = 1000.f; - camera->NearClip = 0.01f; - transform->Orientation = glm::angleAxis(glm::radians(15.0f),glm::vec3(1,0,0)); - collision = AddComponent(entcamera, "Collision"); - collision->Phantom = false; - bounds = AddComponent(entcamera, "Bounds"); - bounds->VolumeVector = glm::vec3(0.1f,0.1f,0.1f); + + // Fucking lights ent = CreateEntity(); transform = AddComponent(ent, "Transform"); - transform->Position = glm::vec3(0.f, 4.f, 0.f); - transform->Position = glm::vec3(10.f, 2.f, 5.f); + transform->Position = glm::vec3(0.f, 7.f, 0.f); pointLight = AddComponent(ent, "PointLight"); pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); - pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0); - pointLight->Diffuse = glm::vec3(1.0, 1.0, 5.0); + pointLight->Diffuse = glm::vec3(0.0, 0.0, 5.0); pointLight->constantAttenuation = 0.f; pointLight->linearAttenuation = 1.f; pointLight->quadraticAttenuation = 0.f; @@ -54,13 +39,10 @@ void GameWorld::Initialize() model->ModelFile = "Models/sphere.obj"; ent = CreateEntity(); transform = AddComponent(ent, "Transform"); - transform->Position = glm::vec3(10.f, 4.f, 0.f); - transform->Position = glm::vec3(0.f, 2.f, 5.f); + transform->Position = glm::vec3(10.f, 7.f, 0.f); pointLight = AddComponent(ent, "PointLight"); pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); - pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0); - pointLight->Specular = glm::vec3(3.0, 3.0, 3.0); - pointLight->Diffuse = glm::vec3(3.0, 0.0, 0.0); + pointLight->Diffuse = glm::vec3(0.0, 5.0, 0.0); pointLight->constantAttenuation = 0.f; pointLight->linearAttenuation = 1.f; pointLight->quadraticAttenuation = 0.f; @@ -69,12 +51,10 @@ void GameWorld::Initialize() model->ModelFile = "Models/sphere.obj"; ent = CreateEntity(); transform = AddComponent(ent, "Transform"); - transform->Position = glm::vec3(0.f, -4.f, 0.f); - transform->Position = glm::vec3(-10.f, 2.f, 5.f); + transform->Position = glm::vec3(-10.f, 7.f, 0.f); pointLight = AddComponent(ent, "PointLight"); pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); - pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0); - pointLight->Diffuse = glm::vec3(0.0, 3.0, 0.0); + pointLight->Diffuse = glm::vec3(5.0, 0.0, 0.0); pointLight->constantAttenuation = 0.f; pointLight->linearAttenuation = 1.f; pointLight->quadraticAttenuation = 0.f; @@ -93,18 +73,42 @@ 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"; + pointLight = AddComponent(m_Player, "PointLight"); + pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); + pointLight->Diffuse = glm::vec3(0.4, 0.4, 1.0); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 1.f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; AddComponent(m_Player, "Input"); collision = AddComponent(m_Player, "Collision"); 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(); + + // Camera + entcamera = CreateEntity(m_Player); + SetProperty(entcamera, "Name", std::string("Camera")); + transform = AddComponent(entcamera, "Transform"); + AddComponent(entcamera, "Input"); + transform->Position = glm::vec3(0.f, 10.f, 14.f); + camera = AddComponent(entcamera, "Camera"); + camera->FOV = 45.f; + camera->FarClip = 1000.f; + camera->NearClip = 0.01f; + transform->Orientation = glm::angleAxis(glm::radians(15.0f),glm::vec3(1,0,0)); + + + + + /*ent = CreateEntity(); transform = AddComponent(ent, "Transform"); transform->Position = glm::vec3(10.f, 4.f, 0.f); model = AddComponent(ent, "Model"); @@ -113,7 +117,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/Models/powerup.mtl b/Escape-the-Dawn/Models/powerup.mtl new file mode 100644 index 0000000..2ec2034 --- /dev/null +++ b/Escape-the-Dawn/Models/powerup.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'powerup.blend' +# Material Count: 1 + +newmtl Material.001 +Ns 96.078431 +Ka 0.000000 0.000000 0.000000 +Kd 0.640000 0.640000 0.640000 +Ks 0.500000 0.500000 0.500000 +Ni 1.000000 +d 1.000000 +illum 2 +map_Kd powerupTexture.png diff --git a/Escape-the-Dawn/Models/powerupTexture.png b/Escape-the-Dawn/Models/powerupTexture.png new file mode 100644 index 0000000..4a0b586 Binary files /dev/null and b/Escape-the-Dawn/Models/powerupTexture.png differ diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index c554dc7..1f6bef8 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -3,12 +3,21 @@ Renderer::Renderer() { m_VSync = false; +#ifdef DEBUG m_DrawNormals = false; m_DrawWireframe = false; + m_DrawBounds = true; +#else + m_DrawNormals = false; + m_DrawWireframe = false; + m_DrawBounds = false; +#endif - 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; } void Renderer::Initialize() @@ -92,7 +101,7 @@ void Renderer::LoadContent() m_DebugAABB = CreateAABB(); m_ScreenQuad = CreateQuad(); - CreateShadowMap(2048*2); + CreateShadowMap(m_ShadowMapRes); } void Renderer::CreateShadowMap(int resolution) @@ -128,31 +137,33 @@ void Renderer::Draw(double dt) DrawScene(); #ifdef DEBUG - glEnable(GL_BLEND); - glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); - m_ShaderProgramDebugAABB.Bind(); - for (auto tuple : AABBsToRender) { - glm::mat4 modelMatrix; - bool colliding; - std::tie(modelMatrix, colliding) = tuple; - // Model matrix - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 MVP = cameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - // Color - glm::vec4 color(1.f, 1.f, 1.f, 0.f); - if (colliding) - color = glm::vec4(1.f, 0.f, 0.f, 0.f); - glUniform4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "Color"), 1, glm::value_ptr(color)); - glBindVertexArray(m_DebugAABB); - glDrawArrays(GL_LINES, 0, 24); + // Draw bounding boxes + if (m_DrawBounds) { + glEnable(GL_BLEND); + glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); + m_ShaderProgramDebugAABB.Bind(); + for (auto tuple : AABBsToRender) { + glm::mat4 modelMatrix; + bool colliding; + std::tie(modelMatrix, colliding) = tuple; + // Model matrix + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 MVP = cameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + // Color + glm::vec4 color(1.f, 1.f, 1.f, 0.f); + if (colliding) + color = glm::vec4(1.f, 0.f, 0.f, 0.f); + glUniform4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "Color"), 1, glm::value_ptr(color)); + glBindVertexArray(m_DebugAABB); + glDrawArrays(GL_LINES, 0, 24); + } } - AABBsToRender.clear(); + + //DrawDebugShadowMap(); #endif - DrawDebugShadowMap(); - - ModelsToRender.clear(); + ClearStuff(); glfwSwapBuffers(m_Window); } @@ -173,33 +184,51 @@ 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)); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), 3, Light_position.data()); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), 3, Light_specular.data()); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), 3, Light_diffuse.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), 3, Light_constantAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), 3, Light_linearAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), 3, Light_quadraticAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), 3, Light_spotExponent.data()); + 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()); + glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), Lights, Light_diffuse.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights, Light_constantAttenuation.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights, Light_linearAttenuation.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights, Light_quadraticAttenuation.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights, Light_spotExponent.data()); if (m_DrawWireframe) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } 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) { @@ -217,12 +246,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(); @@ -231,12 +261,13 @@ void Renderer::DrawShadowMap() m_ShaderProgramShadows.Bind(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - for (int i = 0; i < ModelsToRender.size(); i++) + for (auto tuple : ModelsToRender) { - ModelData* modelData = ModelsToRender.at(i); - auto model = modelData->model; + Model* model; + glm::mat4 modelMatrix; + std::tie(model, modelMatrix) = tuple; - MVP = depthCamera * modelData->ModelMatrix; + MVP = depthCamera * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glBindVertexArray(model->VAO); @@ -266,14 +297,15 @@ void Renderer::DrawModels(ShaderProgram &shader) glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; - for (int i = 0; i < ModelsToRender.size(); i++) + for (auto tuple : ModelsToRender) { - ModelData* modelData = ModelsToRender.at(i); - auto model = modelData->model; + Model* model; + glm::mat4 modelMatrix; + std::tie(model, modelMatrix) = tuple; - MVP = cameraMatrix * modelData->ModelMatrix; + MVP = cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelData->ModelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { @@ -296,9 +328,9 @@ void Renderer::AddTextToDraw() void Renderer::AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation, glm::vec3 scale) { - glm::mat4 ModelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); // You can now use ModelMatrix to build the MVP matrix - ModelsToRender.push_back(new ModelData(model, ModelMatrix)); + ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix)); } void Renderer::AddPointLightToDraw( @@ -324,7 +356,7 @@ void Renderer::AddPointLightToDraw( Light_linearAttenuation.push_back(_linearAttenuation); Light_quadraticAttenuation.push_back(_quadraticAttenuation); Light_spotExponent.push_back(_spotExponent); - + Lights = Light_constantAttenuation.size(); } void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding) @@ -426,3 +458,16 @@ GLuint Renderer::CreateAABB() return vao; } + +void Renderer::ClearStuff() +{ + AABBsToRender.clear(); + ModelsToRender.clear(); + Light_position.clear(); + Light_specular.clear(); + Light_diffuse.clear(); + Light_constantAttenuation.clear(); + Light_linearAttenuation.clear(); + Light_quadraticAttenuation.clear(); + Light_spotExponent.clear(); +} \ No newline at end of file diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 3174fd5..2c4c492 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -30,15 +30,8 @@ public: int HEIGHT, WIDTH; - struct ModelData - { - std::shared_ptr model; - glm::mat4 ModelMatrix; - ModelData(std::shared_ptr _model, glm::mat4 _modelMatrix) - : model(_model), ModelMatrix(_modelMatrix) {} - }; - - std::vector ModelsToRender; + std::list> ModelsToRender; + int Lights; std::vector Light_position; std::vector Light_specular; std::vector Light_diffuse; @@ -46,7 +39,7 @@ public: std::vector Light_linearAttenuation; std::vector Light_quadraticAttenuation; std::vector Light_spotExponent; - std::vector> AABBsToRender; + std::list> AABBsToRender; Renderer(); @@ -76,6 +69,8 @@ public: void DrawNormals(bool val) { m_DrawNormals = val; } bool DrawWireframe() const { return m_DrawWireframe; } void DrawWireframe(bool val) { m_DrawWireframe = val; } + bool DrawBounds() const { return m_DrawBounds; } + void DrawBounds(bool val) { m_DrawBounds = val; } private: GLFWwindow* m_Window; @@ -84,7 +79,9 @@ private: bool m_VSync; bool m_DrawNormals; bool m_DrawWireframe; + bool m_DrawBounds; + int m_ShadowMapRes; glm::vec3 m_SunPosition; glm::vec3 m_SunTarget; glm::mat4 m_SunProjection; @@ -101,7 +98,8 @@ private: ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; ShaderProgram m_ShaderProgramDebugAABB; - + + void ClearStuff(); void DrawScene(); void DrawModels(ShaderProgram &shader); void DrawShadowMap(); diff --git a/Escape-the-Dawn/Shaders/Fragment.glsl b/Escape-the-Dawn/Shaders/Fragment.glsl index a852e1b..8eeca87 100644 --- a/Escape-the-Dawn/Shaders/Fragment.glsl +++ b/Escape-the-Dawn/Shaders/Fragment.glsl @@ -6,15 +6,15 @@ uniform mat4 view; layout(binding=0) uniform sampler2D texture0; layout(binding=1) uniform sampler2D shadowMap; -const int numberOfLights = 3; - -uniform vec3 position[numberOfLights]; -uniform vec3 specular[numberOfLights]; -uniform vec3 diffuse[numberOfLights]; -uniform float constantAttenuation[numberOfLights]; -uniform float linearAttenuation[numberOfLights]; -uniform float quadraticAttenuation[numberOfLights]; -uniform float spotExponent[numberOfLights]; +const int maxNumberOfLights = 82; +uniform int numberOfLights; +uniform vec3 position[maxNumberOfLights]; +uniform vec3 specular[maxNumberOfLights]; +uniform vec3 diffuse[maxNumberOfLights]; +uniform float constantAttenuation[maxNumberOfLights]; +uniform float linearAttenuation[maxNumberOfLights]; +uniform float quadraticAttenuation[maxNumberOfLights]; +uniform float spotExponent[maxNumberOfLights]; in VertexData { vec3 Position; @@ -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/Sounds/hum.wav b/Escape-the-Dawn/Sounds/hum.wav new file mode 100644 index 0000000..30601bf Binary files /dev/null and b/Escape-the-Dawn/Sounds/hum.wav differ diff --git a/Escape-the-Dawn/Sounds/siren.wav b/Escape-the-Dawn/Sounds/siren.wav new file mode 100644 index 0000000..54a0238 Binary files /dev/null and b/Escape-the-Dawn/Sounds/siren.wav differ 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/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index 097f05f..ab91656 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -20,6 +20,10 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID // Clear old collisions collisionComponent->CollidingEntities.clear(); + // Quit out if we're not interested in collision events + if (!collisionComponent->Interested) + return; + auto entities = m_World->GetEntities(); for (auto pair : *entities) { @@ -43,7 +47,8 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID //pair.first, pair.second; if(entity == entity2) - break; + continue; + Intersects(entity, entity2); } } diff --git a/Escape-the-Dawn/Systems/InputSystem.cpp b/Escape-the-Dawn/Systems/InputSystem.cpp index ef7f17d..495cc55 100644 --- a/Escape-the-Dawn/Systems/InputSystem.cpp +++ b/Escape-the-Dawn/Systems/InputSystem.cpp @@ -47,6 +47,10 @@ void Systems::InputSystem::Update(double dt) if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2]) { m_Renderer->DrawNormals(!m_Renderer->DrawNormals()); } + // Bounds + if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3]) { + m_Renderer->DrawBounds(!m_Renderer->DrawBounds()); + } #endif } diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp index 76d591d..2e71570 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp @@ -5,14 +5,14 @@ Systems::LevelGenerationSystem::LevelGenerationSystem( World* world ) { elapsedtime = 0; - + velocity = 0; startx = 0; startyz = glm::vec2(0, -1000); } void Systems::LevelGenerationSystem::SpawnObstacle() { - typeRandom = 0 + (rand() % 3); + typeRandom = 0 + (rand() % 10); EntityID ent; @@ -23,45 +23,64 @@ void Systems::LevelGenerationSystem::SpawnObstacle() bounds = m_World->AddComponent(ent, "Bounds"); collision = m_World->AddComponent(ent, "Collision"); model = m_World->AddComponent(ent, "Model"); + auto sound = m_World->AddComponent(ent, "SoundEmitter"); + positionRandom = -500 + startx + (rand() % 1000); + transform->Position = glm::vec3(positionRandom, startyz); + //transform->Velocity = glm::vec3(0.f, 10.f, 100.f); - switch (typeRandom) - { - case 0: // Mountain stuff :D + sound->Loop = true; + sound->Gain = 1.f; + sound->ReferenceDistance = 15.f; + m_World->GetSystem("SoundSystem")->PlaySound(sound, "Sounds/hum.wav"); + + if(typeRandom >= 0 && typeRandom < 2) // Mountain stuff :D { - transform->Position = glm::vec3( positionRandom, startyz); // fix position float scale = (float)(rand() % 1000) / 250; transform->Scale = glm::vec3(scale); bounds->VolumeVector = glm::vec3(9, 12, 7); bounds->Origin = glm::vec3(1,11,-1); model->ModelFile = "Models/obstacle_mountain_1.obj"; } - break; - case 1: // Single Mountain :D + else if(typeRandom >= 2 && typeRandom < 5) // Single Mountain :D { - transform->Position = glm::vec3( positionRandom, startyz); // fix position float scale = (float)(rand() % 1000) / 200; transform->Scale = glm::vec3(scale); bounds->VolumeVector = glm::vec3(3.5f, 7.5f, 3.5f); bounds->Origin = glm::vec3(0,7.5f,0); model->ModelFile = "Models/obstacle_mountain_2.obj"; } - break; - case 2: + else if(typeRandom >= 5 && typeRandom < 8) { - transform->Position = glm::vec3( positionRandom, startyz); // fix position float scale = (float)(rand() % 1000) / 100; transform->Scale = glm::vec3(scale); bounds->VolumeVector = glm::vec3(1, 1, 1); bounds->Origin = glm::vec3(0,1,0); model->ModelFile = "Models/obstacle_cube_1.obj"; } - break; - default: + if(typeRandom >= 8 && typeRandom < 10) + { + float scale = (float)(rand() % 1000) / 100; + bounds->VolumeVector = glm::vec3(4, 4, 4); + bounds->Origin = glm::vec3(0,4,0); + pointLight = m_World->AddComponent(ent, "PointLight"); + pointLight->Specular = glm::vec3(1.0, 1.0, 1.0); + pointLight->Diffuse = glm::vec3(0.3, 1.0, 0.3); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 1.f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; - break; - } + powerUp = m_World->AddComponent(ent, "PowerUp"); + powerUp->Speed = 10.f; + + model->ModelFile = "Models/powerup.obj"; + } + + + // Put it below ground level + transform->Position.y -= bounds->VolumeVector.y * 2.f; } @@ -75,14 +94,22 @@ void Systems::LevelGenerationSystem::Update( double dt ) elapsedtime = 0; } - std::vector removethis; + std::list removethis; for(auto ent : obstacles) { - auto transformComponent = m_World->GetComponent(ent, "Transform"); - transformComponent->Position.z += 100.f * dt; + auto transform = m_World->GetComponent(ent, "Transform"); - if(transformComponent->Position.z > 100) + transform->Velocity.z = -velocity; + transform->Position += transform->Velocity * (float)dt; + + // Stop raising obstacles when they reach ground level + if (transform->Velocity.y > 0 && transform->Position.y >= 0) { + transform->Position.y = 0; + transform->Velocity.y = 0; + } + + if(transform->Position.z > 800) { removethis.push_back(ent); } @@ -104,6 +131,7 @@ void Systems::LevelGenerationSystem::UpdateEntity( double dt, EntityID entity, E auto transformComponent = m_World->GetComponent(entity, "Transform"); startyz = glm::vec2(0, transformComponent->Position.z - 1000); startx = transformComponent->Position.x; + velocity = transformComponent->Velocity.z; } } diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.h b/Escape-the-Dawn/Systems/LevelGenerationSystem.h index 0d86012..864c047 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.h +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.h @@ -2,11 +2,16 @@ #define LevelGenerationSystem_h__ #include "System.h" +#include "Systems/SoundSystem.h" #include "World.h" #include "Components/Transform.h" #include "Components/Bounds.h" #include "Components/Collision.h" #include "Components/Model.h" +#include "Components/PointLight.h" +#include "Components/SoundEmitter.h" +#include "Components/PowerUp.h" + namespace Systems { @@ -30,12 +35,14 @@ namespace Systems double elapsedtime; std::list obstacles; - + float velocity; std::shared_ptr transform; std::shared_ptr bounds; std::shared_ptr collision; std::shared_ptr model; + std::shared_ptr pointLight; + std::shared_ptr powerUp; }; } diff --git a/Escape-the-Dawn/Systems/PlayerSystem.cpp b/Escape-the-Dawn/Systems/PlayerSystem.cpp index 4b5e918..6f76501 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.cpp +++ b/Escape-the-Dawn/Systems/PlayerSystem.cpp @@ -3,6 +3,8 @@ Systems::PlayerSystem::PlayerSystem( World* world ) : System(world) { + m_PlayerSpeed = 20; + m_PlayerOriginalBounds = glm::vec3(0); } void Systems::PlayerSystem::Update(double dt) @@ -18,14 +20,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa if (input == nullptr) return; - float speed = 50.0f; - 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; } @@ -59,19 +59,15 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa //--------------------------------------------------------------------- // TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } - - auto collision = m_World->GetComponent(entity, "Collision"); - if(collision->CollidingEntities.size() > 0) { - // 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)); @@ -79,7 +75,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) { @@ -95,7 +91,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) { @@ -119,6 +115,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 f0dd0db..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 m_PlayerSpeed; + glm::vec3 m_PlayerOriginalBounds; }; } diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 4a27d0a..f8b97eb 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -20,6 +20,7 @@ public: void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentCreated(std::string type, std::shared_ptr component) override; + void OnComponentRemoved(std::string type, Component* component) override; void PlaySound(std::shared_ptr emitter, std::string fileName); private: @@ -35,7 +36,7 @@ private: unsigned long dataSize; - std::map m_Source; + std::map m_Sources; std::map m_BufferCache; // string = fileName }; diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index 88789c7..e5cdb3c 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -18,6 +18,9 @@ Systems::SoundSystem::SoundSystem(World* world) } alGetError(); + + alSpeedOfSound(340.29f); // Speed of sound + alDistanceModel(AL_INVERSE_DISTANCE); } void Systems::SoundSystem::Update(double dt) @@ -54,10 +57,10 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); if(soundEmitter != nullptr) { - ALuint source = m_Source[soundEmitter.get()]; - alSourcei(source, AL_GAIN, soundEmitter->Gain); - alSourcei(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); - alSourcei(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); + ALuint source = m_Sources[soundEmitter]; + alSourcef(source, AL_GAIN, soundEmitter->Gain); + //alSourcef(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); + alSourcef(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); alSourcef(source, AL_PITCH, soundEmitter->Pitch); alSourcei(source, AL_LOOPING, soundEmitter->Loop); @@ -74,17 +77,30 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string fileName) { + if (m_Sources.find(emitter.get()) == m_Sources.end()) + return; + ALuint buffer = LoadFile(fileName); - ALuint source = m_Source[emitter.get()]; + ALuint source = m_Sources[emitter.get()]; alSourcei(source, AL_BUFFER, buffer); - alSourcePlay(m_Source[emitter.get()]); + alSourcePlay(m_Sources[emitter.get()]); } void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) { if(type == "SoundEmitter") { ALuint source = CreateSource(); - m_Source[component.get()] = source; + m_Sources[component.get()] = source; + } +} + +void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* component) +{ + if(type == "SoundEmitter") { + if (m_Sources.find(component) != m_Sources.end()) { + ALuint source = m_Sources[component]; + alDeleteSources(1, &source); + } } } 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); } diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index ce8ca37..add6ae3 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -60,7 +60,7 @@ public: std::shared_ptr AddComponent(EntityID entity, std::string componentType); std::shared_ptr AddComponent(EntityID entity, std::string componentType); template - std::shared_ptr GetComponent(EntityID entity, std::string componentType); + T* GetComponent(EntityID entity, std::string componentType); /*std::vector GetEntityChildren(EntityID entity);*/ @@ -117,9 +117,9 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template -std::shared_ptr World::GetComponent(EntityID entity, std::string componentType) +T* World::GetComponent(EntityID entity, std::string componentType) { - return std::static_pointer_cast(m_EntityComponents[entity][componentType]); + return (T*)m_EntityComponents[entity][componentType].get(); } #endif // World_h__ \ No newline at end of file