aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/Shader.cpp
blob: 111f39d9da2afa5db1f6cf3522bdf0975622230f (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
45
46
47
48
49
50
51
52
#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) {
            mgl_shader_program_deinit(&shader_program);
            shader_program.id = 0;
        }
        
        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, float value) {
        return mgl_shader_program_set_uniform_float(&shader_program, name, value) == 0;
    }

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

    bool Shader::set_uniform(const char *name, Color color) {
        return mgl_shader_program_set_uniform_color(&shader_program, name, { color.r, color.g, color.b, color.a }) == 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);
    }
}