mesh.cpp 3.8 KB

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