From 0a13b3fe342db83eef2c0ddc991a44452df77aa3 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 12 Feb 2015 12:37:21 +0100 Subject: [PATCH] extracted projection and viewport to camera class --- src/app.cpp | 33 +++++++++++------------- src/app.hpp | 4 ++- src/camera.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/camera.hpp | 55 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 src/camera.cpp create mode 100644 src/camera.hpp diff --git a/src/app.cpp b/src/app.cpp index d54e8df..de7a39e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -7,9 +7,9 @@ namespace { constexpr GLfloat vtx_coords[] = { - -1.0f, -1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, - 0.0f, 1.0f, -1.0f, + -1.0f, -1.0f, -5.0f, + 1.0f, -1.0f, -5.0f, + 0.0f, 1.0f, -5.0f, }; } @@ -23,8 +23,8 @@ Application::Application() , ctx(window.CreateContext()) , init_glew() , program() +, cam() , vtx_buf(0) -, mvp() , mvp_handle(0) , running(false) { GLContext::EnableVSync(); @@ -60,20 +60,6 @@ Application::Application() glBindBuffer(GL_ARRAY_BUFFER, vtx_buf); glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW); - glm::mat4 projection = glm::perspective( - 45.0f, // FOV in degrees - 1.0f, // aspect ratio - 0.1f, // near clip - 100.0f // far clip - ); - glm::mat4 view = glm::lookAt( - glm::vec3(0, 0, 0), // observer - glm::vec3(0, 0, -1), // target - glm::vec3(0, 1, 0) // up - ); - glm::mat4 model(1.0f); // identity: no transformation - mvp = projection * view * model; - mvp_handle = program.UniformLocation("MVP"); glClearColor(0.0, 0.0, 0.0, 1.0); @@ -108,6 +94,15 @@ void Application::HandleEvents() { case SDL_QUIT: running = false; break; + case SDL_WINDOWEVENT: + switch (event.window.event) { + case SDL_WINDOWEVENT_RESIZED: + cam.Viewport(event.window.data1, event.window.data2); + break; + default: + break; + } + break; default: break; } @@ -119,6 +114,8 @@ void Application::Render() { program.Use(); + glm::mat4 model(1.0f); // identity: no transformation + glm::mat4 mvp(cam.MakeMVP(model)); glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); glEnableVertexAttribArray(0); diff --git a/src/app.hpp b/src/app.hpp index 9ec5f1f..5148a7a 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -4,6 +4,7 @@ #include #include +#include "camera.hpp" #include "init.hpp" #include "shader.hpp" @@ -33,8 +34,9 @@ private: InitGLEW init_glew; Program program; + Camera cam; + GLuint vtx_buf; - glm::mat4 mvp; GLuint mvp_handle; bool running; diff --git a/src/camera.cpp b/src/camera.cpp new file mode 100644 index 0000000..8309fe6 --- /dev/null +++ b/src/camera.cpp @@ -0,0 +1,68 @@ +#include "camera.hpp" + +#include +#include + + +namespace blank { + +Camera::Camera() +: fov(45.0f) +, aspect(1.0f) +, near_clip(0.1f) +, far_clip(100.0f) +, position(0, 0, 0) +, target(0, 0, -1) +, up(0, 1, 0) +, projection(glm::perspective(fov, aspect, near_clip, far_clip)) +, view(glm::lookAt(position, target, up)) +, vp(projection * view) { + +} + +Camera::~Camera() { + +} + + +void Camera::Viewport(int width, int height) { + Viewport(0, 0, width, height); +} + +void Camera::Viewport(int x, int y, int width, int height) { + glViewport(x, y, width, height); + Aspect(width, height); +} + +void Camera::FOV(float f) { + fov = f; + UpdateProjection(); +} + +void Camera::Aspect(float r) { + aspect = r; + UpdateProjection(); +} + +void Camera::Aspect(float w, float h) { + Aspect(w / h); +} + +void Camera::Clip(float near, float far) { + near_clip = near; + far_clip = far; + UpdateProjection(); +} + + +void Camera::UpdateProjection() { + projection = glm::perspective(fov, aspect, near_clip, far_clip); + vp = projection * view; +} + +void Camera::UpdateView() { + view = glm::lookAt(position, target, up); + vp = projection * view; +} + +} diff --git a/src/camera.hpp b/src/camera.hpp new file mode 100644 index 0000000..5be71e2 --- /dev/null +++ b/src/camera.hpp @@ -0,0 +1,55 @@ +#ifndef BLANK_CAMERA_HPP_ +#define BLANK_CAMERA_HPP_ + +#include + + +namespace blank { + +class Camera { + +public: + Camera(); + ~Camera(); + + Camera(const Camera &) = delete; + Camera &operator =(const Camera &) = delete; + + glm::mat4 MakeMVP(const glm::mat4 &m) const { return vp * m; } + + void Viewport(int width, int height); + void Viewport(int x, int y, int width, int height); + + void FOV(float f); + void Aspect(float r); + void Aspect(float w, float h); + void Clip(float near, float far); + + void Position(glm::vec3 pos) { position = pos; UpdateView(); } + void Move(glm::vec3 delta) { position += delta; UpdateView(); } + + void LookAt(glm::vec3 tgt) { target = tgt; UpdateView(); } + +private: + void UpdateProjection(); + void UpdateView(); + +private: + float fov; + float aspect; + float near_clip; + float far_clip; + + glm::vec3 position; + glm::vec3 target; + glm::vec3 up; + + glm::mat4 projection; + glm::mat4 view; + glm::mat4 vp; + +}; + +} + +#endif -- 2.39.2