-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.c
More file actions
83 lines (70 loc) · 2.46 KB
/
Copy pathshader.c
File metadata and controls
83 lines (70 loc) · 2.46 KB
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
#include "shader.h"
#include <stdio.h>
#include <stdlib.h>
#include <gl.h>
void shader_create(Shader *s) {
s->program = glCreateProgram();
}
void shader_use(Shader *s) {
glUseProgram(s->program);
}
void shader_compile(Shader *s, const GLuint shaderTypeEnum, const GLchar *shaderSource) {
GLuint shader = glCreateShader(shaderTypeEnum);
glShaderSource(shader, 1, &shaderSource, NULL);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[INFOLOG_LEN];
glGetShaderInfoLog(shader, INFOLOG_LEN, NULL, infoLog);
fprintf(stderr, "[%s] Compilation error\n%s\n", s->name, infoLog);
exit(EXIT_FAILURE);
}
// Attach shader to the program
glAttachShader(s->program, shader);
glDeleteShader(shader);
}
void shader_link(Shader *s) {
glLinkProgram(s->program);
GLint success;
glGetProgramiv(s->program, GL_LINK_STATUS, &success);
if (!success) {
GLchar infoLog[INFOLOG_LEN];
glGetProgramInfoLog(s->program, INFOLOG_LEN, NULL, infoLog);
fprintf(stderr, "[%s] Link error\n%s\n", s->name, infoLog);
exit(EXIT_FAILURE);
}
}
void shader_set_uniform_int(Shader *s, const GLchar *uniform_name, const int value) {
shader_use(s);
glUniform1i(shader_get_uniform_location(s, uniform_name), value);
}
void shader_set_uniform_float(Shader *s, const GLchar *uniform_name, const float value) {
shader_use(s);
glUniform1f(shader_get_uniform_location(s, uniform_name), value);
}
void shader_set_uniform_vec(Shader *s, const GLchar *uniform_name, const int dim, const float value[]) {
shader_use(s);
switch (dim) {
case 2: {
glUniform2fv(shader_get_uniform_location(s, uniform_name), 1, value);
} break;
case 3: {
glUniform3fv(shader_get_uniform_location(s, uniform_name), 1, value);
} break;
case 4: {
glUniform4fv(shader_get_uniform_location(s, uniform_name), 1, value);
} break;
default:
fprintf(stderr, "[%s] Cannot set only set vec{2|3|4} uniforms", s->name);
exit(EXIT_FAILURE);
}
}
GLint shader_get_uniform_location(Shader *s, const GLchar *uniform_name) {
GLint location = glGetUniformLocation(s->program, uniform_name);
if (location < 0) {
fprintf(stderr, "[%s] Uniform \"%s\" not found in the program (location = %d)", s->name, uniform_name, location);
exit(EXIT_FAILURE);
}
return location;
}