-
Notifications
You must be signed in to change notification settings - Fork 0
/
sageShader.cpp
378 lines (312 loc) · 9.21 KB
/
sageShader.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/******************************************************************************
* SAGE - Scalable Adaptive Graphics Environment
*
* Module: sageShader.cpp
* Author : Luc Renambot
*
* Copyright (C) 2012 Electronic Visualization Laboratory,
* University of Illinois at Chicago
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the distribution.
* * Neither the name of the University of Illinois at Chicago nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Direct questions, comments etc about SAGE to renambot@uic.edu
* http://sagecommons.org/
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(WIN32)
#define GLEW_STATIC 1
#endif
#include <GL/glew.h>
#if defined(__APPLE__)
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#include "sageShader.h"
//
// Returns 1 if an OpenGL error occurred, 0 otherwise.
//
/*int GLprintError(const char *file, int line)
{
GLenum glErr;
int retCode = 0;
glErr = glGetError();
while (glErr != GL_NO_ERROR)
{
fprintf(stderr, "GLSL> glError in file %s @ line %d: %s\n", file, line, gluErrorString(glErr));
retCode = 1;
glErr = glGetError();
}
return retCode;
}*/
//
// Print out the information log for a shader object
//
void GLSLprintShaderInfoLog(GLuint shader)
{
int infologLength = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
if (infologLength > 0)
{
infoLog = (GLchar *)malloc(infologLength);
if (infoLog == NULL)
{
fprintf(stderr, "GLSL> ERROR: Could not allocate InfoLog buffer\n");
exit(1);
}
glGetShaderInfoLog(shader, infologLength, &charsWritten, infoLog);
if (charsWritten>0)
fprintf(stderr, "GLSL> Shader InfoLog:%s\n", infoLog);
free(infoLog);
}
}
//
// Print out the information log for a program object
//
void GLSLprintProgramInfoLog(GLuint program)
{
int infologLength = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infologLength);
if (infologLength > 0)
{
infoLog = (GLchar *)malloc(infologLength);
if (infoLog == NULL)
{
fprintf(stderr, "GLSL> ERROR: Could not allocate InfoLog buffer\n");
exit(1);
}
glGetProgramInfoLog(program, infologLength, &charsWritten, infoLog);
if (charsWritten>0)
fprintf(stderr, "GLSL> Program InfoLog:%s\n", infoLog);
free(infoLog);
}
}
//
// Returns the size in bytes of the shader fileName.
// If an error occurred, it returns -1.
//
// File name convention:
//
// <fileName>.vert
// <fileName>.frag
//
int
GLSLShaderSize(char *fileName, int shaderType)
{
int fd;
char name[256];
int count = -1;
memset(name, 0, 256);
strcpy(name, fileName);
switch (shaderType)
{
case GLSLVertexShader:
strcat(name, ".vert");
break;
case GLSLFragmentShader:
strcat(name, ".frag");
break;
default:
fprintf(stderr, "GLSL> ERROR: unknown shader file type\n");
exit(1);
break;
}
//
// Open the file, seek to the end to find its length
//
#ifdef WIN32
fd = _open(name, _O_RDONLY);
if (fd != -1)
{
count = _lseek(fd, 0, SEEK_END) + 1;
_close(fd);
}
#else
fd = open(name, O_RDONLY);
if (fd != -1)
{
count = lseek(fd, 0, SEEK_END) + 1;
close(fd);
}
#endif
return count;
}
//
// Reads a shader from the supplied file and returns the shader in the
// arrays passed in. Returns 1 if successful, 0 if an error occurred.
// The parameter size is an upper limit of the amount of bytes to read.
// It is ok for it to be too big.
//
int
GLSLreadShader(char *fileName, int shaderType, char *shaderText, int size)
{
FILE *fh;
char name[100];
int count;
strcpy(name, fileName);
switch (shaderType)
{
case GLSLVertexShader:
strcat(name, ".vert");
break;
case GLSLFragmentShader:
strcat(name, ".frag");
break;
default:
fprintf(stderr, "GLSL> ERROR: unknown shader file type\n");
exit(1);
break;
}
//
// Open the file
//
fh = fopen(name, "r");
if (!fh)
return -1;
//
// Get the shader from a file.
//
fseek(fh, 0, SEEK_SET);
count = (int) fread(shaderText, 1, size, fh);
shaderText[count] = '\0';
if (ferror(fh))
count = 0;
fclose(fh);
return count;
}
//
// Allocate memory to hold the source of our shaders.
//
int
GLSLreadShaderSource(char *fileName, GLchar **vertexShader, GLchar **fragmentShader)
{
int vSize, fSize;
fprintf(stderr, "GLSL> load shader %s\n", fileName);
if (vertexShader) {
vSize = GLSLShaderSize(fileName, GLSLVertexShader);
if (vSize == -1) {
fprintf(stderr, "GLSL> Cannot determine size of the vertex shader %s\n", fileName);
return 0;
}
*vertexShader = (GLchar *) malloc(vSize);
//
// Read the source code
//
if (!GLSLreadShader(fileName, GLSLVertexShader, *vertexShader, vSize)) {
fprintf(stderr, "GLSL> Cannot read the file %s.vert\n", fileName);
return 0;
}
}
if (fragmentShader) {
fSize = GLSLShaderSize(fileName, GLSLFragmentShader);
if (fSize == -1) {
fprintf(stderr, "GLSL> Cannot determine size of the fragment shader %s\n", fileName);
return 0;
}
*fragmentShader = (GLchar *) malloc(fSize);
if (!GLSLreadShader(fileName, GLSLFragmentShader, *fragmentShader, fSize)) {
fprintf(stderr, "GLSL> Cannot read the file %s.frag\n", fileName);
return 0;
}
}
return 1;
}
//install fragment shader only
GLuint GLSLinstallFragmentShader(const GLchar *Fragment){
GLuint FS, Prog;
GLint fragCompiled, linked;
// Create a program object
Prog = glCreateProgram();
if (Fragment) {
FS = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(FS, 1, &Fragment, NULL);
glCompileShader(FS);
glGetShaderiv(FS, GL_COMPILE_STATUS, &fragCompiled);
GLSLprintShaderInfoLog(FS);
if (!fragCompiled)
return 0;
glAttachShader(Prog, FS);
// Link the program object and print out the info log
glLinkProgram(Prog);
glGetProgramiv(Prog, GL_LINK_STATUS, &linked);
GLSLprintProgramInfoLog(Prog);
if (!linked)
return 0;
glUseProgram(Prog);
return Prog;
}
return 0;
}
GLuint GLSLinstallShaders(const GLchar *Vertex, const GLchar *Fragment)
{
GLuint VS, FS, Prog; // handles to objects
GLint vertCompiled, fragCompiled; // status values
GLint linked;
// Create a program object
Prog = glCreateProgram();
// Create a vertex shader object and a fragment shader object
if (Vertex) {
VS = glCreateShader(GL_VERTEX_SHADER);
// Load source code strings into shaders
glShaderSource(VS, 1, &Vertex, NULL);
// Compile the vertex shader, and print out
// the compiler log file.
glCompileShader(VS);
glGetShaderiv(VS, GL_COMPILE_STATUS, &vertCompiled);
GLSLprintShaderInfoLog(VS);
if (!vertCompiled)
return 0;
glAttachShader(Prog, VS);
}
if (Fragment) {
FS = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(FS, 1, &Fragment, NULL);
glCompileShader(FS);
glGetShaderiv(FS, GL_COMPILE_STATUS, &fragCompiled);
GLSLprintShaderInfoLog(FS);
if (!fragCompiled)
return 0;
glAttachShader(Prog, FS);
}
// Link the program object and print out the info log
glLinkProgram(Prog);
glGetProgramiv(Prog, GL_LINK_STATUS, &linked);
GLSLprintProgramInfoLog(Prog);
if (!linked)
return 0;
return Prog;
}