From e4c443042b846aa93a9be0414deeffadaac42762 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 9 Mar 2014 18:08:56 +0100 Subject: [PATCH 1/4] =?UTF-8?q?SoundSystem=20p=C3=A5b=C3=B6rjat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Escape-the-Dawn/Components/SoundEmitter.h | 1 - Escape-the-Dawn/Escape-the-Dawn.vcxproj | 13 ++----- Escape-the-Dawn/Systems/SoundSystem.h | 29 ++++++++++++++++ Escape-the-Dawn/Systems/SoundsSystem.cpp | 41 +++++++++++++++++++++++ 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index 01772cd..441afe0 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -10,7 +10,6 @@ namespace Components struct SoundEmitter : Component { - std::string SoundFile; float Volume; float MaxRange; }; diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 4fbe3f4..b4ee946 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -40,14 +40,17 @@ + $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath) $(SolutionDir)\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath) $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) + $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\include;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) $(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) $(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) + $(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) $(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath) @@ -98,16 +101,11 @@ - - - - - @@ -135,13 +133,9 @@ - - - - @@ -154,7 +148,6 @@ - diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index e69de29..f6e8acc 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -0,0 +1,29 @@ +#ifndef SoundEmitter_h__ +#define SoundEmitter_h__ + +#include "System.h" +#include "Components/SoundEmitter.h" + +namespace System +{ + +class SoundSystem : public System +{ +public: + SoundSystem(World* world) : System(world) + { + + } + void Update(double dt) override; + void Update(double dt, EntityID entity, EntityID parent) override; + void OnComponentCreated(std::string type, std::shared_ptr component) override; + void PlaySound(std::shared_ptr emitter, std::string fileName); + void LoadFile(std::string fileName); +private: + ALCcontext* context; + +}; + +} +#endif // !SoundEmitter_h__ + diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index e69de29..8dd2ef4 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -0,0 +1,41 @@ +#include "SoundSystem.h" +#include "World.h" +#include +#include + +System::SoundSystem::SoundSystem(World* world) +{ + ALCdevice* Device = alcOpenDevice(nullptr); + + if(Device) + { + context = alcCreateContext(Device, nullptr); + alcMakeContextCurrent(context); + } + + +} + +void System::SoundSystem::Update(double dt) +{ + +} + +void System::SoundSystem::Update(double dt, EntityID entity, EntityID parent) +{ + auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + if(soundEmitter == nullptr) + return; + + +} + +void System::SoundSystem::PlaySound(std::shared_ptr emitter, std::string fileName) +{ + LoadFile(fileName); +} + +void System::SoundSystem::LoadFile(std::string fileName) +{ + loadWavFile() +} \ No newline at end of file From 34889b3b0e1f276f997d3a7775b8d6421cf4c745 Mon Sep 17 00:00:00 2001 From: imon Date: Sun, 9 Mar 2014 19:05:57 +0100 Subject: [PATCH 2/4] Entity properties using boost::any. You'll need Boost now. --- Escape-the-Dawn/Components/Bounds.h | 7 +++---- Escape-the-Dawn/Escape-the-Dawn.vcxproj | 4 ++-- Escape-the-Dawn/World.h | 16 ++++++++++++++++ Executable/Executable.vcxproj | 4 ++-- Tests/main.cpp | 22 +++++++++++++++++++++- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/Escape-the-Dawn/Components/Bounds.h b/Escape-the-Dawn/Components/Bounds.h index 591165b..3845485 100644 --- a/Escape-the-Dawn/Components/Bounds.h +++ b/Escape-the-Dawn/Components/Bounds.h @@ -4,15 +4,14 @@ #include "Component.h" #include - namespace Components { struct Bounds : Component { - //Axis Aligned Bounding Box - glm::vec3 origin; - glm::vec3 volumeVector; //The vector that defines the volume of the BB, it goes from one corner to the opposite one + //Axis Aligned Bounding Box + glm::vec3 Origin; + glm::vec3 VolumeVector; //The vector that defines the volume of the BB, it goes from one corner to the opposite one }; } diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 4fbe3f4..d2fcda5 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -44,10 +44,10 @@ $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) - $(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) + $(BOOST_ROOT);$(SolutionDir)\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) - $(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) + $(BOOST_ROOT);$(SolutionDir)\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) $(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath) diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index c8803e5..7f46c7f 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -2,10 +2,13 @@ #define World_h__ #include +#include #include #include #include +#include + #include "logging.h" #include "Factory.h" @@ -36,6 +39,17 @@ public: EntityID GetEntityParent(EntityID entity); + template + T GetProperty(EntityID entity, std::string property) + { + return boost::any_cast(m_EntityProperties[entity][property]); + } + + void SetProperty(EntityID entity, std::string property, boost::any value) + { + m_EntityProperties[entity][property] = value; + } + template std::shared_ptr AddComponent(EntityID entity, std::string componentType); std::shared_ptr AddComponent(EntityID entity, std::string componentType); @@ -59,6 +73,8 @@ protected: // A bottom to top tree. A map of child entities to parent entities. std::unordered_map m_EntityParents; + std::unordered_map> m_EntityProperties; + std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; diff --git a/Executable/Executable.vcxproj b/Executable/Executable.vcxproj index e63646d..d4307fe 100644 --- a/Executable/Executable.vcxproj +++ b/Executable/Executable.vcxproj @@ -39,13 +39,13 @@ - $(SolutionDir)\Escape-the-Dawn;$(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) + $(SolutionDir)\Escape-the-Dawn;$(BOOST_ROOT);$(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) $(SolutionDir)bin\ $(SolutionName)-$(Configuration) $(SolutionDir)obj\$(ProjectName)\ - $(SolutionDir)\Escape-the-Dawn;$(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) + $(SolutionDir)\Escape-the-Dawn;$(BOOST_ROOT);$(SolutionDir)\Libraries\SOIL\src;\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) $(SolutionDir)bin\ $(SolutionName)-$(Configuration) $(SolutionDir)obj\$(ProjectName)\ diff --git a/Tests/main.cpp b/Tests/main.cpp index 027263d..fdbaa18 100644 --- a/Tests/main.cpp +++ b/Tests/main.cpp @@ -1,6 +1,7 @@ #include "logging.h" #include "World.h" +#include "GameWorld.h" #include "Components/Transform.h" #define BOOST_TEST_MODULE World @@ -24,15 +25,19 @@ BOOST_AUTO_TEST_CASE(Parenting) BOOST_AUTO_TEST_CASE(SceneGraphTraversal) { - World world; + std::shared_ptr renderer(new Renderer()); + renderer->Initialize(); + GameWorld world(renderer); EntityID ent1 = world.CreateEntity(); auto t1 = world.AddComponent(ent1, "Transform"); + BOOST_REQUIRE(t1 != nullptr); t1->Position[0] = 1.0f; t1->Position[1] = 1.0f; t1->Position[2] = 1.0f; EntityID ent2 = world.CreateEntity(ent1); auto t2 = world.AddComponent(ent2, "Transform"); + BOOST_REQUIRE(t2 != nullptr); t2->Position[0] = 1.0f; t2->Position[1] = 1.0f; t2->Position[2] = 1.0f; @@ -61,4 +66,19 @@ BOOST_AUTO_TEST_CASE(ComponentCreation) EntityID ent = world.CreateEntity(); BOOST_CHECK(world.AddComponent(ent, "Transform") != nullptr); BOOST_CHECK(world.AddComponent(ent, "Input") != nullptr); +} + +BOOST_AUTO_TEST_CASE(Properties) +{ + World world; + + EntityID ent = world.CreateEntity(); + + world.SetProperty(ent, "String", std::string("derpfish")); + world.SetProperty(ent, "Integer", 123); + world.SetProperty(ent, "Float", 0.04f); + + BOOST_CHECK(world.GetProperty(ent, "String") == "derpfish"); + BOOST_CHECK(world.GetProperty(ent, "Integer") == 123); + BOOST_CHECK(world.GetProperty(ent, "Float") == 0.04f); } \ No newline at end of file From 7a69bb3faccccb0b8cfd2093c08b5e0c177aaa90 Mon Sep 17 00:00:00 2001 From: Tobias Dahl Date: Sun, 9 Mar 2014 20:13:02 +0100 Subject: [PATCH 3/4] WIP Renderer 65% --- Escape-the-Dawn/Model.cpp | 1 + Escape-the-Dawn/Model.h | 2 + Escape-the-Dawn/Renderer.cpp | 70 ++++++++++++++++++++++++++- Escape-the-Dawn/Renderer.h | 5 ++ Escape-the-Dawn/ShaderProgram.cpp | 5 ++ Escape-the-Dawn/ShaderProgram.h | 2 + Escape-the-Dawn/Shaders/Fragment.glsl | 2 +- Escape-the-Dawn/Shaders/Vertex.glsl | 2 +- 8 files changed, 85 insertions(+), 4 deletions(-) diff --git a/Escape-the-Dawn/Model.cpp b/Escape-the-Dawn/Model.cpp index cd0f28d..685833c 100644 --- a/Escape-the-Dawn/Model.cpp +++ b/Escape-the-Dawn/Model.cpp @@ -142,6 +142,7 @@ bool Model::Loadobj(const char* path, std::vector & out_vertices, st } + void Model::CreateBuffers( std::vector _Vertices, std::vector _Normals, std::vector_TextureCoords) { diff --git a/Escape-the-Dawn/Model.h b/Escape-the-Dawn/Model.h index 3be26dc..5ce4c11 100644 --- a/Escape-the-Dawn/Model.h +++ b/Escape-the-Dawn/Model.h @@ -46,6 +46,8 @@ public: std::vector &out_normals, std::vector & out_TextureCoords ); + + glm::mat4 GetMatrix(); void CreateBuffers( std::vector _Vertices, diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index f567de0..739ecd8 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -4,6 +4,47 @@ Renderer::Renderer() { } +// ------------ TEMPCAMERA -------------------- +struct Camera +{ + glm::vec3 Position; + float Pitch; + float Yaw; + float Roll; + + glm::mat4 getViewMatrix() + { + glm::mat4 view; + view = glm::rotate(view, Pitch, glm::vec3(1,0,0)); + view = glm::rotate(view, Yaw, glm::vec3(0,1,0)); + view = glm::rotate(view, Roll, glm::vec3(0,0,1)); + view = glm::translate(view, -Position); + + return view; + } + + glm::vec3 Forward() + { + glm::mat4 Orientation; + Orientation = glm::rotate(Orientation, Pitch, glm::vec3(1,0,0)); + Orientation = glm::rotate(Orientation, Yaw, glm::vec3(0,1,0)); + + return glm::vec3(glm::vec4(0,0,-1,0) * Orientation); + } + + glm::vec3 Right() + { + glm::mat4 Orientation; + Orientation = glm::rotate(Orientation, Pitch, glm::vec3(1,0,0)); + Orientation = glm::rotate(Orientation, Yaw, glm::vec3(0,1,0)); + + return glm::vec3(glm::vec4(1,0,0,0) * Orientation); + } + + Camera() : Position(0.0f, 2.0f, 0.0f), Pitch(1.0f), Yaw(4.5f), Roll(0.f) { } +} MyCamera; +//-----------------TempCamera-.--------------------- + void Renderer::Initialize() { // Initialize GLFW @@ -13,7 +54,9 @@ void Renderer::Initialize() } // Create a window - m_Window = glfwCreateWindow(1280, 720, "OpenGL", nullptr, nullptr); + WIDTH = 1280; + HEIGHT = 720; + m_Window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", nullptr, nullptr); if (!m_Window) { LOG_ERROR("GLFW: Failed to create window"); exit(EXIT_FAILURE); @@ -47,7 +90,28 @@ void Renderer::Draw(double _dt) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(1.0f, 1.0f, 0.0f, 1.0f); - //m_ShaderProgram->Bind(); + m_ShaderProgram.Bind(); + + viewMatrix = glm::mat4(); + + viewMatrix = MyCamera.getViewMatrix(); //TEMP + + glm::mat4 MVP; + + for(int i = 0; i < ModelsToRender.size(); i++) + { + glActiveTexture(GL_TEXTURE0); + //FIXA MED FLERA TEXTURER + glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->texture[0]->texture); + //MVP = projectionMatrix * viewMatrix * /* ModelMatrix */; + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glBindVertexArray(ModelsToRender[i]->VAO); + glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->Vertices.size()); + } + +// Vertices; +// std::vector Normals; +// std::vector TextureCoords; glfwSwapBuffers(m_Window); } @@ -75,4 +139,6 @@ void Renderer::LoadContent() m_ShaderProgram.AddShader(std::unique_ptr(new FragmentShader("Shaders/Fragment.glsl"))); m_ShaderProgram.Compile(); m_ShaderProgram.Link(); + + projectionMatrix = glm::perspective(45.0f, (float)WIDTH / HEIGHT, 0.01f, 100.0f); } diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index c8e4fd0..8a93f9f 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -26,9 +26,14 @@ class Renderer { public: ShaderProgram m_ShaderProgram; + glm::mat4 viewMatrix; + glm::mat4 projectionMatrix; + + int HEIGHT, WIDTH; std::vector ModelsToRender; + Renderer(); void Initialize(); diff --git a/Escape-the-Dawn/ShaderProgram.cpp b/Escape-the-Dawn/ShaderProgram.cpp index 7c5d133..cd08061 100644 --- a/Escape-the-Dawn/ShaderProgram.cpp +++ b/Escape-the-Dawn/ShaderProgram.cpp @@ -128,6 +128,11 @@ GLuint ShaderProgram::Link() return m_ShaderProgramHandle; } +GLuint ShaderProgram::GetHandle() +{ + return m_ShaderProgramHandle; +} + void ShaderProgram::Bind() { if (m_ShaderProgramHandle == 0) diff --git a/Escape-the-Dawn/ShaderProgram.h b/Escape-the-Dawn/ShaderProgram.h index 3f2dfb1..894575a 100644 --- a/Escape-the-Dawn/ShaderProgram.h +++ b/Escape-the-Dawn/ShaderProgram.h @@ -76,6 +76,8 @@ public: void Compile(); GLuint Link(); + GLuint GetHandle(); + void Bind(); void Unbind(); diff --git a/Escape-the-Dawn/Shaders/Fragment.glsl b/Escape-the-Dawn/Shaders/Fragment.glsl index d74889f..2efef52 100644 --- a/Escape-the-Dawn/Shaders/Fragment.glsl +++ b/Escape-the-Dawn/Shaders/Fragment.glsl @@ -1,4 +1,4 @@ -#version 440 +#version 430 uniform mat4 MVP; uniform sampler2D texture0; diff --git a/Escape-the-Dawn/Shaders/Vertex.glsl b/Escape-the-Dawn/Shaders/Vertex.glsl index 92b8723..042411c 100644 --- a/Escape-the-Dawn/Shaders/Vertex.glsl +++ b/Escape-the-Dawn/Shaders/Vertex.glsl @@ -1,4 +1,4 @@ -#version 440 +#version 430 uniform mat4 MVP; From fcbf802cc262d14b53cf54bc58c8f9b3089a4ac0 Mon Sep 17 00:00:00 2001 From: imon Date: Sun, 9 Mar 2014 20:32:57 +0100 Subject: [PATCH 4/4] Added Camera. --- Escape-the-Dawn/Camera.cpp | 74 +++++++++++++++++++ Escape-the-Dawn/Camera.h | 53 +++++++++++++ Escape-the-Dawn/Escape-the-Dawn.vcxproj | 2 + .../Escape-the-Dawn.vcxproj.filters | 2 + Escape-the-Dawn/Renderer.cpp | 51 ++----------- Escape-the-Dawn/Renderer.h | 5 +- 6 files changed, 140 insertions(+), 47 deletions(-) create mode 100644 Escape-the-Dawn/Camera.cpp create mode 100644 Escape-the-Dawn/Camera.h diff --git a/Escape-the-Dawn/Camera.cpp b/Escape-the-Dawn/Camera.cpp new file mode 100644 index 0000000..6deeed0 --- /dev/null +++ b/Escape-the-Dawn/Camera.cpp @@ -0,0 +1,74 @@ +#include "Camera.h" + +Camera::Camera(float yFOV, float aspectRatio, float nearClip, float farClip) +{ + m_FOV = yFOV; + m_AspectRatio = aspectRatio; + m_NearClip = nearClip; + m_FarClip = farClip; + + m_Position = glm::vec3(0.0); + m_Pitch = 0.f; + m_Yaw = 0.f; + + UpdateProjectionMatrix(); + 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::Right() +{ + return glm::rotate(glm::vec3(1.f, 0.f, 0.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); +} + +glm::mat4 Camera::Orientation() +{ + glm::mat4 orientation(1.f); + orientation = glm::rotate(orientation, m_Pitch, glm::vec3(1.f, 0.f, 0.f)); + orientation = glm::rotate(orientation, m_Yaw, glm::vec3(0.f, 1.f, 0.f)); + return orientation; +} + +void Camera::AspectRatio(float val) +{ + m_AspectRatio = val; + UpdateProjectionMatrix(); +} + +void Camera::Position(glm::vec3 val) +{ + m_Position = val; + UpdateViewMatrix(); +} + + +void Camera::Pitch(float val) +{ + m_Pitch = val; + UpdateViewMatrix(); +} + +void Camera::Yaw(float val) +{ + m_Yaw = val; + UpdateViewMatrix(); +} + +void Camera::UpdateProjectionMatrix() +{ + m_ProjectionMatrix = glm::perspective( + m_FOV, + m_AspectRatio, + m_NearClip, + m_FarClip + ); +} + +void Camera::UpdateViewMatrix() +{ + m_ViewMatrix = glm::translate(Orientation(), -m_Position); +} diff --git a/Escape-the-Dawn/Camera.h b/Escape-the-Dawn/Camera.h new file mode 100644 index 0000000..0b6ecb5 --- /dev/null +++ b/Escape-the-Dawn/Camera.h @@ -0,0 +1,53 @@ +#ifndef Camera_h__ +#define Camera_h__ + +#define GLM_FORCE_RADIANS +#include +#include +#include +#include + +class Camera +{ +public: + Camera(float yFOV, float aspectRatio, float nearClip, float farClip); + + glm::vec3 Forward(); + glm::vec3 Right(); + glm::mat4 Orientation(); + + float AspectRatio() const { return m_AspectRatio; } + void AspectRatio(float val); + + glm::vec3 Position() const { return m_Position; } + void Position(glm::vec3 val); + + float Pitch() const { return m_Pitch; } + void Pitch(float val); + float Yaw() const { return m_Yaw; } + void Yaw(float val); + + glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; } + void ProjectionMatrix(glm::mat4 val) { m_ProjectionMatrix = val; } + + glm::mat4 ViewMatrix() const { return m_ViewMatrix; } + void ViewMatrix(glm::mat4 val) { m_ViewMatrix = val; } + +private: + void UpdateProjectionMatrix(); + void UpdateViewMatrix(); + + float m_FOV; + float m_AspectRatio; + float m_NearClip; + float m_FarClip; + + glm::vec3 m_Position; + float m_Pitch; + float m_Yaw; + + glm::mat4 m_ProjectionMatrix; + glm::mat4 m_ViewMatrix; +}; + +#endif // Camera_h__ diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index 04d7205..f2c42f7 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -97,6 +97,7 @@ + @@ -117,6 +118,7 @@ + diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters index ca2171c..80dae46 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -43,6 +43,7 @@ GUI + @@ -140,6 +141,7 @@ GUI + diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 739ecd8..1ad7dee 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -4,47 +4,6 @@ Renderer::Renderer() { } -// ------------ TEMPCAMERA -------------------- -struct Camera -{ - glm::vec3 Position; - float Pitch; - float Yaw; - float Roll; - - glm::mat4 getViewMatrix() - { - glm::mat4 view; - view = glm::rotate(view, Pitch, glm::vec3(1,0,0)); - view = glm::rotate(view, Yaw, glm::vec3(0,1,0)); - view = glm::rotate(view, Roll, glm::vec3(0,0,1)); - view = glm::translate(view, -Position); - - return view; - } - - glm::vec3 Forward() - { - glm::mat4 Orientation; - Orientation = glm::rotate(Orientation, Pitch, glm::vec3(1,0,0)); - Orientation = glm::rotate(Orientation, Yaw, glm::vec3(0,1,0)); - - return glm::vec3(glm::vec4(0,0,-1,0) * Orientation); - } - - glm::vec3 Right() - { - glm::mat4 Orientation; - Orientation = glm::rotate(Orientation, Pitch, glm::vec3(1,0,0)); - Orientation = glm::rotate(Orientation, Yaw, glm::vec3(0,1,0)); - - return glm::vec3(glm::vec4(1,0,0,0) * Orientation); - } - - Camera() : Position(0.0f, 2.0f, 0.0f), Pitch(1.0f), Yaw(4.5f), Roll(0.f) { } -} MyCamera; -//-----------------TempCamera-.--------------------- - void Renderer::Initialize() { // Initialize GLFW @@ -82,6 +41,9 @@ void Renderer::Initialize() } glEnable(GL_DEPTH_TEST); + // Create Camera + m_Camera = std::make_shared(90.f, WIDTH / HEIGHT, 0.01f, 1000.f); + LoadContent(); } @@ -92,18 +54,15 @@ void Renderer::Draw(double _dt) m_ShaderProgram.Bind(); - viewMatrix = glm::mat4(); - - viewMatrix = MyCamera.getViewMatrix(); //TEMP + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; - for(int i = 0; i < ModelsToRender.size(); i++) { glActiveTexture(GL_TEXTURE0); //FIXA MED FLERA TEXTURER glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->texture[0]->texture); - //MVP = projectionMatrix * viewMatrix * /* ModelMatrix */; + MVP = cameraMatrix; // * ModelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glBindVertexArray(ModelsToRender[i]->VAO); glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->Vertices.size()); diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 8a93f9f..99c7ef7 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -19,6 +19,7 @@ #include #include "glerror.h" +#include "Camera.h" #include "ShaderProgram.h" #include "Model.h" @@ -33,7 +34,6 @@ public: std::vector ModelsToRender; - Renderer(); void Initialize(); @@ -46,11 +46,14 @@ public: void LoadContent(); GLFWwindow* GetWindow() const { return m_Window; } + std::shared_ptr GetCamera() const { return m_Camera; } private: GLFWwindow* m_Window; GLint m_glVersion[2]; GLchar* m_glVendor; + + std::shared_ptr m_Camera; }; #endif // Renderer_h__ \ No newline at end of file