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.

Mesh.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. * \file src/Mesh.cpp
  3. * \brief OpenGL Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "TextureManager.h"
  9. #include "Mesh.h"
  10. Mesh::Mesh(const std::vector<glm::vec3>& vert,
  11. const std::vector<IndexedRectangle>& rect,
  12. const std::vector<IndexedRectangle>& tri,
  13. const std::vector<IndexedColoredRectangle>& coloredRect,
  14. const std::vector<IndexedColoredRectangle>& coloredTri) {
  15. for (auto& t : rect) {
  16. indicesBuff.push_back(0);
  17. verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  18. verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  19. verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  20. verticesBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
  21. texturesBuff.push_back(t.texture);
  22. }
  23. for (auto& t : tri) {
  24. indicesBuff.push_back(1);
  25. verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  26. verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  27. verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  28. texturesBuff.push_back(t.texture);
  29. }
  30. for (auto& t : coloredRect) {
  31. indicesColorBuff.push_back(0);
  32. verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  33. verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  34. verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  35. verticesColorBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
  36. colorsBuff.push_back(glm::vec3(t.r, t.g, t.b));
  37. }
  38. for (auto& t : coloredTri) {
  39. indicesColorBuff.push_back(1);
  40. verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  41. verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  42. verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  43. colorsBuff.push_back(glm::vec3(t.r, t.g, t.b));
  44. }
  45. }
  46. void Mesh::prepare() {
  47. std::vector<unsigned short> ind;
  48. std::vector<glm::vec3> vert;
  49. std::vector<glm::vec2> uvBuff;
  50. std::vector<unsigned int> tex;
  51. int vertIndex = 0;
  52. for (int i = 0; i < indicesBuff.size(); i++) {
  53. unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
  54. for (int v = 0; v < ((indicesBuff.at(i) == 0) ? 4 : 3); v++) {
  55. ind.push_back(vert.size());
  56. vert.push_back(verticesBuff.at(vertIndex + v));
  57. uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
  58. tex.push_back(texture);
  59. }
  60. if (indicesBuff.at(i) == 0) {
  61. ind.push_back(ind.at(ind.size() - 2));
  62. ind.push_back(ind.at(ind.size() - 5));
  63. }
  64. vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
  65. }
  66. assert((ind.size() % 3) == 0);
  67. assert(vert.size() == tex.size());
  68. assert(vert.size() == uvBuff.size());
  69. indicesBuff = std::move(ind);
  70. vertices.bufferData(vert);
  71. uvs.bufferData(uvBuff);
  72. texturesBuff = std::move(tex);
  73. std::vector<unsigned short> indCol;
  74. std::vector<glm::vec3> vertCol;
  75. std::vector<glm::vec3> cols;
  76. vertIndex = 0;
  77. for (int i = 0; i < indicesColorBuff.size(); i++) {
  78. for (int v = 0; v < ((indicesColorBuff.at(i) == 0) ? 4 : 3); v++) {
  79. indCol.push_back(vertCol.size());
  80. vertCol.push_back(verticesColorBuff.at(vertIndex + v));
  81. cols.push_back(colorsBuff.at(i));
  82. }
  83. if (indicesColorBuff.at(i) == 0) {
  84. indCol.push_back(indCol.at(indCol.size() - 2));
  85. indCol.push_back(indCol.at(indCol.size() - 5));
  86. }
  87. vertIndex += (indicesColorBuff.at(i) == 0) ? 4 : 3;
  88. }
  89. assert((indCol.size() % 3) == 0);
  90. assert(vertCol.size() == cols.size());
  91. indicesColor.bufferData(indCol);
  92. verticesColor.bufferData(vertCol);
  93. colors.bufferData(cols);
  94. verticesBuff.clear();
  95. indicesColorBuff.clear();
  96. verticesColorBuff.clear();
  97. colorsBuff.clear();
  98. }
  99. void Mesh::display(glm::mat4 MVP) {
  100. if (indicesBuff.size() > 0) {
  101. unsigned int indexStart = 0;
  102. unsigned int indexPos = 1;
  103. unsigned int texture = texturesBuff.at(indicesBuff.at(0));
  104. while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
  105. while ((indexPos < indicesBuff.size())
  106. && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
  107. indexPos++;
  108. }
  109. std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
  110. indicesBuff.begin() + indexPos);
  111. indices.bufferData(ind);
  112. Shader::drawGL(vertices, uvs, indices, texture, MVP);
  113. if (indexPos < indicesBuff.size()) {
  114. indexStart = indexPos;
  115. indexPos += 1;
  116. texture = texturesBuff.at(indicesBuff.at(indexStart));
  117. }
  118. }
  119. }
  120. if (indicesColor.getSize() > 0)
  121. Shader::drawGL(verticesColor, colors, indicesColor, MVP);
  122. }