Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Room.cpp 3.9KB

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