]> git.localhorst.tv Git - blank.git/blob - src/model.cpp
remove move branching from interface
[blank.git] / src / model.cpp
1 #include "model.hpp"
2
3 #include <iostream>
4
5
6 namespace blank {
7
8 Model::Model()
9 :  va(0)
10 , handle{}
11 , count(0) {
12         glGenVertexArrays(1, &va);
13         glGenBuffers(ATTRIB_COUNT, handle);
14 }
15
16 Model::~Model() {
17         glDeleteBuffers(ATTRIB_COUNT, handle);
18         glDeleteVertexArrays(1, &va);
19 }
20
21 Model::Model(Model &&other)
22 : va(other.va)
23 , count(other.count) {
24         other.va = 0;
25         for (int i = 0; i < ATTRIB_COUNT; ++i) {
26                 handle[i] = other.handle[i];
27                 other.handle[i] = 0;
28         }
29 }
30
31 Model &Model::operator =(Model &&other) {
32         std::swap(va, other.va);
33         for (int i = 0; i < ATTRIB_COUNT; ++i) {
34                 std::swap(handle[i], other.handle[i]);
35         }
36         count = other.count;
37         return *this;
38 }
39
40 void Model::Update(const Buffer &buf) {
41         glBindVertexArray(va);
42         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
43         glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
44         glEnableVertexAttribArray(ATTRIB_VERTEX);
45         glVertexAttribPointer(
46                 ATTRIB_VERTEX, // location (for shader)
47                 3,             // size
48                 GL_FLOAT,      // type
49                 GL_FALSE,      // normalized
50                 0,             // stride
51                 nullptr        // offset
52         );
53
54 #ifndef NDEBUG
55         if (buf.colors.size() < buf.vertices.size()) {
56                 std::cerr << "Model: not enough colors!" << std::endl;
57         }
58 #endif
59         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
60         glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
61         glEnableVertexAttribArray(ATTRIB_COLOR);
62         glVertexAttribPointer(
63                 ATTRIB_COLOR,  // location (for shader)
64                 3,             // size
65                 GL_FLOAT,      // type
66                 GL_FALSE,      // normalized
67                 0,             // stride
68                 nullptr        // offset
69         );
70
71 #ifndef NDEBUG
72         if (buf.normals.size() < buf.vertices.size()) {
73                 std::cerr << "Model: not enough normals!" << std::endl;
74         }
75 #endif
76         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
77         glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
78         glEnableVertexAttribArray(ATTRIB_NORMAL);
79         glVertexAttribPointer(
80                 ATTRIB_NORMAL, // location (for shader)
81                 3,             // size
82                 GL_FLOAT,      // type
83                 GL_FALSE,      // normalized
84                 0,             // stride
85                 nullptr        // offset
86         );
87
88         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
89         glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
90         count = buf.indices.size();
91 }
92
93
94 void Model::Draw() const {
95         glBindVertexArray(va);
96         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
97         glDrawElements(
98                 GL_TRIANGLES,    // how
99                 count,           // count
100                 GL_UNSIGNED_INT, // type
101                 nullptr          // offset
102         );
103 }
104
105
106 BlockModel::BlockModel()
107 : va(0)
108 , handle{}
109 , count(0) {
110         glGenVertexArrays(1, &va);
111         glGenBuffers(ATTRIB_COUNT, handle);
112 }
113
114 BlockModel::~BlockModel() {
115         glDeleteBuffers(ATTRIB_COUNT, handle);
116         glDeleteVertexArrays(1, &va);
117 }
118
119 BlockModel::BlockModel(BlockModel &&other)
120 : va(other.va)
121 , count(other.count) {
122         other.va = 0;
123         for (int i = 0; i < ATTRIB_COUNT; ++i) {
124                 handle[i] = other.handle[i];
125                 other.handle[i] = 0;
126         }
127 }
128
129 BlockModel &BlockModel::operator =(BlockModel &&other) {
130         std::swap(va, other.va);
131         for (int i = 0; i < ATTRIB_COUNT; ++i) {
132                 std::swap(handle[i], other.handle[i]);
133         }
134         count = other.count;
135         return *this;
136 }
137
138 void BlockModel::Update(const Buffer &buf) {
139         glBindVertexArray(va);
140         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
141         glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
142         glEnableVertexAttribArray(ATTRIB_VERTEX);
143         glVertexAttribPointer(
144                 ATTRIB_VERTEX, // location (for shader)
145                 3,             // size
146                 GL_FLOAT,      // type
147                 GL_FALSE,      // normalized
148                 0,             // stride
149                 nullptr        // offset
150         );
151
152 #ifndef NDEBUG
153         if (buf.colors.size() < buf.vertices.size()) {
154                 std::cerr << "BlockModel: not enough colors!" << std::endl;
155         }
156 #endif
157         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
158         glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
159         glEnableVertexAttribArray(ATTRIB_COLOR);
160         glVertexAttribPointer(
161                 ATTRIB_COLOR,  // location (for shader)
162                 3,             // size
163                 GL_FLOAT,      // type
164                 GL_FALSE,      // normalized
165                 0,             // stride
166                 nullptr        // offset
167         );
168
169 #ifndef NDEBUG
170         if (buf.normals.size() < buf.vertices.size()) {
171                 std::cerr << "BlockModel: not enough normals!" << std::endl;
172         }
173 #endif
174         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
175         glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
176         glEnableVertexAttribArray(ATTRIB_NORMAL);
177         glVertexAttribPointer(
178                 ATTRIB_NORMAL, // location (for shader)
179                 3,             // size
180                 GL_FLOAT,      // type
181                 GL_FALSE,      // normalized
182                 0,             // stride
183                 nullptr        // offset
184         );
185
186 #ifndef NDEBUG
187         if (buf.lights.size() < buf.vertices.size()) {
188                 std::cerr << "BlockModel: not enough lights!" << std::endl;
189         }
190 #endif
191         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
192         glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
193         glEnableVertexAttribArray(ATTRIB_LIGHT);
194         glVertexAttribPointer(
195                 ATTRIB_LIGHT, // location (for shader)
196                 1,            // size
197                 GL_FLOAT,     // type
198                 GL_FALSE,     // normalized
199                 0,            // stride
200                 nullptr       // offset
201         );
202
203         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
204         glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
205         count = buf.indices.size();
206 }
207
208
209 void BlockModel::Draw() const {
210         glBindVertexArray(va);
211         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
212         glDrawElements(
213                 GL_TRIANGLES,    // how
214                 count,           // count
215                 GL_UNSIGNED_INT, // type
216                 nullptr          // offset
217         );
218 }
219
220 OutlineModel::OutlineModel()
221 : vertices()
222 , colors()
223 , indices()
224 , va(0)
225 , handle{}
226 , dirty(false) {
227         glGenVertexArrays(1, &va);
228         glGenBuffers(ATTRIB_COUNT, handle);
229 }
230
231 OutlineModel::~OutlineModel() {
232         glDeleteBuffers(ATTRIB_COUNT, handle);
233         glDeleteVertexArrays(1, &va);
234 }
235
236
237 void OutlineModel::Clear() {
238         vertices.clear();
239         colors.clear();
240         indices.clear();
241         Invalidate();
242 }
243
244 void OutlineModel::Reserve(int v, int i) {
245         vertices.reserve(v);
246         colors.reserve(v);
247         indices.reserve(i);
248 }
249
250
251 void OutlineModel::Update() {
252         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
253         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
254         glEnableVertexAttribArray(ATTRIB_VERTEX);
255         glVertexAttribPointer(
256                 ATTRIB_VERTEX, // location (for shader)
257                 3,             // size
258                 GL_FLOAT,      // type
259                 GL_FALSE,      // normalized
260                 0,             // stride
261                 nullptr        // offset
262         );
263
264 #ifndef NDEBUG
265         if (colors.size() < vertices.size()) {
266                 std::cerr << "OutlineModel: not enough colors!" << std::endl;
267                 colors.resize(vertices.size(), { 1, 0, 1 });
268         }
269 #endif
270         glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
271         glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
272         glEnableVertexAttribArray(ATTRIB_COLOR);
273         glVertexAttribPointer(
274                 ATTRIB_COLOR,  // location (for shader)
275                 3,             // size
276                 GL_FLOAT,      // type
277                 GL_FALSE,      // normalized
278                 0,             // stride
279                 nullptr        // offset
280         );
281
282         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
283         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
284
285         dirty = false;
286 }
287
288
289 void OutlineModel::Draw() {
290         glBindVertexArray(va);
291
292         if (dirty) {
293                 Update();
294         }
295
296         glEnable(GL_LINE_SMOOTH);
297         glLineWidth(2.0f);
298
299         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
300         glDrawElements(
301                 GL_LINES,          // how
302                 indices.size(),    // count
303                 GL_UNSIGNED_SHORT, // type
304                 nullptr            // offset
305         );
306 }
307
308 }