]> git.localhorst.tv Git - blank.git/commitdiff
extracted projection and viewport to camera class
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 12 Feb 2015 11:37:21 +0000 (12:37 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 12 Feb 2015 19:44:03 +0000 (20:44 +0100)
src/app.cpp
src/app.hpp
src/camera.cpp [new file with mode: 0644]
src/camera.hpp [new file with mode: 0644]

index d54e8df56d1d0e875da4e599cca3ae67ea4b67cb..de7a39ea48add859bcebda28a3f9fcfbe6efe00a 100644 (file)
@@ -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);
index 9ec5f1f9b7de81c710aa74c9ce60247a995f86ca..5148a7a411a30669425b376ee9b73394802510c8 100644 (file)
@@ -4,6 +4,7 @@
 #include <glm/glm.hpp>
 #include <glm/gtc/matrix_transform.hpp>
 
+#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 (file)
index 0000000..8309fe6
--- /dev/null
@@ -0,0 +1,68 @@
+#include "camera.hpp"
+
+#include <GL/glew.h>
+#include <glm/gtc/matrix_transform.hpp>
+
+
+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 (file)
index 0000000..5be71e2
--- /dev/null
@@ -0,0 +1,55 @@
+#ifndef BLANK_CAMERA_HPP_
+#define BLANK_CAMERA_HPP_
+
+#include <glm/glm.hpp>
+
+
+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