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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. * \file src/Room.cpp
  3. * \brief World Room Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <glm/gtc/matrix_transform.hpp>
  9. #include <glm/gtx/intersect.hpp>
  10. #include "global.h"
  11. #include "Game.h"
  12. #include "Log.h"
  13. #include "Render.h"
  14. #include "Room.h"
  15. #include "TextureManager.h"
  16. void Room::display(glm::mat4 view, glm::mat4 projection) {
  17. glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
  18. mesh->display(model, view, projection);
  19. for (auto& m : models) {
  20. m->display(view, projection);
  21. }
  22. if (Render::getMode() == RenderMode::Wireframe)
  23. bbox->display(projection * view);
  24. }
  25. bool Room::isWall(unsigned long sector) {
  26. assert(sector < sectors.size());
  27. //! \fixme is (sector > 0) correct??
  28. return ((sector > 0) && sectors.at(sector)->isWall());
  29. }
  30. long Room::getSector(float x, float z, float* floor, float* ceiling) {
  31. assert(floor != nullptr);
  32. assert(ceiling != nullptr);
  33. long sector = getSector(x, z);
  34. if ((sector >= 0) && (sector < (long)sectors.size())) {
  35. *floor = sectors.at(sector)->getFloor();
  36. *ceiling = sectors.at(sector)->getCeiling();
  37. }
  38. return sector;
  39. }
  40. long Room::getSector(float x, float z) {
  41. long sector = (((((int)x - (int)pos[0]) / 1024) *
  42. numZSectors) + (((int)z - (int)pos[2]) / 1024));
  43. if (sector < 0)
  44. return -1;
  45. return sector;
  46. }
  47. void Room::getHeightAtPosition(float x, float* y, float z) {
  48. long sector = getSector(x, z);
  49. if ((sector >= 0) && (sector < (long)sectors.size()))
  50. *y = sectors.at(sector)->getFloor();
  51. }
  52. int Room::getAdjoiningRoom(float x, float y, float z,
  53. float x2, float y2, float z2) {
  54. glm::vec3 orig(x, y, z);
  55. glm::vec3 dir(x2 - x, y2 - y, z2 - z);
  56. glm::vec3 intersect;
  57. for (unsigned long i = 0; i < portals.size(); i++) {
  58. if ((glm::intersectLineTriangle(orig, dir, portals.at(i)->getVertex(0),
  59. portals.at(i)->getVertex(1),
  60. portals.at(i)->getVertex(2), intersect))
  61. || (glm::intersectLineTriangle(orig, dir, portals.at(i)->getVertex(0),
  62. portals.at(i)->getVertex(3),
  63. portals.at(i)->getVertex(1), intersect)))
  64. return portals.at(i)->getAdjoiningRoom();
  65. }
  66. return -1;
  67. }
  68. // --------------------------------------
  69. unsigned long Room::sizePortals() {
  70. return portals.size();
  71. }
  72. Portal& Room::getPortal(unsigned long index) {
  73. assert(index < portals.size());
  74. return *portals.at(index);
  75. }
  76. void Room::addPortal(Portal* p) {
  77. portals.emplace_back(p);
  78. }
  79. unsigned long Room::sizeSectors() {
  80. return sectors.size();
  81. }
  82. Sector& Room::getSector(unsigned long index) {
  83. assert(index < sectors.size());
  84. return *sectors.at(index);
  85. }
  86. void Room::addSector(Sector* s) {
  87. sectors.emplace_back(s);
  88. }
  89. unsigned long Room::sizeModels() {
  90. return models.size();
  91. }
  92. StaticModel& Room::getModel(unsigned long index) {
  93. assert(index < models.size());
  94. return *models.at(index);
  95. }
  96. void Room::addModel(StaticModel* s) {
  97. models.emplace_back(s);
  98. }
  99. unsigned long Room::sizeLights() {
  100. return lights.size();
  101. }
  102. Light& Room::getLight(unsigned long index) {
  103. assert(index < lights.size());
  104. return *lights.at(index);
  105. }
  106. void Room::addLight(Light* l) {
  107. lights.emplace_back(l);
  108. }
  109. unsigned long Room::sizeSprites() {
  110. return sprites.size();
  111. }
  112. Sprite& Room::getSprite(unsigned long index) {
  113. assert(index < sprites.size());
  114. return *sprites.at(index);
  115. }
  116. void Room::addSprite(Sprite* s) {
  117. sprites.emplace_back(s);
  118. }