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 11KB

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