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

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