sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
camera_p.cpp
1#include "camera.h"
2
3using namespace sgltk;
4
5P_Camera::P_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
12P_Camera::P_Camera(const P_Camera& camera) :
13 Camera(camera.position, camera.direction, camera.up) {
14
15 fovy = camera.fovy;
16 width = camera.width;
17 height = camera.height;
18 near_plane = camera.near_plane;
19 far_plane = camera.far_plane;
21}
22
23P_Camera::P_Camera(glm::vec3 position,
24 glm::vec3 direction,
25 glm::vec3 up) :
27
29};
30
31P_Camera::P_Camera(glm::vec3 position,
32 glm::vec3 direction,
33 glm::vec3 up,
34 float fovy,
35 float width,
36 float height,
37 float near_plane,
38 float far_plane) :
40
41 this->fovy = fovy;
42 this->width = width;
43 this->height = height;
44 this->near_plane = near_plane;
45 this->far_plane = far_plane;
47}
48
49P_Camera::~P_Camera() {
50}
51
53
54 projection_matrix = glm::perspective(fovy,
55 (GLfloat)width / (GLfloat)height,
57}
58
59std::vector<glm::vec3> P_Camera::calculate_frustum_points() {
60
61 std::vector<glm::vec4> ndc = {
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 glm::vec4( 1, -1, 1, 1),
68 glm::vec4( 1, 1, 1, 1),
69 glm::vec4(-1, 1, 1, 1)
70 };
71
72 glm::mat4 mat = glm::inverse(projection_matrix * view_matrix);
73
74 glm::vec4 tmp;
75 std::vector<glm::vec3> ret(ndc.size());
76 for(unsigned int i = 0; i < ndc.size(); i++) {
77 tmp = mat * ndc[i];
78 ret[i] = glm::vec3(tmp) / tmp[3];
79 }
80 return ret;
81}
82
83std::vector<float> P_Camera::calculate_frustum_distance(glm::vec3 point) {
84
85 glm::vec3 cam_dir = glm::normalize(direction);
86
87 glm::vec3 far_center = point + far_plane * cam_dir;
88 glm::vec3 near_center = point + near_plane * cam_dir;
89
90 std::vector<glm::vec3> fpoints = calculate_frustum_points();
91
92 glm::vec3 left_normal = glm::normalize(glm::cross(fpoints[7] - fpoints[3],
93 fpoints[0] - fpoints[3]));
94 glm::vec3 right_normal = glm::normalize(glm::cross(fpoints[1] - fpoints[2],
95 fpoints[6] - fpoints[2]));
96 glm::vec3 top_normal = glm::normalize(glm::cross(fpoints[2] - fpoints[3],
97 fpoints[7] - fpoints[3]));
98 glm::vec3 bottom_normal = glm::normalize(glm::cross(fpoints[4] - fpoints[0],
99 fpoints[1] - fpoints[0]));
100
101 std::vector<float> ret(6);
102
103 ret[0] = glm::dot(cam_dir, position - far_center);
104 ret[1] = glm::dot(-cam_dir, position - near_center);
105 ret[2] = glm::dot(left_normal, position - fpoints[3]);
106 ret[3] = glm::dot(right_normal, position - fpoints[2]);
107 ret[4] = glm::dot(top_normal, position - fpoints[3]);
108 ret[5] = glm::dot(bottom_normal, position - fpoints[0]);
109
110 return ret;
111}
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
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
float fovy
The vertical field of view angle of the camera.
Definition camera.h:33
glm::mat4 projection_matrix
The perspective projection matrix.
Definition camera.h:21
std::vector< glm::vec3 > calculate_frustum_points()
Calculates the corner points of the camera's frustum.
Definition camera_p.cpp:59
void update_projection_matrix()
Recalculates the projection matrix.
Definition camera_p.cpp:52
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_p.cpp:83