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,
};
}
, ctx(window.CreateContext())
, init_glew()
, program()
+, cam()
, vtx_buf(0)
-, mvp()
, mvp_handle(0)
, running(false) {
GLContext::EnableVSync();
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);
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;
}
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);
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
+#include "camera.hpp"
#include "init.hpp"
#include "shader.hpp"
InitGLEW init_glew;
Program program;
+ Camera cam;
+
GLuint vtx_buf;
- glm::mat4 mvp;
GLuint mvp_handle;
bool running;
--- /dev/null
+#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;
+}
+
+}
--- /dev/null
+#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