mesh.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "mesh.h"
  2. #include "gl/buffer.h"
  3. #include "render_constants.h"
  4. #include <GL/gl.h>
  5. #include <QDebug>
  6. #include <QOpenGLFunctions_3_3_Core>
  7. #include <memory>
  8. #include <vector>
  9. namespace Render::GL {
  10. using namespace Render::GL::ComponentCount;
  11. Mesh::Mesh(const std::vector<Vertex> &vertices,
  12. const std::vector<unsigned int> &indices)
  13. : m_vertices(vertices), m_indices(indices) {}
  14. Mesh::~Mesh() = default;
  15. void Mesh::setup_buffers() {
  16. if (QOpenGLContext::currentContext() == nullptr) {
  17. qWarning() << "Mesh::setup_buffers called without current GL context; "
  18. "skipping VAO/VBO creation";
  19. return;
  20. }
  21. initializeOpenGLFunctions();
  22. m_vao = std::make_unique<VertexArray>();
  23. m_vbo = std::make_unique<Buffer>(Buffer::Type::Vertex);
  24. m_ebo = std::make_unique<Buffer>(Buffer::Type::Index);
  25. m_vao->bind();
  26. m_vbo->set_data(m_vertices);
  27. m_ebo->set_data(m_indices);
  28. std::vector<int> const layout = {Vec3, Vec3, Vec2};
  29. m_vao->add_vertexBuffer(*m_vbo, layout);
  30. m_vao->set_index_buffer(*m_ebo);
  31. m_vao->unbind();
  32. GLenum err = glGetError();
  33. if (err != GL_NO_ERROR) {
  34. qWarning() << "Mesh::setup_buffers GL error" << err;
  35. }
  36. }
  37. auto Mesh::prepare_draw(const char *caller_name) -> bool {
  38. if (!m_vao) {
  39. setup_buffers();
  40. }
  41. #ifndef NDEBUG
  42. if (QOpenGLContext::currentContext() == nullptr) {
  43. qWarning() << caller_name
  44. << "called without current GL context; skipping draw"
  45. << "indices" << m_indices.size();
  46. return false;
  47. }
  48. #endif
  49. m_vao->bind();
  50. #ifndef NDEBUG
  51. initializeOpenGLFunctions();
  52. GLenum preErr = glGetError();
  53. if (preErr != GL_NO_ERROR) {
  54. qWarning() << caller_name << "pre-draw GL error" << preErr << "vao"
  55. << (m_vao ? m_vao->id() : 0) << "indices" << m_indices.size();
  56. }
  57. #endif
  58. return true;
  59. }
  60. void Mesh::draw() {
  61. if (!prepare_draw("Mesh::draw")) {
  62. return;
  63. }
  64. glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()),
  65. GL_UNSIGNED_INT, nullptr);
  66. m_vao->unbind();
  67. #ifndef NDEBUG
  68. GLenum err = glGetError();
  69. if (err != GL_NO_ERROR) {
  70. qWarning() << "Mesh::draw GL error" << err << "indices" << m_indices.size();
  71. }
  72. #endif
  73. }
  74. void Mesh::draw_instanced(std::size_t instance_count) {
  75. if (instance_count == 0) {
  76. return;
  77. }
  78. if (!prepare_draw("Mesh::draw_instanced")) {
  79. return;
  80. }
  81. glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()),
  82. GL_UNSIGNED_INT, nullptr,
  83. static_cast<GLsizei>(instance_count));
  84. m_vao->unbind();
  85. #ifndef NDEBUG
  86. GLenum err = glGetError();
  87. if (err != GL_NO_ERROR) {
  88. qWarning() << "Mesh::draw_instanced GL error" << err << "indices"
  89. << m_indices.size() << "instances" << instance_count;
  90. }
  91. #endif
  92. }
  93. void Mesh::unbind_vao() {
  94. if (m_vao) {
  95. m_vao->unbind();
  96. }
  97. }
  98. void Mesh::draw_instanced_raw(std::size_t instance_count) {
  99. initializeOpenGLFunctions();
  100. glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()),
  101. GL_UNSIGNED_INT, nullptr,
  102. static_cast<GLsizei>(instance_count));
  103. #ifndef NDEBUG
  104. GLenum err = glGetError();
  105. if (err != GL_NO_ERROR) {
  106. qWarning() << "Mesh::draw_instanced_raw GL error" << err << "indices"
  107. << m_indices.size() << "instances" << instance_count;
  108. }
  109. #endif
  110. }
  111. auto create_quad_mesh() -> std::unique_ptr<Mesh> {
  112. std::vector<Vertex> const vertices = {
  113. {{-1.0F, -1.0F, 0.0F}, {0.0F, 0.0F, 1.0F}, {0.0F, 0.0F}},
  114. {{1.0F, -1.0F, 0.0F}, {0.0F, 0.0F, 1.0F}, {1.0F, 0.0F}},
  115. {{1.0F, 1.0F, 0.0F}, {0.0F, 0.0F, 1.0F}, {1.0F, 1.0F}},
  116. {{-1.0F, 1.0F, 0.0F}, {0.0F, 0.0F, 1.0F}, {0.0F, 1.0F}}};
  117. std::vector<unsigned int> const indices = {0, 1, 2, 2, 3, 0};
  118. return std::make_unique<Mesh>(vertices, indices);
  119. }
  120. auto create_cube_mesh() -> std::unique_ptr<Mesh> {
  121. std::vector<Vertex> const vertices = {
  122. {{-1.0F, -1.0F, 1.0F}, {0.0F, 0.0F, 1.0F}, {0.0F, 0.0F}},
  123. {{1.0F, -1.0F, 1.0F}, {0.0F, 0.0F, 1.0F}, {1.0F, 0.0F}},
  124. {{1.0F, 1.0F, 1.0F}, {0.0F, 0.0F, 1.0F}, {1.0F, 1.0F}},
  125. {{-1.0F, 1.0F, 1.0F}, {0.0F, 0.0F, 1.0F}, {0.0F, 1.0F}},
  126. {{-1.0F, -1.0F, -1.0F}, {0.0F, 0.0F, -1.0F}, {1.0F, 0.0F}},
  127. {{-1.0F, 1.0F, -1.0F}, {0.0F, 0.0F, -1.0F}, {1.0F, 1.0F}},
  128. {{1.0F, 1.0F, -1.0F}, {0.0F, 0.0F, -1.0F}, {0.0F, 1.0F}},
  129. {{1.0F, -1.0F, -1.0F}, {0.0F, 0.0F, -1.0F}, {0.0F, 0.0F}},
  130. };
  131. std::vector<unsigned int> const indices = {0, 1, 2, 2, 3, 0,
  132. 4, 5, 6, 6, 7, 4,
  133. 4, 0, 3, 3, 5, 4,
  134. 1, 7, 6, 6, 2, 1,
  135. 3, 2, 6, 6, 5, 3,
  136. 4, 7, 1, 1, 0, 4};
  137. return std::make_unique<Mesh>(vertices, indices);
  138. }
  139. auto create_plane_mesh(float width, float height,
  140. int subdivisions) -> std::unique_ptr<Mesh> {
  141. std::vector<Vertex> vertices;
  142. std::vector<unsigned int> indices;
  143. float const half_width = width * 0.5F;
  144. float const half_height = height * 0.5F;
  145. for (int z = 0; z <= subdivisions; ++z) {
  146. for (int x = 0; x <= subdivisions; ++x) {
  147. float x_pos = (x / static_cast<float>(subdivisions)) * width - half_width;
  148. float z_pos =
  149. (z / static_cast<float>(subdivisions)) * height - half_height;
  150. vertices.push_back({{x_pos, 0.0F, z_pos},
  151. {0.0F, 1.0F, 0.0F},
  152. {x / static_cast<float>(subdivisions),
  153. z / static_cast<float>(subdivisions)}});
  154. }
  155. }
  156. for (int z = 0; z < subdivisions; ++z) {
  157. for (int x = 0; x < subdivisions; ++x) {
  158. int top_left = z * (subdivisions + 1) + x;
  159. int top_right = top_left + 1;
  160. int bottom_left = (z + 1) * (subdivisions + 1) + x;
  161. int bottom_right = bottom_left + 1;
  162. indices.push_back(top_left);
  163. indices.push_back(bottom_left);
  164. indices.push_back(top_right);
  165. indices.push_back(top_right);
  166. indices.push_back(bottom_left);
  167. indices.push_back(bottom_right);
  168. }
  169. }
  170. return std::make_unique<Mesh>(vertices, indices);
  171. }
  172. } // namespace Render::GL