-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
149 lines (128 loc) · 5.07 KB
/
Copy pathCamera.cpp
File metadata and controls
149 lines (128 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Camera.h"
#include<stdio.h>
Camera::Camera() {
reset();
}
Camera::~Camera() {
}
void Camera::reset() {
orientLookAt(glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
}
void Camera::setEyePoint(glm::vec3 eyePoint) {
orientLookAt(eyePoint, look_vector,up_vector);
}
//orientLookAt
// this function is basically the same as orientLookVec, but with a point instead of a vector.
// So, calculate the vector based on the point and eyepoint, and then call orientLookVec.
void Camera::orientLookAt(glm::vec3 eyePoint, glm::vec3 lookatPoint, glm::vec3 upVec) {
glm::vec3 lookVec = glm::normalize(lookatPoint - eyePoint);
orientLookVec(eyePoint, lookVec, upVec);
}
//orientLookVec
// this function sets the private member variables eye_point, look_vector, and up_vector.
// it then calculates the translation matrix, determines the basis vectors for camera world based
// on the provided look vector and up vectors, and then calculates the rotation matrix to rotate
// the world toward the lookVector supplied
void Camera::orientLookVec(glm::vec3 eyePoint, glm::vec3 lookVec, glm::vec3 upVec) {
eye_point = eyePoint;
look_vector = lookVec;
up_vector = upVec;
calc_translation_matrix(eye_point);
calc_basis_vectors(lookVec, upVec);
calc_rotation_matrix();
}
//calc_basis_vectors
// this function calculates the correct basis vectors for camera space, based on a look vector and up vector
void Camera::calc_basis_vectors(glm::vec3 lookVec, glm::vec3 upVec) {
//establish the basis vectors of the view space
w = glm::normalize(-1.0f * lookVec); //inverse look vector
u = glm::normalize(glm::cross(upVec, w));
v = glm::cross(w, u);
}
//calc_rotation_matrix
// this function assumes that the uvw basis vectors have already been calculated
// it updates the rotation matrix to rotate the camera to look in the right direction
void Camera::calc_rotation_matrix() {
//establish the rotation matrix
glm::vec3 xyz_array[3] = { x, y, z };
glm::vec3 uvw_array[3] = { u, v, w };
rotation_matrix = glm::mat4(1.0f);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3;j++) {
rotation_matrix[j][i] = glm::dot(uvw_array[i], xyz_array[j]);
}
}
}
//calc_translation_matrix
// this function calculates the translation matrix (move negatively in x,y,z by the vector v)
// to move the world away from the eye (E) of the camera.
// it's basically the same as 'translate', but is named more conveniently.
void Camera::calc_translation_matrix(glm::vec3 E) {
translation_matrix = glm::mat4(1.0f);
translation_matrix[3][0] = -E[0];
translation_matrix[3][1] = -E[1];
translation_matrix[3][2] = -E[2];
}
//getModelViewMatrix()
// returns the modelviewmatrix.
// weird thing happening - to make the same as GLM::lookAt, have to use transpose (why?)
glm::mat4 Camera::getModelViewMatrix(){
return rotation_matrix * translation_matrix;
}
//getScaleMatrix()
//NEAR_PLANE 0.01f
//#define FAR_PLANE 20.0f
//#define VIEW_ANGLE 60.0f
// caculates the scaling matrix to bring the camera pyramid in the correct dimensions
// (-1 to 1 for x and y, 0 to -1 for z)
glm::mat4 Camera::getScaleMatrix(){
//float f = farPlane;
scaling_matrix = glm::mat4(1.0f);
scaling_matrix[0][0] = 0.0866022f; //1.0f/(tan(glm::radians(viewAngle) / 2.0f) * farPlane
scaling_matrix[1][1] = 0.0866022f;//1.0f / (tan(glm::radians(viewAngle) / 2.0f) * ((float)screenWidth / (float)screenHeight) * farPlane);
scaling_matrix[2][2] = 0.05f; //1.0f / f;
return scaling_matrix;
}
glm::mat4 Camera::getInverseScaleMatrix(){
//float f = farPlane;
inverse_scaling_matrix = glm::mat4(1.0f);
inverse_scaling_matrix[0][0] = 11.5470380f;//tan(glm::radians(viewAngle) / 2.0f) * farPlane;
inverse_scaling_matrix[1][1] = 11.5470380f;//tan(glm::radians(viewAngle) / 2.0f) * ((float)screenWidth / (float)screenHeight) * farPlane;
inverse_scaling_matrix[2][2] = 20.0f;
inverse_scaling_matrix[3][3] = 1.0f;
return inverse_scaling_matrix;
}
glm::mat4 Camera::getInverseModelViewMatrix(){
glm::mat4 inverse_translation_matrix = glm::mat4(1.0);
inverse_translation_matrix[3][0] = eye_point[0];
inverse_translation_matrix[3][1] = eye_point[1];
inverse_translation_matrix[3][2] = eye_point[2];
glm::mat4 inverse_rotation_matrix = glm::transpose(rotation_matrix);
return inverse_translation_matrix * inverse_rotation_matrix;
}
glm::mat4 Camera::getInverseRotationMatrix(){
return glm::transpose(rotation_matrix);
}
glm::mat4 Camera::getInverseTranslationMatrix(){
glm::mat4 inverse_translation_matrix = glm::mat4(1.0);
inverse_translation_matrix[3][0] = eye_point[0];
inverse_translation_matrix[3][1] = eye_point[1];
inverse_translation_matrix[3][2] = eye_point[2];
return inverse_translation_matrix;
}
glm::vec3 Camera::getEyePoint(){
return eye_point;
}
glm::vec3 Camera::getLookVector(){
return look_vector;
}
//fixing for first run
glm::vec3 Camera::getUpVector(){
return up_vector;
}
int Camera::getScreenWidth(){
return screenWidth;
}
int Camera::getScreenHeight() {
return screenHeight;
}