sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
camera_ip.cpp
1#include "camera.h"
2
3using namespace sgltk;
4
5IP_Camera::IP_Camera() :
6 Camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0)) {
7
8 update_projection_matrix();
9}
10
11IP_Camera::IP_Camera(const IP_Camera& camera) :
12 Camera(camera.position, camera.direction, camera.up) {
13
14 fovy = camera.fovy;
15 width = camera.width;
16 height = camera.height;
17 near_plane = camera.near_plane;
19}
20
21IP_Camera::IP_Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 up) :
23
25};
26
27IP_Camera::IP_Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 up, float fovy,
28 float width, float height, float near_plane) :
30
31 this->fovy = fovy;
32 this->width = width;
33 this->height = height;
34 this->near_plane = near_plane;
36}
37
38IP_Camera::~IP_Camera() {
39}
40
42
43 projection_matrix = glm::infinitePerspective(fovy,
44 (GLfloat)width / (GLfloat)height,
46}
47
48std::vector<glm::vec3> IP_Camera::calculate_frustum_points() {
49
50 std::vector<glm::vec4> ndc = {
51 glm::vec4(-1, -1, -1, 1),
52 glm::vec4( 1, -1, -1, 1),
53 glm::vec4( 1, 1, -1, 1),
54 glm::vec4(-1, 1, -1, 1),
55 };
56
57 glm::mat4 mat = glm::inverse(projection_matrix * view_matrix);
58
59 glm::vec4 tmp;
60 std::vector<glm::vec3> ret(ndc.size());
61 for(unsigned int i = 0; i < ndc.size(); i++) {
62 tmp = mat * ndc[i];
63 ret[i] = glm::vec3(tmp) / tmp[3];
64 }
65 return ret;
66}
67
68std::vector<float> IP_Camera::calculate_frustum_distance(glm::vec3 position) {
69
70 glm::vec3 cam_dir = glm::normalize(direction);
71
72 glm::vec3 near_center = position + near_plane * cam_dir;
73
74 std::vector<glm::vec3> fpoints = calculate_frustum_points();
75
76 glm::vec3 left_normal = glm::normalize(glm::cross(fpoints[3] - position,
77 fpoints[0] - fpoints[3]));
78 glm::vec3 right_normal = glm::normalize(glm::cross(fpoints[1] - fpoints[2],
79 fpoints[2] - position));
80 glm::vec3 top_normal = glm::normalize(glm::cross(fpoints[2] - fpoints[3],
81 fpoints[3] - position));
82 glm::vec3 bottom_normal = glm::normalize(glm::cross(fpoints[0] - position,
83 fpoints[1] - fpoints[0]));
84
85 std::vector<float> ret(5);
86 ret[0] = glm::dot(-cam_dir, position - near_center);
87 ret[1] = glm::dot(left_normal, position - fpoints[3]);
88 ret[2] = glm::dot(right_normal, position - fpoints[2]);
89 ret[3] = glm::dot(top_normal, position - fpoints[3]);
90 ret[4] = glm::dot(bottom_normal, position - fpoints[0]);
91
92 return ret;
93}
Manages cameras.
Definition camera.h:12
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_ip.cpp:48
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_ip.cpp:68
void update_projection_matrix()
Recalculates the projection matrix.
Definition camera_ip.cpp:41