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

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