-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.h
100 lines (81 loc) · 2.26 KB
/
camera.h
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
#ifndef CAMERA_H
#define CAMERA_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
class Camera
{
private:
glm::vec3 cameraPosition;
glm::vec3 cameraDirection;
glm::vec3 worldUp;
float cameraSpeed;
float cameraSens;
float yaw = -90.0f;
float pitch = 0.0f;
public:
Camera(glm::vec3 startPosition, float cameraSpeed, float cameraSens = 0.1f)
{
this->cameraSpeed = cameraSpeed;
this->cameraSens = cameraSens;
cameraPosition = startPosition;
cameraDirection = glm::vec3(0.0f, 0.0f, -1.0f); // assuming camera starts off looking straight
worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
}
void moveForward(float deltaTime)
{
glm::vec3 forwardVector = glm::normalize(glm::vec3(cameraDirection.x, 0, cameraDirection.z));
cameraPosition += cameraSpeed * forwardVector * deltaTime;
}
void moveRight(float deltaTime)
{
glm::vec3 localRight = glm::normalize(glm::cross(cameraDirection, worldUp));
cameraPosition += localRight * cameraSpeed * deltaTime;
}
void moveLeft(float deltaTime)
{
glm::vec3 localRight = glm::normalize(glm::cross(cameraDirection, worldUp));
cameraPosition -= localRight * cameraSpeed * deltaTime;
}
void moveBackward(float deltaTime)
{
glm::vec3 forwardVector = glm::normalize(glm::vec3(cameraDirection.x, 0, cameraDirection.z));
cameraPosition -= cameraSpeed * forwardVector * deltaTime;
}
void moveUp(float deltaTime)
{
cameraPosition += cameraSpeed * worldUp * deltaTime;
}
void moveDown(float deltaTime)
{
cameraPosition -= cameraSpeed * worldUp * deltaTime;
}
void rotateMouseToAngles(float xoffset, float yoffset)
{
yaw += xoffset * cameraSens;
pitch += yoffset * cameraSens;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraDirection = glm::normalize(direction);
}
glm::mat4 getViewMatrix()
{
return glm::lookAt(cameraPosition, cameraPosition + cameraDirection, worldUp);
}
glm::vec3 getWorldPosition()
{
return cameraPosition;
}
void setSpeed(float speed)
{
this->cameraSpeed = speed;
}
};
#endif