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 << "EntityMesh: not enough HSL modifiers!" << std::endl;
23 if (buf.rgb_mods.size() < buf.vertices.size()) {
24 std::cerr << "EntityMesh: 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, true);
35 vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods, true);
36 vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
37 vao.PushIndices(ATTRIB_INDEX, buf.indices);
41 void BlockMesh::Update(const Buffer &buf) noexcept {
43 if (buf.tex_coords.size() < buf.vertices.size()) {
44 std::cerr << "BlockMesh: not enough tex coords!" << std::endl;
46 if (buf.hsl_mods.size() < buf.vertices.size()) {
47 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
49 if (buf.rgb_mods.size() < buf.vertices.size()) {
50 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
52 if (buf.lights.size() < buf.vertices.size()) {
53 std::cerr << "BlockMesh: not enough lights!" << std::endl;
58 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
59 vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
60 vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods, true);
61 vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods, true);
62 vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
63 vao.PushIndices(ATTRIB_INDEX, buf.indices);
67 void PrimitiveMesh::Buffer::FillRect(
70 const glm::vec2 &pivot
75 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
76 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
77 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
78 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
80 colors.resize(4, color);
82 indices.assign({ 0, 2, 1, 1, 2, 3 });
85 void PrimitiveMesh::Buffer::OutlineBox(const AABB &box, const Color &color) {
89 vertices.emplace_back(box.min.x, box.min.y, box.min.z);
90 vertices.emplace_back(box.min.x, box.min.y, box.max.z);
91 vertices.emplace_back(box.min.x, box.max.y, box.min.z);
92 vertices.emplace_back(box.min.x, box.max.y, box.max.z);
93 vertices.emplace_back(box.max.x, box.min.y, box.min.z);
94 vertices.emplace_back(box.max.x, box.min.y, box.max.z);
95 vertices.emplace_back(box.max.x, box.max.y, box.min.z);
96 vertices.emplace_back(box.max.x, box.max.y, box.max.z);
98 colors.resize(8, color);
101 0, 1, 1, 3, 3, 2, 2, 0, // left
102 4, 5, 5, 7, 7, 6, 6, 4, // right
103 0, 4, 1, 5, 3, 7, 2, 6, // others
108 void PrimitiveMesh::Update(const Buffer &buf) noexcept {
110 if (buf.colors.size() < buf.vertices.size()) {
111 std::cerr << "PrimitiveMesh: not enough colors!" << std::endl;
116 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
117 vao.PushAttribute(ATTRIB_COLOR, buf.colors, true);
118 vao.PushIndices(ATTRIB_INDEX, buf.indices);
122 void PrimitiveMesh::DrawLines() const noexcept {
123 glEnable(GL_LINE_SMOOTH);
125 vao.DrawLineElements();
129 void SkyBoxMesh::LoadUnitBox() {
132 { 1.0f, 1.0f, 1.0f },
133 { 1.0f, 1.0f, -1.0f },
134 { 1.0f, -1.0f, 1.0f },
135 { 1.0f, -1.0f, -1.0f },
136 { -1.0f, 1.0f, 1.0f },
137 { -1.0f, 1.0f, -1.0f },
138 { -1.0f, -1.0f, 1.0f },
139 { -1.0f, -1.0f, -1.0f },
152 void SkyBoxMesh::Update(const Buffer &buf) noexcept {
154 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
155 vao.PushIndices(ATTRIB_INDEX, buf.indices);
159 void SpriteMesh::Buffer::LoadRect(
161 const glm::vec2 &pivot,
162 const glm::vec2 &tex_begin,
163 const glm::vec2 &tex_end
168 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
169 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
170 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
171 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
173 coords.emplace_back(tex_begin.x, tex_begin.y);
174 coords.emplace_back(tex_end.x, tex_begin.y);
175 coords.emplace_back(tex_begin.x, tex_end.y);
176 coords.emplace_back(tex_end.x, tex_end.y);
178 indices.assign({ 0, 2, 1, 1, 2, 3 });
182 void SpriteMesh::Update(const Buffer &buf) noexcept {
184 if (buf.coords.size() < buf.vertices.size()) {
185 std::cerr << "SpriteMesh: not enough coords!" << std::endl;
190 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
191 vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
192 vao.PushIndices(ATTRIB_INDEX, buf.indices);