1 #include "PrimitiveMesh.hpp"
2 #include "SkyBoxMesh.hpp"
3 #include "SpriteMesh.hpp"
5 #include "../geometry/primitive.hpp"
14 void PrimitiveMesh::Buffer::FillRect(
17 const glm::vec2 &pivot
22 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
23 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
24 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
25 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
27 colors.resize(4, color);
29 indices.assign({ 0, 2, 1, 1, 2, 3 });
32 void PrimitiveMesh::Buffer::OutlineBox(const geometry::AABB &box, const Color &color) {
36 vertices.emplace_back(box.min.x, box.min.y, box.min.z);
37 vertices.emplace_back(box.min.x, box.min.y, box.max.z);
38 vertices.emplace_back(box.min.x, box.max.y, box.min.z);
39 vertices.emplace_back(box.min.x, box.max.y, box.max.z);
40 vertices.emplace_back(box.max.x, box.min.y, box.min.z);
41 vertices.emplace_back(box.max.x, box.min.y, box.max.z);
42 vertices.emplace_back(box.max.x, box.max.y, box.min.z);
43 vertices.emplace_back(box.max.x, box.max.y, box.max.z);
45 colors.resize(8, color);
48 0, 1, 1, 3, 3, 2, 2, 0, // left
49 4, 5, 5, 7, 7, 6, 6, 4, // right
50 0, 4, 1, 5, 3, 7, 2, 6, // others
55 void PrimitiveMesh::Update(const Buffer &buf) noexcept {
57 if (buf.colors.size() < buf.vertices.size()) {
58 std::cerr << "PrimitiveMesh: not enough colors!" << std::endl;
63 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
64 vao.PushAttribute(ATTRIB_COLOR, buf.colors, true);
65 vao.PushIndices(ATTRIB_INDEX, buf.indices);
69 void PrimitiveMesh::DrawLines() const noexcept {
70 glEnable(GL_LINE_SMOOTH);
72 vao.DrawLineElements();
76 void SkyBoxMesh::LoadUnitBox() {
80 { 1.0f, 1.0f, -1.0f },
81 { 1.0f, -1.0f, 1.0f },
82 { 1.0f, -1.0f, -1.0f },
83 { -1.0f, 1.0f, 1.0f },
84 { -1.0f, 1.0f, -1.0f },
85 { -1.0f, -1.0f, 1.0f },
86 { -1.0f, -1.0f, -1.0f },
99 void SkyBoxMesh::Update(const Buffer &buf) noexcept {
101 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
102 vao.PushIndices(ATTRIB_INDEX, buf.indices);
106 void SpriteMesh::Buffer::LoadRect(
108 const glm::vec2 &pivot,
109 const glm::vec2 &tex_begin,
110 const glm::vec2 &tex_end
115 vertices.emplace_back( -pivot.x, -pivot.y, 0.0f);
116 vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f);
117 vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
118 vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
120 coords.emplace_back(tex_begin.x, tex_begin.y);
121 coords.emplace_back(tex_end.x, tex_begin.y);
122 coords.emplace_back(tex_begin.x, tex_end.y);
123 coords.emplace_back(tex_end.x, tex_end.y);
125 indices.assign({ 0, 2, 1, 1, 2, 3 });
129 void SpriteMesh::Update(const Buffer &buf) noexcept {
131 if (buf.coords.size() < buf.vertices.size()) {
132 std::cerr << "SpriteMesh: not enough coords!" << std::endl;
137 vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
138 vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
139 vao.PushIndices(ATTRIB_INDEX, buf.indices);