Commit 87ad0980 authored by Lázár Bence Kis's avatar Lázár Bence Kis
Browse files

Load bone matrices using UBO

parent e544a420
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
#pragma once

#include <osi/opengl.hpp>

#define MAX_BONES 80

namespace gfx {

struct BoneUBO {
    static GLuint BONE_UBO;
};

};
+6 −0
Original line number Diff line number Diff line
@@ -60,6 +60,10 @@ struct Shader : public com::File {
        return true;
    }

    void set_ubo(GLint ubo_name, auto value) const {
        set_ubo(ubo_name, value);
    }

    /**
     * Sets the view matrix, projection matrix, and camera position uniforms, if present.
     * @param view view matrix
@@ -130,6 +134,8 @@ struct Shader : public com::File {
    static void set_uniform(detail::ShaderGL *shader, std::string const &var_name, mat4x4 const &value);
    static void set_uniform(detail::ShaderGL *shader, std::string const &var_name, std::vector<mat4x4> const &value);

    static void set_ubo(GLuint id, std::vector<mat4x4> const &value);

    lighting m_lighting_type;
    std::unique_ptr<detail::ShaderGL> m_gl_shader;
};

gfx/src/bone_ubo.cpp

0 → 100644
+3 −0
Original line number Diff line number Diff line
#include <gfx/bone_ubo.hpp>

GLuint gfx::BoneUBO::BONE_UBO = 0;
+22 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include <memory>
#include <sstream>
#include <iostream>
#include <gfx/bone_ubo.hpp>

namespace gfx::detail {

@@ -23,6 +24,7 @@ private:
    GLuint m_vao;
    GLuint m_ebo;
    std::vector<GLuint> m_vbos;
    GLuint m_bone_ubo;
};

BufferGL::BufferGL()
@@ -275,6 +277,20 @@ void BufferGL::compile(Buffer const* const buffer)

            glVertexAttribDivisor(layout, 0);
            INVARIANT(glGetError() == 0U);

            // create uniform buffer object for bone matrices
            glGenBuffers(1, &m_bone_ubo);
            INVARIANT(glGetError() == 0U);

            glBindBuffer(GL_UNIFORM_BUFFER, m_bone_ubo);
            INVARIANT(glGetError() == 0U);

            /// TODO: GL_DYNAMIC_DRAW
            glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * MAX_BONES, nullptr, GL_DYNAMIC_DRAW);
            INVARIANT(glGetError() == 0U);

            glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_bone_ubo);
            INVARIANT(glGetError() == 0U);
        }

        glBindVertexArray(0);
@@ -285,6 +301,12 @@ void BufferGL::compile(Buffer const* const buffer)

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        ASSUMPTION(glGetError() == 0U);


        // BONE UBO
        static bool first_pass = true;
        if (first_pass) {
        }
    }
    catch(...)
    {
+8 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <utils/development.hpp>
#include <utils/invariants.hpp>
#include <algorithm>
#include <gfx/bone_ubo.hpp>

namespace gfx::detail {

@@ -421,8 +422,14 @@ void Renderer::present_object(com::Folder *const object, CommonVisitData const &
                shader->maybe_set_uniform(uniform->name(), uniform->get());
            }
            else if (auto uniform = dynamic_cast<com::VectorMat4x4*>(f)) {
                if (uniform->name() == "bones") {
                    shader->set_ubo(BoneUBO::BONE_UBO, uniform->get());
                    
                }
                else {
                    shader->maybe_set_uniform(uniform->name(), uniform->get());
                }
            }
            /// TODO: Is there a better solution?
            /// dynamic_cast<com::MathFile>(f) does not work
        }
Loading