aboutsummaryrefslogtreecommitdiff
path: root/09-september/tomcat/renderer/shader.c
blob: 3f6a5d7b2176c25b32432ffe1bab71a0d5840ecc (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "renderer.h"
#include "../util/util.h"

#include <stdlib.h>
#include <string.h>

#define MAX_HASH_SHADER   16
static Shader *shader_hash_table[MAX_HASH_SHADER];

static void CompileShader(const char *source, GLuint shaderID)
{
    glShaderSource(shaderID, 1, &source, 0);
    glCompileShader(shaderID);
    GLint error;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &error);
    if(error != GL_TRUE)
    {
        GLint logLenth;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLenth);
        GLchar buffer[logLenth];
        glGetShaderInfoLog(shaderID, logLenth, &logLenth, buffer);
        glDeleteShader(shaderID);
        Util_FatalError("Some shader failed to compile:\n%s", buffer);
    }
}

Shader *shader_new(const char *name, const char *vertexShaderPath, const char *fragShaderPath)
{
    if(strlen(name) >= MAX_PATH_LENGTH)
        Util_FatalError("File following shader name is too long: %s", name);

    Shader *s;
    s = shader_get(name);
    if(s != NULL)
        return s;

    char *vertexShaderSource = Util_LoadFile(vertexShaderPath);
    char *fragmentShaderSource = Util_LoadFile(fragShaderPath);

    GLuint vs = 0, fs = 0, program;
    vs = glCreateShader(GL_VERTEX_SHADER);
    fs = glCreateShader(GL_FRAGMENT_SHADER);

    if(vs == 0 || fs == 0)
        Util_FatalError("Shaders could not be created\n");

    program = glCreateProgram();

    CompileShader(vertexShaderSource, vs);
    CompileShader(fragmentShaderSource, fs);

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glLinkProgram(program);

    GLint error;
    glGetProgramiv(program, GL_LINK_STATUS, &error);

    if(error != GL_TRUE)
    {
        GLint logLength;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);

        GLchar buffer[logLength];
        glGetProgramInfoLog(program, logLength, &logLength, buffer);

        glDeleteProgram(program);
        glDeleteShader(vs);
        glDeleteShader(fs);

        Util_FatalError("Shader program failed to link!:\n%s", buffer);
    }
    /** Free some usless resources **/
    glDetachShader(program, vs);
    glDetachShader(program, fs);
    glDeleteShader(vs);
    glDeleteShader(fs);

    free(vertexShaderSource);
    free(fragmentShaderSource);

    /** Alloc the new texture **/
    s = malloc( sizeof(Shader) );
    memset(s, 0, sizeof(Shader) );
    s->id = program;

    /** Register inside the resource manager **/
    unsigned int hash_ = Util_Hash( name );
    hash_ %= MAX_HASH_SHADER;

    render.shaders[render.num_shaders] = s;
    render.num_shaders += 1;

    strcpy(s->name, name);
    s->_hash_next = shader_hash_table[hash_];
    shader_hash_table[hash_] = s;

    s->_hash = hash_;

    /** Return the final result **/
    return s;
}

Shader *shader_get(const char *name)
{
    Shader *s;

    unsigned int hash_ = Util_Hash( name );
    hash_ %= MAX_HASH_SHADER;

    if(shader_hash_table[hash_] != NULL)
    {
        for(s = shader_hash_table[hash_]; s; s = s->_hash_next)
        {
            if( s->_hash == hash_ )
                return s;
        }
    }
    return NULL;
}

void shader_purge(Shader *shader)
{
    /** Purge the opengl data **/
    if(shader->id != 0)
    {
        glUseProgram(0);
        glDeleteProgram(shader->id);
        shader->id = 0;
    }
}

GLint shader_get_uniform_location( Shader *s, const char *uniformName )
{
    GLint u = glGetUniformLocation(s->id, uniformName);
    if(u == GL_INVALID_INDEX)
        Util_FatalError("Uniform \"%s\" could not be found!", uniformName);
    else
        return u;

    return 0;
}

GLint shader_get_attrib_location( Shader *s, const char *attributeName )
{
    GLint attrLocation = glGetAttribLocation(s->id, attributeName);
    if(attrLocation < 0)
        Util_FatalError("Attribute \"%s\" could not be found!\n", attributeName);
    return attrLocation;
}

void shader_set_uniform_mat4( Shader *s, const char *name, const float matrix[16] )
{
    GLint location = shader_get_uniform_location(s, name);
    glUniformMatrix4fv(location, 1, GL_FALSE, matrix);
}

void shader_set_uniform_float( Shader *s, const char *name, const float val )
{
    GLint location = shader_get_uniform_location(s, name);
    glUniform1f(location, val);
}

void shader_set_uniform_vec2( Shader *s, const char *name, const float vec[2] )
{
    GLint location = shader_get_uniform_location(s, name);
    glUniform2fv(location, 1, vec);
}

void shader_set_uniform_vec3( Shader *s, const char *name, const float vec[3] )
{
    GLint location = shader_get_uniform_location(s, name);
    glUniform3fv(location, 1, vec);
}

void shader_set_uniform_vec4( Shader *s, const char *name, const float vec[4] )
{
    GLint location = shader_get_uniform_location(s, name);
    glUniform4fv(location, 1, vec);
}

void shader_set_uniform_int( Shader *s, const char *name, const int val )
{
    GLint location = shader_get_uniform_location(s, name);
    glUniform1i(location, val);
}