]> git.localhorst.tv Git - blank.git/blob - src/graphics/mesh.cpp
renamed OutlineMesh -> PrimitiveMesh
[blank.git] / src / graphics / mesh.cpp
1 #include "BlockMesh.hpp"
2 #include "EntityMesh.hpp"
3 #include "PrimitiveMesh.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 PrimitiveMesh::Buffer::FillRect(
76         float w, float h,
77         const glm::vec4 &color,
78         const glm::vec2 &pivot
79 ) {
80         Clear();
81         Reserve(4, 6);
82
83         vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
84         vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
85         vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
86         vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
87
88         colors.resize(4, color);
89
90         indices.assign({ 0, 2, 1, 1, 2, 3 });
91 }
92
93
94 void PrimitiveMesh::Update(const Buffer &buf) noexcept {
95 #ifndef NDEBUG
96         if (buf.colors.size() < buf.vertices.size()) {
97                 std::cerr << "PrimitiveMesh: not enough colors!" << std::endl;
98         }
99 #endif
100
101         vao.Bind();
102         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
103         vao.PushAttribute(ATTRIB_COLOR, buf.colors);
104         vao.PushIndices(ATTRIB_INDEX, buf.indices);
105 }
106
107
108 void PrimitiveMesh::DrawLines() noexcept {
109         glEnable(GL_LINE_SMOOTH);
110         glLineWidth(2.0f);
111         vao.DrawLineElements();
112 }
113
114 void PrimitiveMesh::DrawTriangles() noexcept {
115         vao.DrawTriangleElements();
116 }
117
118
119 void SkyBoxMesh::LoadUnitBox() {
120         Buffer buffer;
121         buffer.vertices = {
122                 {  1.0f,  1.0f,  1.0f },
123                 {  1.0f,  1.0f, -1.0f },
124                 {  1.0f, -1.0f,  1.0f },
125                 {  1.0f, -1.0f, -1.0f },
126                 { -1.0f,  1.0f,  1.0f },
127                 { -1.0f,  1.0f, -1.0f },
128                 { -1.0f, -1.0f,  1.0f },
129                 { -1.0f, -1.0f, -1.0f },
130         };
131         buffer.indices = {
132                 5, 7, 3,  3, 1, 5,
133                 6, 7, 5,  5, 4, 6,
134                 3, 2, 0,  0, 1, 3,
135                 6, 4, 0,  0, 2, 6,
136                 5, 1, 0,  0, 4, 5,
137                 7, 6, 3,  3, 6, 2,
138         };
139         Update(buffer);
140 }
141
142 void SkyBoxMesh::Update(const Buffer &buf) noexcept {
143         vao.Bind();
144         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
145         vao.PushIndices(ATTRIB_INDEX, buf.indices);
146 }
147
148 void SkyBoxMesh::Draw() const noexcept {
149         vao.DrawTriangleElements();
150 }
151
152
153 void SpriteMesh::Buffer::LoadRect(
154         float w, float h,
155         const glm::vec2 &pivot,
156         const glm::vec2 &tex_begin,
157         const glm::vec2 &tex_end
158 ) {
159         Clear();
160         Reserve(4, 6);
161
162         vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
163         vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
164         vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
165         vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
166
167         coords.emplace_back(tex_begin.x, tex_begin.y);
168         coords.emplace_back(tex_end.x,   tex_begin.y);
169         coords.emplace_back(tex_begin.x, tex_end.y);
170         coords.emplace_back(tex_end.x,   tex_end.y);
171
172         indices.assign({ 0, 2, 1, 1, 2, 3 });
173 }
174
175
176 void SpriteMesh::Update(const Buffer &buf) noexcept {
177 #ifndef NDEBUG
178         if (buf.coords.size() < buf.vertices.size()) {
179                 std::cerr << "SpriteMesh: not enough coords!" << std::endl;
180         }
181 #endif
182
183         vao.Bind();
184         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
185         vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
186         vao.PushIndices(ATTRIB_INDEX, buf.indices);
187 }
188
189
190 void SpriteMesh::Draw() noexcept {
191         vao.DrawTriangleElements();
192 }
193
194 }