-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
81 lines (70 loc) · 2.06 KB
/
Copy pathcamera.cpp
File metadata and controls
81 lines (70 loc) · 2.06 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
#include "camera.h"
#include <cmath>
#include "window.h"
#define PI 3.1415926f
float deg2rad(float rad) {
return PI * rad / 180.f;
}
Camera::Camera(Vec3f pos, Vec3f front, Vec3f up, float yaw, float pitch)
{
this->position = pos;
this->front = front.normalize();
this->worldUp = up.normalize();
this->right = cross(this->worldUp, -1.f*this->front);
this->up = cross(-1.f*this->front, this->right);
this->yaw = yaw;
this->pitch = pitch;
updateVector();
}
void Camera::updateVector() {
float x = std::cos(deg2rad(pitch)) * std::sin(deg2rad(yaw));
float y = std::sin(deg2rad(pitch));
float z = std::cos(deg2rad(pitch)) * std::cos(deg2rad(yaw));
// std::cout << yaw << ", " << pitch << std::endl;
// std::cout << x << ", " << y << ", " << z << std::endl;
this->front = Vec3f(x, y, z).normalize();
this->right = cross(worldUp, -1.f*front).normalize();
this->up = cross(-1.f*front, right).normalize();
}
void Camera::setPos(Vec3f &pos) {
position = pos;
}
void Camera::setFrontVector(Vec3f &front_vec) {
front = front_vec;
}
void Camera::processInput() {
if (Window::screenKeys[VK_LEFT]) {
position = position - .2f * right;
}
if (Window::screenKeys[VK_RIGHT]) {
position = position + .2f * right;
}
if (Window::screenKeys[VK_UP]) {
position = position + .2f * front;
}
if (Window::screenKeys[VK_DOWN]) {
position = position - .2f * front;
}
if (Window::screenKeys['A']) {
// std::cout << "A" << std::endl;
yaw = yaw + 5.f;
}
if (Window::screenKeys['D']) {
yaw = yaw - 5.f;
}
if (Window::screenKeys['W']) {
pitch = pitch + 5.f;
}
if (Window::screenKeys['S']) {
pitch = pitch - 5.f;
}
if (pitch > 89.f) {
pitch = 89.f;
}
if (pitch < -89.f) {
pitch = -89.f;
}
updateVector();
// std::cout << front.x << ", " << front.y << ", " << front.z << std::endl;
// std::cout << position.x << ", " << position.y << ", " << position.z << std::endl;
}