summary refs log tree commit diff
path: root/src/GFX/Shading/Program.cpp
blob: 6efb30b1ad93c07cdadb9c50dc5138a0e6ed5f86 (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
#include <GL/glew.h>
#include <stdexcept>
#include "Program.hpp"

namespace MC::GFX::Shading {

Program::Program(Shader vertex, Shader fragment) {
    m_program = glCreateProgram();

    glAttachShader(m_program, fragment.get());
    glAttachShader(m_program, vertex.get());

    glLinkProgram(m_program);

    glDeleteShader(fragment.get());
    glDeleteShader(vertex.get());

    GLint success;
    glGetProgramiv(m_program, GL_LINK_STATUS, &success);
    if(!success) {
        char message[512] = {};
        glGetProgramInfoLog(m_program, 512, nullptr, message);

        throw std::runtime_error(message);
    }
}

void Program::bind() const {
    glUseProgram(m_program);
}

void Program::unbind() const {
    glUseProgram(0);
}

Uniform Program::uniform(const std::string& name) const {
    auto index = glGetUniformLocation(m_program, name.c_str());

    return {name, static_cast<uint32_t>(index)};
}

uint32_t Program::get() const {
    return m_program;
}

}