]> git.localhorst.tv Git - blank.git/blob - src/model.hpp
don't push block normals to GPU
[blank.git] / src / model.hpp
1 #ifndef BLANK_MODEL_HPP_
2 #define BLANK_MODEL_HPP_
3
4 #include <vector>
5 #include <GL/glew.h>
6 #include <glm/glm.hpp>
7
8
9 namespace blank {
10
11 class Model {
12
13 public:
14         using Position = glm::vec3;
15         using Color = glm::vec3;
16         using Normal = glm::vec3;
17         using Index = unsigned int;
18
19         using Positions = std::vector<Position>;
20         using Colors = std::vector<Color>;
21         using Normals = std::vector<Normal>;
22         using Indices = std::vector<Index>;
23
24 public:
25         struct Buffer {
26
27                 Positions vertices;
28                 Colors colors;
29                 Normals normals;
30                 Indices indices;
31
32                 void Clear() {
33                         vertices.clear();
34                         colors.clear();
35                         normals.clear();
36                         indices.clear();
37                 }
38
39                 void Reserve(size_t p, size_t i) {
40                         vertices.reserve(p);
41                         colors.reserve(p);
42                         normals.reserve(p);
43                         indices.reserve(i);
44                 }
45
46         };
47
48 public:
49         Model();
50         ~Model();
51
52         Model(const Model &) = delete;
53         Model &operator =(const Model &) = delete;
54
55         Model(Model &&);
56         Model &operator =(Model &&);
57
58         void Update(const Buffer &);
59
60         void Draw() const;
61
62 private:
63         enum Attribute {
64                 ATTRIB_VERTEX,
65                 ATTRIB_COLOR,
66                 ATTRIB_NORMAL,
67                 ATTRIB_INDEX,
68                 ATTRIB_COUNT,
69         };
70
71         GLuint va;
72         GLuint handle[ATTRIB_COUNT];
73         size_t count;
74
75 };
76
77
78 class BlockModel {
79
80 public:
81         using Position = glm::vec3;
82         using Color = glm::vec3;
83         using Light = float;
84         using Index = unsigned int;
85
86         using Positions = std::vector<Position>;
87         using Colors = std::vector<Color>;
88         using Lights = std::vector<Light>;
89         using Indices = std::vector<Index>;
90
91 public:
92         struct Buffer {
93
94                 Positions vertices;
95                 Colors colors;
96                 Lights lights;
97                 Indices indices;
98
99                 void Clear() {
100                         vertices.clear();
101                         colors.clear();
102                         lights.clear();
103                         indices.clear();
104                 }
105
106                 void Reserve(size_t p, size_t i) {
107                         vertices.reserve(p);
108                         colors.reserve(p);
109                         lights.reserve(p);
110                         indices.reserve(i);
111                 }
112
113         };
114
115 public:
116         BlockModel();
117         ~BlockModel();
118
119         BlockModel(const BlockModel &) = delete;
120         BlockModel &operator =(const Model &) = delete;
121
122         BlockModel(BlockModel &&);
123         BlockModel &operator =(BlockModel &&);
124
125         void Update(const Buffer &);
126
127         void Draw() const;
128
129 private:
130         enum Attribute {
131                 ATTRIB_VERTEX,
132                 ATTRIB_COLOR,
133                 ATTRIB_LIGHT,
134                 ATTRIB_INDEX,
135                 ATTRIB_COUNT,
136         };
137
138         GLuint va;
139         GLuint handle[ATTRIB_COUNT];
140         size_t count;
141
142 };
143
144
145 class OutlineModel {
146
147 public:
148         using Position = glm::vec3;
149         using Color = glm::vec3;
150         using Index = unsigned short;
151
152         using Positions = std::vector<Position>;
153         using Colors = std::vector<Color>;
154         using Indices = std::vector<Index>;
155
156 public:
157         Positions vertices;
158         Colors colors;
159         Indices indices;
160
161 public:
162         OutlineModel();
163         ~OutlineModel();
164
165         OutlineModel(const OutlineModel &) = delete;
166         OutlineModel &operator =(const OutlineModel &) = delete;
167
168         void Invalidate() { dirty = true; }
169
170         void Clear();
171         void Reserve(int vtx_count, int idx_count);
172
173         void Draw();
174
175 private:
176         void Update();
177
178 private:
179         enum Attribute {
180                 ATTRIB_VERTEX,
181                 ATTRIB_COLOR,
182                 ATTRIB_INDEX,
183                 ATTRIB_COUNT,
184         };
185
186         GLuint va;
187         GLuint handle[ATTRIB_COUNT];
188         bool dirty;
189
190 };
191
192 }
193
194 #endif