Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Camera.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*!
  2. * \file src/Camera.cpp
  3. * \brief Camera class
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <glm/gtc/matrix_transform.hpp>
  9. #include "global.h"
  10. #include "system/Window.h"
  11. #include "Camera.h"
  12. #define NEAR 0
  13. #define FAR 1
  14. #define TOP 2
  15. #define BOTTOM 3
  16. #define LEFT 4
  17. #define RIGHT 5
  18. #define NTL 0
  19. #define NBL 1
  20. #define NBR 2
  21. #define NTR 3
  22. #define FTL 4
  23. #define FBL 5
  24. #define FBR 6
  25. #define FTR 7
  26. struct Plane {
  27. glm::vec3 normal, pos;
  28. float d;
  29. Plane(glm::vec3 n = glm::vec3(0.0f, 0.0f, 0.0f),
  30. glm::vec3 p = glm::vec3(0.0f, 0.0f, 0.0f)) {
  31. set(n, p);
  32. }
  33. void set(glm::vec3 a, glm::vec3 b, glm::vec3 c) {
  34. glm::vec3 aux1 = a - b;
  35. glm::vec3 aux2 = c - b;
  36. normal = glm::normalize(glm::cross(aux2, aux1));
  37. pos = b;
  38. d = -glm::dot(normal, pos);
  39. }
  40. void set(glm::vec3 n, glm::vec3 p) {
  41. normal = n;
  42. pos = p;
  43. d = -glm::dot(normal, pos);
  44. }
  45. float distance(glm::vec3 p) {
  46. return d + glm::dot(normal, p);
  47. }
  48. };
  49. // ----------------------------------------------------------------------------
  50. const static float fov = 45.0f;
  51. const static float nearDist = 0.1f;
  52. const static float farDist = 75000.0f;
  53. const static float freeCameraStep = 256.0f;
  54. glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
  55. glm::vec2 Camera::rot(0.0f, 0.0f);
  56. glm::vec3 Camera::lastPos(1.0f, 0.0f, 0.0f);
  57. glm::vec2 Camera::lastRot(1.0f, 0.0f);
  58. glm::vec2 Camera::lastSize(0.0f, 0.0f);
  59. glm::mat4 Camera::projection(1.0f);
  60. glm::mat4 Camera::view(1.0f);
  61. float Camera::rotationDeltaX = 0.75f;
  62. float Camera::rotationDeltaY = 0.75f;
  63. bool Camera::updateViewFrustum = true;
  64. static Plane planes[6];
  65. static glm::vec3 frustumColors[6] = {
  66. glm::vec3(1.0f, 0.0f, 0.0f),
  67. glm::vec3(0.0f, 1.0f, 0.0f),
  68. glm::vec3(0.0f, 0.0f, 1.0f),
  69. glm::vec3(1.0f, 1.0f, 0.0f),
  70. glm::vec3(0.0f, 1.0f, 1.0f),
  71. glm::vec3(1.0f, 0.0f, 1.0f)
  72. };
  73. static glm::vec3 frustumVertices[8];
  74. void Camera::reset() {
  75. pos = glm::vec3(0.0f, 0.0f, 0.0f);
  76. rot = glm::vec2(0.0f, 0.0f);
  77. lastPos = glm::vec3(1.0f, 0.0f, 0.0f);
  78. lastRot = glm::vec2(1.0f, 0.0f);
  79. lastSize = glm::vec2(0.0f, 0.0f);
  80. projection = glm::mat4(1.0f);
  81. view = glm::mat4(1.0f);
  82. }
  83. void Camera::handleAction(ActionEvents action, bool isFinished) {
  84. if (isFinished)
  85. return;
  86. glm::vec3 dir(
  87. glm::cos(rot.y) * glm::sin(rot.x),
  88. glm::sin(rot.y),
  89. glm::cos(rot.y) * glm::cos(rot.x)
  90. );
  91. glm::vec3 right(
  92. glm::sin(rot.x - glm::pi<float>() / 2.0f),
  93. 0.0f,
  94. glm::cos(rot.x - glm::pi<float>() / 2.0f)
  95. );
  96. glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
  97. if (action == forwardAction) {
  98. pos += dir * freeCameraStep;
  99. } else if (action == backwardAction) {
  100. pos -= dir * freeCameraStep;
  101. } else if (action == leftAction) {
  102. pos += right * freeCameraStep;
  103. } else if (action == rightAction) {
  104. pos -= right * freeCameraStep;
  105. } else if (action == jumpAction) {
  106. pos += up * freeCameraStep;
  107. } else if (action == crouchAction) {
  108. pos -= up * freeCameraStep;
  109. } else if (action == useAction) {
  110. } else if (action == holsterAction) {
  111. } else if (action == walkAction) {
  112. }
  113. }
  114. void Camera::handleMouseMotion(int x, int y) {
  115. while (x > 0) {
  116. rot.x += rotationDeltaX;
  117. x--;
  118. }
  119. while (x < 0) {
  120. rot.x -= rotationDeltaX;
  121. x++;
  122. }
  123. while (y > 0) {
  124. if (rot.y > -(glm::pi<float>() / 2.0f)) {
  125. rot.y -= rotationDeltaY;
  126. }
  127. y--;
  128. }
  129. while (y < 0) {
  130. if (rot.y < (glm::pi<float>() / 2.0f)) {
  131. rot.y += rotationDeltaY;
  132. }
  133. y++;
  134. }
  135. while (rot.x > (glm::pi<float>() * 2.0f))
  136. rot.x -= glm::pi<float>() * 2.0f;
  137. while (rot.x < -(glm::pi<float>() * 2.0f))
  138. rot.x += glm::pi<float>() * 2.0f;
  139. }
  140. bool Camera::update() {
  141. glm::vec2 size(getWindow().getWidth(), getWindow().getHeight());
  142. if ((lastPos == pos) && (lastRot == rot) && (lastSize == size))
  143. return false;
  144. if (lastSize != size) {
  145. projection = glm::perspective(fov, size.x / size.y, nearDist, farDist);
  146. lastSize = size;
  147. }
  148. glm::vec3 dir(
  149. glm::cos(rot.y) * glm::sin(rot.x),
  150. glm::sin(rot.y),
  151. glm::cos(rot.y) * glm::cos(rot.x)
  152. );
  153. glm::vec3 right(
  154. glm::sin(rot.x - glm::pi<float>() / 2.0f),
  155. 0.0f,
  156. glm::cos(rot.x - glm::pi<float>() / 2.0f)
  157. );
  158. glm::vec3 up = glm::cross(right, dir);
  159. view = glm::lookAt(pos, pos + dir, up);
  160. if (!updateViewFrustum)
  161. return false;
  162. dir = glm::normalize(dir);
  163. right = glm::normalize(right);
  164. up = glm::normalize(up);
  165. static float tang = glm::tan(glm::radians(fov * 0.5f));
  166. static float nh = nearDist * tang;
  167. float nw = nh * (size.x / size.y);
  168. static float fh = farDist * tang;
  169. float fw = fh * (size.x / size.y);
  170. glm::vec3 nearCenter = pos + dir * nearDist;
  171. glm::vec3 farCenter = pos + dir * farDist;
  172. frustumVertices[NTL] = nearCenter + up * nh - right * nw;
  173. frustumVertices[NTR] = nearCenter + up * nh + right * nw;
  174. frustumVertices[NBL] = nearCenter - up * nh - right * nw;
  175. frustumVertices[NBR] = nearCenter - up * nh + right * nw;
  176. frustumVertices[FTL] = farCenter + up * fh - right * fw;
  177. frustumVertices[FTR] = farCenter + up * fh + right * fw;
  178. frustumVertices[FBL] = farCenter - up * fh - right * fw;
  179. frustumVertices[FBR] = farCenter - up * fh + right * fw;
  180. #if 1
  181. planes[TOP].set(frustumVertices[NTR], frustumVertices[NTL], frustumVertices[FTL]);
  182. planes[BOTTOM].set(frustumVertices[NBL], frustumVertices[NBR], frustumVertices[FBR]);
  183. planes[LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[FBL]);
  184. planes[RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
  185. planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
  186. planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
  187. #else
  188. planes[NEAR].set(-dir, nearCenter);
  189. planes[FAR].set(dir, farCenter);
  190. glm::vec3 aux = glm::normalize((nearCenter + up * nh) - pos);
  191. glm::vec3 normal = glm::cross(aux, right);
  192. planes[TOP].set(normal, nearCenter + up * nh);
  193. aux = glm::normalize((nearCenter - up * nh) - pos);
  194. normal = glm::cross(right, aux);
  195. planes[BOTTOM].set(normal, nearCenter - up * nh);
  196. aux = glm::normalize((nearCenter - right * nw) - pos);
  197. normal = glm::cross(aux, up);
  198. planes[LEFT].set(normal, nearCenter - right * nw);
  199. aux = glm::normalize((nearCenter + right * nw) - pos);
  200. normal = glm::cross(up, aux);
  201. planes[RIGHT].set(normal, nearCenter + right * nw);
  202. #endif
  203. lastPos = pos;
  204. lastRot = rot;
  205. return true;
  206. }
  207. bool Camera::boxInFrustum(BoundingBox b) {
  208. for (int i = 0; i < 6; i++) {
  209. if (planes[i].distance(b.getVertexP(planes[i].normal)) < 0)
  210. return false;
  211. }
  212. return true;
  213. }
  214. void Camera::displayFrustum(glm::mat4 MVP) {
  215. std::vector<glm::vec3> verts;
  216. std::vector<glm::vec3> cols;
  217. std::vector<unsigned short> inds;
  218. verts.push_back(frustumVertices[NTL]);
  219. verts.push_back(frustumVertices[NTR]);
  220. verts.push_back(frustumVertices[NBR]);
  221. verts.push_back(frustumVertices[NBL]);
  222. verts.push_back(frustumVertices[FTR]);
  223. verts.push_back(frustumVertices[FTL]);
  224. verts.push_back(frustumVertices[FBL]);
  225. verts.push_back(frustumVertices[FBR]);
  226. verts.push_back(frustumVertices[NBL]);
  227. verts.push_back(frustumVertices[NBR]);
  228. verts.push_back(frustumVertices[FBR]);
  229. verts.push_back(frustumVertices[FBL]);
  230. verts.push_back(frustumVertices[NTR]);
  231. verts.push_back(frustumVertices[NTL]);
  232. verts.push_back(frustumVertices[FTL]);
  233. verts.push_back(frustumVertices[FTR]);
  234. verts.push_back(frustumVertices[NTL]);
  235. verts.push_back(frustumVertices[NBL]);
  236. verts.push_back(frustumVertices[FBL]);
  237. verts.push_back(frustumVertices[FTL]);
  238. verts.push_back(frustumVertices[NBR]);
  239. verts.push_back(frustumVertices[NTR]);
  240. verts.push_back(frustumVertices[FTR]);
  241. verts.push_back(frustumVertices[FBR]);
  242. for (int i = 0; i < 6; i++) {
  243. cols.push_back(frustumColors[i]);
  244. cols.push_back(frustumColors[i]);
  245. cols.push_back(frustumColors[i]);
  246. cols.push_back(frustumColors[i]);
  247. inds.push_back(4 * i);
  248. inds.push_back((4 * i) + 1);
  249. inds.push_back((4 * i) + 2);
  250. inds.push_back((4 * i) + 3);
  251. inds.push_back((4 * i) + 2);
  252. inds.push_back(4 * i);
  253. }
  254. Window::drawGL(verts, cols, inds, MVP);
  255. }