-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
128 lines (103 loc) · 2.74 KB
/
Camera.cpp
File metadata and controls
128 lines (103 loc) · 2.74 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
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
#include <cstdio>
#define PI 3.1415926535
void Camera::init(int screenWidth, int screenHeight)
{
_screenWidth = screenWidth;
_screenHeight = screenHeight;
_fov = 70.0f;
_zNear = 0.2f;
_zFar = 150.0f;
_pitch = _yaw = _roll = 0;
_sensitivity = 0.002f;
_position = glm::vec3(0.0f, 0.0f, 0.0f);
_direction = glm::vec3(0.0f, 0.0f, -1.0f);
_up = glm::vec3(0.0f, 1.0f, 0.0f);
_projectionMatrix = createProjectionMatrix();
}
void Camera::update()
{
glm::vec3 direction;
direction.x = cos(glm::radians(_pitch)) * sin(glm::radians(_yaw));
direction.y = sin(glm::radians(_pitch));
direction.z = -(cos(glm::radians(_pitch)) * cos(glm::radians(_yaw)));
_direction = glm::normalize(direction);
glm::vec3 right;
right.x = -sin(glm::radians(_yaw) - PI/2.0f);
right.y = 0.0f;
right.z = cos(glm::radians(_yaw) - PI/2.0f);
_right = right;
_up = glm::normalize(glm::cross(_right, _direction));
}
void Camera::moveForward(float speed)
{
_position += speed * _direction;
}
void Camera::moveBackward(float speed)
{
_position -= speed * _direction;
}
void Camera::moveRight(float speed)
{
_position += speed * _right;
}
void Camera::moveLeft(float speed)
{
_position -= speed * _right;
}
void Camera::moveUp(float speed)
{
_position += speed * _up;
}
void Camera::moveDown(float speed)
{
_position -= speed * _up;
}
void Camera::rotateUp(float speed)
{
_pitch += speed;
}
void Camera::rotateDown(float speed)
{
_pitch -= speed;
}
void Camera::rotateLeft(float speed)
{
_yaw -= speed;
}
void Camera::rotateRight(float speed)
{
_yaw += speed;
}
glm::vec3 Camera::screenToWorldCoords(float mouseX, float mouseY)
{
printf("Screen coords: %f, %f\n", mouseX, mouseY);
glm::vec3 worldCoords;
glm::mat4 invertedView = glm::inverse(getViewMatrix());
glm::mat4 invertedProj = glm::inverse(_projectionMatrix);
glm::vec4 mouseCoords(mouseX, mouseY, 1, 1);
glm::vec4 result = mouseCoords * invertedProj * invertedView;
printf("New Coords: %f, %f\n", result.x, result.y);
return glm::vec3(result);
}
glm::vec2 Camera::screenToNDC()
{
return glm::vec2(((_mouseCoords.x * 2.0f)/ _screenWidth) - 1.0f,
(1.0f - (_mouseCoords.y * 2.0f)/ _screenHeight));
}
glm::mat4 Camera::createProjectionMatrix()
{
glm::mat4 projMatrix;
projMatrix = glm::perspective(glm::radians(_fov), (float)_screenWidth/(float)_screenHeight, _zNear, _zFar);
_projectionMatrix = projMatrix;
return projMatrix;
}
glm::mat4 Camera::getViewMatrix()
{
return glm::lookAt(_position, _position + _direction, _up);
}
glm::mat4 Camera::getProjectionMatrix()
{
return _projectionMatrix;
}