-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.c
127 lines (106 loc) · 2.79 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
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
#include <mksn-gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *load_source(char *filename)
{
char *data;
int len;
FILE *file = fopen(filename, "rb");
if (!file) {
fprintf(stderr, "error: cannot open '%s'\n", filename);
return NULL;
}
fseek(file, 0, 2);
len = ftell(file);
fseek(file, 0, 0);
data = (char *)malloc(len+1);
fread(data, 1, len, file);
data[len] = 0;
fclose(file);
return data;
}
int compile_shader(char *vertfile, char *fragfile)
{
char log[800];
char *vsrc, *fsrc;
int prog, vs, fs, len;
vsrc = load_source(vertfile);
fsrc = load_source(fragfile);
if (!vsrc || !fsrc)
return 0;
vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, (const char **)&vsrc, 0);
glCompileShader(vs);
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
glGetShaderInfoLog(vs, sizeof log, &len, log);
fprintf(stderr, "%s:\n%s", vertfile, log);
}
fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, (const char **)&fsrc, 0);
glCompileShader(fs);
glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
glGetShaderInfoLog(fs, sizeof log, &len, log);
fprintf(stderr, "%s:\n%s", fragfile, log);
}
free(vsrc);
free(fsrc);
prog = glCreateProgram();
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
glGetProgramInfoLog(prog, sizeof log, &len, log);
fprintf(stderr, "%s/%s:\n%s", vertfile, fragfile, log);
}
return prog;
}
#if 0
static void print_shader_log(char *kind, int shader)
{
int len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
char *log = malloc(len + 1);
glGetShaderInfoLog(shader, len, NULL, log);
fprintf(stderr, "--- glsl %s shader compile results ---\n%s\n", kind, log);
free(log);
}
static void print_program_log(int program)
{
int len;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
char *log = malloc(len + 1);
glGetProgramInfoLog(program, len, NULL, log);
fprintf(stderr, "--- glsl program link results ---\n%s\n", log);
free(log);
}
int compile_shader(const char *vert_src, const char *frag_src)
{
int status;
int vert = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vert, 1, &vert_src, NULL);
glCompileShader(vert);
glGetShaderiv(vert, GL_COMPILE_STATUS, &status);
if (!status)
print_shader_log("vertex", vert);
int frag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(frag, 1, &frag_src, NULL);
glCompileShader(frag);
glGetShaderiv(frag, GL_COMPILE_STATUS, &status);
if (!status)
print_shader_log("fragment", frag);
int prog = glCreateProgram();
glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &status);
if (!status)
print_program_log(prog);
glDeleteShader(vert);
glDeleteShader(frag);
return prog;
}
#endif