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