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

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