From fcbf802cc262d14b53cf54bc58c8f9b3089a4ac0 Mon Sep 17 00:00:00 2001 From: imon Date: Sun, 9 Mar 2014 20:32:57 +0100 Subject: [PATCH] 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