-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglext.c
348 lines (258 loc) · 7.18 KB
/
glext.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// heligun: 3D flight sim shooter
// Copyright (c) 2016 Joseph Kuziel
//
// This software is MIT licensed.
//
#include "glext.h"
#include "sdlext.h"
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL_image.h>
void glextGetPixelFormat(
Uint32 sdl_format
, GLenum* gl_format
, GLenum* gl_type
) {
if(gl_format == NULL || gl_type == NULL) {
return;
}
switch(sdl_format) {
case SDL_PIXELFORMAT_RGB24:
*gl_format = GL_RGB;
*gl_type = GL_UNSIGNED_BYTE;
break;
case SDL_PIXELFORMAT_RGBA8888:
*gl_format = GL_RGBA;
*gl_type = GL_UNSIGNED_BYTE;
break;
}
}
GLenum glextCheckError() {
GLenum gl_error = glGetError();
switch(gl_error) {
case GL_INVALID_ENUM:
printf("[GL] Invalid value\n");
break;
case GL_INVALID_VALUE:
printf("[GL] Numeric argument out of range\n");
break;
case GL_INVALID_OPERATION:
printf("[GL] Not allowed in current state\n");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
printf("[GL] Invalid framebuffer operation\n");
break;
case GL_OUT_OF_MEMORY:
printf("[GL] Out of memory\n");
break;
// case GL_STACK_UNDERFLOW:
// printf("[GL] Stack underflow\n");
// break;
//
// case GL_STACK_OVERFLOW:
// printf("[GL] Stack overflow\n");
// break;
}
return gl_error;
}
void glextPrintShaderInfoLog(
GLuint shader
) {
GLint log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
if(log_length > 0) {
char* log = (char*)malloc(log_length);
GLint actual_log_length = 0;
glGetShaderInfoLog(shader, log_length, &actual_log_length, log);
if(actual_log_length > 0) {
printf("%s\n", log);
}
free(log);
}
}
void glextPrintProgramInfoLog(
GLuint program
) {
GLint log_length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
if(log_length > 0) {
char* log = (char*)malloc(log_length);
GLint actual_log_length = 0;
glGetProgramInfoLog(program, log_length, &actual_log_length, log);
if(actual_log_length > 0) {
printf("%s\n", log);
}
free(log);
}
}
GLuint glextLoadShader(
const char* shader_filename
, GLenum shader_type
) {
if(shader_filename == NULL) {
printf("No shader file name specified\n");
return 0;
}
GLchar* shader_src = sdlextReadTextFile(shader_filename);
if(shader_src == NULL) {
printf("Could not load shader file: %s\n", shader_filename);
return 0;
}
GLuint shader = glCreateShader(shader_type);
if(shader == 0) {
glextCheckError();
return 0;
}
#ifdef __EMSCRIPTEN__
GLchar* shader_lines[] = { "precision mediump float;", shader_src };
glShaderSource(shader, 2, (const GLchar**)shader_lines, NULL);
#else
GLchar* shader_lines[] = { shader_src };
glShaderSource(shader, 1, (const GLchar**)shader_lines, NULL);
#endif
free(shader_src);
return shader;
}
int glextCompileShader(
GLuint shader
) {
glCompileShader(shader);
GLint compile_status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if(compile_status != GL_TRUE) {
glextPrintShaderInfoLog(shader);
return 1;
}
return 0;
}
int glextLinkProgram(
GLuint program
) {
glLinkProgram(program);
GLint link_status = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &link_status);
if(link_status != GL_TRUE) {
glextPrintProgramInfoLog(program);
return 1;
}
return 0;
}
GLuint glextCreateShaderProgram(
const char* vertex_shader_filename
, const char* fragment_shader_filename
) {
// Load shaders
GLuint vertex_shader =
glextLoadShader(vertex_shader_filename, GL_VERTEX_SHADER);
if(vertex_shader == 0) {
return 0;
}
GLuint fragment_shader =
glextLoadShader(fragment_shader_filename, GL_FRAGMENT_SHADER);
if(fragment_shader == 0) {
return 0;
}
// Compile shaders
if(glextCompileShader(vertex_shader) != 0) {
printf("Failed to compile shader: %s\n", vertex_shader_filename);
return 0;
}
if(glextCompileShader(fragment_shader) != 0) {
printf("Failed to compile shader: %s\n", fragment_shader_filename);
return 0;
}
// Create program
GLuint program = glCreateProgram();
if(program == 0) {
glextCheckError();
return 0;
}
// Attach shaders
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
// Link program
if(glextLinkProgram(program) != 0) {
return 0;
}
return program;
}
GLuint glextLoadTexture(
const char* filename
, GLuint repeat
, GLuint linearFilter
) {
if(filename == NULL) {
printf("Texture filename not specified\n");
return 0;
}
SDL_Surface* surface = IMG_Load(filename);
if(surface == NULL) {
printf("Could not load texture \"%s\": %s\n", filename, IMG_GetError());
return 0;
}
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
GLint filterParam = (linearFilter == 0 ? GL_NEAREST : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterParam);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterParam);
GLint repeatParam = (repeat == 0 ? GL_CLAMP_TO_EDGE : GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeatParam);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeatParam);
GLenum pixel_format = GL_RGBA;
GLenum pixel_type = GL_UNSIGNED_BYTE;
glextGetPixelFormat(surface->format->format, &pixel_format, &pixel_type);
SDL_LockSurface(surface);
glTexImage2D(
GL_TEXTURE_2D
, 0
, pixel_format
, surface->w
, surface->h
, 0
, pixel_format
, pixel_type
, surface->pixels
);
SDL_UnlockSurface(surface);
SDL_FreeSurface(surface);
return texture;
}
int glextBindTextureToUniform(
GLuint program
, GLuint texture
, GLuint unit
, const char* uniform
) {
glUseProgram(program);
GLint uniform_loc = glGetUniformLocation(program, uniform);
if(uniform_loc == -1) {
printf("Could not find uniform location: %s\n", uniform);
return 1;
}
glUniform1i(uniform_loc, unit);
return glextCheckError();
}
GLuint glextCreateVBO(
GLfloat* vertices
, GLuint count
, GLint size
, GLenum usage
) {
GLuint geom_buffer = 0;
glGenBuffers(1, &geom_buffer);
if(geom_buffer == 0) {
printf("Could not create geometry buffer\n");
return 0;
}
glBindBuffer(GL_ARRAY_BUFFER, geom_buffer);
glBufferData(
GL_ARRAY_BUFFER
, sizeof(GLfloat) * count * size
, vertices
, usage
);
glVertexAttribPointer(0, size, GL_FLOAT, GL_FALSE, 0, NULL);
return geom_buffer;
}