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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*!
  2. * \file src/Mesh.cpp
  3. * \brief Textured/Colored 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(t.index);
  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(t.index);
  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 x = 0; x < ((indicesBuff.at(i) == 0) ? 4 : 3); x++) {
  55. int v = x;
  56. if (v == 0)
  57. v = 2;
  58. else if (v == 2)
  59. v = 0;
  60. ind.push_back(vert.size());
  61. vert.push_back(verticesBuff.at(vertIndex + v));
  62. uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
  63. tex.push_back(texture);
  64. }
  65. if (indicesBuff.at(i) == 0) {
  66. ind.push_back(ind.at(ind.size() - 4));
  67. ind.push_back(ind.at(ind.size() - 3));
  68. }
  69. vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
  70. }
  71. assertEqual(ind.size() % 3, 0);
  72. assertEqual(vert.size(), tex.size());
  73. assertEqual(vert.size(), uvBuff.size());
  74. indicesBuff = std::move(ind);
  75. vertices.bufferData(vert);
  76. uvs.bufferData(uvBuff);
  77. texturesBuff = std::move(tex);
  78. std::vector<unsigned short> indCol;
  79. std::vector<glm::vec3> vertCol;
  80. std::vector<glm::vec3> cols;
  81. vertIndex = 0;
  82. for (int i = 0; i < indicesColorBuff.size(); i++) {
  83. for (int x = 0; x < ((indicesColorBuff.at(i) == 0) ? 4 : 3); x++) {
  84. int v = x;
  85. if (v == 0)
  86. v = 2;
  87. else if (v == 2)
  88. v = 0;
  89. indCol.push_back(vertCol.size());
  90. vertCol.push_back(verticesColorBuff.at(vertIndex + v));
  91. glm::vec4 c = TextureManager::getPalette(colorsBuff.at(i));
  92. cols.push_back(glm::vec3(c.x, c.y, c.z));
  93. }
  94. if (indicesColorBuff.at(i) == 0) {
  95. indCol.push_back(indCol.at(indCol.size() - 4));
  96. indCol.push_back(indCol.at(indCol.size() - 3));
  97. }
  98. vertIndex += (indicesColorBuff.at(i) == 0) ? 4 : 3;
  99. }
  100. assertEqual(indCol.size() % 3, 0);
  101. assertEqual(vertCol.size(), cols.size());
  102. indicesColor.bufferData(indCol);
  103. verticesColor.bufferData(vertCol);
  104. colors.bufferData(cols);
  105. verticesBuff.clear();
  106. indicesColorBuff.clear();
  107. verticesColorBuff.clear();
  108. colorsBuff.clear();
  109. }
  110. void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
  111. if (indicesBuff.size() > 0) {
  112. unsigned int indexStart = 0;
  113. unsigned int indexPos = 1;
  114. unsigned int texture = texturesBuff.at(indicesBuff.at(0));
  115. while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
  116. while ((indexPos < indicesBuff.size())
  117. && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
  118. indexPos++;
  119. }
  120. std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
  121. indicesBuff.begin() + indexPos);
  122. indices.bufferData(ind);
  123. Shader::drawGL(vertices, uvs, indices, texture, MVP, TextureStorage::GAME, shaderTexture);
  124. if (indexPos < indicesBuff.size()) {
  125. indexStart = indexPos;
  126. indexPos += 1;
  127. texture = texturesBuff.at(indicesBuff.at(indexStart));
  128. }
  129. }
  130. }
  131. if (indicesColor.getSize() > 0)
  132. Shader::drawGL(verticesColor, colors, indicesColor, MVP, GL_TRIANGLES, shaderTexture);
  133. }