2
0

mesh.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "mesh.h"
  2. #include <QOpenGLFunctions_3_3_Core>
  3. namespace Render::GL {
  4. Mesh::Mesh(const std::vector<Vertex> &vertices,
  5. const std::vector<unsigned int> &indices)
  6. : m_vertices(vertices), m_indices(indices) {}
  7. Mesh::~Mesh() = default;
  8. void Mesh::setupBuffers() {
  9. initializeOpenGLFunctions();
  10. m_vao = std::make_unique<VertexArray>();
  11. m_vbo = std::make_unique<Buffer>(Buffer::Type::Vertex);
  12. m_ebo = std::make_unique<Buffer>(Buffer::Type::Index);
  13. m_vao->bind();
  14. m_vbo->setData(m_vertices);
  15. m_ebo->setData(m_indices);
  16. std::vector<int> layout = {3, 3, 2};
  17. m_vao->addVertexBuffer(*m_vbo, layout);
  18. m_vao->setIndexBuffer(*m_ebo);
  19. m_vao->unbind();
  20. }
  21. void Mesh::draw() {
  22. if (!m_vao) {
  23. setupBuffers();
  24. }
  25. m_vao->bind();
  26. initializeOpenGLFunctions();
  27. glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()),
  28. GL_UNSIGNED_INT, nullptr);
  29. m_vao->unbind();
  30. }
  31. Mesh *createQuadMesh() {
  32. std::vector<Vertex> vertices = {
  33. {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}},
  34. {{1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
  35. {{1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
  36. {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}}};
  37. std::vector<unsigned int> indices = {0, 1, 2, 2, 3, 0};
  38. return new Mesh(vertices, indices);
  39. }
  40. Mesh *createCubeMesh() {
  41. std::vector<Vertex> vertices = {
  42. {{-1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}},
  43. {{1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
  44. {{1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
  45. {{-1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
  46. {{-1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f}},
  47. {{-1.0f, 1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 1.0f}},
  48. {{1.0f, 1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f}},
  49. {{1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}},
  50. };
  51. std::vector<unsigned int> indices = {0, 1, 2, 2, 3, 0,
  52. 4, 5, 6, 6, 7, 4,
  53. 4, 0, 3, 3, 5, 4,
  54. 1, 7, 6, 6, 2, 1,
  55. 3, 2, 6, 6, 5, 3,
  56. 4, 7, 1, 1, 0, 4};
  57. return new Mesh(vertices, indices);
  58. }
  59. Mesh *createPlaneMesh(float width, float height, int subdivisions) {
  60. std::vector<Vertex> vertices;
  61. std::vector<unsigned int> indices;
  62. float halfWidth = width * 0.5f;
  63. float halfHeight = height * 0.5f;
  64. for (int z = 0; z <= subdivisions; ++z) {
  65. for (int x = 0; x <= subdivisions; ++x) {
  66. float xPos = (x / static_cast<float>(subdivisions)) * width - halfWidth;
  67. float zPos = (z / static_cast<float>(subdivisions)) * height - halfHeight;
  68. vertices.push_back({{xPos, 0.0f, zPos},
  69. {0.0f, 1.0f, 0.0f},
  70. {x / static_cast<float>(subdivisions),
  71. z / static_cast<float>(subdivisions)}});
  72. }
  73. }
  74. for (int z = 0; z < subdivisions; ++z) {
  75. for (int x = 0; x < subdivisions; ++x) {
  76. int topLeft = z * (subdivisions + 1) + x;
  77. int topRight = topLeft + 1;
  78. int bottomLeft = (z + 1) * (subdivisions + 1) + x;
  79. int bottomRight = bottomLeft + 1;
  80. indices.push_back(topLeft);
  81. indices.push_back(bottomLeft);
  82. indices.push_back(topRight);
  83. indices.push_back(topRight);
  84. indices.push_back(bottomLeft);
  85. indices.push_back(bottomRight);
  86. }
  87. }
  88. return new Mesh(vertices, indices);
  89. }
  90. } // namespace Render::GL