Zen 0.3.0
Loading...
Searching...
No Matches
QuadBuilder.cpp
1#include "QuadBuilder.h"
2#include <ZEN_Shader.h>
3#include <glm/gtc/constants.hpp>
4#include <glm/gtc/matrix_transform.hpp>
5#include <memory>
6
7using namespace Zen;
8
9void QuadBuilder::init(std::shared_ptr<Shader> shader) {
10
11 m_shader = shader;
12
13 m_vao.reset(VertexArray::Create());
14
15 m_vbo.reset(VertexBuffer::Create(nullptr, sizeof(QuadVertex) * 4));
16 m_vbo->setLayout({
17 {ShaderDataType::Float2, "a_Position"},
18 {ShaderDataType::Float4, "a_Color" },
19 });
20 m_vao->addVertexBuffer(m_vbo);
21
22 static const uint32_t kIndices[6] = {0, 1, 2, 2, 3, 0};
23
24 m_ibo.reset(IndexBuffer::Create(const_cast<uint32_t *>(kIndices), 6));
25 m_vao->setIndexBuffer(m_ibo);
26}
27
28void QuadBuilder::drawQuad(const glm::vec2 &position,
29 const glm::vec2 &size,
30 const glm::vec4 &colour) {
31 const glm::vec2 h = 0.5f * size;
32
33 QuadVertex verts[4] = {
34 {{position.x - h.x, position.y - h.y}, colour},
35 {{position.x + h.x, position.y - h.y}, colour},
36 {{position.x + h.x, position.y + h.y}, colour},
37 {{position.x - h.x, position.y + h.y}, colour},
38 };
39
40 m_vbo->setData(verts, sizeof(verts));
41
42 m_shader->bind();
43 Renderer::submit(m_shader, m_vao);
44}