diff --git a/Escape-the-Dawn/Camera.h b/Escape-the-Dawn/Camera.h index 973ff5d..a15a495 100644 --- a/Escape-the-Dawn/Camera.h +++ b/Escape-the-Dawn/Camera.h @@ -10,7 +10,6 @@ public: glm::vec3 Forward(); glm::vec3 Right(); - glm::quat Orientation(); float AspectRatio() const { return m_AspectRatio; } void AspectRatio(float val); diff --git a/Escape-the-Dawn/CubemapTexture.cpp b/Escape-the-Dawn/CubemapTexture.cpp new file mode 100644 index 0000000..764c098 --- /dev/null +++ b/Escape-the-Dawn/CubemapTexture.cpp @@ -0,0 +1,47 @@ +#include "CubemapTexture.h" + +CubemapTexture::CubemapTexture(char* posXFile, char* negXFile, char* posYFile, char* negYFile, char* posZFile, char* negZFile) +{ + m_TextureFiles[0] = posXFile; + m_TextureFiles[1] = negXFile; + m_TextureFiles[2] = posYFile; + m_TextureFiles[3] = negYFile; + m_TextureFiles[4] = posZFile; + m_TextureFiles[5] = negZFile; + + Load(); +} + +CubemapTexture::~CubemapTexture() +{ + //glDeleteTextures(1, &m_Texture); +} + +void CubemapTexture::Load() +{ + m_Texture = SOIL_load_OGL_cubemap( + m_TextureFiles[0], + m_TextureFiles[1], + m_TextureFiles[2], + m_TextureFiles[3], + m_TextureFiles[4], + m_TextureFiles[5], + SOIL_LOAD_AUTO, + SOIL_CREATE_NEW_ID, + 0); + + if (m_Texture == 0) + LOG_ERROR("SOIL cubemap loading error: %s", SOIL_last_result()); + + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); +} + +void CubemapTexture::Bind(GLenum textureUnit) +{ + glActiveTexture(textureUnit); + glBindTexture(GL_TEXTURE_CUBE_MAP, m_Texture); +} diff --git a/Escape-the-Dawn/CubemapTexture.h b/Escape-the-Dawn/CubemapTexture.h new file mode 100644 index 0000000..fd55721 --- /dev/null +++ b/Escape-the-Dawn/CubemapTexture.h @@ -0,0 +1,31 @@ +#ifndef CubemapTexture_h__ +#define CubemapTexture_h__ + +#include "OpenGL.h" +#include "glerror.h" +#include + +#include "logging.h" + +class CubemapTexture +{ +public: + CubemapTexture() { } + CubemapTexture( + char* posXFile, + char* negXFile, + char* posYFile, + char* negYFile, + char* posZFile, + char* negZFile); + ~CubemapTexture(); + + void Load(); + void Bind(GLenum textureSlot); + +private: + char* m_TextureFiles[6]; + GLuint m_Texture; +}; + +#endif // CubemapTexture_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 0b8fcfa..7fd3220 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -98,6 +98,7 @@ + @@ -109,6 +110,7 @@ + @@ -126,6 +128,7 @@ + @@ -155,6 +158,7 @@ + @@ -174,6 +178,8 @@ + + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index e19e833..84ffe0f 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -47,6 +47,8 @@ + + Systems @@ -151,6 +153,8 @@ + + Systems @@ -187,6 +191,12 @@ Shaders + + Shaders + + + Shaders + diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 1eca1bd..ccd6242 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -11,6 +11,7 @@ void GameWorld::Initialize() std::shared_ptr camera; std::shared_ptr bounds; std::shared_ptr collision; + std::shared_ptr soundEmitter; EntityID ent; // Fucking lights diff --git a/Escape-the-Dawn/GameWorld.cpp.orig b/Escape-the-Dawn/GameWorld.cpp.orig new file mode 100644 index 0000000..12d11a3 --- /dev/null +++ b/Escape-the-Dawn/GameWorld.cpp.orig @@ -0,0 +1,169 @@ +#include "GameWorld.h" + + +void GameWorld::Initialize() +{ + World::Initialize(); + + AddSystem("LevelGenerationSystem"); + AddSystem("InputSystem"); + AddSystem("CollisionSystem"); + //AddSystem("ParticleSystem"); + AddSystem("PlayerSystem"); + AddSystem("SoundSystem"); + AddSystem("RenderSystem"); + + std::shared_ptr transform; + std::shared_ptr model; + std::shared_ptr pointLight; + std::shared_ptr camera; + std::shared_ptr bounds; + std::shared_ptr collision; + std::shared_ptr soundEmitter; + EntityID ent; + + + + + // Fucking lights + ent = CreateEntity(); + transform = AddComponent(ent, "Transform"); + 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(0.0, 0.0, 5.0); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 1.f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; + model = AddComponent(ent, "Model"); + model->ModelFile = "Models/sphere.obj"; + ent = CreateEntity(); + transform = AddComponent(ent, "Transform"); + 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(0.0, 5.0, 0.0); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 1.f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; + model = AddComponent(ent, "Model"); + model->ModelFile = "Models/sphere.obj"; + ent = CreateEntity(); + transform = AddComponent(ent, "Transform"); + 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(5.0, 0.0, 0.0); + pointLight->constantAttenuation = 0.f; + pointLight->linearAttenuation = 1.f; + pointLight->quadraticAttenuation = 0.f; + pointLight->spotExponent = 0.0f; + model = AddComponent(ent, "Model"); + model->ModelFile = "Models/sphere.obj"; + + //ground + ent = CreateEntity(); + transform = AddComponent(ent, "Transform"); + transform->Position = glm::vec3(0.f, 0.f, 0.f); + model = AddComponent(ent, "Model"); + model->ModelFile = "Models/plane.obj"; + + // Player + m_Player = CreateEntity(); + SetProperty(m_Player, "Name", std::string("PlayerShip")); + transform = AddComponent(m_Player, "Transform"); + 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"); +<<<<<<< HEAD + bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3); + bounds->VolumeVector = glm::vec3(4.f,0.7f,4); + soundEmitter = AddComponent(m_Player, "SoundEmitter"); + soundEmitter->Loop = true; + soundEmitter->MaxDistance = FLT_MAX; + soundEmitter->ReferenceDistance = 10; + soundEmitter->Gain = 1; + soundEmitter->Pitch = 1; + soundEmitter->Path = "Sounds/hallelujah.wav"; + GetSystem("SoundSystem")->PlaySound(soundEmitter); +======= + bounds->Origin = glm::vec3(0, 0, 2.f); + bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1); +>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605 + + + // 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"); + model->ModelFile = "Models/ship.obj"; + collision = AddComponent(player2, "Collision"); + collision->Phantom = false; + bounds = AddComponent(player2, "Bounds"); + bounds->Origin = transform->Position; + bounds->VolumeVector = glm::vec3(2.f,2.f,2.f);*/ +} + +void GameWorld::Update(double dt) +{ + World::Update(dt); +} + +void GameWorld::RegisterComponents() +{ + m_ComponentFactory.Register("Bounds", []() { return new Components::Bounds(); }); + m_ComponentFactory.Register("Camera", []() { return new Components::Camera(); }); + m_ComponentFactory.Register("Collision", []() { return new Components::Collision(); }); + m_ComponentFactory.Register("DirectionalLight", []() { return new Components::DirectionalLight(); }); + m_ComponentFactory.Register("Input", []() { return new Components::Input(); }); + m_ComponentFactory.Register("Model", []() { return new Components::Model(); }); + m_ComponentFactory.Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); + m_ComponentFactory.Register("PointLight", []() { return new Components::PointLight(); }); + m_ComponentFactory.Register("PowerUp", []() { return new Components::PowerUp(); }); + m_ComponentFactory.Register("SoundEmitter", []() { return new Components::SoundEmitter(); }); + m_ComponentFactory.Register("Sprite", []() { return new Components::Sprite(); }); + m_ComponentFactory.Register("Stat", []() { return new Components::Stat(); }); + m_ComponentFactory.Register("Template", []() { return new Components::Template(); }); + m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); +} + +void GameWorld::RegisterSystems() +{ + m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); + m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); }); + m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); + //m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); + m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); + m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); + m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); }); +} \ No newline at end of file diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index b2e985f..39e3b2c 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -99,6 +99,13 @@ void Renderer::LoadContent() m_ShaderProgramDebugAABB.Compile(); m_ShaderProgramDebugAABB.Link(); + m_ShaderProgramSkybox.AddShader(std::shared_ptr(new VertexShader("Shaders/Skybox.vert.glsl"))); + m_ShaderProgramSkybox.AddShader(std::shared_ptr(new FragmentShader("Shaders/Skybox.frag.glsl"))); + m_ShaderProgramSkybox.Compile(); + m_ShaderProgramSkybox.Link(); + + m_Skybox = std::make_shared(CubemapTexture("Textures/right.jpg", "Textures/left.jpg", "Textures/top.jpg", "Textures/bottom.jpg", "Textures/front.jpg", "Textures/back.jpg")); + m_DebugAABB = CreateAABB(); m_ScreenQuad = CreateQuad(); CreateShadowMap(m_ShadowMapRes); @@ -133,6 +140,7 @@ void Renderer::Draw(double dt) { glDisable(GL_BLEND); + DrawSkybox(); DrawShadowMap(); DrawScene(); @@ -167,13 +175,26 @@ void Renderer::Draw(double dt) glfwSwapBuffers(m_Window); } -void Renderer::DrawScene() +void Renderer::DrawSkybox() { glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, WIDTH, HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glClearColor(1.0f, 1.0f, 0.0f, 1.0f); + m_ShaderProgramSkybox.Bind(); + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(m_Camera->Orientation()); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramSkybox.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(cameraMatrix)); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + m_Skybox->Draw(); +} + +void Renderer::DrawScene() +{ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, WIDTH, HEIGHT); + + glClear(GL_DEPTH_BUFFER_BIT); + //glClearColor(1.0f, 1.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); @@ -466,6 +487,33 @@ GLuint Renderer::CreateAABB() return vao; } +GLuint Renderer::CreateSkybox() +{ + glm::vec3 skyBoxVertices[] = + { + glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), + glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, 1.0f), + glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), + glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(-1.0f, -1.0f, 1.0f), + glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, -1.0f, 1.0f), + glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, -1.0f, -1.0f) + }; + + GLuint vbo, vao; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(skyBoxVertices), skyBoxVertices, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); + glEnableVertexAttribArray(0); + glBindVertexArray(0); + + return vao; +} void Renderer::ClearStuff() { AABBsToRender.clear(); diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 777643c..f140c4f 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -15,6 +15,7 @@ #include "ShaderProgram.h" #include "Model.h" #include "Components/PointLight.h" +#include "Skybox.h" class Renderer { @@ -66,6 +67,9 @@ public: void DrawWireframe(bool val) { m_DrawWireframe = val; } bool DrawBounds() const { return m_DrawBounds; } void DrawBounds(bool val) { m_DrawBounds = val; } + void DrawSkybox(); + + private: GLFWwindow* m_Window; @@ -76,6 +80,8 @@ private: bool m_DrawWireframe; bool m_DrawBounds; + std::shared_ptr m_Skybox; + int m_ShadowMapRes; glm::vec3 m_SunPosition; glm::vec3 m_SunTarget; @@ -93,6 +99,7 @@ private: ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; ShaderProgram m_ShaderProgramDebugAABB; + ShaderProgram m_ShaderProgramSkybox; void ClearStuff(); void DrawScene(); @@ -102,6 +109,7 @@ private: GLuint CreateQuad(); void DrawDebugShadowMap(); GLuint CreateAABB(); + GLuint CreateSkybox(void); }; diff --git a/Escape-the-Dawn/Shaders/Skybox.frag.glsl b/Escape-the-Dawn/Shaders/Skybox.frag.glsl new file mode 100644 index 0000000..66956b4 --- /dev/null +++ b/Escape-the-Dawn/Shaders/Skybox.frag.glsl @@ -0,0 +1,15 @@ +#version 430 + +uniform samplerCube CubemapTexture; + +in VertexData { + vec3 TextureCoord; +} Input; + +out vec4 FragColor; + +void main() +{ + FragColor = texture(CubemapTexture, Input.TextureCoord); + //FragColor = vec4(1.0, 1.0, 1.0, 0.0); +} \ No newline at end of file diff --git a/Escape-the-Dawn/Shaders/Skybox.vert.glsl b/Escape-the-Dawn/Shaders/Skybox.vert.glsl new file mode 100644 index 0000000..f4f8c4c --- /dev/null +++ b/Escape-the-Dawn/Shaders/Skybox.vert.glsl @@ -0,0 +1,15 @@ +#version 430 + +uniform mat4 MVP; + +layout(location = 0) in vec3 Position; + +out VertexData { + vec3 TextureCoord; +} Output; + +void main() +{ + gl_Position = MVP * vec4(Position, 1.0); + Output.TextureCoord = Position; +} \ No newline at end of file diff --git a/Escape-the-Dawn/Skybox.cpp b/Escape-the-Dawn/Skybox.cpp new file mode 100644 index 0000000..db2218a --- /dev/null +++ b/Escape-the-Dawn/Skybox.cpp @@ -0,0 +1,79 @@ +#include "Skybox.h" + +Skybox::Skybox(CubemapTexture cubemap) +{ + m_Cubemap = cubemap; + + float cubeVertices[] = { + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + }; + //std::copy(cubeVertices, cubeVertices + (3*8 - 1), m_CubeVertices); + + unsigned int cubeIndices[] = { + // Back + 0, 2, 3, + 0, 1, 2, + + // Right + 1, 6, 2, + 1, 5, 6, + + // Front + 5, 7, 6, + 5, 4, 7, + + // Left + 4, 3, 7, + 4, 0, 3, + + // Top + 3, 6, 7, + 3, 2, 6, + + // Bottom + 4, 1, 0, + 4, 5, 1, + }; + //std::copy(cubeIndices, cubeIndices + (3*12 - 1), m_CubeIndices); + + GLuint vbo; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, 3 * 8 * sizeof(float), cubeVertices, GL_STATIC_DRAW); + + glGenBuffers(1, &ibo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * 12 * sizeof(int), cubeIndices, GL_STATIC_DRAW); + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(0); + + GLERROR(""); +} + + +Skybox::~Skybox() +{ + +} + +void Skybox::Draw() +{ + m_Cubemap.Bind(GL_TEXTURE0); + + glBindVertexArray(vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); + glDepthMask(GL_FALSE); + glDrawElements(GL_TRIANGLES, 3 * 12, GL_UNSIGNED_INT, 0); + glDepthMask(GL_TRUE); +} diff --git a/Escape-the-Dawn/Skybox.h b/Escape-the-Dawn/Skybox.h new file mode 100644 index 0000000..ff014e9 --- /dev/null +++ b/Escape-the-Dawn/Skybox.h @@ -0,0 +1,29 @@ +#ifndef Skybox_h__ +#define Skybox_h__ + +#include + +#include "OpenGL.h" +#include "logging.h" +#include "glerror.h" +#include "CubemapTexture.h" + +class Skybox +{ +public: + Skybox(CubemapTexture cubemap); + ~Skybox(); + + void Draw(); + +private: + CubemapTexture m_Cubemap; + + GLuint ibo; + GLuint vao; + + float m_CubeVertices[3 * 8]; + int m_CubeIndices[1]; +}; + +#endif // Skybox_h__ \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp.orig b/Escape-the-Dawn/Systems/CollisionSystem.cpp.orig new file mode 100644 index 0000000..572fc4a --- /dev/null +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp.orig @@ -0,0 +1,134 @@ +#include "CollisionSystem.h" +#include "World.h" + +void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + /*if (parent != 0) + { + auto transform = m_World->GetComponent(entity, "Transform"); + auto parentTransform = m_World->GetComponent(parent, "Transform"); + + transform->Position[0] += parentTransform->Position[0]; + transform->Position[1] += parentTransform->Position[1]; + transform->Position[2] += parentTransform->Position[2]; +<<<<<<< HEAD + } +======= + }*/ + + auto collisionComponent = m_World->GetComponent(entity, "Collision"); + if (!collisionComponent) + return; + + // Clear old collisions + collisionComponent->CollidingEntities.clear(); + + // Quit out if we're not interested in collision events + if (!collisionComponent->Interested) + return; +>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605 + + auto entities = m_World->GetEntities(); + + for (auto pair : *entities) { + EntityID entity2 = pair.first; + EntityID parent2 = pair.second; + + // Check if entity2 is a child of entity + /*auto currentParent = parent; + bool childOfEntity2 = false; + while (currentParent != 0) { + if (currentParent == entity2) { + childOfEntity2 = true; + break; + } + currentParent = entities[currentParent]; + } + if (childOfEntity2) + continue;*/ + + // Check if we're the parent to entity2 + + //pair.first, pair.second; + if(entity == entity2) + continue; + + Intersects(entity, entity2); + } +} + +void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity) +{ + auto aTransform = m_World->GetComponent(aEntity, "Transform"); + if(aTransform == nullptr) + return; + auto bTransform = m_World->GetComponent(bEntity, "Transform"); + if(bTransform == nullptr) + return; + + auto aCollisionComponent = m_World->GetComponent(aEntity, "Collision"); + if(aCollisionComponent == nullptr) + return; + auto bCollisionComponent = m_World->GetComponent(bEntity, "Collision"); + if(bCollisionComponent == nullptr) + return; + + + auto aBounds = m_World->GetComponent(aEntity, "Bounds"); + + auto bBounds = m_World->GetComponent(bEntity, "Bounds"); + + glm::vec3 aPos = aTransform->Position + (aBounds->Origin * aTransform->Scale); + glm::vec3 bPos = bTransform->Position + (bBounds->Origin * bTransform->Scale); + + glm::vec3 aMax = aPos + aBounds->VolumeVector * aTransform->Scale; + glm::vec3 aMin = aPos - aBounds->VolumeVector * aTransform->Scale; + glm::vec3 bMax = bPos + bBounds->VolumeVector * bTransform->Scale; + glm::vec3 bMin = bPos - bBounds->VolumeVector * bTransform->Scale; + + if (aMin.x <= bMax.x && bMin.x <= aMax.x) { + if (aMin.y <= bMax.y && bMin.y <= aMax.y) { + if (aMin.z <= bMax.z && bMin.z <= aMax.z) { + aCollisionComponent->CollidingEntities.push_back(aEntity); + bCollisionComponent->CollidingEntities.push_back(bEntity); + return; + } + } + } +} + +void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr bounds) +{ + float width = abs(bounds->VolumeVector.x); + float height = abs(bounds->VolumeVector.y); + float depth = abs(bounds->VolumeVector.z); + + GLfloat vertices[] = { + bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0, + bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0, + }; + + GLuint vbo_vertices; + glGenBuffers(1, &vbo_vertices); + glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + GLushort elements[] = { + 0, 1, 2, 3, + 4, 5, 6, 7, + 0, 4, 1, 5, + 2, 6, 3, 7 + }; + GLuint ibo_elements; + glGenBuffers(1, &ibo_elements); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp index b1035b6..b578fd8 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp @@ -32,7 +32,7 @@ void Systems::LevelGenerationSystem::SpawnObstacle() sound->Loop = true; sound->Gain = 1.f; - sound->ReferenceDistance = 15.f; + sound->ReferenceDistance = 4.f; m_World->GetSystem("SoundSystem")->PlaySound(sound, "Sounds/hum.wav"); if(typeRandom >= 0 && typeRandom < 1) // Mountain stuff :D diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 303b5c5..07142f1 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -20,7 +20,9 @@ public: 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); + void PlaySound(std::shared_ptr emitter, std::string path); // Use if you want to play a temporary .wav file not from component + void PlaySound(std::shared_ptr emitter); // Use if you want to play .wav file from component // imon no hate plx T.T + void StopSound(std::shared_ptr emitter); private: ALuint LoadFile(std::string fileName); diff --git a/Escape-the-Dawn/Systems/SoundSystem.h.orig b/Escape-the-Dawn/Systems/SoundSystem.h.orig new file mode 100644 index 0000000..b6ccbfb --- /dev/null +++ b/Escape-the-Dawn/Systems/SoundSystem.h.orig @@ -0,0 +1,51 @@ +#ifndef SoundEmitter_h__ +#define SoundEmitter_h__ + +#include "System.h" +#include "Components/Transform.h" +#include "Components/SoundEmitter.h" +#include +#include +#include +#include + +namespace Systems +{ + +class SoundSystem : public System +{ +public: + SoundSystem(World* world); + + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + void OnComponentCreated(std::string type, std::shared_ptr component) override; +<<<<<<< HEAD + void PlaySound(std::shared_ptr emitter, std::string path); // Use if you want to play a temporary .wav file not from component + void PlaySound(std::shared_ptr emitter); // Use if you want to play .wav file from component // imon no hate plx T.T + void StopSound(std::shared_ptr emitter); +======= + void OnComponentRemoved(std::string type, Component* component) override; + void PlaySound(std::shared_ptr emitter, std::string fileName); +>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605 + +private: + ALuint LoadFile(std::string fileName); + ALuint CreateSource(); + + //File-info + char type[4]; + unsigned long size, chunkSize; + short formatType, channels; + unsigned long sampleRate, avgBytesPerSec; + short bytesPerSample, bitsPerSample; + unsigned long dataSize; + + + std::map m_Sources; + std::map m_BufferCache; // string = fileName +}; + +} +#endif // !SoundEmitter_h__ + diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index e5cdb3c..8e1f6ed 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -86,6 +86,19 @@ void Systems::SoundSystem::PlaySound(std::shared_ptr e alSourcePlay(m_Sources[emitter.get()]); } +void Systems::SoundSystem::PlaySound(std::shared_ptr emitter) +{ + ALuint buffer = LoadFile(emitter->Path); + ALuint source = m_Sources[emitter.get()]; + alSourcei(source, AL_BUFFER, buffer); + alSourcePlay(m_Sources[emitter.get()]); +} + +void Systems::SoundSystem::StopSound(std::shared_ptr emitter) +{ + alSourceStop(m_Sources[emitter.get()]); +} + void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) { if(type == "SoundEmitter") { @@ -104,13 +117,13 @@ void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* compo } } -ALuint Systems::SoundSystem::LoadFile(std::string fileName) +ALuint Systems::SoundSystem::LoadFile(std::string path) { - if (m_BufferCache.find(fileName) != m_BufferCache.end()) - return m_BufferCache[fileName]; + if (m_BufferCache.find(path) != m_BufferCache.end()) + return m_BufferCache[path]; FILE *fp = NULL; - fp = fopen(fileName.c_str(), "rb"); + fp = fopen(path.c_str(), "rb"); //CHECK FOR VALID WAVE-FILE fread(type, sizeof(char), 4, fp); @@ -176,7 +189,7 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName) alBufferData(buffer, format, buf, dataSize, sampleRate); delete[] buf; - m_BufferCache[fileName] = buffer; + m_BufferCache[path] = buffer; return buffer; } @@ -185,5 +198,8 @@ ALuint Systems::SoundSystem::CreateSource() ALuint source; alGenSources((ALuint)1, &source); + alDopplerFactor(1); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect + alDopplerVelocity(350.f); // Defines the velocity of the sound + return source; } \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp.orig b/Escape-the-Dawn/Systems/SoundsSystem.cpp.orig new file mode 100644 index 0000000..0cf29de --- /dev/null +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp.orig @@ -0,0 +1,210 @@ +#include "SoundSystem.h" +#include "World.h" + +Systems::SoundSystem::SoundSystem(World* world) + : System(world) +{ + //initialize OpenAL + ALCdevice* Device = alcOpenDevice(NULL); + ALCcontext* context; + if(Device) + { + context = alcCreateContext(Device, NULL); + alcMakeContextCurrent(context); + } + else + { + LOG_ERROR("OMG OPEN AL FAIL"); + } + + alGetError(); + + alSpeedOfSound(340.29f); // Speed of sound + alDistanceModel(AL_INVERSE_DISTANCE); +} + +void Systems::SoundSystem::Update(double dt) +{ + +} + +void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if (transformComponent == nullptr) + return; + + auto entityName = m_World->GetProperty(entity, "Name"); + if (entityName == "Camera") + { + glm::vec3 playerPos = transformComponent->Position; + ALfloat listenerPos[3] = { playerPos.x, playerPos.y, -playerPos.z }; + + glm::vec3 playerVel = transformComponent->Velocity; + ALfloat listenerVel[3] = { playerVel.x, playerVel.y, -playerVel.z }; + + glm::fquat playerQuatOri = transformComponent->Orientation; + glm::vec3 playerOriFW = glm::rotate(playerQuatOri, glm::vec3(0, 0, -1)); + glm::vec3 playerOriUP = glm::rotate(playerQuatOri, glm::vec3(0, 1, 0)); + ALfloat listenerOri[6] = { playerOriFW.x, playerOriFW.y, playerOriFW.z, playerOriUP.x, playerOriUP.y, playerOriUP.z }; + + //Listener + alListenerfv(AL_POSITION, listenerPos); + alListenerfv(AL_VELOCITY, listenerVel); + alListenerfv(AL_ORIENTATION, listenerOri); + } + + auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + if(soundEmitter != nullptr) + { + 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); + + glm::vec3 emitterPos = transformComponent->Position; + ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, -emitterPos.z }; + + glm::vec3 emitterVel= transformComponent->Velocity; + ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, -emitterVel.z }; + + alSourcefv(source, AL_POSITION, sourcePos); + alSourcefv(source, AL_VELOCITY, sourceVel); + } +} + +void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string path) +{ +<<<<<<< HEAD + ALuint buffer = LoadFile(path); + ALuint source = m_Source[emitter.get()]; +======= + if (m_Sources.find(emitter.get()) == m_Sources.end()) + return; + + ALuint buffer = LoadFile(fileName); + ALuint source = m_Sources[emitter.get()]; +>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605 + alSourcei(source, AL_BUFFER, buffer); + alSourcePlay(m_Sources[emitter.get()]); +} + +void Systems::SoundSystem::PlaySound(std::shared_ptr emitter) +{ + ALuint buffer = LoadFile(emitter->Path); + ALuint source = m_Source[emitter.get()]; + alSourcei(source, AL_BUFFER, buffer); + alSourcePlay(m_Source[emitter.get()]); +} + +void Systems::SoundSystem::StopSound(std::shared_ptr emitter) +{ + alSourceStop(m_Source[emitter.get()]); +} + +void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) +{ + if(type == "SoundEmitter") { + ALuint source = CreateSource(); + 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); + } + } +} + +ALuint Systems::SoundSystem::LoadFile(std::string path) +{ + if (m_BufferCache.find(path) != m_BufferCache.end()) + return m_BufferCache[path]; + + FILE *fp = NULL; + fp = fopen(path.c_str(), "rb"); + + //CHECK FOR VALID WAVE-FILE + fread(type, sizeof(char), 4, fp); + if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') { + LOG_ERROR("ERROR: No RIFF in WAVE-file"); + return 0; + } + + fread(&size, sizeof(unsigned long), 1, fp); + fread(type, sizeof(char), 4, fp); + if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') { + LOG_ERROR("ERROR: Not WAVE-file"); + return 0; + } + + fread(type, sizeof(char), 4, fp); + if(type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ') { + LOG_ERROR("ERROR: No fmt in WAVE-file"); + return 0; + } + + //READ THE DATA FROM WAVE-FILE + fread(&chunkSize, sizeof(unsigned long), 1, fp); + fread(&formatType, sizeof(short), 1, fp); + fread(&channels, sizeof(short), 1, fp); + fread(&sampleRate, sizeof(unsigned long), 1, fp); + fread(&avgBytesPerSec, sizeof(unsigned long), 1, fp); + fread(&bytesPerSample, sizeof(short), 1, fp); + fread(&bitsPerSample, sizeof(short), 1, fp); + + fread(type, sizeof(char), 4, fp); + if(type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a') + { + LOG_ERROR("ERROR: WAVE-file Missing data"); + return 0; + } + + fread(&dataSize, sizeof(unsigned long), 1, fp); + + unsigned char* buf = new unsigned char[dataSize]; + fread(buf, sizeof(unsigned char), dataSize, fp); + fclose(fp); + + // Create buffer + ALuint format = 0; + if(bitsPerSample == 8) + { + if(channels == 1) + format = AL_FORMAT_MONO8; + else if(channels == 2) + format = AL_FORMAT_STEREO8; + } + if(bitsPerSample == 16) + { + if (channels == 1) + format = AL_FORMAT_MONO16; + else if (channels == 2) + format = AL_FORMAT_STEREO16; + } + + ALuint buffer; + alGenBuffers(1, &buffer); + alBufferData(buffer, format, buf, dataSize, sampleRate); + delete[] buf; + + m_BufferCache[path] = buffer; + return buffer; +} + +ALuint Systems::SoundSystem::CreateSource() +{ + ALuint source; + alGenSources((ALuint)1, &source); + + alDopplerFactor(2.f); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect + alDopplerVelocity(350.f); // Defines the velocity of the sound + + return source; +} \ No newline at end of file diff --git a/Escape-the-Dawn/Textures/back.jpg b/Escape-the-Dawn/Textures/back.jpg new file mode 100644 index 0000000..0da3ae4 Binary files /dev/null and b/Escape-the-Dawn/Textures/back.jpg differ diff --git a/Escape-the-Dawn/Textures/bottom.jpg b/Escape-the-Dawn/Textures/bottom.jpg new file mode 100644 index 0000000..2e7d15e Binary files /dev/null and b/Escape-the-Dawn/Textures/bottom.jpg differ diff --git a/Escape-the-Dawn/Textures/front.jpg b/Escape-the-Dawn/Textures/front.jpg new file mode 100644 index 0000000..d49b8b1 Binary files /dev/null and b/Escape-the-Dawn/Textures/front.jpg differ diff --git a/Escape-the-Dawn/Textures/left.jpg b/Escape-the-Dawn/Textures/left.jpg new file mode 100644 index 0000000..7adbdc8 Binary files /dev/null and b/Escape-the-Dawn/Textures/left.jpg differ diff --git a/Escape-the-Dawn/Textures/right.jpg b/Escape-the-Dawn/Textures/right.jpg new file mode 100644 index 0000000..b4aaa73 Binary files /dev/null and b/Escape-the-Dawn/Textures/right.jpg differ diff --git a/Escape-the-Dawn/Textures/top.jpg b/Escape-the-Dawn/Textures/top.jpg new file mode 100644 index 0000000..80a9b48 Binary files /dev/null and b/Escape-the-Dawn/Textures/top.jpg differ diff --git a/Escape-the-Dawn/Textures/tron_back.jpg b/Escape-the-Dawn/Textures/tron_back.jpg new file mode 100644 index 0000000..e7ad4f1 Binary files /dev/null and b/Escape-the-Dawn/Textures/tron_back.jpg differ diff --git a/Escape-the-Dawn/Textures/tron_bottom.jpg b/Escape-the-Dawn/Textures/tron_bottom.jpg new file mode 100644 index 0000000..c7b60e2 Binary files /dev/null and b/Escape-the-Dawn/Textures/tron_bottom.jpg differ diff --git a/Escape-the-Dawn/Textures/tron_front.jpg b/Escape-the-Dawn/Textures/tron_front.jpg new file mode 100644 index 0000000..e69ae42 Binary files /dev/null and b/Escape-the-Dawn/Textures/tron_front.jpg differ diff --git a/Escape-the-Dawn/Textures/tron_left.jpg b/Escape-the-Dawn/Textures/tron_left.jpg new file mode 100644 index 0000000..947b208 Binary files /dev/null and b/Escape-the-Dawn/Textures/tron_left.jpg differ diff --git a/Escape-the-Dawn/Textures/tron_right.jpg b/Escape-the-Dawn/Textures/tron_right.jpg new file mode 100644 index 0000000..85ccab0 Binary files /dev/null and b/Escape-the-Dawn/Textures/tron_right.jpg differ diff --git a/Escape-the-Dawn/Textures/tron_top.jpg b/Escape-the-Dawn/Textures/tron_top.jpg new file mode 100644 index 0000000..78e193f Binary files /dev/null and b/Escape-the-Dawn/Textures/tron_top.jpg differ