Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Camera.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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::vec3 Camera::drawPos(0.0f, 0.0f, 0.0f);
  37. glm::quat Camera::quaternion(glm::vec3(0.0f, 0.0f, 0.0f));
  38. glm::vec3 Camera::posSpeed(0.0f, 0.0f, 0.0f);
  39. glm::vec2 Camera::rotSpeed(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. drawPos = glm::vec3(0.0f, 0.0f, 0.0f);
  49. quaternion = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
  50. posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
  51. rotSpeed = glm::vec2(0.0f, 0.0f);
  52. dirty = true;
  53. projection = glm::mat4(1.0f);
  54. view = glm::mat4(1.0f);
  55. setSize(Window::getSize());
  56. }
  57. void Camera::setSize(glm::i32vec2 s) {
  58. //! \fixme TODO instead of mirroring the Y axis in the shader, scale with -1 here
  59. projection = glm::perspective(fov, float(s.x) / float(s.y), nearDist, farDist);
  60. }
  61. void Camera::handleAction(ActionEvents action, bool isFinished) {
  62. float factor = 1.0f;
  63. if (isFinished)
  64. factor = -1.0f;
  65. if (action == forwardAction) {
  66. posSpeed += dirUnit * maxSpeed * factor;
  67. } else if (action == backwardAction) {
  68. posSpeed -= dirUnit * maxSpeed * factor;
  69. } else if (action == leftAction) {
  70. posSpeed += rightUnit * maxSpeed * factor;
  71. } else if (action == rightAction) {
  72. posSpeed -= rightUnit * maxSpeed * factor;
  73. } else if (action == jumpAction) {
  74. posSpeed += upUnit * maxSpeed * factor;
  75. } else if (action == crouchAction) {
  76. posSpeed -= upUnit * maxSpeed * factor;
  77. } else {
  78. return;
  79. }
  80. dirty = true;
  81. }
  82. void Camera::handleMouseMotion(int x, int y) {
  83. if (x != 0) {
  84. quaternion = glm::quat(upUnit * (rotationDeltaX * x)) * quaternion;
  85. dirty = true;
  86. }
  87. if (y != 0) {
  88. static int lastDir = 0;
  89. float a = glm::dot(upUnit, quaternion * upUnit);
  90. if (((lastDir >= 0) && (y < 0)) || ((lastDir <= 0) && (y > 0)) || (a > 0.5f)) {
  91. quaternion = glm::quat(quaternion * -rightUnit * (rotationDeltaY * y)) * quaternion;
  92. dirty = true;
  93. // TODO find better way to clamp Y rotation axis!
  94. if (a > 0.5f)
  95. lastDir = y;
  96. }
  97. }
  98. }
  99. void Camera::handleControllerAxis(float value, KeyboardButton axis) {
  100. if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
  101. value = 0.0f;
  102. // TODO clamp Y rotation axis somehow...?
  103. if (axis == leftXAxis) {
  104. posSpeed.x = -maxSpeed * value;
  105. } else if (axis == leftYAxis) {
  106. posSpeed.z = maxSpeed * value;
  107. } else if (axis == rightXAxis) {
  108. rotSpeed.x = controllerViewFactor * value;
  109. } else if (axis == rightYAxis) {
  110. rotSpeed.y = controllerViewFactor * value;
  111. } else {
  112. return;
  113. }
  114. dirty = true;
  115. }
  116. void Camera::handleControllerButton(KeyboardButton button, bool released) {
  117. if (button == aButton) {
  118. handleAction(jumpAction, released);
  119. } else if (button == bButton) {
  120. handleAction(crouchAction, released);
  121. } else if (button == padUp) {
  122. handleAction(forwardAction, released);
  123. } else if (button == padDown) {
  124. handleAction(backwardAction, released);
  125. } else if (button == padLeft) {
  126. handleAction(leftAction, released);
  127. } else if (button == padRight) {
  128. handleAction(rightAction, released);
  129. } else {
  130. return;
  131. }
  132. dirty = true;
  133. }
  134. bool Camera::update() {
  135. if ((!dirty) && equal(posSpeed, 0.0f) && equal(rotSpeed, 0.0f))
  136. return false;
  137. float dT = RunTime::getLastFrameTime();
  138. pos += quaternion * posSpeed * dT;
  139. if (glm::epsilonNotEqual(rotSpeed.x, 0.0f, controllerDeadZone))
  140. quaternion = glm::quat(upUnit * rotationDeltaX * rotSpeed.x * dT) * quaternion;
  141. if (glm::epsilonNotEqual(rotSpeed.y, 0.0f, controllerDeadZone))
  142. quaternion = glm::quat(quaternion * -rightUnit * rotationDeltaY * rotSpeed.y * dT) * quaternion;
  143. glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
  144. glm::mat4 rotate = glm::toMat4(quaternion);
  145. view = glm::inverse(translate * rotate);
  146. if (updateViewFrustum)
  147. calculateFrustumPlanes();
  148. dirty = false;
  149. return updateViewFrustum;
  150. }
  151. glm::vec2 Camera::getRotation() {
  152. float x = glm::dot(dirUnit, quaternion * dirUnit);
  153. float y = glm::dot(upUnit, quaternion * upUnit);
  154. return glm::vec2(x, y);
  155. }
  156. // ----------------------------------------------------------------------------
  157. class FrustumPlane {
  158. public:
  159. FrustumPlane() : normal(glm::vec3(0.0f, 0.0f, 0.0f)), d(0.0f) { }
  160. void set(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
  161. normal = glm::normalize(glm::cross(v3 - v2, v1 - v2));
  162. d = -glm::dot(normal, v2);
  163. }
  164. float distance(glm::vec3 p) {
  165. return d + glm::dot(normal, p);
  166. }
  167. private:
  168. glm::vec3 normal;
  169. float d;
  170. };
  171. // ----------------------------------------------------------------------------
  172. #define NEAR 0
  173. #define FAR 1
  174. #define TOP 2
  175. #define BOTTOM 3
  176. #define LEFT 4
  177. #define RIGHT 5
  178. #define NTL 0
  179. #define NBL 1
  180. #define NBR 2
  181. #define NTR 3
  182. #define FTL 4
  183. #define FBL 5
  184. #define FBR 6
  185. #define FTR 7
  186. static FrustumPlane planes[6];
  187. static glm::vec3 frustumColors[6] = {
  188. glm::vec3(1.0f, 0.0f, 0.0f), // NEAR, red
  189. glm::vec3(0.0f, 1.0f, 0.0f), // FAR, green
  190. glm::vec3(0.0f, 0.0f, 1.0f), // TOP, blue
  191. glm::vec3(1.0f, 1.0f, 0.0f), // BOTTOM, yellow
  192. glm::vec3(0.0f, 1.0f, 1.0f), // LEFT, light-blue
  193. glm::vec3(1.0f, 0.0f, 1.0f) // RIGHT, pink
  194. };
  195. static glm::vec3 frustumVertices[8];
  196. void Camera::calculateFrustumPlanes() {
  197. glm::mat4 combo = projection * view;
  198. // Calculate frustum corners to display them
  199. glm::mat4 inverse = glm::inverse(combo);
  200. frustumVertices[NTL] = glm::vec3(1.0f, 1.0f, 0.0f);
  201. frustumVertices[NTR] = glm::vec3(-1.0f, 1.0f, 0.0f);
  202. frustumVertices[NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
  203. frustumVertices[NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
  204. frustumVertices[FTL] = glm::vec3(1.0f, 1.0f, 1.0f);
  205. frustumVertices[FTR] = glm::vec3(-1.0f, 1.0f, 1.0f);
  206. frustumVertices[FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
  207. frustumVertices[FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
  208. for (int i = 0; i < 8; i++) {
  209. glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
  210. frustumVertices[i] = glm::vec3(t) / t.w;
  211. frustumVertices[i].y *= -1.0f;
  212. }
  213. // Set planes used for frustum culling
  214. planes[TOP].set(frustumVertices[NTR], frustumVertices[NTL], frustumVertices[FTL]);
  215. planes[BOTTOM].set(frustumVertices[NBL], frustumVertices[NBR], frustumVertices[FBR]);
  216. planes[LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[FBL]);
  217. planes[RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
  218. planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
  219. planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
  220. drawPos = getPosition();
  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. verts.clear();
  284. cols.clear();
  285. inds.clear();
  286. verts.push_back(drawPos);
  287. cols.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
  288. inds.push_back(0);
  289. Window::drawGL(verts, cols, inds, MVP, GL_POINTS);
  290. }