forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLSL_helper.cpp
315 lines (252 loc) · 8.08 KB
/
GLSL_helper.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
/*
* GLSL_helper.cpp
* CSC473
*
* Many useful helper functions for GLSL shaders - gleaned from various sources including orange book
* Created by zwood on 2/21/10.
*
*/
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <stdexcept>
#include "GLSL_helper.h"
using namespace std;
/*function to help load the shaders (both vertex and fragment */
bool installShader(const char *vShaderFName, const char *fShaderFName,
GLuint &ShadeProg) {
const GLchar *vShaderStr = textFileRead((char*) vShaderFName);
const GLchar *fShaderStr = textFileRead((char*) fShaderFName);
if (vShaderStr == NULL || fShaderStr == NULL) {
return false;
}
GLuint VS; //handles to shader object
GLuint FS; //handles to frag shader object
GLint vCompiled, fCompiled, linked; //status of shader
VS = glCreateShader(GL_VERTEX_SHADER);
FS = glCreateShader(GL_FRAGMENT_SHADER);
//load the source
glShaderSource(VS, 1, &vShaderStr, NULL);
glShaderSource(FS, 1, &fShaderStr, NULL);
//compile vertex shader and print log
glCompileShader(VS);
/* check shader status requires helper functions */
printOpenGLError();
glGetShaderiv(VS, GL_COMPILE_STATUS, &vCompiled);
cout << "Vertex ";
printShaderInfoLog(VS);
if (!vCompiled) {
printf("Error compiling vertex shader '%s'\n", vShaderFName);
}
//compile frag shader and print log
glCompileShader(FS);
/* check shader status requires helper functions */
printOpenGLError();
glGetShaderiv(FS, GL_COMPILE_STATUS, &fCompiled);
cout << "Frag ";
printShaderInfoLog(FS);
if (!vCompiled) {
printf("Error compiling fragment shader '%s'\n", fShaderFName);
}
if (!vCompiled || !fCompiled) {
return false;
}
//create a program object and attach the compiled shader
ShadeProg = glCreateProgram();
glAttachShader(ShadeProg, VS);
glAttachShader(ShadeProg, FS);
glLinkProgram(ShadeProg);
/* check shader status requires helper functions */
printOpenGLError();
glGetProgramiv(ShadeProg, GL_LINK_STATUS, &linked);
printProgramInfoLog(ShadeProg);
glUseProgram(ShadeProg);
// TODO - check linked status
printf("sucessfully installed shaders '%s' and '%s'\n=================\n",
vShaderFName, fShaderFName);
return true;
}
void checkGlErrors() {
const GLenum errCode = glGetError();
if (errCode != GL_NO_ERROR) {
string error("GL Error: ");
//error += reinterpret_cast<const char*>(gluErrorString(errCode));
cerr << error << endl;
throw runtime_error(error);
}
}
int printOglError (const char *file, int line) {
/* Returns 1 if an OpenGL error occurred, 0 otherwise. */
GLenum glErr;
int retCode = 0;
glErr = glGetError ();
while (glErr != GL_NO_ERROR)
{
//printf ("glError in file %s @ line %d: %s\n", file, line, gluErrorString (glErr));
retCode = 1;
glErr = glGetError ();
}
return retCode;
}
void printShaderInfoLog (GLuint shader)
{
GLint infologLength = 0;
GLint charsWritten = 0;
GLchar *infoLog;
printOpenGLError (); // Check for OpenGL errors
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infologLength);
printOpenGLError (); // Check for OpenGL errors
if (infologLength > 0)
{
infoLog = (GLchar *)malloc (infologLength);
if (infoLog == NULL)
{
puts ("ERROR: Could not allocate InfoLog buffer");
exit (1);
}
glGetShaderInfoLog (shader, infologLength, &charsWritten, infoLog);
printf ("Shader InfoLog:\n%s----\n", infoLog);
free (infoLog);
}
printOpenGLError(); // Check for OpenGL errors
}
/* Print out the information log for a program object */
void printProgramInfoLog (GLuint program)
{
GLint infologLength = 0;
GLint charsWritten = 0;
GLchar *infoLog;
printOpenGLError (); // Check for OpenGL errors
glGetProgramiv (program, GL_INFO_LOG_LENGTH, &infologLength);
printOpenGLError (); // Check for OpenGL errors
if (infologLength > 0)
{
infoLog = (GLchar *)malloc (infologLength);
if (infoLog == NULL)
{
puts ("ERROR: Could not allocate InfoLog buffer");
exit (1);
}
glGetProgramInfoLog (program, infologLength, &charsWritten, infoLog);
printf ("Program InfoLog:\n%s----\n", infoLog);
free (infoLog);
}
printOpenGLError (); // Check for OpenGL errors
}
//A helper routine to make it easier to set uniform variables in the shader
GLint getUniLoc(GLuint program, const GLchar *name) {
GLint loc;
loc = glGetUniformLocation(program, name);
if (loc ==1) {
printf("No such uniform named %s\n", name);
}
printOpenGLError();
return loc;
}
void getGLversion() {
/*
GLint major, minor;
major = minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
printf("GL Version (integer): %d.%d\n", major, minor);
*/
// From GLSL 4.0 Cookbook
const GLubyte *renderer = glGetString( GL_RENDERER );
const GLubyte *vendor = glGetString( GL_VENDOR );
const GLubyte *version = glGetString( GL_VERSION );
const GLubyte *glslVersion = glGetString( GL_SHADING_LANGUAGE_VERSION );
printf("GL Vendor: %s\n", vendor);
printf("GL Renderer: %s\n", renderer);
printf("GL Version (string): %s\n", version);
printf("GLSL Version: %s\n", glslVersion);
}
// textfile.cpp
//
// simple reading and writing for text files
//
// www.lighthouse3d.com
//
// You may use these functions freely.
// they are provided as is, and no warranties, either implicit,
// or explicit are given
//////////////////////////////////////////////////////////////////////
char *textFileRead(char *fn) {
FILE *fp;
char *content = NULL;
int count=0;
if (fn != NULL) {
fp = fopen(fn,"rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
int textFileWrite(char *fn, char *s) {
FILE *fp;
int status = 0;
if (fn != NULL) {
fp = fopen(fn,"w");
if (fp != NULL) {
if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
status = 1;
fclose(fp);
}
}
return(status);
}
#ifdef DO_NOT_DEFINE
// from stackoverflow.com. Only works when there's no frag shader
void usePointSprites() {
const GLint texWidth = 256;
const GLint texHeight = 256;
const float texHalfWidth = texWidth/2.0;
const float texHalfHeight = texHeight/2.0;
unsigned char* pData = new unsigned char[texWidth*texHeight*4];
for(int y=0; y<texHeight; ++y){
for(int x=0; x<texWidth; ++x){
int offs = (x + y*texWidth) * 4;
float xoffs = ((float)x - texHalfWidth) / texHalfWidth;
float yoffs = ((float)y - texHalfWidth) / texHalfHeight;
float alpha = 1.0f - std::sqrt(xoffs*xoffs + yoffs*yoffs);
if(alpha < 0.0f)
alpha = 0.0f;
pData[offs + 0] = 255; //r
pData[offs + 1] = 0; //g
pData[offs + 2] = 0; //b
pData[offs + 3] = 255.0f * alpha; // *
//printf("alpha: %f\n", pData[x + y*texWidth + 3]);
}
}
GLuint texture_name;
glGenTextures(1, &texture_name);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_name);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPointSize(32.0f);
/*
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR);
// glDisable(GL_DEPTH_TEST);
*/
glEnable(GL_POINT_SMOOTH);
}
#endif