aboutsummaryrefslogtreecommitdiff
path: root/shader.cpp
blob: 0a1268e37e1dc46bbbe71d738bffcf7186cc30f3 (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
134
135
136
137
#include "shader.h"

#include <iostream>

static int setupShader(GLenum shaderType, const char *src)
{
    GLuint shader = glCreateShader(shaderType);
    glShaderSource(shader, 1, &src, nullptr);
    glCompileShader(shader);

    int status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE) {
        char buf[1024];
        glGetShaderInfoLog(shader, sizeof buf, nullptr, buf);
        std::cerr << "Error: "
                  << (shaderType == GL_VERTEX_SHADER ? "vertex" : "fragment")
                  << " shader: compilation failed: "
                  << buf
                  << std::endl;
    }
    return shader;
}

Shader::Shader(const char *vertexSrc, const char *fragmentSrc, const char *geometrySrc)
{
    GLuint vertex = setupShader(GL_VERTEX_SHADER, vertexSrc);
    GLuint fragment = setupShader(GL_FRAGMENT_SHADER, fragmentSrc);
    GLuint geometry;
    if (geometrySrc != nullptr) {
        geometry = setupShader(GL_GEOMETRY_SHADER, geometrySrc);
    }

    int status;
    m_program = glCreateProgram();
    glAttachShader(m_program, vertex);
    glAttachShader(m_program, fragment);
    if (geometrySrc != nullptr) {
        glAttachShader(m_program, geometry);
    }
    glLinkProgram(m_program);
    glGetProgramiv(m_program, GL_LINK_STATUS, &status);
    if (status == GL_FALSE) {
        char buf[1024];
        glGetProgramInfoLog(m_program, sizeof buf, nullptr, buf);
        std::cerr << "Error: shader linking failed: "
                  << buf
                  << std::endl;
    }

    if (geometrySrc != nullptr) {
        glDeleteShader(geometry);
    }
    glDeleteShader(fragment);
    glDeleteShader(vertex);
}

Shader::~Shader()
{
    glDeleteProgram(m_program);
}

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

void Shader::release() const
{
    glUseProgram(0);
}

GLint Shader::uniformLocation(const char *name) const
{
    return glGetUniformLocation(m_program, name);
}

void Shader::setUniform(GLint location, GLint value) const
{
    glUniform1i(location, value);
}

void Shader::setUniform(GLint location, GLfloat value) const
{
    glUniform1f(location, value);
}

void Shader::setUniform(GLint location, GLfloat mat[4][4]) const
{
    // GL_FALSE for column-major
    glUniformMatrix4fv(location, 1, GL_FALSE, &mat[0][0]);
}

void Shader::setUniform1dArray(GLint location, const GLfloat *values, int count) const
{
    glUniform1fv(location, count, values);
}

void Shader::setUniform2dArray(GLint location, const GLfloat *values, int count) const
{
    glUniform2fv(location, count, values);
}

void Shader::setUniform3dArray(GLint location, const GLfloat *values, int count) const
{
    glUniform3fv(location, count, values);
}

void Shader::setUniform(const char *name, GLint value) const
{
    setUniform(uniformLocation(name), value);
}

void Shader::setUniform(const char *name, GLfloat value) const
{
    setUniform(uniformLocation(name), value);
}

void Shader::setUniform1dArray(const char *name, const GLfloat *values, int count) const
{
    setUniform1dArray(uniformLocation(name), values, count);
}

void Shader::setUniform2dArray(const char *name, const GLfloat *values, int count) const
{
    setUniform2dArray(uniformLocation(name), values, count);
}

void Shader::setUniform3dArray(const char *name, const GLfloat *values, int count) const
{
    setUniform3dArray(uniformLocation(name), values, count);
}

void Shader::setUniform(const char *name, GLfloat mat[4][4]) const
{
    setUniform(uniformLocation(name), mat);
}