-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
64 lines (50 loc) · 1.8 KB
/
util.h
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
#ifndef _UTIL_H_
#define _UTIL_H_
#include <stdio.h>
#include <glad/glad.h>
bool compile_shader_file(int shader, const char* path)
{
#define SHADER_SOURCE_BUFFER_SIZE 1000
char shader_source_buffer[SHADER_SOURCE_BUFFER_SIZE];
int success = true;
FILE *source_file = fopen(path, "r");
if (source_file)
{
size_t length = fread(shader_source_buffer, sizeof(char), SHADER_SOURCE_BUFFER_SIZE - 1, source_file);
if (length == SHADER_SOURCE_BUFFER_SIZE - 1) {
fputs("SHADER_SOURCE_BUFFER overflow", stderr);
}
if (!ferror(source_file))
{
shader_source_buffer[length] = '\0';
const char* source[1] = { shader_source_buffer };
glShaderSource(shader, 1, source, NULL);
glCompileShader(shader);
int compilation_successful;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compilation_successful);
if (!compilation_successful)
{
#define ERROR_LOG_BUFFER_SIZE 512
char error_log_buffer[ERROR_LOG_BUFFER_SIZE];
glGetShaderInfoLog(shader, ERROR_LOG_BUFFER_SIZE, NULL, error_log_buffer);
int shader_type;
glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
const char* shader_type_string = (shader_type == GL_VERTEX_SHADER) ? "vertex" : "fragment";
fprintf(stderr, "%s shader compilation error:\n%s\n", shader_type_string, error_log_buffer);
}
fclose(source_file);
}
else
{
fputs("Error reading from shader source file", stderr);
success = false;
}
}
else
{
fputs("Couldn't open shader source file", stderr);
success = false;
}
return success;
}
#endif