]> git.localhorst.tv Git - gong.git/blob - src/graphics/SpriteMesh.hpp
code, assets, and other stuff stolen from blank
[gong.git] / src / graphics / SpriteMesh.hpp
1 #ifndef GONG_GRPAHICS_SPRITEMESH_HPP_
2 #define GONG_GRPAHICS_SPRITEMESH_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 graphics {
13
14 class SpriteMesh {
15
16 public:
17         using Position = glm::vec3;
18         using TexCoord = glm::vec2;
19         using Index = unsigned short;
20
21         using Positions = std::vector<Position>;
22         using TexCoords = std::vector<TexCoord>;
23         using Indices = std::vector<Index>;
24
25         enum Attribute {
26                 ATTRIB_VERTEX,
27                 ATTRIB_TEXCOORD,
28                 ATTRIB_INDEX,
29                 ATTRIB_COUNT,
30         };
31
32         struct Buffer {
33
34                 Positions vertices;
35                 TexCoords coords;
36                 Indices indices;
37
38                 void Clear() noexcept {
39                         vertices.clear();
40                         coords.clear();
41                         indices.clear();
42                 }
43
44                 void Reserve(size_t p, size_t i) {
45                         vertices.reserve(p);
46                         coords.reserve(p);
47                         indices.reserve(i);
48                 }
49
50                 void LoadRect(
51                         float w, float h,
52                         const glm::vec2 &pivot = glm::vec2(0.0f),
53                         const glm::vec2 &tex_begin = glm::vec2(0.0f),
54                         const glm::vec2 &tex_end = glm::vec2(1.0f, 1.0f)
55                 );
56
57         };
58
59         using VAO = VertexArray<ATTRIB_COUNT>;
60
61 public:
62         void Update(const Buffer &) noexcept;
63
64         bool Empty() const noexcept {
65                 return vao.Empty();
66         }
67
68         void Draw() const noexcept {
69                 vao.DrawTriangleElements();
70         }
71
72 private:
73         VAO vao;
74
75 };
76
77 }
78 }
79
80 #endif