-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.c
75 lines (63 loc) · 1.84 KB
/
shader.c
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
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <GL/gl3w.h>
#include "shader.h"
int loadShader( unsigned int shader, const char * path ) {
// First, let's get the filesize
struct stat s;
stat(path, &s);
int shader_length = s.st_size;
// Open the file in read-only mode
FILE * shaderfile;
shaderfile = fopen(path, "rb");
// Allocate required mem
char * shadercode;
shadercode = malloc(shader_length + 1); // + extra byte for \0
shadercode[shader_length] = '\0';
// Read the data
fread(shadercode, sizeof(char), shader_length, shaderfile);
// Tell OpenGL what the source is
glShaderSource(shader, 1, (const char **)&shadercode, &shader_length);
// Clean up after ourselves!
free(shadercode);
fclose(shaderfile);
return 0;
};
void compileShader( unsigned int shader ) {
int errorLength = 0;
int success;
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if( !success ) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &errorLength);
if( errorLength > 1 ) {
char* errorMessage = malloc(errorLength);
glGetShaderInfoLog(shader, errorLength, NULL, errorMessage);
fprintf(stderr, "%s\n", errorMessage);
free(errorMessage);
}
}
}
int buildShader( unsigned int vs, unsigned int fs, unsigned int po ) {
compileShader(vs);
compileShader(fs);
glAttachShader(po, vs);
glAttachShader(po, fs);
glLinkProgram(po);
return checkProgForErrors(po);
};
int checkProgForErrors( unsigned int prog ) {
int errorLength = 0;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &errorLength);
if(errorLength > 1) { // 0 characters of length + \0 = 1 character of length
char* errorMessage;
errorMessage = malloc(errorLength);
glGetProgramInfoLog(prog, errorLength, NULL, errorMessage);
fprintf(stderr, "%s\n", errorMessage);
free(errorMessage);
return 1;
}
return 0;
}