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.

RoomMesh.cpp 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = ind;
  61. vertices = vert;
  62. textures = tex;
  63. }
  64. void RoomMesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {
  65. glm::mat4 MVP = projection * view * model;
  66. if (indices.size() > 0) {
  67. unsigned int indexStart = 0;
  68. unsigned int indexPos = 1;
  69. unsigned int texture = textures.at(indices.at(0));
  70. while ((indexStart != indexPos) && (indexPos < indices.size())) {
  71. while ((indexPos < indices.size()) && (textures.at(indices.at(indexPos)) == texture))
  72. indexPos++;
  73. std::vector<unsigned short> ind(indices.begin() + indexStart, indices.begin() + indexPos);
  74. Window::drawGL(vertices, uvs, ind, MVP, texture);
  75. if (indexPos < indices.size()) {
  76. indexStart = indexPos;
  77. indexPos += 1;
  78. texture = textures.at(indices.at(indexStart));
  79. }
  80. }
  81. }
  82. }