sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
camera_o.cpp
1#include "camera.h"
2
3using namespace sgltk;
4
5O_Camera::O_Camera() : Camera(glm::vec3(0, 0, 0),
6 glm::vec3(0, 0, -1),
7 glm::vec3(0, 1, 0)) {
8
9 update_projection_matrix();
10}
11
12O_Camera::O_Camera(const O_Camera& camera) :
13 Camera(camera.position, camera.direction, camera.up) {
14
15 width = camera.width;
16 height = camera.height;
17 near_plane = camera.near_plane;
18 far_plane = camera.far_plane;
20}
21
22O_Camera::O_Camera(glm::vec3 position,
23 glm::vec3 direction,
24 glm::vec3 up) :
26
28};
29
30O_Camera::O_Camera(glm::vec3 position,
31 glm::vec3 direction,
32 glm::vec3 up,
33 float width,
34 float height,
35 float near_plane,
36 float far_plane) :
38
39 this->width = width;
40 this->height = height;
41 this->near_plane = near_plane;
42 this->far_plane = far_plane;
44}
45
46O_Camera::~O_Camera() {
47}
48
50
51 projection_matrix = glm::ortho(-width / 2, width / 2,
52 -height / 2, height / 2,
54}
55
56std::vector<glm::vec3> O_Camera::calculate_frustum_points() {
57
58 std::vector<glm::vec4> ndc = {
59 glm::vec4(-1, -1, -1, 1),
60 glm::vec4( 1, -1, -1, 1),
61 glm::vec4( 1, 1, -1, 1),
62 glm::vec4(-1, 1, -1, 1),
63 glm::vec4(-1, -1, 1, 1),
64 glm::vec4( 1, -1, 1, 1),
65 glm::vec4( 1, 1, 1, 1),
66 glm::vec4(-1, 1, 1, 1)
67 };
68
69 glm::mat4 mat = glm::inverse(projection_matrix * view_matrix);
70
71 std::vector<glm::vec3> ret(ndc.size());
72 for(unsigned int i = 0; i < ndc.size(); i++) {
73 ret[i] = glm::vec3(mat * ndc[i]);
74 }
75 return ret;
76}
77
78std::vector<float> O_Camera::calculate_frustum_distance(glm::vec3 point) {
79
80 glm::vec3 cam_dir = glm::normalize(direction);
81
82 glm::vec3 far_center = point + far_plane * cam_dir;
83 glm::vec3 near_center = point + near_plane * cam_dir;
84
85 std::vector<glm::vec3> fpoints = calculate_frustum_points();
86
87 glm::vec3 left_normal = glm::cross(fpoints[7] - fpoints[3], fpoints[0] - fpoints[3]);
88 glm::vec3 right_normal = glm::cross(fpoints[1] - fpoints[2], fpoints[6] - fpoints[2]);
89 glm::vec3 top_normal = glm::cross(fpoints[7] - fpoints[3], fpoints[2] - fpoints[3]);
90 glm::vec3 bottom_normal = glm::cross(fpoints[1] - fpoints[0], fpoints[4] - fpoints[0]);
91
92 std::vector<float> ret(6);
93 ret[0] = glm::dot(position - far_center, cam_dir);
94 ret[1] = glm::dot(position - near_center, -cam_dir);
95 ret[2] = glm::dot(position - fpoints[3], left_normal);
96 ret[3] = glm::dot(position - fpoints[2], right_normal);
97 ret[4] = glm::dot(position - fpoints[3], top_normal);
98 ret[5] = glm::dot(position - fpoints[0], bottom_normal);
99
100 return ret;
101}
102
103void O_Camera::calculate_bounding_frustum(O_Camera& camera, glm::vec3 direction, float offset) {
104
105 std::vector<glm::vec3> fpoints = camera.calculate_frustum_points();
106
107 glm::vec3 forward = glm::normalize(direction);
108 glm::vec3 right = normalize(glm::cross(forward, glm::vec3(0, 1, 0)));
109 glm::vec3 up = normalize(glm::cross(right, forward));
110
111 glm::mat4 lm = glm::lookAt(glm::vec3(0), forward, up);
112 glm::mat4 lm_inv = glm::inverse(lm);
113
114 for(unsigned int i = 0; i < fpoints.size(); i++) {
115 fpoints[i] = glm::vec3(lm * glm::vec4(fpoints[i], 1));
116 }
117
118 glm::vec3 min = fpoints[0];
119 glm::vec3 max = fpoints[0];
120
121 for(int i = 1; i < 8; i++) {
122 for(int j = 0; j < 3; j++) {
123 if(fpoints[i][j] > max[j])
124 max[j] = fpoints[i][j];
125 else if(fpoints[i][j] < min[j])
126 min[j] = fpoints[i][j];
127 }
128 }
129
130 far_plane = abs(max[2] - min[2]);
131 width = abs(max[0] - min[0]);
132 height = abs(max[1] - min[1]);
133
134 position = glm::vec3(lm_inv * glm::vec4(0.5f * (min + max), 1));
135
137 projection_matrix = glm::ortho(-0.5f * width - offset, 0.5f * width + offset,
138 -0.5f * height - offset, 0.5f * height + offset,
139 -0.5f * far_plane - offset, 0.5f * far_plane + offset);
140}
141
142void O_Camera::calculate_bounding_frustum(P_Camera& camera, glm::vec3 direction, float offset) {
143
144 std::vector<glm::vec3> fpoints = camera.calculate_frustum_points();
145
146 glm::vec3 forward = glm::normalize(direction);
147 glm::vec3 right = normalize(glm::cross(forward, glm::vec3(0, 1, 0)));
148 glm::vec3 up = normalize(glm::cross(right, forward));
149
150 glm::mat4 lm = glm::lookAt(glm::vec3(0), forward, up);
151 glm::mat4 lm_inv = glm::inverse(lm);
152
153 for(unsigned int i = 0; i < fpoints.size(); i++) {
154 fpoints[i] = glm::vec3(lm * glm::vec4(fpoints[i], 1));
155 }
156
157 glm::vec3 min = fpoints[0];
158 glm::vec3 max = fpoints[0];
159
160 for(int i = 1; i < 8; i++) {
161 for(int j = 0; j < 3; j++) {
162 if(fpoints[i][j] > max[j])
163 max[j] = fpoints[i][j];
164 else if(fpoints[i][j] < min[j])
165 min[j] = fpoints[i][j];
166 }
167 }
168
169 far_plane = abs(max[2] - min[2]);
170 width = abs(max[0] - min[0]);
171 height = abs(max[1] - min[1]);
172
173 position = glm::vec3(lm_inv * glm::vec4(0.5f * (min + max), 1));
174
176 projection_matrix = glm::ortho(-0.5f * width - offset, 0.5f * width + offset,
177 -0.5f * height - offset, 0.5f * height + offset,
178 -0.5f * far_plane - offset, 0.5f * far_plane + offset);
179}
Manages cameras.
Definition camera.h:12
float far_plane
The far plane of the camera frustum.
Definition camera.h:37
float height
Viewport height.
Definition camera.h:29
glm::vec3 right
The right vector of the camera.
Definition camera.h:57
float width
Viewport width.
Definition camera.h:25
glm::mat4 view_matrix
The view matrix.
Definition camera.h:17
glm::vec3 direction
The direction the camera is pointing.
Definition camera.h:49
glm::vec3 up
The up vector of the camera.
Definition camera.h:53
glm::vec3 position
The camera position.
Definition camera.h:45
Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 up)
Definition camera.cpp:5
float near_plane
The near plane of the camera frustum.
Definition camera.h:41
void update_view_matrix()
Recalculates the view matrix.
Definition camera.cpp:25
glm::mat4 projection_matrix
The perspective projection matrix.
Definition camera.h:21
void update_projection_matrix()
Recalculates the projection matrix.
Definition camera_o.cpp:49
std::vector< glm::vec3 > calculate_frustum_points()
Calculates the corner points of the camera's frustum.
Definition camera_o.cpp:56
void calculate_bounding_frustum(O_Camera &camera, glm::vec3 direction, float offset)
Sets the camera parameters in such a way as to make the frustum of this camera the bounding box of th...
Definition camera_o.cpp:103
std::vector< float > calculate_frustum_distance(glm::vec3 point)
Calculates the distances of a point to all planes of the camera's frustum. Positive values indicate t...
Definition camera_o.cpp:78
Manages perspective cameras.
Definition camera.h:127
std::vector< glm::vec3 > calculate_frustum_points()
Calculates the corner points of the camera's frustum.
Definition camera_p.cpp:59