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.

Room.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. * \file src/Room.cpp
  3. * \brief Room in World
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Log.h"
  9. #include "Room.h"
  10. #include "imgui/imgui.h"
  11. #include <glm/gtc/matrix_transform.hpp>
  12. #include <glm/gtx/intersect.hpp>
  13. bool Room::showBoundingBox = false;
  14. bool Room::showRoomModels = true;
  15. bool Room::showRoomSprites = true;
  16. bool Room::showRoomGeometry = true;
  17. Room::Room(glm::vec3 _pos, BoundingBox* _bbox, RoomMesh* _mesh, unsigned int f,
  18. int a, int x, int z, int i) : pos(_pos), bbox(_bbox), mesh(_mesh), flags(f),
  19. alternateRoom(a), numXSectors(x), numZSectors(z), roomIndex(i) {
  20. model = glm::translate(glm::mat4(1.0f), pos);
  21. }
  22. void Room::display(glm::mat4 VP) {
  23. if (showRoomGeometry) {
  24. mesh->display(VP * model);
  25. }
  26. if (showRoomModels) {
  27. for (auto& m : models) {
  28. m->display(VP);
  29. }
  30. }
  31. if (showRoomSprites) {
  32. for (auto& s : sprites) {
  33. s->display(VP);
  34. }
  35. }
  36. if (showBoundingBox) {
  37. bbox->display(VP, glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 0.0f, 1.0f));
  38. }
  39. if (Portal::getShowBoundingBox()) {
  40. for (auto& p : portals) {
  41. p->display(VP);
  42. }
  43. }
  44. }
  45. bool Room::isWall(unsigned long sector) {
  46. orAssertLessThan(sector, sectors.size());
  47. //! \fixme is (sector > 0) correct??
  48. return ((sector > 0) && sectors.at(sector)->isWall());
  49. }
  50. long Room::getSector(float x, float z, float* floor, float* ceiling) {
  51. orAssert(floor != nullptr);
  52. orAssert(ceiling != nullptr);
  53. long sector = getSector(x, z);
  54. if ((sector >= 0) && (sector < (long)sectors.size())) {
  55. *floor = sectors.at(sector)->getFloor();
  56. *ceiling = sectors.at(sector)->getCeiling();
  57. }
  58. return sector;
  59. }
  60. long Room::getSector(float x, float z) {
  61. long sector = (((((int)x - (int)pos[0]) / 1024) *
  62. numZSectors) + (((int)z - (int)pos[2]) / 1024));
  63. if (sector < 0)
  64. return -1;
  65. return sector;
  66. }
  67. void Room::getHeightAtPosition(float x, float* y, float z) {
  68. long sector = getSector(x, z);
  69. if ((sector >= 0) && (sector < (long)sectors.size()))
  70. *y = sectors.at(sector)->getFloor();
  71. }
  72. int Room::getAdjoiningRoom(float x, float y, float z,
  73. float x2, float y2, float z2) {
  74. glm::vec3 orig(x, y, z);
  75. glm::vec3 dir(x2 - x, y2 - y, z2 - z);
  76. glm::vec3 intersect;
  77. for (unsigned long i = 0; i < portals.size(); i++) {
  78. if ((glm::intersectLineTriangle(orig, dir, portals.at(i)->getVertex(0),
  79. portals.at(i)->getVertex(1),
  80. portals.at(i)->getVertex(2), intersect))
  81. || (glm::intersectLineTriangle(orig, dir, portals.at(i)->getVertex(0),
  82. portals.at(i)->getVertex(3),
  83. portals.at(i)->getVertex(1), intersect)))
  84. return portals.at(i)->getAdjoiningRoom();
  85. }
  86. return -1;
  87. }
  88. void Room::displayUI() {
  89. ImGui::PushID(roomIndex);
  90. ImGui::Text("%03d", roomIndex);
  91. ImGui::NextColumn();
  92. ImGui::Text("%03d", alternateRoom);
  93. ImGui::NextColumn();
  94. ImGui::Text("0x%04X", flags);
  95. ImGui::NextColumn();
  96. if (models.size() > 0) {
  97. if (ImGui::TreeNode("...##model")) {
  98. for (auto& m : models) {
  99. m->displayUI();
  100. }
  101. ImGui::TreePop();
  102. }
  103. } else {
  104. ImGui::Text("None");
  105. }
  106. ImGui::NextColumn();
  107. if (portals.size() > 0) {
  108. if (ImGui::TreeNode("...##portal")) {
  109. for (auto& p : portals) {
  110. p->displayUI();
  111. }
  112. ImGui::TreePop();
  113. }
  114. } else {
  115. ImGui::Text("None");
  116. }
  117. ImGui::NextColumn();
  118. ImGui::PopID();
  119. }