aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/Shader.cpp
blob: c23d32b6931652ef02473f957b369cbe39d79b4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "../../include/mglpp/graphics/Shader.hpp"

namespace mgl {
    Shader::Shader() {
        shader_program.id = 0;
    }

    Shader::~Shader() {
        mgl_shader_program_deinit(&shader_program);
    }

    bool Shader::load_from_file(const char *filepath, Type type) {
        if(shader_program.id)
            return false;
        
        int res = mgl_shader_program_init(&shader_program);
        if(res != 0)
            return false;

        res = mgl_shader_program_add_shader_from_file(&shader_program, filepath, (mgl_shader_type)type);
        if(res != 0)
            return false;

        return mgl_shader_program_finalize(&shader_program) == 0;
    }

    bool Shader::set_uniform(const char *name, vec2f value) {
        if(!shader_program.id)
            return false;
        return mgl_shader_program_set_uniform_vec2f(&shader_program, name, { value.x, value.y }) == 0;
    }

    bool Shader::is_valid() const {
        return shader_program.id != 0;
    }

    // static
    void Shader::use(Shader *shader) {
        if(shader)
            mgl_shader_program_use(&shader->shader_program);
        else
            mgl_shader_program_use(nullptr);
    }
}