|
@@ -16,6 +16,7 @@
|
16
|
16
|
|
17
|
17
|
#include <glm/gtc/epsilon.hpp>
|
18
|
18
|
#include <glm/gtc/matrix_transform.hpp>
|
|
19
|
+#include <glm/gtc/quaternion.hpp>
|
19
|
20
|
#include <glm/gtx/quaternion.hpp>
|
20
|
21
|
|
21
|
22
|
static bool equal(float a, float b) {
|
|
@@ -36,7 +37,7 @@ const static float fov = 45.0f;
|
36
|
37
|
const static float nearDist = 0.1f;
|
37
|
38
|
const static float farDist = 75000.0f;
|
38
|
39
|
const static float maxSpeed = 2048.0f;
|
39
|
|
-const static float controllerViewFactor = 384.0f;
|
|
40
|
+const static float controllerViewFactor = glm::pi<float>();
|
40
|
41
|
const static float controllerDeadZone = 0.1f;
|
41
|
42
|
|
42
|
43
|
const static glm::vec3 rightUnit(1.0f, 0.0f, 0.0f);
|
|
@@ -44,7 +45,7 @@ const static glm::vec3 upUnit(0.0f, 1.0f, 0.0f);
|
44
|
45
|
const static glm::vec3 dirUnit(0.0f, 0.0f, -1.0f);
|
45
|
46
|
|
46
|
47
|
glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
|
47
|
|
-glm::quat Camera::quaternion(glm::vec3(0.0f, 0.0f, 0.0f));
|
|
48
|
+glm::vec2 Camera::rot(glm::pi<float>(), 0.0f);
|
48
|
49
|
glm::vec3 Camera::posSpeed(0.0f, 0.0f, 0.0f);
|
49
|
50
|
glm::vec2 Camera::rotSpeed(0.0f, 0.0f);
|
50
|
51
|
glm::mat4 Camera::projection(1.0f);
|
|
@@ -56,7 +57,7 @@ bool Camera::dirty = true;
|
56
|
57
|
|
57
|
58
|
void Camera::reset() {
|
58
|
59
|
pos = glm::vec3(0.0f, 0.0f, 0.0f);
|
59
|
|
- quaternion = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
|
|
60
|
+ rot = glm::vec2(glm::pi<float>(), 0.0f);
|
60
|
61
|
posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
|
61
|
62
|
rotSpeed = glm::vec2(0.0f, 0.0f);
|
62
|
63
|
dirty = true;
|
|
@@ -96,23 +97,38 @@ void Camera::handleAction(ActionEvents action, bool isFinished) {
|
96
|
97
|
}
|
97
|
98
|
|
98
|
99
|
void Camera::handleMouseMotion(int x, int y) {
|
99
|
|
- if (x != 0) {
|
100
|
|
- quaternion = glm::quat(upUnit * (rotationDeltaX * x)) * quaternion;
|
|
100
|
+ if ((x != 0) || (y != 0))
|
101
|
101
|
dirty = true;
|
|
102
|
+
|
|
103
|
+ while (x > 0) {
|
|
104
|
+ rot.x += rotationDeltaX;
|
|
105
|
+ x--;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ while (x < 0) {
|
|
109
|
+ rot.x -= rotationDeltaX;
|
|
110
|
+ x++;
|
102
|
111
|
}
|
103
|
112
|
|
104
|
|
- if (y != 0) {
|
105
|
|
- static int lastDir = 0;
|
106
|
|
- float a = glm::dot(upUnit, quaternion * upUnit);
|
107
|
|
- if (((lastDir >= 0) && (y < 0)) || ((lastDir <= 0) && (y > 0)) || (a > 0.5f)) {
|
108
|
|
- quaternion = glm::quat(quaternion * -rightUnit * (rotationDeltaY * y)) * quaternion;
|
109
|
|
- dirty = true;
|
|
113
|
+ while (y > 0) {
|
|
114
|
+ if (rot.y > -(glm::pi<float>() / 2.0f)) {
|
|
115
|
+ rot.y -= rotationDeltaY;
|
|
116
|
+ }
|
|
117
|
+ y--;
|
|
118
|
+ }
|
110
|
119
|
|
111
|
|
- // TODO find better way to clamp Y rotation axis!
|
112
|
|
- if (a > 0.5f)
|
113
|
|
- lastDir = y;
|
|
120
|
+ while (y < 0) {
|
|
121
|
+ if (rot.y < (glm::pi<float>() / 2.0f)) {
|
|
122
|
+ rot.y += rotationDeltaY;
|
114
|
123
|
}
|
|
124
|
+ y++;
|
115
|
125
|
}
|
|
126
|
+
|
|
127
|
+ while (rot.x > (glm::pi<float>() * 2.0f))
|
|
128
|
+ rot.x -= glm::pi<float>() * 2.0f;
|
|
129
|
+
|
|
130
|
+ while (rot.x < -(glm::pi<float>() * 2.0f))
|
|
131
|
+ rot.x += glm::pi<float>() * 2.0f;
|
116
|
132
|
}
|
117
|
133
|
|
118
|
134
|
void Camera::handleControllerAxis(float value, KeyboardButton axis) {
|
|
@@ -128,7 +144,7 @@ void Camera::handleControllerAxis(float value, KeyboardButton axis) {
|
128
|
144
|
} else if (axis == rightXAxis) {
|
129
|
145
|
rotSpeed.x = controllerViewFactor * value;
|
130
|
146
|
} else if (axis == rightYAxis) {
|
131
|
|
- rotSpeed.y = controllerViewFactor * value;
|
|
147
|
+ rotSpeed.y = -controllerViewFactor * value;
|
132
|
148
|
} else {
|
133
|
149
|
return;
|
134
|
150
|
}
|
|
@@ -161,13 +177,13 @@ bool Camera::update() {
|
161
|
177
|
return false;
|
162
|
178
|
|
163
|
179
|
float dT = RunTime::getLastFrameTime();
|
164
|
|
- pos += quaternion * posSpeed * dT;
|
|
180
|
+ rot += rotSpeed * dT;
|
165
|
181
|
|
166
|
|
- if (glm::epsilonNotEqual(rotSpeed.x, 0.0f, controllerDeadZone))
|
167
|
|
- quaternion = glm::quat(upUnit * rotationDeltaX * rotSpeed.x * dT) * quaternion;
|
|
182
|
+ glm::quat quatY = glm::angleAxis(rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
|
|
183
|
+ glm::quat quatX = glm::angleAxis(rot.y, glm::vec3(1.0f, 0.0f, 0.0f));
|
|
184
|
+ glm::quat quaternion = quatY * quatX;
|
168
|
185
|
|
169
|
|
- if (glm::epsilonNotEqual(rotSpeed.y, 0.0f, controllerDeadZone))
|
170
|
|
- quaternion = glm::quat(quaternion * -rightUnit * rotationDeltaY * rotSpeed.y * dT) * quaternion;
|
|
186
|
+ pos += quaternion * posSpeed * dT;
|
171
|
187
|
|
172
|
188
|
glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
|
173
|
189
|
glm::mat4 rotate = glm::toMat4(quaternion);
|
|
@@ -180,12 +196,6 @@ bool Camera::update() {
|
180
|
196
|
return updateViewFrustum;
|
181
|
197
|
}
|
182
|
198
|
|
183
|
|
-glm::vec2 Camera::getRotation() {
|
184
|
|
- float x = glm::dot(dirUnit, quaternion * dirUnit);
|
185
|
|
- float y = glm::dot(upUnit, quaternion * upUnit);
|
186
|
|
- return glm::vec2(x, y);
|
187
|
|
-}
|
188
|
|
-
|
189
|
199
|
// ----------------------------------------------------------------------------
|
190
|
200
|
|
191
|
201
|
class FrustumPlane {
|