-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyingCamera.cpp
More file actions
134 lines (115 loc) · 3.18 KB
/
Copy pathFlyingCamera.cpp
File metadata and controls
134 lines (115 loc) · 3.18 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
129
130
131
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include "FlyingCamera.h"
#include <GL/glut.h>
//// MÉTODOS CLASE FLYINGCAMERA ////
//* Constructores *//
FlyingCamera::FlyingCamera() {
Camera();
this->mouseX = 0;
this->mouseY = 0;
this->state = false;
}
FlyingCamera::FlyingCamera(int id, const Vector3D& pos, const Vector3D& orientation) {
Camera(id, pos, orientation);
}
FlyingCamera::FlyingCamera(int id, float& x, float& y, float& z) {
Camera(id, x, y, z);
this->mouseX = 0;
this->mouseY = 0;
this->state = false;
}
//* Getters *//
inline float FlyingCamera::getMouseX() const {
return this->mouseX;
}
inline float FlyingCamera::getMouseY() const {
return this->mouseY;
}
char FlyingCamera::getState() const {
return this->state;
}
//* Setters *//
inline void FlyingCamera::setMouseY(const int& mouseY) {
this->mouseY = mouseY;
}
inline void FlyingCamera::setMouseX(const int& mouseX) {
this->mouseX = mouseX;
}
void FlyingCamera::setState(const char c) {
this->state = c;
}
//* Movimiento *//
void FlyingCamera::processMouseMovement(const int& x, const int& y)
{
if (this->state == 'a') {
this->setOrientation(this->getOrientation() + Vector3D(0.0, x - this->getMouseX(), 0.0));
this->setMouseX(x);
this->setMouseY(y);
}
}
void FlyingCamera::processMouseClick(const int& button, const int& state, const int& x, const int& y)
{
if (this->state == 'a') {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
this->setMouseX(x);
this->setMouseY(y);
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
this->setMouseX(0);
this->setMouseY(0);
}
}
}
void FlyingCamera::processKeyPressed(unsigned char key, int px, int py)
{
if (this->state == 'a') {
switch (key) {
case 'w':
case 'W':
this->update(0.1, Vector3D(1.0, 0.0, 0.0));
break;
case 's':
case 'S':
this->update(-0.1, Vector3D(1.0, 0.0, 0.0));
break;
case 'd':
case 'D':
this->update(-0.1, Vector3D(0.0, 0.0, 1.0));
break;
case 'a':
case 'A':
this->update(0.1, Vector3D(0.0, 0.0, 1.0));
break;
}
}
}
//* Otros *//
//Renderizado
void FlyingCamera::render() {
glRotatef(getOrientation().getX(), 1.0, 0.0, 0.0);
glRotatef(getOrientation().getY(), 0.0, 1.0, 0.0);
glRotatef(getOrientation().getZ(), 0.0, 0.0, 1.0);
glTranslatef(-this->getPosition().getX(), -this->getPosition().getY(), -this->getPosition().getZ());
}
//Actualización
void FlyingCamera::update(const float& time, const Vector3D& direction)
{
//Cálculo de los nuevos ejes en función de la orientación
float yOrientationInRadians = this->getOrientation().getY() * M_PI / 180;
float xOrientationInRadians = this->getOrientation().getX() * M_PI / 180;
float xComponent = -cos(xOrientationInRadians) * sin(yOrientationInRadians);
float yComponent = sin(xOrientationInRadians);
float zComponent = cos(xOrientationInRadians) * cos(yOrientationInRadians);
Vector3D velocity(xComponent, yComponent, zComponent);
//Si se mueve en el eje X
if (direction.getX() == 1.0) {
this->setPosition(this->getPosition() - velocity * time);
}
//Si se mueve en otro eje (Z)
else {
velocity = Vector3D(zComponent, yComponent, -xComponent);
this->setPosition(this->getPosition() - velocity * time);
}
}