Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Camera.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*!
  2. * \file src/Camera.cpp
  3. * \brief Camera class
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <limits>
  9. #include <glm/gtc/epsilon.hpp>
  10. #include <glm/gtc/matrix_transform.hpp>
  11. #include <glm/gtx/quaternion.hpp>
  12. #include "global.h"
  13. #include "RunTime.h"
  14. #include "system/Window.h"
  15. #include "Camera.h"
  16. static bool equal(float a, float b) {
  17. return glm::epsilonEqual(a, b, std::numeric_limits<float>::epsilon());
  18. }
  19. static bool equal(glm::vec2 a, float b) {
  20. return equal(a.x, b) && equal(a.y, b);
  21. }
  22. static bool equal(glm::vec3 a, float b) {
  23. return equal(a.x, b) && equal(a.y, b) && equal(a.z, b);
  24. }
  25. // ----------------------------------------------------------------------------
  26. const static float fov = 45.0f;
  27. const static float nearDist = 0.1f;
  28. const static float farDist = 75000.0f;
  29. const static float maxSpeed = 2048.0f;
  30. const static float controllerViewFactor = 384.0f;
  31. const static float controllerDeadZone = 0.1f;
  32. const static glm::vec3 rightUnit(1.0f, 0.0f, 0.0f);
  33. const static glm::vec3 upUnit(0.0f, 1.0f, 0.0f);
  34. const static glm::vec3 dirUnit(0.0f, 0.0f, -1.0f);
  35. glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
  36. glm::quat Camera::quaternion(glm::vec3(0.0f, 0.0f, 0.0f));
  37. glm::vec3 Camera::posSpeed(0.0f, 0.0f, 0.0f);
  38. glm::vec2 Camera::rotSpeed(0.0f, 0.0f);
  39. glm::vec2 Camera::lastSize(0.0f, 0.0f);
  40. glm::mat4 Camera::projection(1.0f);
  41. glm::mat4 Camera::view(1.0f);
  42. float Camera::rotationDeltaX = 0.75f;
  43. float Camera::rotationDeltaY = 0.75f;
  44. bool Camera::updateViewFrustum = true;
  45. bool Camera::dirty = true;
  46. void Camera::reset() {
  47. pos = glm::vec3(0.0f, 0.0f, 0.0f);
  48. quaternion = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
  49. posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
  50. rotSpeed = glm::vec2(0.0f, 0.0f);
  51. dirty = true;
  52. lastSize = glm::vec2(0.0f, 0.0f);
  53. projection = glm::mat4(1.0f);
  54. view = glm::mat4(1.0f);
  55. }
  56. void Camera::handleAction(ActionEvents action, bool isFinished) {
  57. float factor = 1.0f;
  58. if (isFinished)
  59. factor = -1.0f;
  60. if (action == forwardAction) {
  61. posSpeed += dirUnit * maxSpeed * factor;
  62. } else if (action == backwardAction) {
  63. posSpeed -= dirUnit * maxSpeed * factor;
  64. } else if (action == leftAction) {
  65. posSpeed += rightUnit * maxSpeed * factor;
  66. } else if (action == rightAction) {
  67. posSpeed -= rightUnit * maxSpeed * factor;
  68. } else if (action == jumpAction) {
  69. posSpeed += upUnit * maxSpeed * factor;
  70. } else if (action == crouchAction) {
  71. posSpeed -= upUnit * maxSpeed * factor;
  72. } else {
  73. return;
  74. }
  75. dirty = true;
  76. }
  77. void Camera::handleMouseMotion(int x, int y) {
  78. if (x != 0) {
  79. quaternion = glm::quat(upUnit * (rotationDeltaX * x)) * quaternion;
  80. dirty = true;
  81. }
  82. static int lastDir = 0;
  83. if (y != 0) {
  84. float a = glm::dot(upUnit, quaternion * upUnit);
  85. if (((lastDir >= 0) && (y < 0)) || ((lastDir <= 0) && (y > 0)) || (a > 0.5f)) {
  86. quaternion = glm::quat(quaternion * -rightUnit * (rotationDeltaY * y)) * quaternion;
  87. dirty = true;
  88. // TODO find better way to clamp Y rotation axis!
  89. if (a > 0.5f)
  90. lastDir = y;
  91. }
  92. }
  93. }
  94. void Camera::handleControllerAxis(float value, KeyboardButton axis) {
  95. if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
  96. value = 0.0f;
  97. // TODO clamp Y rotation axis somehow...?
  98. if (axis == leftXAxis) {
  99. posSpeed.x = -maxSpeed * value;
  100. } else if (axis == leftYAxis) {
  101. posSpeed.z = maxSpeed * value;
  102. } else if (axis == rightXAxis) {
  103. rotSpeed.x = controllerViewFactor * value;
  104. } else if (axis == rightYAxis) {
  105. rotSpeed.y = controllerViewFactor * value;
  106. } else {
  107. return;
  108. }
  109. dirty = true;
  110. }
  111. void Camera::handleControllerButton(KeyboardButton button, bool released) {
  112. if (button == aButton) {
  113. handleAction(jumpAction, released);
  114. } else if (button == bButton) {
  115. handleAction(crouchAction, released);
  116. } else if (button == padUp) {
  117. handleAction(forwardAction, released);
  118. } else if (button == padDown) {
  119. handleAction(backwardAction, released);
  120. } else if (button == padLeft) {
  121. handleAction(leftAction, released);
  122. } else if (button == padRight) {
  123. handleAction(rightAction, released);
  124. } else {
  125. return;
  126. }
  127. dirty = true;
  128. }
  129. bool Camera::update() {
  130. glm::vec2 size(Window::getSize());
  131. if (lastSize != size) {
  132. //! \fixme TODO instead of mirroring the Y axis in the shader, scale with -1 here
  133. projection = glm::perspective(fov, size.x / size.y, nearDist, farDist);
  134. lastSize = size;
  135. }
  136. if ((!dirty) && equal(posSpeed, 0.0f) && equal(rotSpeed, 0.0f))
  137. return false;
  138. float dT = getRunTime().getLastFrameTime();
  139. pos += quaternion * posSpeed * dT;
  140. if (glm::epsilonNotEqual(rotSpeed.x, 0.0f, controllerDeadZone))
  141. quaternion = glm::quat(upUnit * rotationDeltaX * rotSpeed.x * dT) * quaternion;
  142. if (glm::epsilonNotEqual(rotSpeed.y, 0.0f, controllerDeadZone))
  143. quaternion = glm::quat(quaternion * -rightUnit * rotationDeltaY * rotSpeed.y * dT) * quaternion;
  144. glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
  145. glm::mat4 rotate = glm::toMat4(quaternion);
  146. view = glm::inverse(translate * rotate);
  147. if (updateViewFrustum)
  148. calculateFrustumPlanes();
  149. dirty = false;
  150. return updateViewFrustum;
  151. }
  152. glm::vec2 Camera::getRotation() {
  153. float x = glm::dot(dirUnit, quaternion * dirUnit);
  154. float y = glm::dot(upUnit, quaternion * upUnit);
  155. return glm::vec2(x, y);
  156. }
  157. // ----------------------------------------------------------------------------
  158. class FrustumPlane {
  159. public:
  160. FrustumPlane() : normal(glm::vec3(0.0f, 0.0f, 0.0f)), d(0.0f) { }
  161. void set(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
  162. normal = glm::normalize(glm::cross(v3 - v2, v1 - v2));
  163. d = -glm::dot(normal, v2);
  164. }
  165. float distance(glm::vec3 p) {
  166. return d + glm::dot(normal, p);
  167. }
  168. private:
  169. glm::vec3 normal;
  170. float d;
  171. };
  172. // ----------------------------------------------------------------------------
  173. #define NEAR 0
  174. #define FAR 1
  175. #define TOP 2
  176. #define BOTTOM 3
  177. #define LEFT 4
  178. #define RIGHT 5
  179. #define NTL 0
  180. #define NBL 1
  181. #define NBR 2
  182. #define NTR 3
  183. #define FTL 4
  184. #define FBL 5
  185. #define FBR 6
  186. #define FTR 7
  187. static FrustumPlane planes[6];
  188. static glm::vec3 frustumColors[6] = {
  189. glm::vec3(1.0f, 0.0f, 0.0f), // NEAR, red
  190. glm::vec3(0.0f, 1.0f, 0.0f), // FAR, green
  191. glm::vec3(0.0f, 0.0f, 1.0f), // TOP, blue
  192. glm::vec3(1.0f, 1.0f, 0.0f), // BOTTOM, yellow
  193. glm::vec3(0.0f, 1.0f, 1.0f), // LEFT, light-blue
  194. glm::vec3(1.0f, 0.0f, 1.0f) // RIGHT, pink
  195. };
  196. static glm::vec3 frustumVertices[8];
  197. void Camera::calculateFrustumPlanes() {
  198. glm::mat4 combo = projection * view;
  199. // Calculate frustum corners to display them
  200. glm::mat4 inverse = glm::inverse(combo);
  201. frustumVertices[NTL] = glm::vec3(1.0f, 1.0f, 0.0f);
  202. frustumVertices[NTR] = glm::vec3(-1.0f, 1.0f, 0.0f);
  203. frustumVertices[NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
  204. frustumVertices[NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
  205. frustumVertices[FTL] = glm::vec3(1.0f, 1.0f, 1.0f);
  206. frustumVertices[FTR] = glm::vec3(-1.0f, 1.0f, 1.0f);
  207. frustumVertices[FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
  208. frustumVertices[FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
  209. for (int i = 0; i < 8; i++) {
  210. glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
  211. frustumVertices[i] = glm::vec3(t) / t.w;
  212. frustumVertices[i].y *= -1.0f;
  213. }
  214. // Set planes used for frustum culling
  215. planes[TOP].set(frustumVertices[NTR], frustumVertices[NTL], frustumVertices[FTL]);
  216. planes[BOTTOM].set(frustumVertices[NBL], frustumVertices[NBR], frustumVertices[FBR]);
  217. planes[LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[FBL]);
  218. planes[RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
  219. planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
  220. planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
  221. }
  222. bool Camera::boxInFrustum(BoundingBox b) {
  223. for (int i = 0; i < 6; i++) {
  224. int out = 0, in = 0;
  225. for (int c = 0; (c < 8) && ((in == 0) || (out == 0)); c++) {
  226. if (planes[i].distance(b.getCorner(c)) < 0)
  227. out++;
  228. else
  229. in++;
  230. }
  231. if (in == 0)
  232. return false;
  233. }
  234. return true;
  235. }
  236. void Camera::displayFrustum(glm::mat4 MVP) {
  237. std::vector<glm::vec3> verts;
  238. std::vector<glm::vec3> cols;
  239. std::vector<unsigned short> inds;
  240. // Near
  241. verts.push_back(frustumVertices[NTL]);
  242. verts.push_back(frustumVertices[NTR]);
  243. verts.push_back(frustumVertices[NBR]);
  244. verts.push_back(frustumVertices[NBL]);
  245. // Far
  246. verts.push_back(frustumVertices[FTR]);
  247. verts.push_back(frustumVertices[FTL]);
  248. verts.push_back(frustumVertices[FBL]);
  249. verts.push_back(frustumVertices[FBR]);
  250. // Top
  251. verts.push_back(frustumVertices[NTR]);
  252. verts.push_back(frustumVertices[NTL]);
  253. verts.push_back(frustumVertices[FTL]);
  254. verts.push_back(frustumVertices[FTR]);
  255. // Bottom
  256. verts.push_back(frustumVertices[NBL]);
  257. verts.push_back(frustumVertices[NBR]);
  258. verts.push_back(frustumVertices[FBR]);
  259. verts.push_back(frustumVertices[FBL]);
  260. // Left
  261. verts.push_back(frustumVertices[NTL]);
  262. verts.push_back(frustumVertices[NBL]);
  263. verts.push_back(frustumVertices[FBL]);
  264. verts.push_back(frustumVertices[FTL]);
  265. // Right
  266. verts.push_back(frustumVertices[NBR]);
  267. verts.push_back(frustumVertices[NTR]);
  268. verts.push_back(frustumVertices[FTR]);
  269. verts.push_back(frustumVertices[FBR]);
  270. for (int i = 0; i < 6; i++) {
  271. cols.push_back(frustumColors[i]);
  272. cols.push_back(frustumColors[i]);
  273. cols.push_back(frustumColors[i]);
  274. cols.push_back(frustumColors[i]);
  275. inds.push_back(4 * i);
  276. inds.push_back((4 * i) + 1);
  277. inds.push_back((4 * i) + 2);
  278. inds.push_back((4 * i) + 3);
  279. inds.push_back((4 * i) + 2);
  280. inds.push_back(4 * i);
  281. }
  282. Window::drawGL(verts, cols, inds, MVP);
  283. }