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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*!
  2. * \file src/Camera.cpp
  3. * \brief Camera, View Frustum
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <limits>
  9. #include "imgui/imgui.h"
  10. #include "global.h"
  11. #include "RunTime.h"
  12. #include "World.h"
  13. #include "system/Shader.h"
  14. #include "system/Sound.h"
  15. #include "system/Window.h"
  16. #include "Camera.h"
  17. #include <glm/gtc/epsilon.hpp>
  18. #include <glm/gtc/matrix_transform.hpp>
  19. #include <glm/gtc/quaternion.hpp>
  20. #include <glm/gtx/quaternion.hpp>
  21. static bool equal(float a, float b) {
  22. return glm::epsilonEqual(a, b, std::numeric_limits<float>::epsilon());
  23. }
  24. static bool equal(glm::vec2 a, float b) {
  25. return equal(a.x, b) && equal(a.y, b);
  26. }
  27. static bool equal(glm::vec3 a, float b) {
  28. return equal(a.x, b) && equal(a.y, b) && equal(a.z, b);
  29. }
  30. // ----------------------------------------------------------------------------
  31. const float Camera::fov = 45.0f;
  32. const float Camera::nearDist = 0.1f;
  33. const float Camera::farDist = 75000.0f;
  34. const static float maxSpeed = 3072.0f;
  35. const static float controllerDeadZone = 0.33f;
  36. const static float controllerViewFactor = glm::pi<float>();
  37. const static float rotationAngleClamp = glm::pi<float>() * 2.0f;
  38. const static float rotationAngleVertMax = glm::pi<float>() / 2.0f;
  39. const static float runFactor = 2.5f;
  40. const static glm::vec3 rightUnit(1.0f, 0.0f, 0.0f);
  41. const static glm::vec3 upUnit(0.0f, 1.0f, 0.0f);
  42. const static glm::vec3 dirUnit(0.0f, 0.0f, -1.0f);
  43. glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
  44. glm::vec2 Camera::rot(glm::pi<float>(), 0.0f);
  45. glm::vec3 Camera::posSpeed(0.0f, 0.0f, 0.0f);
  46. glm::vec2 Camera::rotSpeed(0.0f, 0.0f);
  47. glm::mat4 Camera::projection(1.0f);
  48. glm::mat4 Camera::view(1.0f);
  49. float Camera::rotationDeltaX = 0.75f;
  50. float Camera::rotationDeltaY = 0.75f;
  51. bool Camera::updateViewFrustum = true;
  52. bool Camera::dirty = true;
  53. bool Camera::movingFaster = false;
  54. bool Camera::keepInRoom = false;
  55. int Camera::room = -1;
  56. void Camera::reset() {
  57. pos = glm::vec3(0.0f, 0.0f, 0.0f);
  58. rot = glm::vec2(glm::pi<float>(), 0.0f);
  59. posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
  60. rotSpeed = glm::vec2(0.0f, 0.0f);
  61. dirty = true;
  62. projection = glm::mat4(1.0f);
  63. view = glm::mat4(1.0f);
  64. room = -1;
  65. setSize(Window::getSize());
  66. }
  67. void Camera::setSize(glm::i32vec2 s) {
  68. projection = glm::perspective(fov, float(s.x) / float(s.y), nearDist, farDist);
  69. }
  70. void Camera::handleAction(ActionEvents action, bool isFinished) {
  71. float factor = 1.0f;
  72. if (isFinished)
  73. factor = -1.0f;
  74. if (action == forwardAction) {
  75. posSpeed += dirUnit * maxSpeed * factor;
  76. } else if (action == backwardAction) {
  77. posSpeed -= dirUnit * maxSpeed * factor;
  78. } else if (action == leftAction) {
  79. posSpeed -= rightUnit * maxSpeed * factor;
  80. } else if (action == rightAction) {
  81. posSpeed += rightUnit * maxSpeed * factor;
  82. } else if (action == jumpAction) {
  83. posSpeed += upUnit * maxSpeed * factor;
  84. } else if (action == crouchAction) {
  85. posSpeed -= upUnit * maxSpeed * factor;
  86. } else if (action == walkAction) {
  87. movingFaster = !isFinished;
  88. } else {
  89. return;
  90. }
  91. dirty = true;
  92. }
  93. void Camera::handleMouseMotion(int x, int y) {
  94. if ((x != 0) || (y != 0))
  95. dirty = true;
  96. while (x > 0) {
  97. rot.x -= rotationDeltaX;
  98. x--;
  99. }
  100. while (x < 0) {
  101. rot.x += rotationDeltaX;
  102. x++;
  103. }
  104. while (y > 0) {
  105. if (rot.y > -rotationAngleVertMax) {
  106. rot.y -= rotationDeltaY;
  107. }
  108. y--;
  109. }
  110. while (y < 0) {
  111. if (rot.y < rotationAngleVertMax) {
  112. rot.y += rotationDeltaY;
  113. }
  114. y++;
  115. }
  116. }
  117. void Camera::handleControllerAxis(float value, KeyboardButton axis) {
  118. if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
  119. value = 0.0f;
  120. if (axis == leftXAxis) {
  121. posSpeed.x = maxSpeed * value;
  122. } else if (axis == leftYAxis) {
  123. posSpeed.z = maxSpeed * value;
  124. } else if (axis == rightXAxis) {
  125. rotSpeed.x = -controllerViewFactor * value;
  126. } else if (axis == rightYAxis) {
  127. rotSpeed.y = -controllerViewFactor * value;
  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. while (rot.x > rotationAngleClamp)
  137. rot.x -= rotationAngleClamp;
  138. while (rot.x < -rotationAngleClamp)
  139. rot.x += rotationAngleClamp;
  140. while (rot.y > rotationAngleClamp)
  141. rot.y -= rotationAngleClamp;
  142. while (rot.y < -rotationAngleClamp)
  143. rot.y += rotationAngleClamp;
  144. float dT = RunTime::getLastFrameTime();
  145. glm::vec2 newRot = rot + rotSpeed * dT;
  146. if ((newRot.y > -rotationAngleVertMax) && (newRot.y < rotationAngleVertMax))
  147. rot = newRot;
  148. else
  149. rotSpeed = glm::vec2(0.0f, 0.0f);
  150. static glm::quat quatZ = glm::angleAxis(glm::pi<float>(), glm::vec3(0.0f, 0.0f, 1.0f));
  151. glm::quat quatY = glm::angleAxis(rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
  152. glm::quat quatX = glm::angleAxis(rot.y, glm::vec3(1.0f, 0.0f, 0.0f));
  153. glm::quat quaternion = quatZ * quatY * quatX;
  154. float factor = movingFaster ? runFactor : 1.0f;
  155. glm::vec3 clampedSpeed = posSpeed * factor;
  156. if (glm::length(clampedSpeed) > (maxSpeed * factor)) {
  157. clampedSpeed = glm::normalize(clampedSpeed) * maxSpeed * factor;
  158. }
  159. glm::vec3 newPos = pos + (quaternion * clampedSpeed * dT);
  160. if (keepInRoom) {
  161. if ((room < 0) || (room >= World::sizeRoom())) {
  162. keepInRoom = false;
  163. pos = newPos;
  164. } else if (World::getRoom(room).getBoundingBox().inBox(newPos)) {
  165. pos = newPos;
  166. }
  167. } else {
  168. pos = newPos;
  169. }
  170. glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
  171. glm::mat4 rotate = glm::toMat4(quaternion);
  172. view = glm::inverse(translate * rotate);
  173. if (updateViewFrustum)
  174. calculateFrustumPlanes();
  175. glm::vec3 at(0.0f, 0.0f, -1.0f);
  176. glm::vec3 up(0.0f, 1.0f, 0.0f);
  177. Sound::listenAt(pos, quaternion * at, quaternion * up);
  178. dirty = false;
  179. return updateViewFrustum;
  180. }
  181. // ----------------------------------------------------------------------------
  182. class FrustumPlane {
  183. public:
  184. FrustumPlane() : normal(glm::vec3(0.0f, 0.0f, 0.0f)), d(0.0f) { }
  185. void set(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
  186. normal = glm::normalize(glm::cross(v3 - v2, v1 - v2));
  187. d = -glm::dot(normal, v2);
  188. }
  189. float distance(glm::vec3 p) {
  190. return d + glm::dot(normal, p);
  191. }
  192. private:
  193. glm::vec3 normal;
  194. float d;
  195. };
  196. // ----------------------------------------------------------------------------
  197. #define NEAR 0
  198. #define FAR 1
  199. #define TOP 2
  200. #define BOTTOM 3
  201. #define LEFT 4
  202. #define RIGHT 5
  203. #define NTL 0
  204. #define NBL 1
  205. #define NBR 2
  206. #define NTR 3
  207. #define FTL 4
  208. #define FBL 5
  209. #define FBR 6
  210. #define FTR 7
  211. static FrustumPlane planes[6];
  212. static glm::vec3 frustumColors[6] = {
  213. glm::vec3(1.0f, 0.0f, 0.0f), // NEAR, red
  214. glm::vec3(0.0f, 1.0f, 0.0f), // FAR, green
  215. glm::vec3(0.0f, 0.0f, 1.0f), // TOP, blue
  216. glm::vec3(1.0f, 1.0f, 0.0f), // BOTTOM, yellow
  217. glm::vec3(0.0f, 1.0f, 1.0f), // LEFT, light-blue
  218. glm::vec3(1.0f, 0.0f, 1.0f) // RIGHT, pink
  219. };
  220. static glm::vec3 frustumVertices[8];
  221. static std::vector<glm::vec3> vertexBuffer;
  222. static std::vector<glm::vec3> colorBuffer;
  223. static std::vector<unsigned short> indexBuffer;
  224. static std::vector<glm::vec3> vertexPointBuffer;
  225. static std::vector<glm::vec3> colorPointBuffer;
  226. void Camera::calculateFrustumPlanes() {
  227. vertexBuffer.clear();
  228. vertexPointBuffer.clear();
  229. colorPointBuffer.clear();
  230. glm::mat4 combo = projection * view;
  231. // Calculate frustum corners to display them
  232. glm::mat4 inverse = glm::inverse(combo);
  233. frustumVertices[NTL] = glm::vec3(1.0f, 1.0f, 0.0f);
  234. frustumVertices[NTR] = glm::vec3(-1.0f, 1.0f, 0.0f);
  235. frustumVertices[NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
  236. frustumVertices[NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
  237. frustumVertices[FTL] = glm::vec3(1.0f, 1.0f, 1.0f);
  238. frustumVertices[FTR] = glm::vec3(-1.0f, 1.0f, 1.0f);
  239. frustumVertices[FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
  240. frustumVertices[FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
  241. for (int i = 0; i < 8; i++) {
  242. glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
  243. frustumVertices[i] = glm::vec3(t) / t.w;
  244. }
  245. // Set planes used for frustum culling
  246. planes[TOP].set(frustumVertices[NTR], frustumVertices[NTL], frustumVertices[FTL]);
  247. planes[BOTTOM].set(frustumVertices[NBL], frustumVertices[NBR], frustumVertices[FBR]);
  248. planes[LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[FBL]);
  249. planes[RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
  250. planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
  251. planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
  252. // Near
  253. vertexBuffer.push_back(frustumVertices[NTL]);
  254. vertexBuffer.push_back(frustumVertices[NTR]);
  255. vertexBuffer.push_back(frustumVertices[NBR]);
  256. vertexBuffer.push_back(frustumVertices[NBL]);
  257. // Far
  258. vertexBuffer.push_back(frustumVertices[FTR]);
  259. vertexBuffer.push_back(frustumVertices[FTL]);
  260. vertexBuffer.push_back(frustumVertices[FBL]);
  261. vertexBuffer.push_back(frustumVertices[FBR]);
  262. // Top
  263. vertexBuffer.push_back(frustumVertices[NTR]);
  264. vertexBuffer.push_back(frustumVertices[NTL]);
  265. vertexBuffer.push_back(frustumVertices[FTL]);
  266. vertexBuffer.push_back(frustumVertices[FTR]);
  267. // Bottom
  268. vertexBuffer.push_back(frustumVertices[NBL]);
  269. vertexBuffer.push_back(frustumVertices[NBR]);
  270. vertexBuffer.push_back(frustumVertices[FBR]);
  271. vertexBuffer.push_back(frustumVertices[FBL]);
  272. // Left
  273. vertexBuffer.push_back(frustumVertices[NTL]);
  274. vertexBuffer.push_back(frustumVertices[NBL]);
  275. vertexBuffer.push_back(frustumVertices[FBL]);
  276. vertexBuffer.push_back(frustumVertices[FTL]);
  277. // Right
  278. vertexBuffer.push_back(frustumVertices[NBR]);
  279. vertexBuffer.push_back(frustumVertices[NTR]);
  280. vertexBuffer.push_back(frustumVertices[FTR]);
  281. vertexBuffer.push_back(frustumVertices[FBR]);
  282. // Position indicator
  283. vertexPointBuffer.push_back(getPosition());
  284. colorPointBuffer.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
  285. // Lazy initialization of frustum plane color buffer
  286. if (colorBuffer.size() == 0) {
  287. for (int i = 0; i < 6; i++) {
  288. for (int j = 0; j < 4; j++) {
  289. colorBuffer.push_back(frustumColors[i]);
  290. }
  291. }
  292. }
  293. if (indexBuffer.size() == 0) {
  294. for (int i = 0; i < 6; i++) {
  295. indexBuffer.push_back(static_cast<unsigned short>(4 * i));
  296. indexBuffer.push_back(static_cast<unsigned short>((4 * i) + 1));
  297. indexBuffer.push_back(static_cast<unsigned short>((4 * i) + 2));
  298. indexBuffer.push_back(static_cast<unsigned short>((4 * i) + 3));
  299. indexBuffer.push_back(static_cast<unsigned short>((4 * i) + 2));
  300. indexBuffer.push_back(static_cast<unsigned short>(4 * i));
  301. }
  302. }
  303. }
  304. bool Camera::boxInFrustum(BoundingBox b) {
  305. for (int i = 0; i < 6; i++) {
  306. int out = 0, in = 0;
  307. for (int c = 0; (c < 8) && ((in == 0) || (out == 0)); c++) {
  308. if (planes[i].distance(b.getCorner(c)) >= 0)
  309. out++;
  310. else
  311. in++;
  312. }
  313. if (in == 0)
  314. return false;
  315. }
  316. return true;
  317. }
  318. void Camera::displayFrustum(glm::mat4 MVP) {
  319. Shader::set2DState(true, false);
  320. Shader::drawGL(vertexBuffer, colorBuffer, indexBuffer, MVP);
  321. Shader::drawGL(vertexPointBuffer, colorPointBuffer, MVP, gl::GL_POINTS);
  322. Shader::set2DState(false, false);
  323. }