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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  23. bool Room::isWall(unsigned long sector) {
  24. assert(sector < sectors.size());
  25. //! \fixme is (sector > 0) correct??
  26. return ((sector > 0) && sectors.at(sector)->isWall());
  27. }
  28. long Room::getSector(float x, float z, float* floor, float* ceiling) {
  29. assert(floor != nullptr);
  30. assert(ceiling != nullptr);
  31. long sector = getSector(x, z);
  32. if ((sector >= 0) && (sector < (long)sectors.size())) {
  33. *floor = sectors.at(sector)->getFloor();
  34. *ceiling = sectors.at(sector)->getCeiling();
  35. }
  36. return sector;
  37. }
  38. long Room::getSector(float x, float z) {
  39. long sector = (((((int)x - (int)pos[0]) / 1024) *
  40. numZSectors) + (((int)z - (int)pos[2]) / 1024));
  41. if (sector < 0)
  42. return -1;
  43. return sector;
  44. }
  45. void Room::getHeightAtPosition(float x, float* y, float z) {
  46. long sector = getSector(x, z);
  47. if ((sector >= 0) && (sector < (long)sectors.size()))
  48. *y = sectors.at(sector)->getFloor();
  49. }
  50. int Room::getAdjoiningRoom(float x, float y, float z,
  51. float x2, float y2, float z2) {
  52. glm::vec3 orig(x, y, z);
  53. glm::vec3 dir(x2 - x, y2 - y, z2 - z);
  54. glm::vec3 intersect;
  55. for (unsigned long i = 0; i < portals.size(); i++) {
  56. if ((glm::intersectLineTriangle(orig, dir, portals.at(i)->getVertex(0),
  57. portals.at(i)->getVertex(1),
  58. portals.at(i)->getVertex(2), intersect))
  59. || (glm::intersectLineTriangle(orig, dir, portals.at(i)->getVertex(0),
  60. portals.at(i)->getVertex(3),
  61. portals.at(i)->getVertex(1), intersect)))
  62. return portals.at(i)->getAdjoiningRoom();
  63. }
  64. return -1;
  65. }
  66. // --------------------------------------
  67. unsigned long Room::sizePortals() {
  68. return portals.size();
  69. }
  70. Portal& Room::getPortal(unsigned long index) {
  71. assert(index < portals.size());
  72. return *portals.at(index);
  73. }
  74. void Room::addPortal(Portal* p) {
  75. portals.emplace_back(p);
  76. }
  77. unsigned long Room::sizeSectors() {
  78. return sectors.size();
  79. }
  80. Sector& Room::getSector(unsigned long index) {
  81. assert(index < sectors.size());
  82. return *sectors.at(index);
  83. }
  84. void Room::addSector(Sector* s) {
  85. sectors.emplace_back(s);
  86. }
  87. unsigned long Room::sizeModels() {
  88. return models.size();
  89. }
  90. StaticModel& Room::getModel(unsigned long index) {
  91. assert(index < models.size());
  92. return *models.at(index);
  93. }
  94. void Room::addModel(StaticModel* s) {
  95. models.emplace_back(s);
  96. }
  97. unsigned long Room::sizeLights() {
  98. return lights.size();
  99. }
  100. Light& Room::getLight(unsigned long index) {
  101. assert(index < lights.size());
  102. return *lights.at(index);
  103. }
  104. void Room::addLight(Light* l) {
  105. lights.emplace_back(l);
  106. }
  107. unsigned long Room::sizeSprites() {
  108. return sprites.size();
  109. }
  110. Sprite& Room::getSprite(unsigned long index) {
  111. assert(index < sprites.size());
  112. return *sprites.at(index);
  113. }
  114. void Room::addSprite(Sprite* s) {
  115. sprites.emplace_back(s);
  116. }