mesh.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include "buffer.h"
  3. #include <QOpenGLFunctions_3_3_Core>
  4. #include <array>
  5. #include <functional>
  6. #include <memory>
  7. #include <vector>
  8. namespace Render::GL {
  9. struct Vertex {
  10. std::array<float, 3> position{};
  11. std::array<float, 3> normal{};
  12. std::array<float, 2> tex_coord{};
  13. };
  14. class Mesh : protected QOpenGLFunctions_3_3_Core {
  15. public:
  16. Mesh(const std::vector<Vertex> &vertices,
  17. const std::vector<unsigned int> &indices);
  18. ~Mesh() override;
  19. void draw();
  20. [[nodiscard]] auto getVertices() const -> const std::vector<Vertex> & {
  21. return m_vertices;
  22. }
  23. [[nodiscard]] auto getIndices() const -> const std::vector<unsigned int> & {
  24. return m_indices;
  25. }
  26. [[nodiscard]] auto cloneWithFilteredIndices(
  27. const std::function<bool(unsigned int, unsigned int, unsigned int,
  28. const std::vector<Vertex> &)> &predicate) const
  29. -> std::unique_ptr<Mesh> {
  30. if (!predicate) {
  31. return nullptr;
  32. }
  33. std::vector<unsigned int> filtered;
  34. filtered.reserve(m_indices.size());
  35. for (std::size_t i = 0; i + 2 < m_indices.size(); i += 3) {
  36. unsigned int a = m_indices[i];
  37. unsigned int b = m_indices[i + 1];
  38. unsigned int c = m_indices[i + 2];
  39. if (!predicate(a, b, c, m_vertices)) {
  40. filtered.push_back(a);
  41. filtered.push_back(b);
  42. filtered.push_back(c);
  43. }
  44. }
  45. if (filtered.empty()) {
  46. return nullptr;
  47. }
  48. return std::make_unique<Mesh>(m_vertices, filtered);
  49. }
  50. private:
  51. std::vector<Vertex> m_vertices;
  52. std::vector<unsigned int> m_indices;
  53. std::unique_ptr<VertexArray> m_vao;
  54. std::unique_ptr<Buffer> m_vbo;
  55. std::unique_ptr<Buffer> m_ebo;
  56. void setupBuffers();
  57. };
  58. auto createQuadMesh() -> Mesh *;
  59. auto createCubeMesh() -> Mesh *;
  60. auto createPlaneMesh(float width, float height, int subdivisions = 1) -> Mesh *;
  61. } // namespace Render::GL