]> git.localhorst.tv Git - blank.git/blob - src/graphics/PrimitiveMesh.hpp
577321a73e8204e281a265c8402d193900445cbe
[blank.git] / src / graphics / PrimitiveMesh.hpp
1 #ifndef BLANK_GRAPHICS_PRIMITIVEMESH_HPP_
2 #define BLANK_GRAPHICS_PRIMITIVEMESH_HPP_
3
4 #include "VertexArray.hpp"
5
6 #include <vector>
7 #include <GL/glew.h>
8 #include <glm/glm.hpp>
9
10
11 namespace blank {
12
13 struct AABB;
14
15 class PrimitiveMesh {
16
17 public:
18         using Position = glm::vec3;
19         using Color = glm::tvec4<unsigned char>;
20         using Index = unsigned short;
21
22         using Positions = std::vector<Position>;
23         using Colors = std::vector<Color>;
24         using Indices = std::vector<Index>;
25
26         enum Attribute {
27                 ATTRIB_VERTEX,
28                 ATTRIB_COLOR,
29                 ATTRIB_INDEX,
30                 ATTRIB_COUNT,
31         };
32
33         struct Buffer {
34
35                 Positions vertices;
36                 Colors colors;
37                 Indices indices;
38
39                 void Clear() noexcept {
40                         vertices.clear();
41                         colors.clear();
42                         indices.clear();
43                 }
44
45                 void Reserve(size_t p, size_t i) {
46                         vertices.reserve(p);
47                         colors.reserve(p);
48                         indices.reserve(i);
49                 }
50
51                 void FillRect(
52                         float w, float h,
53                         const Color &color = Color(0),
54                         const glm::vec2 &pivot = glm::vec2(0.0f)
55                 );
56
57                 void OutlineBox(
58                         const AABB &,
59                         const Color &color = Color(0)
60                 );
61
62         };
63
64         using VAO = VertexArray<ATTRIB_COUNT>;
65
66 public:
67         void Update(const Buffer &) noexcept;
68
69         bool Empty() const noexcept {
70                 return vao.Empty();
71         }
72
73         void DrawLines() const noexcept;
74         void DrawTriangles() const noexcept {
75                 vao.DrawTriangleElements();
76         }
77
78 private:
79         VAO vao;
80
81 };
82
83 }
84
85 #endif