-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderSetup.cpp
319 lines (259 loc) · 7.05 KB
/
ShaderSetup.cpp
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
//
// ShaderSetup
//
// Sets up a GLSL shader based on supplied source files.
//
// Based on code from www.lighthouse3d.com
//
// This code can be compiled as either C or C++.
//
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cstring>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "ShaderSetup.h"
///
// readTextFile(name)
//
// Read the text file given as 'name'.
//
// Returns the contents of the file in a dynamically-allocated
// string buffer, or NULL if an error occurs.
///
GLchar *readTextFile( const char *name ) {
FILE *fp;
GLchar *content = NULL;
long count = 0;
if( name != NULL ) {
// Attempt to open the file
// On Windows systems, may want to use "rt" here
fp = fopen( name, "r" );
if( fp != NULL ) {
// Determine its length
fseek( fp, 0, SEEK_END );
count = ftell( fp );
rewind( fp );
if( count > 0 ) {
// Allocate the string buffer
#ifdef __cplusplus
content = new GLchar[ sizeof(GLchar) * (count+1) ];
#else
content = (GLchar *) malloc( sizeof(GLchar) * (count+1) );
#endif
// Read the file into the buffer
count = fread( content, sizeof(GLchar), count, fp );
// Add a NUL terminator
content[count] = '\0';
}
fclose(fp);
} else {
perror( name );
}
} else {
fprintf( stderr, "error: no file name specified\n" );
}
return content;
}
///
// printShaderInfoLog(shader)
//
// Print the information log from a shader compilation attempt
///
void printShaderInfoLog( GLuint shader ) {
GLint length = 0;
GLsizei nchars = 0;
char *log;
// Determine the length of the information log
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &length );
if( length > 0 ) {
// Allocate a buffer for the log
#ifdef __cplusplus
log = new char[ length ];
#else
log = (char *) malloc( length );
#endif
if( log != NULL ) {
// Retrieve the log
glGetShaderInfoLog( shader, length, &nchars, log );
// Report it
if( log[0] != '\0' ) {
printf( "Shader log: '%s'\n", log );
}
#ifdef __cplusplus
delete [] log;
#else
free( log );
#endif
} else {
printf( "Shader log non-empty, but allocate failed\n" );
}
}
}
///
// printProgramInfoLog(shader)
//
// Print a program information log
//
// This is identical to printShaderInfoLog(), except that it uses
// glGetProgramiv() and glGetProgramInfoLog() instead of the *Shader*()
// versions.
///
void printProgramInfoLog( GLuint shader ) {
GLint length = 0;
GLsizei nchars = 0;
char *log;
// Determine the length of the information log
glGetProgramiv( shader, GL_INFO_LOG_LENGTH, &length );
if( length > 0 ) {
// Allocate a buffer for the log
#ifdef __cplusplus
log = new char[ length ];
#else
log = (char *) malloc( length );
#endif
if( log != NULL ) {
// Retrieve the log
glGetProgramInfoLog( shader, length, &nchars, log );
// Report it
if( log[0] != '\0' ) {
printf( "Program log: '%s'\n", log );
}
#ifdef __cplusplus
delete [] log;
#else
free( log );
#endif
} else {
printf( "Program log non-empty, but allocate failed\n" );
}
}
}
///
// errorString( code )
//
// Returns a const char* with a text version of the supplied error code.
///
const char *errorString( ShaderError code ) {
const char *msg;
static char buffer[256];
switch( code ) {
case E_NO_ERROR:
msg = "No error";
break;
case E_VS_LOAD:
msg = "Error loading vertex shader";
break;
case E_FS_LOAD:
msg = "Error loading fragment shader";
break;
case E_VS_COMPILE:
msg = "Error compiling vertex shader";
break;
case E_FS_COMPILE:
msg = "Error compiling fragment shader";
break;
case E_SHADER_LINK:
msg = "Error linking shader";
break;
default:
sprintf( buffer, "Unknown error code %d", code );
msg = (const char *) buffer;
}
return( msg );
}
///
// shaderSetup(vertex,fragment,err)
//
// Set up a GLSL shader program.
//
// Requires the name of a vertex program and a fragment
// program. Returns a handle to the created GLSL program
//
// Arguments:
// vert - vertex shader program source file
// frag - fragment shader program source file
// err - pointer to status variable
//
// On success:
// Returns the GLSL shader program handle, and sets the 'err'
// parameter to E_NO_ERROR.
//
// On failure:
// Returns 0, and assigns an error code to 'err'.
///
GLuint shaderSetup( const char *vert, const char *frag, ShaderError *err ) {
GLchar *vsrc = NULL, *fsrc = NULL;
GLuint vs, fs, prog;
GLint flag;
// Assume that everything will work
*err = E_NO_ERROR;
// Create the shader handles
vs = glCreateShader( GL_VERTEX_SHADER );
fs = glCreateShader( GL_FRAGMENT_SHADER );
// Read in shader source
vsrc = readTextFile( vert );
if( vsrc == NULL ) {
fprintf( stderr, "Error reading vertex shader file %s\n",
vert);
*err = E_VS_LOAD;
return( 0 );
}
fsrc = readTextFile( frag );
if( fsrc == NULL ) {
fprintf( stderr, "Error reading fragment shader file %s\n",
frag);
*err = E_FS_LOAD;
#ifdef __cplusplus
delete [] vsrc;
#else
free( vsrc );
#endif
return( 0 );
}
// Attach the source to the shaders
glShaderSource( vs, 1, (const GLchar **) &vsrc, NULL );
glShaderSource( fs, 1, (const GLchar **) &fsrc, NULL );
// We're done with the source code now
#ifdef __cplusplus
delete [] vsrc;
delete [] fsrc;
#else
free(vsrc);
free(fsrc);
#endif
// Compile the shaders, and print any relevant message logs
glCompileShader( vs );
glGetShaderiv( vs, GL_COMPILE_STATUS, &flag );
printShaderInfoLog( vs );
if( flag == GL_FALSE ) {
*err = E_VS_COMPILE;
return( 0 );
}
glCompileShader( fs );
glGetShaderiv( fs, GL_COMPILE_STATUS, &flag );
printShaderInfoLog( fs );
if( flag == GL_FALSE ) {
*err = E_FS_COMPILE;
return( 0 );
}
// Create the program and attach the shaders
prog = glCreateProgram();
glAttachShader( prog, vs );
glAttachShader( prog, fs );
// Report any message log information
printProgramInfoLog( prog );
// Link the program, and print any message log information
glLinkProgram( prog );
glGetProgramiv( prog, GL_LINK_STATUS, &flag );
printProgramInfoLog( prog );
if( flag == GL_FALSE ) {
*err = E_SHADER_LINK;
return( 0 );
}
return( prog );
}