]> git.localhorst.tv Git - blank.git/blob - src/model/model.cpp
"streamlined" model/VAO handling
[blank.git] / src / model / model.cpp
1 #include "BlockModel.hpp"
2 #include "EntityModel.hpp"
3 #include "OutlineModel.hpp"
4 #include "SpriteModel.hpp"
5
6 #include <algorithm>
7 #include <iostream>
8
9
10 namespace blank {
11
12 void EntityModel::Update(const Buffer &buf) noexcept {
13 #ifndef NDEBUG
14         if (buf.colors.size() < buf.vertices.size()) {
15                 std::cerr << "EntityModel: not enough colors!" << std::endl;
16         }
17         if (buf.normals.size() < buf.vertices.size()) {
18                 std::cerr << "EntityModel: not enough normals!" << std::endl;
19         }
20 #endif
21
22         vao.Bind();
23         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
24         vao.PushAttribute(ATTRIB_COLOR, buf.colors);
25         vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
26         vao.PushIndices(ATTRIB_INDEX, buf.indices);
27 }
28
29
30 void EntityModel::Draw() const noexcept {
31         vao.DrawTriangleElements();
32 }
33
34
35 void BlockModel::Update(const Buffer &buf) noexcept {
36 #ifndef NDEBUG
37         if (buf.colors.size() < buf.vertices.size()) {
38                 std::cerr << "BlockModel: not enough colors!" << std::endl;
39         }
40         if (buf.lights.size() < buf.vertices.size()) {
41                 std::cerr << "BlockModel: not enough lights!" << std::endl;
42         }
43 #endif
44
45         vao.Bind();
46         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
47         vao.PushAttribute(ATTRIB_COLOR, buf.colors);
48         vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
49         vao.PushIndices(ATTRIB_INDEX, buf.indices);
50 }
51
52
53 void BlockModel::Draw() const noexcept {
54         vao.DrawTriangleElements();
55 }
56
57
58 void OutlineModel::Update(const Buffer &buf) noexcept {
59 #ifndef NDEBUG
60         if (buf.colors.size() < buf.vertices.size()) {
61                 std::cerr << "OutlineModel: not enough colors!" << std::endl;
62         }
63 #endif
64
65         vao.Bind();
66         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
67         vao.PushAttribute(ATTRIB_COLOR, buf.colors);
68         vao.PushIndices(ATTRIB_INDEX, buf.indices);
69 }
70
71
72 void OutlineModel::Draw() noexcept {
73         glEnable(GL_LINE_SMOOTH);
74         glLineWidth(2.0f);
75         vao.DrawLineElements();
76 }
77
78
79 void SpriteModel::Buffer::LoadRect(
80         float w, float h,
81         const glm::vec2 &pivot,
82         const glm::vec2 &tex_begin,
83         const glm::vec2 &tex_end
84 ) {
85         Clear();
86         Reserve(4, 6);
87
88         vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
89         vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
90         vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
91         vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
92
93         coords.emplace_back(tex_begin.x, tex_begin.y);
94         coords.emplace_back(tex_end.x,   tex_begin.y);
95         coords.emplace_back(tex_begin.x, tex_end.y);
96         coords.emplace_back(tex_end.x,   tex_end.y);
97
98         indices.assign({ 0, 2, 1, 1, 2, 3 });
99 }
100
101
102 void SpriteModel::Update(const Buffer &buf) noexcept {
103 #ifndef NDEBUG
104         if (buf.coords.size() < buf.vertices.size()) {
105                 std::cerr << "SpriteModel: not enough coords!" << std::endl;
106         }
107 #endif
108
109         vao.Bind();
110         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
111         vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
112         vao.PushIndices(ATTRIB_INDEX, buf.indices);
113 }
114
115
116 void SpriteModel::Draw() noexcept {
117         vao.DrawTriangleElements();
118 }
119
120 }