]> git.localhorst.tv Git - blank.git/blob - src/graphics/mesh.cpp
model -> mesh
[blank.git] / src / graphics / mesh.cpp
1 #include "BlockMesh.hpp"
2 #include "EntityMesh.hpp"
3 #include "OutlineMesh.hpp"
4 #include "SkyBoxMesh.hpp"
5 #include "SpriteMesh.hpp"
6
7 #include <algorithm>
8 #include <iostream>
9
10
11 namespace blank {
12
13 void EntityMesh::Update(const Buffer &buf) noexcept {
14 #ifndef NDEBUG
15         if (buf.tex_coords.size() < buf.vertices.size()) {
16                 std::cerr << "EntityMesh: not enough tex coords!" << std::endl;
17         }
18         if (buf.hsl_mods.size() < buf.vertices.size()) {
19                 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
20         }
21         if (buf.rgb_mods.size() < buf.vertices.size()) {
22                 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
23         }
24         if (buf.normals.size() < buf.vertices.size()) {
25                 std::cerr << "EntityMesh: not enough normals!" << std::endl;
26         }
27 #endif
28
29         vao.Bind();
30         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
31         vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
32         vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
33         vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
34         vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
35         vao.PushIndices(ATTRIB_INDEX, buf.indices);
36 }
37
38
39 void EntityMesh::Draw() const noexcept {
40         vao.DrawTriangleElements();
41 }
42
43
44 void BlockMesh::Update(const Buffer &buf) noexcept {
45 #ifndef NDEBUG
46         if (buf.tex_coords.size() < buf.vertices.size()) {
47                 std::cerr << "BlockMesh: not enough tex coords!" << std::endl;
48         }
49         if (buf.hsl_mods.size() < buf.vertices.size()) {
50                 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
51         }
52         if (buf.rgb_mods.size() < buf.vertices.size()) {
53                 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
54         }
55         if (buf.lights.size() < buf.vertices.size()) {
56                 std::cerr << "BlockMesh: not enough lights!" << std::endl;
57         }
58 #endif
59
60         vao.Bind();
61         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
62         vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
63         vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
64         vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
65         vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
66         vao.PushIndices(ATTRIB_INDEX, buf.indices);
67 }
68
69
70 void BlockMesh::Draw() const noexcept {
71         vao.DrawTriangleElements();
72 }
73
74
75 void OutlineMesh::Update(const Buffer &buf) noexcept {
76 #ifndef NDEBUG
77         if (buf.colors.size() < buf.vertices.size()) {
78                 std::cerr << "OutlineMesh: not enough colors!" << std::endl;
79         }
80 #endif
81
82         vao.Bind();
83         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
84         vao.PushAttribute(ATTRIB_COLOR, buf.colors);
85         vao.PushIndices(ATTRIB_INDEX, buf.indices);
86 }
87
88
89 void OutlineMesh::Draw() noexcept {
90         glEnable(GL_LINE_SMOOTH);
91         glLineWidth(2.0f);
92         vao.DrawLineElements();
93 }
94
95
96 void SkyBoxMesh::LoadUnitBox() {
97         Buffer buffer;
98         buffer.vertices = {
99                 {  1.0f,  1.0f,  1.0f },
100                 {  1.0f,  1.0f, -1.0f },
101                 {  1.0f, -1.0f,  1.0f },
102                 {  1.0f, -1.0f, -1.0f },
103                 { -1.0f,  1.0f,  1.0f },
104                 { -1.0f,  1.0f, -1.0f },
105                 { -1.0f, -1.0f,  1.0f },
106                 { -1.0f, -1.0f, -1.0f },
107         };
108         buffer.indices = {
109                 5, 7, 3,  3, 1, 5,
110                 6, 7, 5,  5, 4, 6,
111                 3, 2, 0,  0, 1, 3,
112                 6, 4, 0,  0, 2, 6,
113                 5, 1, 0,  0, 4, 5,
114                 7, 6, 3,  3, 6, 2,
115         };
116         Update(buffer);
117 }
118
119 void SkyBoxMesh::Update(const Buffer &buf) noexcept {
120         vao.Bind();
121         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
122         vao.PushIndices(ATTRIB_INDEX, buf.indices);
123 }
124
125 void SkyBoxMesh::Draw() const noexcept {
126         vao.DrawTriangleElements();
127 }
128
129
130 void SpriteMesh::Buffer::LoadRect(
131         float w, float h,
132         const glm::vec2 &pivot,
133         const glm::vec2 &tex_begin,
134         const glm::vec2 &tex_end
135 ) {
136         Clear();
137         Reserve(4, 6);
138
139         vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
140         vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
141         vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
142         vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
143
144         coords.emplace_back(tex_begin.x, tex_begin.y);
145         coords.emplace_back(tex_end.x,   tex_begin.y);
146         coords.emplace_back(tex_begin.x, tex_end.y);
147         coords.emplace_back(tex_end.x,   tex_end.y);
148
149         indices.assign({ 0, 2, 1, 1, 2, 3 });
150 }
151
152
153 void SpriteMesh::Update(const Buffer &buf) noexcept {
154 #ifndef NDEBUG
155         if (buf.coords.size() < buf.vertices.size()) {
156                 std::cerr << "SpriteMesh: not enough coords!" << std::endl;
157         }
158 #endif
159
160         vao.Bind();
161         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
162         vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
163         vao.PushIndices(ATTRIB_INDEX, buf.indices);
164 }
165
166
167 void SpriteMesh::Draw() noexcept {
168         vao.DrawTriangleElements();
169 }
170
171 }