-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
71 lines (62 loc) · 1.68 KB
/
util.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
#include "util.h"
GLuint loadShader(const GLchar *vs, const GLchar *fs, int numAttrs,
const char **attrs) {
GLint compiled, linked;
GLuint shader;
GLuint frag, vert;
GLint len;
int i;
/* vertex shader */
vert = glCreateShader(GL_VERTEX_SHADER);
len = strlen(vs);
glShaderSource(vert, 1, &vs, &len);
glCompileShader(vert);
glGetShaderiv(vert, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_FALSE) {
GLint logSz;
GLchar *log;
glGetShaderiv(vert, GL_INFO_LOG_LENGTH, &logSz);
log = malloc(logSz * sizeof(GLchar));
glGetShaderInfoLog(vert, logSz, &logSz, log);
puts("error: vertex shader compilation failed");
puts(log);
free(log);
}
/* fragment shader */
frag = glCreateShader(GL_FRAGMENT_SHADER);
len = strlen(fs);
glShaderSource(frag, 1, &fs, &len);
glCompileShader(frag);
glGetShaderiv(frag, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_FALSE) {
GLint logSz;
GLchar *log;
glGetShaderiv(vert, GL_INFO_LOG_LENGTH, &logSz);
log = malloc(logSz * sizeof(GLchar));
glGetShaderInfoLog(vert, logSz, &logSz, log);
puts("error: fragment shader compilation failed");
puts(log);
free(log);
}
/* link shaders */
shader = glCreateProgram();
glAttachShader(shader, vert);
glAttachShader(shader, frag);
for (i = 0; i < numAttrs; ++i) {
glBindAttribLocation(shader, i, attrs[i]);
}
glBindAttribLocation(shader, 1, "texco");
glLinkProgram(shader);
glGetProgramiv(shader, GL_LINK_STATUS, &linked);
if (linked == GL_FALSE) {
GLint logSz;
GLchar *log;
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &logSz);
log = malloc(logSz * sizeof(GLchar));
glGetProgramInfoLog(shader, logSz, &logSz, log);
puts(fs);
puts(log);
free(log);
}
return shader;
}