1 #include "BlockMesh.hpp"
2 #include "EntityMesh.hpp"
3 #include "PrimitiveMesh.hpp"
4 #include "SkyBoxMesh.hpp"
5 #include "SpriteMesh.hpp"
7 #include "../geometry/primitive.hpp"
15 void EntityMesh::Update(const Buffer &buf) noexcept {
17 if (buf.tex_coords.size() < buf.vertices.size()) {
18 std::cerr << "EntityMesh: not enough tex coords!" << std::endl;
20 if (buf.hsl_mods.size() < buf.vertices.size()) {
21 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
23 if (buf.rgb_mods.size() < buf.vertices.size()) {
24 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
26 if (buf.normals.size() < buf.vertices.size()) {
27 std::cerr << "EntityMesh: not enough normals!" << std::endl;
32 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
33 vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
34 vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
35 vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
36 vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
37 vao.PushIndices(ATTRIB_INDEX, buf.indices);
41 void EntityMesh::Draw() const noexcept {
42 vao.DrawTriangleElements();
46 void BlockMesh::Update(const Buffer &buf) noexcept {
48 if (buf.tex_coords.size() < buf.vertices.size()) {
49 std::cerr << "BlockMesh: not enough tex coords!" << std::endl;
51 if (buf.hsl_mods.size() < buf.vertices.size()) {
52 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
54 if (buf.rgb_mods.size() < buf.vertices.size()) {
55 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
57 if (buf.lights.size() < buf.vertices.size()) {
58 std::cerr << "BlockMesh: not enough lights!" << std::endl;
63 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
64 vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
65 vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
66 vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
67 vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
68 vao.PushIndices(ATTRIB_INDEX, buf.indices);
72 void BlockMesh::Draw() const noexcept {
73 vao.DrawTriangleElements();
77 void PrimitiveMesh::Buffer::FillRect(
79 const glm::vec4 &color,
80 const glm::vec2 &pivot
85 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
86 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
87 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
88 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
90 colors.resize(4, color);
92 indices.assign({ 0, 2, 1, 1, 2, 3 });
95 void PrimitiveMesh::Buffer::OutlineBox(const AABB &box, const glm::vec4 &color) {
99 vertices.emplace_back(box.min.x, box.min.y, box.min.z);
100 vertices.emplace_back(box.min.x, box.min.y, box.max.z);
101 vertices.emplace_back(box.min.x, box.max.y, box.min.z);
102 vertices.emplace_back(box.min.x, box.max.y, box.max.z);
103 vertices.emplace_back(box.max.x, box.min.y, box.min.z);
104 vertices.emplace_back(box.max.x, box.min.y, box.max.z);
105 vertices.emplace_back(box.max.x, box.max.y, box.min.z);
106 vertices.emplace_back(box.max.x, box.max.y, box.max.z);
108 colors.resize(8, color);
111 0, 1, 1, 3, 3, 2, 2, 0, // left
112 4, 5, 5, 7, 7, 6, 6, 4, // right
113 0, 4, 1, 5, 3, 7, 2, 6, // others
118 void PrimitiveMesh::Update(const Buffer &buf) noexcept {
120 if (buf.colors.size() < buf.vertices.size()) {
121 std::cerr << "PrimitiveMesh: not enough colors!" << std::endl;
126 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
127 vao.PushAttribute(ATTRIB_COLOR, buf.colors);
128 vao.PushIndices(ATTRIB_INDEX, buf.indices);
132 void PrimitiveMesh::DrawLines() noexcept {
133 glEnable(GL_LINE_SMOOTH);
135 vao.DrawLineElements();
138 void PrimitiveMesh::DrawTriangles() noexcept {
139 vao.DrawTriangleElements();
143 void SkyBoxMesh::LoadUnitBox() {
146 { 1.0f, 1.0f, 1.0f },
147 { 1.0f, 1.0f, -1.0f },
148 { 1.0f, -1.0f, 1.0f },
149 { 1.0f, -1.0f, -1.0f },
150 { -1.0f, 1.0f, 1.0f },
151 { -1.0f, 1.0f, -1.0f },
152 { -1.0f, -1.0f, 1.0f },
153 { -1.0f, -1.0f, -1.0f },
166 void SkyBoxMesh::Update(const Buffer &buf) noexcept {
168 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
169 vao.PushIndices(ATTRIB_INDEX, buf.indices);
172 void SkyBoxMesh::Draw() const noexcept {
173 vao.DrawTriangleElements();
177 void SpriteMesh::Buffer::LoadRect(
179 const glm::vec2 &pivot,
180 const glm::vec2 &tex_begin,
181 const glm::vec2 &tex_end
186 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
187 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
188 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
189 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
191 coords.emplace_back(tex_begin.x, tex_begin.y);
192 coords.emplace_back(tex_end.x, tex_begin.y);
193 coords.emplace_back(tex_begin.x, tex_end.y);
194 coords.emplace_back(tex_end.x, tex_end.y);
196 indices.assign({ 0, 2, 1, 1, 2, 3 });
200 void SpriteMesh::Update(const Buffer &buf) noexcept {
202 if (buf.coords.size() < buf.vertices.size()) {
203 std::cerr << "SpriteMesh: not enough coords!" << std::endl;
208 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
209 vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
210 vao.PushIndices(ATTRIB_INDEX, buf.indices);
214 void SpriteMesh::Draw() noexcept {
215 vao.DrawTriangleElements();