-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.hpp
95 lines (62 loc) · 2.74 KB
/
camera.hpp
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
#ifndef CAMERA_H
#define CAMERA_H
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/glm.hpp>
#include "math.hpp"
#include "window.h"
#include "Terrain.hpp"
class Camera {
public:
GLFWwindow* window;
// Used to Calcualte View Matrix
glm::vec3 position = glm::vec3(0.0f, 4.0f, -2.0f); // Camera position
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); // Camera Vector that Point to The From of the Camera
glm::vec3 cameraUP = glm::vec3(0.0f, 1.0f, 0.0f); // Camera Vector that Point to Up of The Camera
// Used to Calculate Camera Angle
float yaw = -90.0f; // How Left or Right is Camera
float pitch = 0.0f;; // How High or Low Camera is Aimed
float roll; // How Camera is Titlted on Some side
const float cameraSpeed = 2.0f;
Camera(GLFWwindow* window) {
this->window = window;
}
// Move Camera Round
void move(Terrain terrain) {
float cameraPosX = this->position.x;
float cameraPosZ = this->position.z;
// Find Terrain Height on Current Camera Position
float cameraY = terrain.getHeightOfTerrain(cameraPosX, cameraPosZ, terrain.heights) + 5.0f;
// Test Camera Terrain Coallision
if(this->position.y <= cameraY) {
this->position.y = cameraY;
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
this->position += this->cameraFront * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
/*
Cross Product daje Vektor koji je prav u odnosu na Dva Vektora koja se Mnoze.
Tako dobijamo X koja se stalno menja u zavisnosti od pozicije CemeraFrom i CameraUp.
*/
this->position -= glm::normalize(glm::cross(this->cameraFront, this->cameraUP)) * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
/*
Cross Product daje Vektor koji je prav u odnosu na Dva Vektora koja se Mnoze.
Tako dobijamo X koja se stalno menja u zavisnosti od pozicije CemeraFrom i CameraUp.
*/
this->position += glm::normalize(glm::cross(this->cameraFront, this->cameraUP)) * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
this->position -= cameraSpeed * this->cameraFront;
}
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
this->position += cameraSpeed * this->cameraUP;
}
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
this->position -= cameraSpeed * this->cameraUP;
}
}
};
#endif // CAMERA_H