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

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