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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. void Room::prepare() {
  21. mesh->prepare();
  22. }
  23. /*
  24. void Room::display(bool alpha) {
  25. glPushMatrix();
  26. //LightingSetup();
  27. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  28. if ((!alpha) && Render::getMode() == RenderMode::Wireframe) {
  29. glLineWidth(2.0);
  30. glColor3ubv(RED);
  31. for (unsigned int i = 0; i < sizePortals(); i++) {
  32. Portal& portal = getPortal(i);
  33. float vertices[4][3];
  34. portal.getVertices(vertices);
  35. glBegin(GL_LINE_LOOP);
  36. glVertex3fv(vertices[0]);
  37. glVertex3fv(vertices[1]);
  38. glVertex3fv(vertices[2]);
  39. glVertex3fv(vertices[3]);
  40. glEnd();
  41. }
  42. glLineWidth(1.0);
  43. bbox.display(true, RED, GREEN);
  44. }
  45. glTranslated(pos[0], pos[1], pos[2]);
  46. // Reset since GL_MODULATE used, reset to WHITE
  47. glColor3ubv(WHITE);
  48. switch (Render::getMode()) {
  49. case RenderMode::Wireframe:
  50. mesh.mMode = Mesh::MeshModeWireframe;
  51. break;
  52. case RenderMode::Solid:
  53. mesh.mMode = Mesh::MeshModeSolid;
  54. break;
  55. default:
  56. mesh.mMode = Mesh::MeshModeTexture;
  57. break;
  58. }
  59. if (alpha)
  60. mesh.drawAlpha();
  61. else
  62. mesh.drawSolid();
  63. glPopMatrix();
  64. // Draw other room meshes and sprites
  65. if (alpha || (Render::getMode() == RenderMode::Wireframe)
  66. || (Render::getMode() == RenderMode::Solid)) {
  67. //sortModels(); // TODO
  68. for (unsigned int i = 0; i < sizeModels(); i++)
  69. getModel(i).display();
  70. for (unsigned int i = 0; i < sizeSprites(); i++)
  71. getSprite(i).display();
  72. }
  73. }
  74. */
  75. bool Room::isWall(unsigned long sector) {
  76. assert(sector < sectors.size());
  77. //! \fixme is (sector > 0) correct??
  78. return ((sector > 0) && sectors.at(sector)->isWall());
  79. }
  80. long Room::getSector(float x, float z, float* floor, float* ceiling) {
  81. assert(floor != nullptr);
  82. assert(ceiling != nullptr);
  83. long sector = getSector(x, z);
  84. if ((sector >= 0) && (sector < (long)sectors.size())) {
  85. *floor = sectors.at(sector)->getFloor();
  86. *ceiling = sectors.at(sector)->getCeiling();
  87. }
  88. return sector;
  89. }
  90. long Room::getSector(float x, float z) {
  91. long sector = (((((int)x - (int)pos[0]) / 1024) *
  92. numZSectors) + (((int)z - (int)pos[2]) / 1024));
  93. if (sector < 0)
  94. return -1;
  95. return sector;
  96. }
  97. void Room::getHeightAtPosition(float x, float* y, float z) {
  98. long sector = getSector(x, z);
  99. if ((sector >= 0) && (sector < (long)sectors.size()))
  100. *y = sectors.at(sector)->getFloor();
  101. }
  102. int Room::getAdjoiningRoom(float x, float y, float z,
  103. float x2, float y2, float z2) {
  104. float vertices[4][3];
  105. glm::vec3 orig(x, y, z);
  106. glm::vec3 dir(x2 - x, y2 - y, z2 - z);
  107. glm::vec3 intersect;
  108. glm::vec3 verts[4];
  109. for (unsigned long i = 0; i < portals.size(); i++) {
  110. portals.at(i)->getVertices(vertices);
  111. verts[0] = glm::vec3(vertices[0][0], vertices[0][1], vertices[0][2]);
  112. verts[1] = glm::vec3(vertices[1][0], vertices[1][1], vertices[1][2]);
  113. verts[2] = glm::vec3(vertices[2][0], vertices[2][1], vertices[2][2]);
  114. verts[3] = glm::vec3(vertices[3][0], vertices[3][1], vertices[3][2]);
  115. if ((glm::intersectLineTriangle(orig, dir, verts[0], verts[1], verts[2], intersect))
  116. || (glm::intersectLineTriangle(orig, dir, verts[0], verts[3], verts[1], intersect)))
  117. return portals.at(i)->getAdjoiningRoom();
  118. }
  119. return -1;
  120. }
  121. unsigned long Room::sizeAdjacentRooms() {
  122. return adjacentRooms.size();
  123. }
  124. long Room::getAdjacentRoom(unsigned long index) {
  125. assert(index < adjacentRooms.size());
  126. return adjacentRooms.at(index);
  127. }
  128. void Room::addAdjacentRoom(long r) {
  129. adjacentRooms.emplace_back(r);
  130. }
  131. unsigned long Room::sizePortals() {
  132. return portals.size();
  133. }
  134. Portal& Room::getPortal(unsigned long index) {
  135. assert(index < portals.size());
  136. return *portals.at(index);
  137. }
  138. void Room::addPortal(Portal* p) {
  139. portals.emplace_back(p);
  140. }
  141. unsigned long Room::sizeSectors() {
  142. return sectors.size();
  143. }
  144. Sector& Room::getSector(unsigned long index) {
  145. assert(index < sectors.size());
  146. return *sectors.at(index);
  147. }
  148. void Room::addSector(Sector* s) {
  149. sectors.emplace_back(s);
  150. }
  151. unsigned long Room::sizeModels() {
  152. return models.size();
  153. }
  154. StaticModel& Room::getModel(unsigned long index) {
  155. assert(index < models.size());
  156. return *models.at(index);
  157. }
  158. void Room::addModel(StaticModel* s) {
  159. models.emplace_back(s);
  160. }
  161. unsigned long Room::sizeLights() {
  162. return lights.size();
  163. }
  164. Light& Room::getLight(unsigned long index) {
  165. assert(index < lights.size());
  166. return *lights.at(index);
  167. }
  168. void Room::addLight(Light* l) {
  169. lights.emplace_back(l);
  170. }
  171. unsigned long Room::sizeSprites() {
  172. return sprites.size();
  173. }
  174. Sprite& Room::getSprite(unsigned long index) {
  175. assert(index < sprites.size());
  176. return *sprites.at(index);
  177. }
  178. void Room::addSprite(Sprite* s) {
  179. sprites.emplace_back(s);
  180. }