aboutsummaryrefslogtreecommitdiffstats
path: root/yage/render/shader.cpp
blob: b3aca5390a9899b07124e59d90166fe0154e2a15 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/** ---------------------------------------------------------------------------
 * -*- c++ -*-
 * @file: shader.cpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

#include "shader.h"

#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

using std::runtime_error;

namespace yage
{

Shader::Shader(std::string const &vertex_path, std::string const &fragment_path)
{
    std::string vertex_source, fragment_source;
    std::ifstream vertex_file, fragment_file;

    vertex_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    fragment_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);

    try {
        vertex_file.open(vertex_path);
        fragment_file.open(fragment_path);

        std::ostringstream vertex_stream, fragment_stream;
        vertex_stream << vertex_file.rdbuf();
        fragment_stream << fragment_file.rdbuf();

        vertex_file.close();
        fragment_file.close();

        vertex_source   = vertex_stream.str();
        fragment_source = fragment_stream.str();
    } catch (std::ifstream::failure e) {
        throw runtime_error("File could not be opened: " +
                            std::string(e.what()));
    }

    const char *vertex_source_c   = vertex_source.c_str();
    const char *fragment_source_c = fragment_source.c_str();

    GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_source_c, NULL);
    glCompileShader(vertex_shader);
    errorCheck(vertex_shader, "vertex");

    GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_source_c, NULL);
    glCompileShader(fragment_shader);
    errorCheck(fragment_shader, "fragment");

    program_id_ = glCreateProgram();
    glAttachShader(program_id_, vertex_shader);
    glAttachShader(program_id_, fragment_shader);
    glLinkProgram(program_id_);
    errorCheck(program_id_, "program");

    glDetachShader(program_id_, vertex_shader);
    glDetachShader(program_id_, fragment_shader);

    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);
}

Shader::~Shader()
{
    /// cleans up all the shaders and the program
    if (program_id_ != 0) {
        glDeleteProgram(program_id_);
    }
}

void Shader::use() const
{
    glUseProgram(program_id_);
}

void Shader::setUniform(std::string const &name, int value) const
{
    glUniform1i(getUniformLocation(name), static_cast<GLint>(value));
}

void Shader::setUniform(std::string const &name, float value) const
{
    glUniform1f(getUniformLocation(name), static_cast<GLfloat>(value));
}

void Shader::setUniform(std::string const &name, const glm::mat4 &matrix) const
{
    glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, &(matrix[0][0]));
}

GLint Shader::getUniformLocation(std::string const &uniform_name) const
{
    GLuint location = glGetUniformLocation(program_id_, uniform_name.c_str());
    if (location == GL_INVALID_INDEX) {
        throw std::runtime_error("'" + uniform_name + "' not found");
    }
    return location;
}

void Shader::errorCheck(GLuint shader, std::string const &shader_type) const
{
    int success;
    char info_log[1024];
    if (shader_type != std::string("program")) {
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(shader, 1024, NULL, info_log);
            throw runtime_error(shader_type +
                                " failed to compile: " + std::string(info_log));
        }
    } else {
        glGetProgramiv(shader, GL_LINK_STATUS, &success);
        if (!success) {
            glGetProgramInfoLog(shader, 1024, NULL, info_log);
            throw runtime_error(shader_type +
                                " failed to link: " + std::string(info_log));
        }
    }
}

} // namespace yage