-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl2.c
144 lines (103 loc) · 2.77 KB
/
gl2.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <GL/glew.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "gl2.h"
#include "io.h"
#include "tle.h"
void bmm_gl2_tle(GLuint const object,
PFNGLGETSHADERIVPROC const iv,
PFNGLGETSHADERINFOLOGPROC const infoLog) {
GLint length;
iv(object, GL_INFO_LOG_LENGTH, &length);
if (length <= 0)
return;
size_t const size = (size_t) length;
char *const buf = malloc(size);
if (buf == NULL) {
BMM_TLE_STDS();
return;
}
infoLog(object, (GLsizei) size, NULL, (GLchar *) buf);
BMM_TLE_EXTS(BMM_TLE_NUM_GL, "%s", buf);
free(buf);
}
bool bmm_gl2_compile(GLuint *const pshader,
GLenum const type, char const *const path) {
GLuint const shader = glCreateShader(type);
if (shader == 0)
return false;
size_t size;
void *const buf = bmm_io_conts(&size, path);
if (buf == NULL) {
glDeleteShader(shader);
return false;
}
GLchar const *str = buf;
GLint const length = (GLint) size;
glShaderSource(shader, 1, &str, &length);
free(buf);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == 0) {
bmm_gl2_tle(shader, glGetShaderiv, glGetShaderInfoLog);
glDeleteShader(shader);
return false;
}
*pshader = shader;
return true;
}
bool bmm_gl2_link(GLuint *const pprogram,
GLuint const vshader, GLuint const fshader) {
GLuint const program = glCreateProgram();
if (program == 0)
return false;
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == 0) {
bmm_gl2_tle(program, glGetProgramiv, glGetProgramInfoLog);
glDeleteProgram(program);
return false;
}
*pprogram = program;
return true;
}
bool bmm_gl2_build(GLuint *const pvshader, GLuint *const pfshader,
GLuint *const pprogram, char const *const vpath, char const *const fpath) {
GLuint vshader;
if (!bmm_gl2_compile(&vshader, GL_VERTEX_SHADER, vpath))
return false;
GLuint fshader;
if (!bmm_gl2_compile(&fshader, GL_FRAGMENT_SHADER, fpath)) {
glDeleteShader(vshader);
return false;
}
GLuint program;
if (!bmm_gl2_link(&program, vshader, fshader)) {
glDeleteShader(fshader);
glDeleteShader(vshader);
return false;
}
*pvshader = vshader;
*pfshader = fshader;
*pprogram = program;
return true;
}
bool bmm_gl2_buffer(GLuint *const pbuffer,
GLenum const target, GLenum const usage,
GLvoid const *const data, GLsizei const size) {
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(target, buffer);
glBufferData(target, size, data, usage);
if (glGetError() != GL_NO_ERROR) {
glDeleteBuffers(1, &buffer);
return false;
}
*pbuffer = buffer;
return true;
}