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.

RoomMesh.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. * \file src/RoomMesh.cpp
  3. * \brief World Room Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Mesh.h"
  9. #include "TextureManager.h"
  10. #include "system/Window.h"
  11. #include "RoomMesh.h"
  12. RoomMesh::RoomMesh(const std::vector<RoomVertexTR2>& vert,
  13. const std::vector<IndexedRectangle>& rect,
  14. const std::vector<IndexedRectangle>& tri) {
  15. for (auto& t : rect) {
  16. indices.push_back(0);
  17. vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  18. vertices.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  19. vertices.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  20. vertices.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
  21. textures.push_back(t.texture);
  22. }
  23. for (auto& t : tri) {
  24. indices.push_back(1);
  25. vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  26. vertices.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  27. vertices.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  28. textures.push_back(t.texture);
  29. }
  30. }
  31. void RoomMesh::prepare() {
  32. std::vector<unsigned short> ind;
  33. std::vector<glm::vec3> vert;
  34. std::vector<unsigned int> tex;
  35. std::map<PackedVertex, unsigned short> vertexMap;
  36. int vertIndex = 0;
  37. for (int i = 0; i < indices.size(); i++) {
  38. unsigned int texture = getTextureManager().getTile(textures.at(i)).getTexture();
  39. for (int v = 0; v < ((indices.at(i) == 0) ? 4 : 3); v++) {
  40. glm::vec2 uv = getTextureManager().getTile(textures.at(i)).getUV(v);
  41. PackedVertex p(vertices.at(vertIndex + v), uv, texture);
  42. unsigned short s;
  43. if (findSimilarVertex(p, vertexMap, s)) {
  44. ind.push_back(s); // Vertex already cached
  45. } else {
  46. vertexMap[p] = vert.size();
  47. ind.push_back(vert.size());
  48. vert.push_back(p.pos);
  49. uvs.push_back(p.uv);
  50. tex.push_back(p.tex);
  51. }
  52. }
  53. if (indices.at(i) == 0) {
  54. ind.push_back(ind.at(ind.size() - 2));
  55. ind.push_back(ind.at(ind.size() - 5));
  56. }
  57. vertIndex += (indices.at(i) == 0) ? 4 : 3;
  58. }
  59. assert((ind.size() % 3) == 0);
  60. indices = std::move(ind);
  61. vertices = std::move(vert);
  62. textures = std::move(tex);
  63. }
  64. void RoomMesh::display(glm::mat4 MVP) {
  65. if (indices.size() > 0) {
  66. unsigned int indexStart = 0;
  67. unsigned int indexPos = 1;
  68. unsigned int texture = textures.at(indices.at(0));
  69. while ((indexStart != indexPos) && (indexPos < indices.size())) {
  70. while ((indexPos < indices.size()) && (textures.at(indices.at(indexPos)) == texture))
  71. indexPos++;
  72. std::vector<unsigned short> ind(indices.begin() + indexStart, indices.begin() + indexPos);
  73. Window::drawGL(vertices, uvs, ind, MVP, texture);
  74. if (indexPos < indices.size()) {
  75. indexStart = indexPos;
  76. indexPos += 1;
  77. texture = textures.at(indices.at(indexStart));
  78. }
  79. }
  80. }
  81. }