1 #include "BlockMesh.hpp"
2 #include "EntityMesh.hpp"
3 #include "PrimitiveMesh.hpp"
4 #include "SkyBoxMesh.hpp"
5 #include "SpriteMesh.hpp"
13 void EntityMesh::Update(const Buffer &buf) noexcept {
15 if (buf.tex_coords.size() < buf.vertices.size()) {
16 std::cerr << "EntityMesh: not enough tex coords!" << std::endl;
18 if (buf.hsl_mods.size() < buf.vertices.size()) {
19 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
21 if (buf.rgb_mods.size() < buf.vertices.size()) {
22 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
24 if (buf.normals.size() < buf.vertices.size()) {
25 std::cerr << "EntityMesh: not enough normals!" << std::endl;
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);
39 void EntityMesh::Draw() const noexcept {
40 vao.DrawTriangleElements();
44 void BlockMesh::Update(const Buffer &buf) noexcept {
46 if (buf.tex_coords.size() < buf.vertices.size()) {
47 std::cerr << "BlockMesh: not enough tex coords!" << std::endl;
49 if (buf.hsl_mods.size() < buf.vertices.size()) {
50 std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl;
52 if (buf.rgb_mods.size() < buf.vertices.size()) {
53 std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl;
55 if (buf.lights.size() < buf.vertices.size()) {
56 std::cerr << "BlockMesh: not enough lights!" << std::endl;
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);
70 void BlockMesh::Draw() const noexcept {
71 vao.DrawTriangleElements();
75 void PrimitiveMesh::Buffer::FillRect(
77 const glm::vec4 &color,
78 const glm::vec2 &pivot
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);
88 colors.resize(4, color);
90 indices.assign({ 0, 2, 1, 1, 2, 3 });
94 void PrimitiveMesh::Update(const Buffer &buf) noexcept {
96 if (buf.colors.size() < buf.vertices.size()) {
97 std::cerr << "PrimitiveMesh: not enough colors!" << std::endl;
102 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
103 vao.PushAttribute(ATTRIB_COLOR, buf.colors);
104 vao.PushIndices(ATTRIB_INDEX, buf.indices);
108 void PrimitiveMesh::DrawLines() noexcept {
109 glEnable(GL_LINE_SMOOTH);
111 vao.DrawLineElements();
114 void PrimitiveMesh::DrawTriangles() noexcept {
115 vao.DrawTriangleElements();
119 void SkyBoxMesh::LoadUnitBox() {
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 },
142 void SkyBoxMesh::Update(const Buffer &buf) noexcept {
144 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
145 vao.PushIndices(ATTRIB_INDEX, buf.indices);
148 void SkyBoxMesh::Draw() const noexcept {
149 vao.DrawTriangleElements();
153 void SpriteMesh::Buffer::LoadRect(
155 const glm::vec2 &pivot,
156 const glm::vec2 &tex_begin,
157 const glm::vec2 &tex_end
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);
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);
172 indices.assign({ 0, 2, 1, 1, 2, 3 });
176 void SpriteMesh::Update(const Buffer &buf) noexcept {
178 if (buf.coords.size() < buf.vertices.size()) {
179 std::cerr << "SpriteMesh: not enough coords!" << std::endl;
184 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
185 vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
186 vao.PushIndices(ATTRIB_INDEX, buf.indices);
190 void SpriteMesh::Draw() noexcept {
191 vao.DrawTriangleElements();