forked from mackstann/mona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_utils.c
172 lines (147 loc) · 4.71 KB
/
gl_utils.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
#include "gl_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include "stb_image.h"
/* print errors in shader compilation */
void _print_shader_info_log (GLuint shader_index)
{
int max_length = 2048;
int actual_length = 0;
char log[2048];
glGetShaderInfoLog (shader_index, max_length, &actual_length, log);
printf ("shader info log for GL index %i:\n%s\n", shader_index, log);
}
/* print errors in shader linking */
void _print_programme_info_log (GLuint sp)
{
int max_length = 2048;
int actual_length = 0;
char log[2048];
glGetProgramInfoLog (sp, max_length, &actual_length, log);
printf ("program info log for GL index %i:\n%s", sp, log);
}
const char* read_file(const char* filename)
{
FILE* fp = fopen(filename, "r");
if (!fp) perror("Can't open file"), exit(1);
fseek(fp , 0L , SEEK_END);
long size = ftell(fp);
rewind(fp);
void* buffer = calloc(1, size + 1);
if(NULL == buffer) fclose(fp), fputs("memory alloc fails",stderr),exit(1);
if(1 != fread(buffer, size, 1, fp)) fclose(fp), free(buffer), fputs("file read fails",stderr), exit(1);
fclose(fp);
return (const char*)buffer;
}
unsigned char* load_texture (const char* file_name, int* width, int* height)
{
int n;
// TODO: use SOIL instead?
unsigned char* image_data = (unsigned char*)stbi_load(file_name, width, height, &n, STBI_rgb_alpha);
if (!image_data) {
fprintf (stderr, "ERROR: could not load %s\n", file_name);
return NULL;
}
int width_in_bytes = *width * 4;
unsigned char *top = NULL;
unsigned char *bottom = NULL;
unsigned char temp = 0;
int half_height = *height / 2;
for (int row = 0; row < half_height; row++) {
top = image_data + row * width_in_bytes;
bottom = image_data + (*height - row - 1) * width_in_bytes;
for (int col = 0; col < width_in_bytes; col++) {
temp = *top;
*top = *bottom;
*bottom = temp;
top++;
bottom++;
}
}
// TODO delete image_data
return image_data;
}
const char* GL_type_to_string (GLenum type)
{
switch (type)
{
case GL_BOOL: return "bool";
case GL_INT: return "int";
case GL_FLOAT: return "float";
case GL_FLOAT_VEC2: return "vec2";
case GL_FLOAT_VEC3: return "vec3";
case GL_FLOAT_VEC4: return "vec4";
case GL_FLOAT_MAT2: return "mat2";
case GL_FLOAT_MAT3: return "mat3";
case GL_FLOAT_MAT4: return "mat4";
case GL_SAMPLER_2D: return "sampler2D";
case GL_SAMPLER_3D: return "sampler3D";
case GL_SAMPLER_CUBE: return "samplerCube";
case GL_SAMPLER_2D_SHADOW: return "sampler2DShadow";
default: break;
}
return "other";
}
/* print absolutely everything about a shader - only useful if you get really
stuck wondering why a shader isn't working properly */
void print_all (GLuint sp)
{
int params = -1;
int i;
printf ("--------------------\nshader programme %i info:\n", sp);
glGetProgramiv (sp, GL_LINK_STATUS, ¶ms);
printf ("GL_LINK_STATUS = %i\n", params);
glGetProgramiv (sp, GL_ATTACHED_SHADERS, ¶ms);
printf ("GL_ATTACHED_SHADERS = %i\n", params);
glGetProgramiv (sp, GL_ACTIVE_ATTRIBUTES, ¶ms);
printf ("GL_ACTIVE_ATTRIBUTES = %i\n", params);
for (i = 0; i < params; i++) {
char name[64];
int max_length = 64;
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveAttrib (sp, i, max_length, &actual_length, &size, &type, name);
if (size > 1) {
int j;
for (j = 0; j < size; j++) {
char long_name[64];
int location;
sprintf (long_name, "%s[%i]", name, j);
location = glGetAttribLocation (sp, long_name);
printf (" %i) type:%s name:%s location:%i\n",
i, GL_type_to_string (type), long_name, location);
}
} else {
int location = glGetAttribLocation (sp, name);
printf (" %i) type:%s name:%s location:%i\n",
i, GL_type_to_string (type), name, location);
}
}
glGetProgramiv (sp, GL_ACTIVE_UNIFORMS, ¶ms);
printf ("GL_ACTIVE_UNIFORMS = %i\n", params);
for (i = 0; i < params; i++) {
char name[64];
int max_length = 64;
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveUniform (sp, i, max_length, &actual_length, &size, &type, name);
if (size > 1) {
int j;
for (j = 0; j < size; j++) {
char long_name[64];
int location;
sprintf (long_name, "%s[%i]", name, j);
location = glGetUniformLocation (sp, long_name);
printf (" %i) type:%s name:%s location:%i\n",
i, GL_type_to_string (type), long_name, location);
}
} else {
int location = glGetUniformLocation (sp, name);
printf (" %i) type:%s name:%s location:%i\n",
i, GL_type_to_string (type), name, location);
}
}
_print_programme_info_log (sp);
}