Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.1 kB
2
Indexable
#include "Renderer.hpp"
#include <gl/glew.h>

#include "Shader.hpp"

namespace Engine
{
    struct Vertex
    {
        glm::vec3 coords;
        glm::vec3 color;
    };


    std::vector<Vertex> vertices{};

    void Renderer::Setup()
    {
        glGenVertexArrays(1, &vao);
    }

    void Renderer::NextBatch()
    {
        glBindVertexArray(vao);
        vertices.clear();
    }

    void Renderer::DrawQuad(glm::vec2 offset, glm::vec3 color, glm::vec3 scale)
    {
        const float size = 0.5f;
        vertices.push_back({
            scale * glm::vec3(offset.x + -size, offset.y + size, 0),
            color
        });

        vertices.push_back({
            scale * glm::vec3(offset.x + -size, offset.y + -size, 0),
            color
        });

        vertices.push_back({
            scale * glm::vec3(offset.x + size, offset.y + -size, 0),
            color
        });

        vertices.push_back({
            scale * glm::vec3(offset.x + size, offset.y + -size, 0),
            color
        });
        vertices.push_back({
            scale * glm::vec3(offset.x + size, offset.y + size, 0),
            color
        });
        vertices.push_back({
            scale * glm::vec3(offset.x + -size, offset.y + size, 0),
            color
        });
    }

    void Renderer::DrawBatch()
    {


        glBindVertexArray(vao);
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(glm::vec3)));
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glDrawArrays(GL_TRIANGLES, 0, vertices.size());

        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);

        glBindVertexArray(0);
        glDeleteBuffers(1, &vbo);
    }
}