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/Components/Model.h b/Escape-the-Dawn/Components/Model.h index ce27b3b..11297da 100644 --- a/Escape-the-Dawn/Components/Model.h +++ b/Escape-the-Dawn/Components/Model.h @@ -11,7 +11,7 @@ namespace Components struct Model : Component { - std::string ModelFile; + const char* ModelFile; Color Color; }; 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 e936774..80dae46 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters @@ -26,8 +26,24 @@ + + GUI + + + GUI + + + GUI + + + GUI + + + GUI + + @@ -109,7 +125,23 @@ + + GUI + + + GUI + + + GUI + + + GUI + + + GUI + + diff --git a/Escape-the-Dawn/Model.cpp b/Escape-the-Dawn/Model.cpp index cd0f28d..0dca89d 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) { @@ -169,16 +170,20 @@ void Model::CreateBuffers( std::vector _Vertices, 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..a038c16 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -13,7 +13,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); @@ -39,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(); } @@ -47,7 +52,21 @@ 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(); + + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + + glm::mat4 MVP; + for(int i = 0; i < ModelsToRender.size(); i++) + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->model->texture[0]->texture); + MVP = cameraMatrix * ModelsToRender[i]->ModelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glBindVertexArray(ModelsToRender[i]->model->VAO); + glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->model->Vertices.size()); + } + ModelsToRender.clear(); glfwSwapBuffers(m_Window); } @@ -62,9 +81,12 @@ void Renderer::AddTextToDraw() //Add to draw shit vector } -void Renderer::AddModelToDraw(Model* _model) +void Renderer::AddModelToDraw(Model* _model, glm::vec3 _position, glm::quat _orientation) { - ModelsToRender.push_back(_model); + glm::mat4 RotationMatrix = glm::toMat4(_orientation); + glm::mat4 ModelMatrix = glm::translate(glm::mat4(), _position) * RotationMatrix ; + // You can now use ModelMatrix to build the MVP matrix + ModelsToRender.push_back(new ModelData(_model, ModelMatrix)); } //Fixa med shaders, lägga in alla verts osv. @@ -75,4 +97,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..e526ccd 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -17,8 +17,11 @@ #include #include #include +#include +#include #include "glerror.h" +#include "Camera.h" #include "ShaderProgram.h" #include "Model.h" @@ -26,8 +29,20 @@ class Renderer { public: ShaderProgram m_ShaderProgram; + glm::mat4 viewMatrix; + glm::mat4 projectionMatrix; - std::vector ModelsToRender; + int HEIGHT, WIDTH; + + struct ModelData + { + Model* model; + glm::mat4 ModelMatrix; + ModelData(Model* _model, glm::mat4 _modelMatrix) : model(_model), ModelMatrix(_modelMatrix) + {} + }; + + std::vector ModelsToRender; Renderer(); @@ -35,17 +50,20 @@ public: void Draw(double dt); void DrawText(); - void AddModelToDraw(Model*); + void AddModelToDraw(Model*, glm::vec3, glm::quat); void AddTextToDraw(); 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 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; diff --git a/Escape-the-Dawn/Systems/RenderSystem.cpp b/Escape-the-Dawn/Systems/RenderSystem.cpp index e69de29..ee8d5ff 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.cpp +++ b/Escape-the-Dawn/Systems/RenderSystem.cpp @@ -0,0 +1,37 @@ +#include "RenderSystem.h" +#include "World.h" + + + +void Systems::RenderSystem::OnComponentCreated( std::string type, std:: shared_ptr component ) +{ + if(type == "Model") + { + auto modelComponent = std::static_pointer_cast(component); + + if (m_CachedModels.find(modelComponent->ModelFile) != m_CachedModels.end()) { + m_CachedModels[modelComponent->ModelFile] = new Model(modelComponent->ModelFile); + } + + } +} + +void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID parent ) +{ + auto modelComponent = m_World->GetComponent(entity, "Model"); + if (modelComponent == nullptr) + return; + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if (transformComponent == nullptr) + return; + + Model* model = m_CachedModels[modelComponent->ModelFile]; + + + + m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation); +} + + + + diff --git a/Escape-the-Dawn/Systems/RenderSystem.h b/Escape-the-Dawn/Systems/RenderSystem.h index e69de29..6f02f56 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.h +++ b/Escape-the-Dawn/Systems/RenderSystem.h @@ -0,0 +1,41 @@ +#ifndef RenderSystem_h__ +#define RenderSystem_h__ + +#include "System.h" +#include "Model.h" +#include "Texture.h" +#include "Components/Model.h" +#include "Components/Transform.h" +#include "Renderer.h" +#include + +namespace Systems +{ + + class RenderSystem : public System + { + public: + RenderSystem(World* world, std::shared_ptr renderer) + : System(world), m_Renderer(renderer) {} + + + std::unordered_map m_CachedModels; + + + void AddModelToDraw(); + void OnComponentCreated(std::string type, std:: shared_ptr component) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + void Update(double dt) override; + + private: + std::shared_ptr m_Renderer; + + }; + + +} + + + + +#endif //RenderSystem_h__ \ No newline at end of file