vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
camera.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <cmath>
3
4#include "VEngine/Camera.hpp"
5
6void ven::Camera::setOrthographicProjection(const float left, const float right, const float top, const float bottom, const float near, const float far)
7{
8 m_projectionMatrix = glm::mat4{1.0F};
9 m_projectionMatrix[0][0] = 2.F / (right - left);
10 m_projectionMatrix[1][1] = 2.F / (bottom - top);
11 m_projectionMatrix[2][2] = 1.F / (far - near);
12 m_projectionMatrix[3][0] = -(right + left) / (right - left);
13 m_projectionMatrix[3][1] = -(bottom + top) / (bottom - top);
14 m_projectionMatrix[3][2] = -near / (far - near);
15}
16
17void ven::Camera::setPerspectiveProjection(const float fovy, const float aspect, const float near, const float far)
18{
19 assert(glm::abs(aspect - std::numeric_limits<float>::epsilon()) > 0.0F);
20 const float tanHalfFovy = std::tan(fovy / 2.F);
21 m_projectionMatrix = glm::mat4{0.0F};
22 m_projectionMatrix[0][0] = 1.F / (aspect * tanHalfFovy);
23 m_projectionMatrix[1][1] = 1.F / (tanHalfFovy);
24 m_projectionMatrix[2][2] = far / (far - near);
25 m_projectionMatrix[2][3] = 1.F;
26 m_projectionMatrix[3][2] = -(far * near) / (far - near);
27}
28
29void ven::Camera::setViewDirection(const glm::vec3 position, const glm::vec3 direction, const glm::vec3 up)
30{
31 const glm::vec3 w{normalize(direction)};
32 const glm::vec3 u{normalize(cross(w, up))};
33 const glm::vec3 v{cross(w, u)};
34
35 m_viewMatrix = glm::mat4{1.F};
36 m_viewMatrix[0][0] = u.x;
37 m_viewMatrix[1][0] = u.y;
38 m_viewMatrix[2][0] = u.z;
39 m_viewMatrix[0][1] = v.x;
40 m_viewMatrix[1][1] = v.y;
41 m_viewMatrix[2][1] = v.z;
42 m_viewMatrix[0][2] = w.x;
43 m_viewMatrix[1][2] = w.y;
44 m_viewMatrix[2][2] = w.z;
45 m_viewMatrix[3][0] = -dot(u, position);
46 m_viewMatrix[3][1] = -dot(v, position);
47 m_viewMatrix[3][2] = -dot(w, position);
48
49 m_inverseViewMatrix = glm::mat4{1.F};
50 m_inverseViewMatrix[0][0] = u.x;
51 m_inverseViewMatrix[0][1] = u.y;
52 m_inverseViewMatrix[0][2] = u.z;
53 m_inverseViewMatrix[1][0] = v.x;
54 m_inverseViewMatrix[1][1] = v.y;
55 m_inverseViewMatrix[1][2] = v.z;
56 m_inverseViewMatrix[2][0] = w.x;
57 m_inverseViewMatrix[2][1] = w.y;
58 m_inverseViewMatrix[2][2] = w.z;
59 m_inverseViewMatrix[3][0] = position.x;
60 m_inverseViewMatrix[3][1] = position.y;
61 m_inverseViewMatrix[3][2] = position.z;
62}
63
64void ven::Camera::setViewYXZ(const glm::vec3 position, const glm::vec3 rotation)
65{
66 const float c3 = glm::cos(rotation.z);
67 const float s3 = glm::sin(rotation.z);
68 const float c2 = glm::cos(rotation.x);
69 const float s2 = glm::sin(rotation.x);
70 const float c1 = glm::cos(rotation.y);
71 const float s1 = glm::sin(rotation.y);
72 const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)};
73 const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)};
74 const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)};
75 m_viewMatrix = glm::mat4{1.F};
76 m_viewMatrix[0][0] = u.x;
77 m_viewMatrix[1][0] = u.y;
78 m_viewMatrix[2][0] = u.z;
79 m_viewMatrix[0][1] = v.x;
80 m_viewMatrix[1][1] = v.y;
81 m_viewMatrix[2][1] = v.z;
82 m_viewMatrix[0][2] = w.x;
83 m_viewMatrix[1][2] = w.y;
84 m_viewMatrix[2][2] = w.z;
85 m_viewMatrix[3][0] = -dot(u, position);
86 m_viewMatrix[3][1] = -dot(v, position);
87 m_viewMatrix[3][2] = -dot(w, position);
88
89 m_inverseViewMatrix = glm::mat4{1.F};
90 m_inverseViewMatrix[0][0] = u.x;
91 m_inverseViewMatrix[0][1] = u.y;
92 m_inverseViewMatrix[0][2] = u.z;
93 m_inverseViewMatrix[1][0] = v.x;
94 m_inverseViewMatrix[1][1] = v.y;
95 m_inverseViewMatrix[1][2] = v.z;
96 m_inverseViewMatrix[2][0] = w.x;
97 m_inverseViewMatrix[2][1] = w.y;
98 m_inverseViewMatrix[2][2] = w.z;
99 m_inverseViewMatrix[3][0] = position.x;
100 m_inverseViewMatrix[3][1] = position.y;
101 m_inverseViewMatrix[3][2] = position.z;
102}
This file contains the Camera class.
void setViewYXZ(glm::vec3 position, glm::vec3 rotation)
Definition camera.cpp:64
glm::mat4 m_projectionMatrix
Definition Camera.hpp:33
void setOrthographicProjection(float left, float right, float top, float bottom, float near, float far)
Definition camera.cpp:6
void setPerspectiveProjection(float fovy, float aspect, float near, float far)
Definition camera.cpp:17
void setViewDirection(glm::vec3 position, glm::vec3 direction, glm::vec3 up=glm::vec3{0.F, -1.F, 0.F})
Definition camera.cpp:29