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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. float vertices[4][3];
  53. glm::vec3 orig(x, y, z);
  54. glm::vec3 dir(x2 - x, y2 - y, z2 - z);
  55. glm::vec3 intersect;
  56. glm::vec3 verts[4];
  57. for (unsigned long i = 0; i < portals.size(); i++) {
  58. portals.at(i)->getVertices(vertices);
  59. verts[0] = glm::vec3(vertices[0][0], vertices[0][1], vertices[0][2]);
  60. verts[1] = glm::vec3(vertices[1][0], vertices[1][1], vertices[1][2]);
  61. verts[2] = glm::vec3(vertices[2][0], vertices[2][1], vertices[2][2]);
  62. verts[3] = glm::vec3(vertices[3][0], vertices[3][1], vertices[3][2]);
  63. if ((glm::intersectLineTriangle(orig, dir, verts[0], verts[1], verts[2], intersect))
  64. || (glm::intersectLineTriangle(orig, dir, verts[0], verts[3], verts[1], intersect)))
  65. return portals.at(i)->getAdjoiningRoom();
  66. }
  67. return -1;
  68. }
  69. // --------------------------------------
  70. unsigned long Room::sizePortals() {
  71. return portals.size();
  72. }
  73. Portal& Room::getPortal(unsigned long index) {
  74. assert(index < portals.size());
  75. return *portals.at(index);
  76. }
  77. void Room::addPortal(Portal* p) {
  78. portals.emplace_back(p);
  79. }
  80. unsigned long Room::sizeSectors() {
  81. return sectors.size();
  82. }
  83. Sector& Room::getSector(unsigned long index) {
  84. assert(index < sectors.size());
  85. return *sectors.at(index);
  86. }
  87. void Room::addSector(Sector* s) {
  88. sectors.emplace_back(s);
  89. }
  90. unsigned long Room::sizeModels() {
  91. return models.size();
  92. }
  93. StaticModel& Room::getModel(unsigned long index) {
  94. assert(index < models.size());
  95. return *models.at(index);
  96. }
  97. void Room::addModel(StaticModel* s) {
  98. models.emplace_back(s);
  99. }
  100. unsigned long Room::sizeLights() {
  101. return lights.size();
  102. }
  103. Light& Room::getLight(unsigned long index) {
  104. assert(index < lights.size());
  105. return *lights.at(index);
  106. }
  107. void Room::addLight(Light* l) {
  108. lights.emplace_back(l);
  109. }
  110. unsigned long Room::sizeSprites() {
  111. return sprites.size();
  112. }
  113. Sprite& Room::getSprite(unsigned long index) {
  114. assert(index < sprites.size());
  115. return *sprites.at(index);
  116. }
  117. void Room::addSprite(Sprite* s) {
  118. sprites.emplace_back(s);
  119. }