-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
96 lines (85 loc) · 2.85 KB
/
Camera.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Camera.cpp
*
* Created on: Jan 18, 2015
* Author: alxndr
*/
#pragma once
#include "Camera.hpp"
Camera::Camera( glm::vec3 position,glm::vec3 up,GLfloat yaw, GLfloat pitch):
Direction(glm::vec3(0.0f, 0.0f, -1.0f)),
MovementSpeed(5.0f), MouseSensitivity(1.6f), Zoom(45.0f)
{
this->Position = position;
this->WorldUp = up;
this->Yaw = yaw;
this->Pitch = pitch;
this->updateCameraVectors();
}
Camera::Camera( GLfloat posX, GLfloat posY, GLfloat posZ,
GLfloat upX, GLfloat upY, GLfloat upZ,
GLfloat yaw, GLfloat pitch):
Direction(glm::vec3(0.0f, 0.0f, -1.0f)),
MovementSpeed(5.0f), MouseSensitivity(1.6f), Zoom(45.0f)
{
this->Position = glm::vec3(posX, posY, posZ);
this->WorldUp = glm::vec3(upX, upY, upZ);
this->Yaw = yaw;
this->Pitch = pitch;
this->updateCameraVectors();
}
glm::mat4 Camera::GetViewMatrix()
{
return glm::lookAt(this->Position, this->Position + this->Direction, this->WorldUp);
}
void Camera::ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime)
{
GLfloat velocity = this->MovementSpeed * deltaTime;
if(direction == FORWARD)
this->Position += this->Direction * velocity;
if(direction == BACKWARD)
this->Position -= this->Direction * velocity;
if(direction == LEFT)
this->Position -= this->Right * velocity;
if(direction == RIGHT)
this->Position += this->Right * velocity;
if(direction == UP)
this->Position += this->WorldUp * velocity;
if(direction == DOWN)
this->Position -= this->WorldUp * velocity;
this->updateCameraVectors();
}
void Camera::ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset, GLfloat deltaTime, GLboolean constrainPitch)
{
xoffset *= this->MouseSensitivity;
yoffset *= this->MouseSensitivity;
this->Yaw += xoffset*deltaTime;
this->Pitch -= yoffset*deltaTime;
if(constrainPitch)
{
if(this->Pitch > 89.0f)
this->Pitch = 89.0f;
if(this->Pitch < -89.0f)
this->Pitch = -89.0f;
}
this->updateCameraVectors();
}
void Camera::ProcessMouseScroll(GLfloat yoffset)
{
if(this->Zoom >= 1.0f && this->Zoom <= 45.0f)
this->Zoom -= yoffset;
if(this->Zoom <= 1.0f)
this->Zoom = 1.0f;
if(this->Zoom >= 45.0f)
this->Zoom = 45.0f;
}
void Camera::updateCameraVectors()
{
glm::vec3 front;
front.x = cos(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch));
front.y = sin(glm::radians(this->Pitch));
front.z = sin(glm::radians(this->Yaw)) * cos(glm::radians(this->Pitch));
this->Direction = glm::normalize(front);
this->Right = glm::normalize(glm::cross(this->Direction, this->WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
this->Up = glm::normalize(glm::cross(this->Right, this->Direction));
}