1 #include "BlockModel.hpp"
3 #include "OutlineModel.hpp"
4 #include "SpriteModel.hpp"
11 Model::Model() noexcept
15 glGenVertexArrays(1, &va);
16 glGenBuffers(ATTRIB_COUNT, handle);
19 Model::~Model() noexcept {
20 glDeleteBuffers(ATTRIB_COUNT, handle);
21 glDeleteVertexArrays(1, &va);
24 Model::Model(Model &&other) noexcept
26 , count(other.count) {
28 for (int i = 0; i < ATTRIB_COUNT; ++i) {
29 handle[i] = other.handle[i];
34 Model &Model::operator =(Model &&other) noexcept {
35 std::swap(va, other.va);
36 for (int i = 0; i < ATTRIB_COUNT; ++i) {
37 std::swap(handle[i], other.handle[i]);
43 void Model::Update(const Buffer &buf) noexcept {
44 glBindVertexArray(va);
45 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
46 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
47 glEnableVertexAttribArray(ATTRIB_VERTEX);
48 glVertexAttribPointer(
49 ATTRIB_VERTEX, // location (for shader)
52 GL_FALSE, // normalized
58 if (buf.colors.size() < buf.vertices.size()) {
59 std::cerr << "Model: not enough colors!" << std::endl;
62 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
63 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
64 glEnableVertexAttribArray(ATTRIB_COLOR);
65 glVertexAttribPointer(
66 ATTRIB_COLOR, // location (for shader)
69 GL_FALSE, // normalized
75 if (buf.normals.size() < buf.vertices.size()) {
76 std::cerr << "Model: not enough normals!" << std::endl;
79 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
80 glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
81 glEnableVertexAttribArray(ATTRIB_NORMAL);
82 glVertexAttribPointer(
83 ATTRIB_NORMAL, // location (for shader)
86 GL_FALSE, // normalized
91 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
92 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
93 count = buf.indices.size();
97 void Model::Draw() const noexcept {
98 glBindVertexArray(va);
102 GL_UNSIGNED_INT, // type
108 BlockModel::BlockModel() noexcept
112 glGenVertexArrays(1, &va);
113 glGenBuffers(ATTRIB_COUNT, handle);
116 BlockModel::~BlockModel() noexcept {
117 glDeleteBuffers(ATTRIB_COUNT, handle);
118 glDeleteVertexArrays(1, &va);
121 BlockModel::BlockModel(BlockModel &&other) noexcept
123 , count(other.count) {
125 for (int i = 0; i < ATTRIB_COUNT; ++i) {
126 handle[i] = other.handle[i];
131 BlockModel &BlockModel::operator =(BlockModel &&other) noexcept {
132 std::swap(va, other.va);
133 for (int i = 0; i < ATTRIB_COUNT; ++i) {
134 std::swap(handle[i], other.handle[i]);
140 void BlockModel::Update(const Buffer &buf) noexcept {
141 glBindVertexArray(va);
142 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
143 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
144 glEnableVertexAttribArray(ATTRIB_VERTEX);
145 glVertexAttribPointer(
146 ATTRIB_VERTEX, // location (for shader)
149 GL_FALSE, // normalized
155 if (buf.colors.size() < buf.vertices.size()) {
156 std::cerr << "BlockModel: not enough colors!" << std::endl;
159 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
160 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
161 glEnableVertexAttribArray(ATTRIB_COLOR);
162 glVertexAttribPointer(
163 ATTRIB_COLOR, // location (for shader)
166 GL_FALSE, // normalized
172 if (buf.lights.size() < buf.vertices.size()) {
173 std::cerr << "BlockModel: not enough lights!" << std::endl;
176 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
177 glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
178 glEnableVertexAttribArray(ATTRIB_LIGHT);
179 glVertexAttribPointer(
180 ATTRIB_LIGHT, // location (for shader)
183 GL_FALSE, // normalized
188 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
189 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
190 count = buf.indices.size();
194 void BlockModel::Draw() const noexcept {
195 glBindVertexArray(va);
199 GL_UNSIGNED_INT, // type
204 OutlineModel::OutlineModel() noexcept
211 glGenVertexArrays(1, &va);
212 glGenBuffers(ATTRIB_COUNT, handle);
215 OutlineModel::~OutlineModel() noexcept {
216 glDeleteBuffers(ATTRIB_COUNT, handle);
217 glDeleteVertexArrays(1, &va);
221 void OutlineModel::Clear() noexcept {
228 void OutlineModel::Reserve(int v, int i) {
235 void OutlineModel::Update() noexcept {
236 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
237 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
238 glEnableVertexAttribArray(ATTRIB_VERTEX);
239 glVertexAttribPointer(
240 ATTRIB_VERTEX, // location (for shader)
243 GL_FALSE, // normalized
249 if (colors.size() < vertices.size()) {
250 std::cerr << "OutlineModel: not enough colors!" << std::endl;
251 colors.resize(vertices.size(), { 1, 0, 1 });
254 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
255 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
256 glEnableVertexAttribArray(ATTRIB_COLOR);
257 glVertexAttribPointer(
258 ATTRIB_COLOR, // location (for shader)
261 GL_FALSE, // normalized
266 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
267 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
273 void OutlineModel::Draw() noexcept {
274 glBindVertexArray(va);
280 glEnable(GL_LINE_SMOOTH);
285 indices.size(), // count
286 GL_UNSIGNED_SHORT, // type
292 SpriteModel::SpriteModel() noexcept
299 glGenVertexArrays(1, &va);
300 glGenBuffers(ATTRIB_COUNT, handle);
303 SpriteModel::~SpriteModel() noexcept {
304 glDeleteBuffers(ATTRIB_COUNT, handle);
305 glDeleteVertexArrays(1, &va);
309 void SpriteModel::Clear() noexcept {
316 void SpriteModel::Reserve(int v, int i) {
323 void SpriteModel::LoadRect(
325 const glm::vec2 &pivot,
326 const glm::vec2 &tex_begin,
327 const glm::vec2 &tex_end
332 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
333 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
334 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
335 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
337 coords.emplace_back(tex_begin.x, tex_begin.y);
338 coords.emplace_back(tex_end.x, tex_begin.y);
339 coords.emplace_back(tex_begin.x, tex_end.y);
340 coords.emplace_back(tex_end.x, tex_end.y);
342 indices.assign({ 0, 2, 1, 1, 2, 3 });
348 void SpriteModel::Update() noexcept {
349 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
350 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Position), vertices.data(), GL_STATIC_DRAW);
351 glEnableVertexAttribArray(ATTRIB_VERTEX);
352 glVertexAttribPointer(
353 ATTRIB_VERTEX, // location (for shader)
356 GL_FALSE, // normalized
362 if (coords.size() < vertices.size()) {
363 std::cerr << "SpriteModel: not enough coords!" << std::endl;
364 coords.resize(vertices.size(), { 1, 1 });
367 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_TEXCOORD]);
368 glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(TexCoord), coords.data(), GL_STATIC_DRAW);
369 glEnableVertexAttribArray(ATTRIB_TEXCOORD);
370 glVertexAttribPointer(
371 ATTRIB_TEXCOORD, // location (for shader)
374 GL_FALSE, // normalized
379 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
380 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
386 void SpriteModel::Draw() noexcept {
387 glBindVertexArray(va);
395 indices.size(), // count
396 GL_UNSIGNED_SHORT, // type