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.

Entity.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*!
  2. * \file src/Entity.cpp
  3. * \brief Things in the World
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Log.h"
  10. #include "World.h"
  11. #include "Entity.h"
  12. #include <glm/gtc/matrix_transform.hpp>
  13. #include <imgui/imgui.h>
  14. #define CACHE_SPRITE 0
  15. #define CACHE_MESH 1
  16. #define CACHE_MODEL 2
  17. bool Entity::showEntitySprites = true;
  18. bool Entity::showEntityMeshes = false;
  19. bool Entity::showEntityModels = false;
  20. void Entity::find() {
  21. if ((cache <= -1) || (cacheType <= -1)) {
  22. /*
  23. * The order in which to look for matching objects with the same ID
  24. * seems to be very important!
  25. * If sprites and meshes are searched before models, many objects will
  26. * be displayed wrong (eg. 'bad guy' becomes 'clothes' in tr2/boat)...
  27. */
  28. for (int i = 0; (i < World::sizeSkeletalModel()) && (cache == -1); i++) {
  29. auto& s = World::getSkeletalModel(i);
  30. if (s.getID() == id) {
  31. cacheType = CACHE_MODEL;
  32. cache = i;
  33. break;
  34. }
  35. }
  36. for (int i = 0; (i < World::sizeStaticMesh()) && (cache == -1); i++) {
  37. auto& s = World::getStaticMesh(i);
  38. if (s.getID() == id) {
  39. cacheType = CACHE_MESH;
  40. cache = i;
  41. break;
  42. }
  43. }
  44. for (int i = 0; i < World::sizeSpriteSequence(); i++) {
  45. auto& s = World::getSpriteSequence(i);
  46. if (s.getID() == id) {
  47. cacheType = CACHE_SPRITE;
  48. cache = i;
  49. break;
  50. }
  51. }
  52. orAssertGreaterThan(cache, -1);
  53. orAssertGreaterThan(cacheType, -1);
  54. }
  55. }
  56. void Entity::display(glm::mat4 VP) {
  57. find();
  58. glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
  59. glm::mat4 rotate;
  60. if (cacheType == CACHE_SPRITE) {
  61. rotate = glm::rotate(glm::mat4(1.0f), -Camera::getRotation().x, glm::vec3(0.0f, 1.0f, 0.0f));
  62. } else {
  63. rotate = glm::rotate(glm::mat4(1.0f), rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
  64. }
  65. glm::mat4 model = translate * rotate;
  66. glm::mat4 MVP = VP * model;
  67. if (cacheType == CACHE_SPRITE) {
  68. if (showEntitySprites)
  69. World::getSpriteSequence(cache).display(MVP, sprite);
  70. } else if (cacheType == CACHE_MESH) {
  71. if (showEntityMeshes)
  72. World::getStaticMesh(cache).display(MVP);
  73. } else if (cacheType == CACHE_MODEL) {
  74. if (showEntityModels)
  75. World::getSkeletalModel(cache).display(MVP, animation, frame);
  76. }
  77. }
  78. void Entity::displayUI() {
  79. find();
  80. ImGui::Text("%03d", id);
  81. ImGui::NextColumn();
  82. if (cacheType == CACHE_SPRITE) {
  83. ImGui::Text("SpriteSequence");
  84. } else if (cacheType == CACHE_MESH) {
  85. ImGui::Text("StaticMesh");
  86. } else if (cacheType == CACHE_MODEL) {
  87. ImGui::Text("SkeletalModel");
  88. }
  89. ImGui::NextColumn();
  90. ImGui::Text("%03d", cache);
  91. ImGui::NextColumn();
  92. }