aboutsummaryrefslogtreecommitdiff
path: root/src/shader.c
blob: e36a8d89e306bbd5d33a0aaa2d88d9c053884523 (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
/*
 * This file is part of mverse
 * Copyright (C) 2022  juanvalencia.xyz

 * mverse is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include "shader.h"

static char *getShaderSource(const char *shaderPath);
static void checkShaderCompile(unsigned int shader, const char *shaderPath);
static void checkProgramLink(unsigned int shader);

char *
getShaderSource(const char *shaderPath)
{
    long fileSize;
    char *shaderSource;
    FILE *shaderFile = fopen(shaderPath, "r");

    if (!shaderFile) {
        perror("getShaderSource() ERROR");
        exit(1);
    }

    fseek(shaderFile, 0L, SEEK_END);
    fileSize = ftell(shaderFile);
    if (!fileSize) {
        fprintf(stderr, "getShaderSource() ERROR: %s file is empty\n", shaderPath);
        exit(1);
    }

    rewind(shaderFile);

    shaderSource = malloc(fileSize * sizeof(char));
    if (!fscanf(shaderFile, "%[^\\]", shaderSource)) {
        perror("getShaderSource() ERROR");
        exit(1);
    }

    fclose(shaderFile);
    return shaderSource;
}

void
checkShaderCompile(unsigned int shader, const char *shaderPath)
{
    int success;
    char infoLog[512];

    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(shader, 512, NULL, infoLog);
        fprintf(stderr, "shaderCompile() ERROR\n%s %s\n", shaderPath, infoLog);
        exit(1);
    }
}

void
checkProgramLink(unsigned int shaderProgram)
{
    int success;
    char infoLog[512];
    glLinkProgram(shaderProgram);
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        fprintf(stderr, "shaderProgramLink() ERROR\n%s\n", infoLog);
        exit(1);
    }
}

unsigned int
shaderCreateProgram(const char *vertexShaderPath, const char *fragmentShaderPath)
{
    unsigned int vertexShader, fragmentShader, shaderProgram;
    char *vertexShaderSource, *fragmentShaderSource;

    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    vertexShaderSource = getShaderSource(vertexShaderPath);
    fragmentShaderSource = getShaderSource(fragmentShaderPath);

    glShaderSource(vertexShader, 1, (const char **)&vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    checkShaderCompile(vertexShader, vertexShaderPath);

    glShaderSource(fragmentShader, 1, (const char **)&fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    checkShaderCompile(fragmentShader, fragmentShaderPath);

    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    checkProgramLink(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    free(vertexShaderSource);
    free(fragmentShaderSource);

    return shaderProgram;
}

void
shaderSetfv(
        unsigned int program,
        char *uniformVariable,
        float *data,
        void (*uniform_callback)(int, int, const float *))
{
    unsigned int varLoc = glGetUniformLocation(program, uniformVariable);
    uniform_callback(varLoc, 1, data);
}


void
shaderSetMatrixfv(
        unsigned int program,
        char *uniformVariable,
        float *data,
        void (*uniform_callback)(int, int, unsigned char, const float *))
{
    unsigned int varLoc = glGetUniformLocation(program, uniformVariable);
    uniform_callback(varLoc, 1, GL_TRUE, data);
}

void
shaderSet1f(unsigned int program, char *uniformVariable, float data)
{
    unsigned int varLoc = glGetUniformLocation(program, uniformVariable);
    glUniform1f(varLoc, data);
}

void
shaderSet1i(unsigned int program, char *uniformVariable, int data)
{
    unsigned int varLoc = glGetUniformLocation(program, uniformVariable);
    glUniform1i(varLoc, data);
}
Feel free to download, copy and edit any repo