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.

RoomData.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*!
  2. * \file src/RoomData.cpp
  3. * \brief World Room Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Render.h"
  9. #include "SkeletalModel.h"
  10. #include "World.h"
  11. #include "RoomData.h"
  12. BoundingBox::BoundingBox() {
  13. a[0] = a[1] = a[2] = 0;
  14. b[0] = b[1] = b[2] = 0;
  15. }
  16. void BoundingBox::getBoundingBox(float box[2][3]) {
  17. box[0][0] = a[0];
  18. box[1][0] = b[0];
  19. box[0][1] = a[1];
  20. box[1][1] = b[1];
  21. box[0][2] = a[2];
  22. box[1][2] = b[2];
  23. }
  24. void BoundingBox::setBoundingBox(float min[3], float max[3]) {
  25. a[0] = min[0];
  26. b[0] = max[0];
  27. a[1] = min[1];
  28. b[1] = max[1];
  29. a[2] = min[2];
  30. b[2] = max[2];
  31. }
  32. bool BoundingBox::inBox(float x, float y, float z) {
  33. return ((y > a[1]) && (y < b[1]) && inBoxPlane(x, z));
  34. }
  35. bool BoundingBox::inBoxPlane(float x, float z) {
  36. return ((x > a[0]) && (x < b[0])
  37. && (z > a[2]) && (z < b[2]));
  38. }
  39. void BoundingBox::display(bool points, const unsigned char c1[4], const unsigned char c2[4]) {
  40. // Bind before entering now
  41. //glBindTexture(GL_TEXTURE_2D, 1);
  42. glPointSize(4.0);
  43. //glLineWidth(1.25);
  44. //! \fixme Need to make custom color key for this
  45. glColor3ubv(c1);
  46. glBegin(GL_POINTS);
  47. glVertex3f(b[0], b[1], b[2]);
  48. glVertex3f(a[0], a[1], a[2]);
  49. if (points) {
  50. glVertex3f(b[0], a[1], b[2]);
  51. glVertex3f(a[0], b[1], b[2]);
  52. glVertex3f(b[0], b[1], a[2]);
  53. glVertex3f(a[0], a[1], b[2]);
  54. glVertex3f(a[0], b[1], a[2]);
  55. glVertex3f(b[0], a[1], a[2]);
  56. }
  57. glEnd();
  58. glColor3ubv(c2);
  59. glBegin(GL_LINES);
  60. // max, top quad
  61. glVertex3f(b[0], b[1], b[2]);
  62. glVertex3f(b[0], a[1], b[2]);
  63. glVertex3f(b[0], b[1], b[2]);
  64. glVertex3f(a[0], b[1], b[2]);
  65. glVertex3f(b[0], b[1], b[2]);
  66. glVertex3f(b[0], b[1], a[2]);
  67. // max-min, vertical quads
  68. glVertex3f(a[0], b[1], b[2]);
  69. glVertex3f(a[0], b[1], a[2]);
  70. glVertex3f(b[0], a[1], b[2]);
  71. glVertex3f(b[0], a[1], a[2]);
  72. glVertex3f(b[0], a[1], b[2]);
  73. glVertex3f(a[0], a[1], b[2]);
  74. // min-max, vertical quads
  75. glVertex3f(b[0], b[1], a[2]);
  76. glVertex3f(b[0], a[1], a[2]);
  77. glVertex3f(b[0], b[1], a[2]);
  78. glVertex3f(a[0], b[1], a[2]);
  79. glVertex3f(a[0], b[1], b[2]);
  80. glVertex3f(a[0], a[1], b[2]);
  81. // min, bottom quad
  82. glVertex3f(a[0], a[1], a[2]);
  83. glVertex3f(a[0], b[1], a[2]);
  84. glVertex3f(a[0], a[1], a[2]);
  85. glVertex3f(b[0], a[1], a[2]);
  86. glVertex3f(a[0], a[1], a[2]);
  87. glVertex3f(a[0], a[1], b[2]);
  88. glEnd();
  89. glPointSize(1.0);
  90. //glLineWidth(1.0);
  91. }
  92. // ----------------------------------------------------------------------------
  93. Light::Light(TombRaider& tr, unsigned int room, unsigned int index) {
  94. unsigned int lightFlags, lightType;
  95. tr.getRoomLight(room, index, pos, color,
  96. dir, &att, &cutoff, &lightType, &lightFlags);
  97. switch (lightType) {
  98. case tombraiderLight_typeDirectional:
  99. type = Light::typeDirectional;
  100. break;
  101. case tombraiderLight_typeSpot:
  102. type = Light::typeSpot;
  103. break;
  104. case tombraiderLight_typePoint:
  105. default:
  106. type = Light::typePoint;
  107. }
  108. //! \todo Light flags?
  109. }
  110. void Light::getPos(float p[4]) {
  111. p[0] = pos[0];
  112. p[1] = pos[1];
  113. p[2] = pos[2];
  114. p[3] = pos[3];
  115. }
  116. void Light::getDir(float d[3]) {
  117. d[0] = dir[0];
  118. d[1] = dir[1];
  119. d[2] = dir[2];
  120. }
  121. float Light::getAtt() {
  122. return att;
  123. }
  124. void Light::getColor(float c[4]) {
  125. c[0] = color[0];
  126. c[1] = color[1];
  127. c[2] = color[2];
  128. c[3] = color[3];
  129. }
  130. float Light::getCutoff() {
  131. return cutoff;
  132. }
  133. Light::LightType Light::getType() {
  134. return type;
  135. }
  136. // ----------------------------------------------------------------------------
  137. StaticModel::StaticModel(TombRaider& tr, unsigned int room, unsigned int i) {
  138. tr.getRoomModel(room, i, &index, pos, &yaw);
  139. }
  140. void StaticModel::display() {
  141. StaticMesh& mesh = getWorld().getStaticMesh(index);
  142. if (!getRender().isVisible(pos[0], pos[1], pos[2], mesh.getRadius()))
  143. return;
  144. glPushMatrix();
  145. glTranslated(pos[0], pos[1], pos[2]);
  146. glRotated(yaw, 0, 1, 0);
  147. mesh.display();
  148. glPopMatrix();
  149. }
  150. bool StaticModel::operator<(const StaticModel& other) {
  151. float distA, distB;
  152. distA = getRender().getDistToSphereFromNear(pos[0],
  153. pos[1], pos[2], 128.0f);
  154. distB = getRender().getDistToSphereFromNear(other.pos[0],
  155. other.pos[1], other.pos[2], 128.0f);
  156. return (distA < distB);
  157. }
  158. bool StaticModel::compare(StaticModel* a, StaticModel* b) {
  159. return (*b) < (*a);
  160. }
  161. // ----------------------------------------------------------------------------
  162. Portal::Portal(TombRaider& tr, unsigned int room, unsigned int index, Matrix& transform) {
  163. float portalVertices[12];
  164. tr.getRoomPortal(room, index, &adjoiningRoom, normal, portalVertices);
  165. for (unsigned int j = 0; j < 4; ++j) {
  166. vertices[j][0] = portalVertices[j * 3];
  167. vertices[j][1] = portalVertices[j * 3 + 1];
  168. vertices[j][2] = portalVertices[j * 3 + 2];
  169. // Relative coors in vis portals
  170. transform.multiply3v(vertices[j], vertices[j]);
  171. }
  172. }
  173. Portal::Portal(float vert[4][3], float norm[3], int adj) {
  174. for (unsigned int i = 0; i < 4; i++) {
  175. for (unsigned int j = 0; j < 3; j++) {
  176. vertices[i][j] = vert[i][j];
  177. }
  178. if (i < 3) {
  179. normal[i] = norm[i];
  180. }
  181. }
  182. adjoiningRoom = adj;
  183. }
  184. void Portal::getVertices(float vert[4][3]) {
  185. for (unsigned int i = 0; i < 4; i++) {
  186. for (unsigned int j = 0; j < 3; j++) {
  187. vert[i][j] = vertices[i][j];
  188. }
  189. }
  190. }
  191. int Portal::getAdjoiningRoom() {
  192. return adjoiningRoom;
  193. }
  194. // ----------------------------------------------------------------------------
  195. Sector::Sector(TombRaider& tr, unsigned int room, unsigned int index) {
  196. unsigned int sectorFlags;
  197. int floorDataIndex, boxIndex, roomBelow, roomAbove;
  198. tr.getRoomSector(room, index, &sectorFlags,
  199. &ceiling, &floor, &floorDataIndex, &boxIndex,
  200. &roomBelow, &roomAbove);
  201. wall = (sectorFlags & tombraiderSector_wall);
  202. }
  203. float Sector::getFloor() {
  204. return floor;
  205. }
  206. float Sector::getCeiling() {
  207. return ceiling;
  208. }
  209. bool Sector::isWall() {
  210. return wall;
  211. }