-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
157 lines (126 loc) · 3.98 KB
/
camera.cpp
File metadata and controls
157 lines (126 loc) · 3.98 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
150
151
152
153
154
155
156
157
#include "framework.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
Camera::Camera(int width, int height, float fov, float nearPlane, float farPlane)
{
viewportWidth = width;
viewportHeight = height;
this->fov = fov;
this->nearPlane = nearPlane;
this->farPlane = farPlane;
/*this->left = left;
this->right = right;
this->bottom = bottom;
this->top = top;*/
position = glm::vec3(0.0f, 0.0f, 6.0f);
speed = 3.0f;
rotationSpeed = 0.005f;
forwardDirection = glm::vec3(0.0f, 0.0f, -1.0f);
rightDirection = glm::normalize(glm::cross(forwardDirection, worldUpDirection));
upDirection = glm::normalize(glm::cross(rightDirection, forwardDirection));
calculateProjMatrix();
calculateViewMatrix();
calculateRayDirections();
}
void Camera::onResize(int width, int height)
{
if (viewportWidth == width && viewportHeight == height)
return;
viewportWidth = width;
viewportHeight = height;
//float aspectRatio = (float)width / (float)height;
//bottom = -aspectRatio;
//top = aspectRatio;
calculateProjMatrix();
}
void Camera::onUpdate(int key, float deltaTime)
{
switch (key)
{
case GLFW_KEY_D:
position += rightDirection * speed * deltaTime;
break;
case GLFW_KEY_A:
position -= rightDirection * speed * deltaTime;
break;
case GLFW_KEY_SPACE:
position += upDirection * speed * deltaTime;
break;
case GLFW_KEY_LEFT_SHIFT:
position -= upDirection * speed * deltaTime;
break;
case GLFW_KEY_S:
position -= forwardDirection * speed * deltaTime;
break;
case GLFW_KEY_W:
position += forwardDirection * speed * deltaTime;
break;
}
calculateViewMatrix();
}
void Camera::onMouseUpdate(glm::vec2 offset, float deltaTime)
{
float pitchDelta = offset.y * rotationSpeed;
float yawDelta = offset.x * rotationSpeed;
glm::quat q = glm::normalize(glm::cross(glm::angleAxis(pitchDelta, rightDirection),
glm::angleAxis(-yawDelta, worldUpDirection)));
forwardDirection = glm::normalize(glm::rotate(q, forwardDirection));
rightDirection = glm::normalize(glm::cross(forwardDirection, worldUpDirection));
upDirection = glm::normalize(glm::cross(rightDirection, forwardDirection));
}
std::vector<glm::vec3>& Camera::getOrthographicRayOrigins()
{
return rayOrigins;
}
std::vector<glm::vec3>& Camera::getRayDirections()
{
return rayDirections;
}
void Camera::calculateRayDirections()
{
//rayOrigins.resize(viewportWidth * viewportHeight);
rayDirections.resize(viewportWidth * viewportHeight);
calculateViewMatrix();
for (int i = 0; i < viewportHeight; ++i)
{
for (int j = 0; j < viewportWidth; ++j)
{
glm::vec2 coord{
(float)j / viewportWidth,
(float)i / viewportHeight
};
coord = coord * 2.0f - 1.0f;
//glm::vec4 origin = inverseProjMatrix * glm::vec4(coord.x, coord.y, -2.0f, 1.0f);
//glm::vec3 rayOrigin = glm::vec3(inverseViewMatrix * origin);//glm::vec4(glm::normalize(glm::vec3(origin) / origin.w), 1));
glm::vec4 target = inverseProjMatrix * glm::vec4(coord.x, coord.y, 1.0f, 1.0f);
glm::vec3 rayDirection = glm::vec3(inverseViewMatrix * glm::vec4(glm::normalize(glm::vec3(target) / target.w), 0));
//rayDirection = glm::normalize(rayDirection);
/*rayOrigins[i * viewportWidth + j] = rayOrigin;*/
rayDirections[i * viewportWidth + j] = rayDirection;
}
}
}
void Camera::calculateProjMatrix()
{
/*projMatrix = glm::ortho(left, right, bottom, top);*/
projMatrix = glm::perspective(fov, (float)viewportWidth / (float)viewportHeight, nearPlane, farPlane);
inverseProjMatrix = glm::inverse(projMatrix);
}
void Camera::calculateViewMatrix()
{
/*viewMatrix = glm::mat4(
glm::vec4(rightDirection, 0.0f),
glm::vec4(upDirection, 0.0f),
glm::vec4(forwardDirection, 0.0f),
glm::vec4(position, 1.0)
);*/
viewMatrix = glm::lookAt(position, position + forwardDirection, worldUpDirection);
inverseViewMatrix = glm::inverse(viewMatrix);
/*inverseViewMatrix = glm::mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
position.x, position.y, position.z, 1.0f
);*/
}