Commit 52bfcad4 authored by Lázár Bence Kis's avatar Lázár Bence Kis
Browse files

Textured + normal mapped linear blend skinning

parent e84805c9
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -39,6 +39,29 @@ struct MaterialSystem final : public com::Library {
     */
    Material *insert_default_lit_material(std::string const &name, com::ContextPath const &sub_folders, vec3 color);

    /**
     * Creates the default material using the default lit shader with the specified textures.
     * The textures are used only if the material has not been initialized yet.
     * @param name the newly created material folder name
     * @param sub_folders path of the folder to which the material will be added
     * @param albedo_texture_path determines the context path to the albedo texture
     * @param normal_texture_path determines the context path to the normal texture
     * @return default material with texture
     */
    Material *insert_default_lit_textured_material(std::string const &name, com::ContextPath const &sub_folders,
      com::ContextPath const &albedo_texture_path, com::ContextPath const &normal_texture_path);
    
      /**
     * Creates a new material using a lit shader with the specified textures.
     * @param name the newly created material folder name
     * @param sub_folders path of the folder to which the material will be added
     * @param albedo_texture_path determines the context path to the albedo texture
     * @param normal_texture_path determines the context path to the normal texture
     * @return material with texture
     */
    Material *insert_lit_textured_material(std::string const &name, com::ContextPath const &sub_folders,
      com::ContextPath const &albedo_texture_path, com::ContextPath const &normal_texture_path);

  protected:
    void initialize() override;
    void release() override;
+18 −2
Original line number Diff line number Diff line
@@ -36,6 +36,22 @@ struct ShaderSystem final : public com::Library {
     */
    Shader *default_lit_shader();

    /**
     * @brief Generates a new lit shader with a texture.
     * @param path the folder the shader will be created in
     * @param name the name of the shader
     * @param albedo_texture_path path to the texture used as an albedo map
     * @param normal_texture_path path to the texture used as a normal map
     * @return 
     */
    Shader *lit_textured_shader(const com::ContextPath &path, const std::string &name, const com::ContextPath &albedo_texture_path, const com::ContextPath &normal_texture_path);

    /**
     * Returns the default lit shader with the texture set at initialization.
     * @return the default lit shader with texture
     */
    Shader *default_lit_textured_shader(const com::ContextPath &albedo_texture_path, const com::ContextPath &normal_texture_path);

    /**
     * Generates an unlit shader with vertex colors.
     * @return an unlit shader with vertex colors
@@ -74,8 +90,8 @@ struct ShaderSystem final : public com::Library {

  private:
    com::Folder *m_shaders;
    Shader *m_shader_default, *m_lit_shader_default, *m_forward_unlit_vertex_color_shader, *m_deferred_lighting_shader,
           *m_deferred_lighting_shader_PBR, *m_light_volumes_shader, *m_light_volumes_shader_PBR;
    Shader *m_shader_default, *m_lit_shader_default, *m_lit_textured_shader_default, *m_forward_unlit_vertex_color_shader,
           *m_deferred_lighting_shader, *m_deferred_lighting_shader_PBR, *m_light_volumes_shader, *m_light_volumes_shader_PBR;
    com::Link *m_active_shader;
};

+12 −0
Original line number Diff line number Diff line
@@ -55,4 +55,16 @@ Material *MaterialSystem::insert_default_lit_material(std::string const &name, c
    return material;
}

Material *MaterialSystem::insert_default_lit_textured_material(std::string const &name, com::ContextPath const &sub_folders,
                                           com::ContextPath const &albedo_texture_path, com::ContextPath const &normal_texture_path) {
    auto material = insert_material(name, shader_system()->default_lit_textured_shader(albedo_texture_path, normal_texture_path), sub_folders);
    return material;
}

Material *MaterialSystem::insert_lit_textured_material(std::string const &name, com::ContextPath const &sub_folders,
                                   com::ContextPath const &albedo_texture_path, com::ContextPath const &normal_texture_path) {
    auto material = insert_material(name, shader_system()->lit_textured_shader(sub_folders, name, albedo_texture_path, normal_texture_path), sub_folders);
    return material;
}

} // namespace gfx
+23 −0
Original line number Diff line number Diff line
@@ -401,6 +401,21 @@ void Renderer::quad_pass(const std::vector<const com::Folder *> &lights, ShaderG
    GL_ASSUME_SUCCESS(glDrawArrays(GL_TRIANGLES, 0, 3));
}

void calculate_bone_matrices(std::vector<mat4x4> &bones, com::Folder *folder, com::Frame *root_frame) {
    if (auto bone_frame = folder->locate<com::Frame>({"frame"})) {
        if (auto inverse_bind_link = folder->locate<com::Link>({"inverse_bind.link"})) {
            auto inverse_bind = static_cast<com::FileMat4x4*>(inverse_bind_link->target())->get();
            auto mat = root_frame->in() * bone_frame->out() * inverse_bind;
            auto index = folder->find<com::FileUInt>("joint.index")->get();
            bones[index] = mat;
        }
    }

    for (auto subfolder : folder->subfolders()) {
        calculate_bone_matrices(bones, subfolder, root_frame);
    }
}

void Renderer::present_object(com::Folder *const object, CommonVisitData const &common,
                              com::Frame *frame) {
    Material *const material = object_system()->get_material(object);
@@ -413,6 +428,14 @@ void Renderer::present_object(com::Folder *const object, CommonVisitData const &
    shader->set_camera_uniforms(common.view_matrix, common.projection_matrix, common.camera_pos);
    shader->set_time_uniforms(static_cast<int>(SDL_GetTicks()));

    if (auto bone_file = material->uniforms()->find<com::VectorMat4x4>("bones")) {
        auto bone_uniforms = bone_file->get();
        auto root_frame = dynamic_cast<com::Frame*>(object->locate<com::Link>({"frames", "link.link"})->target());

        calculate_bone_matrices(bone_uniforms, root_frame->folder(), root_frame);
        material->set_uniform("bones", bone_uniforms);
    }

    // if (dynamic_cast<const TextShader*>(shader)) {
        for (auto f : material->uniforms()->files()) {
            if (auto uniform = dynamic_cast<com::FileMat4x4*>(f)) {
+37 −1
Original line number Diff line number Diff line
@@ -320,7 +320,8 @@ namespace gfx {

ShaderSystem::ShaderSystem()
    : com::Library{ self_name() }, m_shaders{ nullptr }, m_shader_default{ nullptr },
      m_lit_shader_default{ nullptr }, m_forward_unlit_vertex_color_shader{ nullptr },
      m_lit_shader_default{ nullptr }, m_lit_textured_shader_default{ nullptr },
      m_forward_unlit_vertex_color_shader{ nullptr },
      m_deferred_lighting_shader{ nullptr }, m_deferred_lighting_shader_PBR{ nullptr },
      m_light_volumes_shader{ nullptr }, m_light_volumes_shader_PBR{ nullptr },
      m_active_shader{ nullptr } {}
@@ -382,6 +383,41 @@ Shader *ShaderSystem::default_lit_shader() {
    return m_lit_shader_default;
}

Shader *ShaderSystem::lit_textured_shader(const com::ContextPath &path, const std::string &name, const com::ContextPath &albedo_texture_path, const com::ContextPath &normal_texture_path) {
    auto shader = insert_shader<ShaderGraph>(path, name, ShaderGraph::LIT);

    auto tex_coords = shader->insert<ShaderGraph::VaryingTexcoordNode>();

    if (!normal_texture_path.empty()) {
        auto normal_ts = shader->insert<ShaderGraph::TextureNode>(normal_texture_path, ShaderGraph::Node::DataType::ElementType::VEC3);
        shader->connect(normal_ts, ShaderGraph::TextureNode::inputs::tex_coord, tex_coords, 0);
        shader->connect(shader->root(), ShaderGraph::MasterNodeLit::inputs::normal_ts, normal_ts, 0);
    }
    else {
        auto normal_ts = shader->insert<ShaderGraph::ConstantNode>(vec3{0.5, 0.5, 1.});
        shader->connect(shader->root(), ShaderGraph::MasterNodeLit::inputs::normal_ts, normal_ts, 0);
    }

    auto color = shader->insert<ShaderGraph::TextureNode>(albedo_texture_path, ShaderGraph::Node::DataType::ElementType::VEC3);
    shader->connect(color, ShaderGraph::TextureNode::inputs::tex_coord, tex_coords, 0);

    shader->connect(shader->root(), ShaderGraph::MasterNodeLit::inputs::ambient, color, 0);
    shader->connect(shader->root(), ShaderGraph::MasterNodeLit::inputs::diffuse, color, 0);
    
    auto zero_vec3 = shader->insert<ShaderGraph::ConstantNode>(vec3{0.});
    shader->connect(shader->root(), ShaderGraph::MasterNodeLit::inputs::emission, zero_vec3, 0);
    shader->connect(shader->root(), ShaderGraph::MasterNodeLit::inputs::specular, zero_vec3, 0);

    return shader;
}

Shader *ShaderSystem::default_lit_textured_shader(const com::ContextPath &albedo_texture_path, const com::ContextPath &normal_texture_path) {
    if (m_lit_textured_shader_default == nullptr) {
        m_lit_textured_shader_default = lit_textured_shader({ "built-in-shaders" }, "default-lit-textured-shader", albedo_texture_path, normal_texture_path);
    }
    return m_lit_textured_shader_default;
}

Shader *ShaderSystem::forward_unlit_vertex_color_shader() {
    if (m_forward_unlit_vertex_color_shader == nullptr) {
        auto shader = insert_shader<ShaderGraph>({ "built-in-shaders" }, "forward-unlit-vertex-color-shader", ShaderGraph::UNLIT);