This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchanCamera.cpp
76 lines (62 loc) · 2.32 KB
/
chanCamera.cpp
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
#include "chanCamera.h"
#include <iostream>
chanCamera::chanCamera(glm::vec3 position, glm::vec3 up) : MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM){
Position = position;
Orientation = glm::quat(0, 0, 0, -1);
RightAngle = 0.f;
UpAngle = 0.f;
updateCameraVectors();
}
chanCamera::chanCamera(float posX, float posY, float posZ) : MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM){
Position = glm::vec3(posX, posY, posZ);
Orientation = glm::quat(0, 0, 0, -1);
RightAngle = 0.f;
UpAngle = 0.f;
updateCameraVectors();
}
glm::mat4 chanCamera::GetViewMatrix(){
// You should know the camera moves reverse relative to user input
glm::quat reverseOrient = glm::conjugate(Orientation);
glm::mat4 rot = glm::mat4_cast(reverseOrient);
glm::mat4 translation = glm::translate(glm::mat4(1.0), -Position);
return rot * translation;
}
void chanCamera::ProcessKeyboard(Camera_Movement direction, float deltaTime){
float velocity = MovementSpeed * deltaTime;
glm::quat qF = Orientation * glm::quat(0, 0, 0, -1) * glm::conjugate(Orientation);
glm::vec3 Right = glm::normalize(glm::cross(Front, glm::vec3(0, 1, 0)));
lastPos = Position;
if (direction == Camera_Movement::FORWARD)
Position += Front * velocity;
if (direction == Camera_Movement::BACKWARD)
Position -= Front * velocity;
if (direction == Camera_Movement::LEFT)
Position -= Right * velocity;
if (direction == Camera_Movement::RIGHT)
Position += Right * velocity;
}
void chanCamera::RevertPos(bool isColliding){
if(isColliding) Position = lastPos;
}
void chanCamera::ProcessMouseMovement(float xoffset, float yoffset, bool constrainPitch){
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
RightAngle += xoffset;
UpAngle += yoffset;
updateCameraVectors();
}
void chanCamera::ProcessMouseScroll(float yoffset){
if (Zoom >= 1.f && Zoom <= 45.f) Zoom -= yoffset;
if (Zoom <= 1.f) Zoom = 1.f;
if (Zoom >= 45.f) Zoom = 45.f;
}
void chanCamera::updateCameraVectors(){
glm::quat aroundY = glm::angleAxis(glm::radians(-RightAngle), glm::vec3(0, 1, 0));// Yaw
glm::quat aroundX = glm::angleAxis(glm::radians(UpAngle), glm::vec3(1, 0, 0));// Pitch
Orientation = aroundY * aroundX;
updateFront();
}
void chanCamera::updateFront() {
glm::quat qF = Orientation * glm::quat(0, 0, 0, -1) * glm::conjugate(Orientation);
Front = { qF.x, qF.y, qF.z };
}