summaryrefslogtreecommitdiff
path: root/src/shader.c
blob: c78a7e5d05fb28364e34d33661a7d2440d3238d9 (plain) (blame)
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
#include "shader.h"
#include <stdio.h>
#include <stdlib.h>



/**
 * Simple method to read shaders from path and return a string
 * containing the code. Function allocates memory which needs 
 * to be cleaned up
 * @param path Path to shader GLSL file
 * @return buf Contents of shader file as a string
 */
char* read_shader(char* path) {
    FILE* file = fopen(path, "r");
    fseek(file, 0, SEEK_END);
    int file_size = ftell(file);
    rewind(file);
    char* buf = malloc(file_size);
    fread(buf, file_size, 1, file);
    return buf;
}

/**
 * Compile some given shader code
 * @param type The type of shader (GL_VERTEX_SHADER, GL_FRAGMENT_SHADER etc)
 * @param code The string corresponding to the shader code
 * @return shader The GLuint pointer to the compiled shader
 */
GLuint compile_shader(GLuint type, const char* code) {
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &code, NULL);
    glCompileShader(shader);
    return shader;
}

/*
 * Given a vertex and fragment shader, link and compile a shader program,
 * returning the GLuint pointing to that program
 * @param vs The vertex shader string, usually returend from read_shader
 * @param fs The fragment shader string, usually returend from read_shader
 * @return shader_program The pointer to the compiled shader
 */
GLuint create_shader_program(const char* vs, const char* fs) {
    GLuint shader_program = glCreateProgram();
    GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vs);
    GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fs);
    glAttachShader(shader_program, fragment_shader);
    glAttachShader(shader_program, vertex_shader);
    glLinkProgram(shader_program);
    return shader_program;
}


GLuint shader_init() {
    char* shaders[2] = { "shaders/fragment.glsl", "shaders/vertex.glsl" };
    char* fragment_shader = read_shader(shaders[0]);
    char* vertex_shader = read_shader(shaders[1]);
    GLuint shader_program = create_shader_program(vertex_shader, fragment_shader);
    free(vertex_shader);
    free(fragment_shader);
    return shader_program;
}