5O_Camera::O_Camera() :
Camera(glm::vec3(0, 0, 0),
9 update_projection_matrix();
12O_Camera::O_Camera(
const O_Camera& camera) :
46O_Camera::~O_Camera() {
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)
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]);
80 glm::vec3 cam_dir = glm::normalize(
direction);
82 glm::vec3 far_center = point +
far_plane * cam_dir;
83 glm::vec3 near_center = point +
near_plane * cam_dir;
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]);
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);
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));
111 glm::mat4 lm = glm::lookAt(glm::vec3(0), forward,
up);
112 glm::mat4 lm_inv = glm::inverse(lm);
114 for(
unsigned int i = 0; i < fpoints.size(); i++) {
115 fpoints[i] = glm::vec3(lm * glm::vec4(fpoints[i], 1));
118 glm::vec3 min = fpoints[0];
119 glm::vec3 max = fpoints[0];
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];
131 width = abs(max[0] - min[0]);
132 height = abs(max[1] - min[1]);
134 position = glm::vec3(lm_inv * glm::vec4(0.5f * (min + max), 1));
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));
150 glm::mat4 lm = glm::lookAt(glm::vec3(0), forward,
up);
151 glm::mat4 lm_inv = glm::inverse(lm);
153 for(
unsigned int i = 0; i < fpoints.size(); i++) {
154 fpoints[i] = glm::vec3(lm * glm::vec4(fpoints[i], 1));
157 glm::vec3 min = fpoints[0];
158 glm::vec3 max = fpoints[0];
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];
170 width = abs(max[0] - min[0]);
171 height = abs(max[1] - min[1]);
173 position = glm::vec3(lm_inv * glm::vec4(0.5f * (min + max), 1));
float far_plane
The far plane of the camera frustum.
float height
Viewport height.
glm::vec3 right
The right vector of the camera.
float width
Viewport width.
glm::mat4 view_matrix
The view matrix.
glm::vec3 direction
The direction the camera is pointing.
glm::vec3 up
The up vector of the camera.
glm::vec3 position
The camera position.
Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 up)
float near_plane
The near plane of the camera frustum.
void update_view_matrix()
Recalculates the view matrix.
glm::mat4 projection_matrix
The perspective projection matrix.
void update_projection_matrix()
Recalculates the projection matrix.
std::vector< glm::vec3 > calculate_frustum_points()
Calculates the corner points of the camera's frustum.
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...
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...
Manages perspective cameras.
std::vector< glm::vec3 > calculate_frustum_points()
Calculates the corner points of the camera's frustum.