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

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