-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLUtils.h
480 lines (390 loc) · 14.1 KB
/
GLUtils.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#ifndef GLUTILS_H
#define GLUTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "glew.h"
#include <GL/gl.h>
#include "Vec.h"
#include "Mat.h"
#include "Array2.h"
#define glCheckError() glCheckErrorFileLine(__FILE__, __LINE__)
bool glCheckErrorFileLine(const char* file,int line);
void glColor(const Vec3f& c);
void glColor(const Vec4f& c);
void glNormal(const Vec3f& n);
void glVertex(const Vec2f& v);
void glVertex(const Vec3f& v);
class GLTexture
{
public:
GLTexture(GLenum target) : target(target),initialized(false) {}
~GLTexture()
{
if (initialized)
{
glDeleteTextures(1,&texture);
}
}
GLTexture& setParameter(GLenum name,GLint param)
{
bind();
glTexParameteri(target,name,param);
return *this;
}
GLTexture& setParameter(GLenum name,GLfloat param)
{
bind();
glTexParameterf(target,name,param);
return *this;
}
GLTexture& setWrapS(GLint param)
{
return setParameter(GL_TEXTURE_WRAP_S,param);
}
GLTexture& setWrapT(GLint param)
{
return setParameter(GL_TEXTURE_WRAP_T,param);
}
GLTexture& setWrapR(GLint param)
{
return setParameter(GL_TEXTURE_WRAP_R,param);
}
GLTexture& setMinFilter(GLint param)
{
return setParameter(GL_TEXTURE_MIN_FILTER,param);
}
GLTexture& setMagFilter(GLint param)
{
return setParameter(GL_TEXTURE_MAG_FILTER,param);
}
GLTexture& setMinLod(GLfloat param)
{
return setParameter(GL_TEXTURE_MIN_LOD,param);
}
GLTexture& setMaxLod(GLfloat param)
{
return setParameter(GL_TEXTURE_MAX_LOD,param);
}
GLTexture& setBaseLevel(GLint param)
{
return setParameter(GL_TEXTURE_BASE_LEVEL,param);
}
GLTexture& setMaxLevel(GLint param)
{
return setParameter(GL_TEXTURE_MAX_LEVEL,param);
}
GLTexture& setLodBias(GLfloat param)
{
return setParameter(GL_TEXTURE_LOD_BIAS,param);
}
GLTexture& setGenerateMipmap(GLboolean param)
{
return setParameter(GL_GENERATE_MIPMAP,param);
}
GLTexture& bind()
{
if (!initialized) init();
glBindTexture(target,texture);
return *this;
}
private:
const GLenum target;
GLuint texture;
bool initialized;
void init()
{
glGenTextures(1,&texture);
initialized = true;
}
};
class GLTexture1D : public GLTexture
{
public:
GLTexture1D() : GLTexture(GL_TEXTURE_1D) {};
~GLTexture1D() {}
GLTexture1D& setParameter(GLenum name,GLint param) { GLTexture::setParameter(name,param); return *this; }
GLTexture1D& setParameter(GLenum name,GLfloat param) { GLTexture::setParameter(name,param); return *this; }
GLTexture1D& setWrapS(GLint param) { GLTexture::setWrapS(param); return *this; }
GLTexture1D& setMinFilter(GLint param) { GLTexture::setMinFilter(param); return *this; }
GLTexture1D& setMagFilter(GLint param) { GLTexture::setMagFilter(param); return *this; }
GLTexture1D& setMinLod(GLfloat param) { GLTexture::setMinLod(param); return *this; }
GLTexture1D& setMaxLod(GLfloat param) { GLTexture::setMaxLod(param); return *this; }
GLTexture1D& setBaseLevel(GLint param) { GLTexture::setBaseLevel(param); return *this; }
GLTexture1D& setMaxLevel(GLint param) { GLTexture::setMaxLevel(param); return *this; }
GLTexture1D& setLodBias(GLfloat param) { GLTexture::setLodBias(param); return *this; }
GLTexture1D& setGenerateMipmap(GLboolean param) { GLTexture::setGenerateMipmap(param); return *this; }
GLTexture1D& bind() { GLTexture::bind(); return *this; }
};
class GLTexture2D : public GLTexture
{
public:
GLTexture2D() : GLTexture(GL_TEXTURE_2D) {};
~GLTexture2D() {}
GLTexture2D& setParameter(GLenum name,GLint param) { GLTexture::setParameter(name,param); return *this; }
GLTexture2D& setParameter(GLenum name,GLfloat param) { GLTexture::setParameter(name,param); return *this; }
GLTexture2D& setWrapS(GLint param) { GLTexture::setWrapS(param); return *this; }
GLTexture2D& setWrapT(GLint param) { GLTexture::setWrapT(param); return *this; }
GLTexture2D& setMinFilter(GLint param) { GLTexture::setMinFilter(param); return *this; }
GLTexture2D& setMagFilter(GLint param) { GLTexture::setMagFilter(param); return *this; }
GLTexture2D& setMinLod(GLfloat param) { GLTexture::setMinLod(param); return *this; }
GLTexture2D& setMaxLod(GLfloat param) { GLTexture::setMaxLod(param); return *this; }
GLTexture2D& setBaseLevel(GLint param) { GLTexture::setBaseLevel(param); return *this; }
GLTexture2D& setMaxLevel(GLint param) { GLTexture::setMaxLevel(param); return *this; }
GLTexture2D& setLodBias(GLfloat param) { GLTexture::setLodBias(param); return *this; }
GLTexture2D& setGenerateMipmap(GLboolean param) { GLTexture::setGenerateMipmap(param); return *this; }
GLTexture2D& bind() { GLTexture::bind(); return *this; }
GLTexture2D& setImage(const Array2<Vec3uc>& image,GLint level=0,GLenum internalFormat=GL_RGB8,GLenum format=GL_RGB)
{
bind();
glTexImage2D(GL_TEXTURE_2D,level,internalFormat,image.width(),image.height(),0,format,GL_UNSIGNED_BYTE,image.data());
return *this;
}
GLTexture2D& setImage(const Array2<Vec4uc>& image,GLint level=0,GLenum internalFormat=GL_RGBA8,GLenum format=GL_RGBA)
{
bind();
glTexImage2D(GL_TEXTURE_2D,level,internalFormat,image.width(),image.height(),0,format,GL_UNSIGNED_BYTE,image.data());
return *this;
}
};
class GLTexture3D : public GLTexture
{
public:
GLTexture3D() : GLTexture(GL_TEXTURE_3D) {};
~GLTexture3D() {}
GLTexture3D& setParameter(GLenum name,GLint param) { GLTexture::setParameter(name,param); return *this; }
GLTexture3D& setParameter(GLenum name,GLfloat param) { GLTexture::setParameter(name,param); return *this; }
GLTexture3D& setWrapS(GLint param) { GLTexture::setWrapS(param); return *this; }
GLTexture3D& setWrapT(GLint param) { GLTexture::setWrapT(param); return *this; }
GLTexture3D& setWrapR(GLint param) { GLTexture::setWrapR(param); return *this; }
GLTexture3D& setMinFilter(GLint param) { GLTexture::setMinFilter(param); return *this; }
GLTexture3D& setMagFilter(GLint param) { GLTexture::setMagFilter(param); return *this; }
GLTexture3D& setMinLod(GLfloat param) { GLTexture::setMinLod(param); return *this; }
GLTexture3D& setMaxLod(GLfloat param) { GLTexture::setMaxLod(param); return *this; }
GLTexture3D& setBaseLevel(GLint param) { GLTexture::setBaseLevel(param); return *this; }
GLTexture3D& setMaxLevel(GLint param) { GLTexture::setMaxLevel(param); return *this; }
GLTexture3D& setLodBias(GLfloat param) { GLTexture::setLodBias(param); return *this; }
GLTexture3D& setGenerateMipmap(GLboolean param) { GLTexture::setGenerateMipmap(param); return *this; }
GLTexture3D& bind() { GLTexture::bind(); return *this; }
};
class GLTextureCubeMap : public GLTexture
{
public:
GLTextureCubeMap() : GLTexture(GL_TEXTURE_CUBE_MAP) {};
~GLTextureCubeMap() {}
GLTextureCubeMap& setParameter(GLenum name,GLint param) { GLTexture::setParameter(name,param); return *this; }
GLTextureCubeMap& setParameter(GLenum name,GLfloat param) { GLTexture::setParameter(name,param); return *this; }
GLTextureCubeMap& setWrapS(GLint param) { GLTexture::setWrapS(param); return *this; }
GLTextureCubeMap& setWrapT(GLint param) { GLTexture::setWrapT(param); return *this; }
GLTextureCubeMap& setMinFilter(GLint param) { GLTexture::setMinFilter(param); return *this; }
GLTextureCubeMap& setMagFilter(GLint param) { GLTexture::setMagFilter(param); return *this; }
GLTextureCubeMap& setMinLod(GLfloat param) { GLTexture::setMinLod(param); return *this; }
GLTextureCubeMap& setMaxLod(GLfloat param) { GLTexture::setMaxLod(param); return *this; }
GLTextureCubeMap& setBaseLevel(GLint param) { GLTexture::setBaseLevel(param); return *this; }
GLTextureCubeMap& setMaxLevel(GLint param) { GLTexture::setMaxLevel(param); return *this; }
GLTextureCubeMap& setLodBias(GLfloat param) { GLTexture::setLodBias(param); return *this; }
GLTextureCubeMap& setGenerateMipmap(GLboolean param) { GLTexture::setGenerateMipmap(param); return *this; }
GLTextureCubeMap& bind() { GLTexture::bind(); return *this; }
GLTextureCubeMap& setImage(GLenum target,const Array2<Vec3uc>& image,GLint level=0,GLenum internalFormat=GL_RGB8,GLenum format=GL_RGB)
{
bind();
glTexImage2D(target,level,internalFormat,image.width(),image.height(),0,format,GL_UNSIGNED_BYTE,image.data());
return *this;
}
GLTextureCubeMap& setImages(const Array2<Vec3uc>& imagePX,
const Array2<Vec3uc>& imageNX,
const Array2<Vec3uc>& imagePY,
const Array2<Vec3uc>& imageNY,
const Array2<Vec3uc>& imagePZ,
const Array2<Vec3uc>& imageNZ,
GLint level=0,GLenum internalFormat=GL_RGB8,GLenum format=GL_RGB)
{
setImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X,imagePX,level,internalFormat,format);
setImage(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,imageNX,level,internalFormat,format);
setImage(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,imagePY,level,internalFormat,format);
setImage(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,imageNY,level,internalFormat,format);
setImage(GL_TEXTURE_CUBE_MAP_POSITIVE_Z,imagePZ,level,internalFormat,format);
setImage(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,imageNZ,level,internalFormat,format);
return *this;
}
};
class GLProgram
{
public:
GLProgram() : initialized(false) {}
~GLProgram()
{
if (initialized)
{
glDetachShader(p,v);
glDetachShader(p,f);
glDeleteProgram(p);
}
}
bool load(const char* vertexShaderFileName,const char* fragmentShaderFileName)
{
if (!initialized) init();
char* vs = NULL;
char* fs = NULL;
vs = textFileRead(vertexShaderFileName);
fs = textFileRead(fragmentShaderFileName);
if (vs==NULL || fs==NULL) return false;
const char* vv = vs;
const char* ff = fs;
glShaderSource(v,1,&vv,NULL);
glShaderSource(f,1,&ff,NULL);
delete vs;
delete fs;
glCompileShader(v);
glCompileShader(f);
printShaderInfoLog(v);
printShaderInfoLog(f);
glLinkProgram(p);
printProgramInfoLog(p);
return true;
}
GLProgram& use()
{
glUseProgram(p);
return *this;
}
GLProgram& setUniform(const char* name,GLint v0)
{
glUniform1i(glGetUniformLocation(p,name),v0);
return *this;
}
GLProgram& setUniform(const char* name,GLfloat v0)
{
glUniform1f(glGetUniformLocation(p,name),v0);
return *this;
}
GLProgram& setUniform(const char* name,GLfloat v0,GLfloat v1)
{
setUniform(name,Vec2f(v0,v1));
return *this;
}
GLProgram& setUniform(const char* name,GLfloat v0,GLfloat v1,GLfloat v2)
{
setUniform(name,Vec3f(v0,v1,v2));
return *this;
}
GLProgram& setUniform(const char* name,GLfloat v0,GLfloat v1,GLfloat v2,GLfloat v3)
{
setUniform(name,Vec4f(v0,v1,v2,v3));
return *this;
}
GLProgram& setUniform(const char* name,const Vec2f& v)
{
glUniform2f(glGetUniformLocation(p,name),v(0),v(1));
return *this;
}
GLProgram& setUniform(const char* name,const Vec3f& v)
{
glUniform3f(glGetUniformLocation(p,name),v(0),v(1),v(2));
return *this;
}
GLProgram& setUniform(const char* name,const Vec4f& v)
{
glUniform4f(glGetUniformLocation(p,name),v(0),v(1),v(2),v(3));
return *this;
}
GLProgram& setUniform(const char* name,const Mat2x2f& m)
{
glUniformMatrix2fv(glGetUniformLocation(p,name),1,1,m.constRowMajorData());
return *this;
}
GLProgram& setUniform(const char* name,const Mat3x3f& m)
{
glUniformMatrix3fv(glGetUniformLocation(p,name),1,1,m.constRowMajorData());
return *this;
}
GLProgram& setUniform(const char* name,const Mat4x4f& m)
{
glUniformMatrix4fv(glGetUniformLocation(p,name),1,1,m.constRowMajorData());
return *this;
}
GLProgram& bindTexture(const char* name,GLTexture& texture,GLint unit)
{
setUniform(name,unit);
glActiveTexture(GL_TEXTURE0 unit);
texture.bind();
return *this;
}
/*
template<typename T> GLProgram& set(const char* name,const T& value)
{
return setUniform(name,value);
}
*/
GLint getAttribLocation(const char* name)
{
return glGetAttribLocation(p,name);
}
private:
GLuint v;
GLuint f;
GLuint p;
bool initialized;
void init()
{
p = glCreateProgram();
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
glAttachShader(p,v);
glAttachShader(p,f);
initialized = true;
};
char* textFileRead(const char* fileName)
{
FILE *fp;
char *content = NULL;
int count=0;
if (fileName != NULL)
{
fp = fopen(fileName,"rt");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0)
{
content = new char[count 1];
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
void printShaderInfoLog(GLuint obj)
{
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetShaderiv(obj,GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 1)
{
infoLog = (char *)malloc(infologLength);
glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
void printProgramInfoLog(GLuint obj)
{
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetProgramiv(obj,GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 1)
{
infoLog = (char *)malloc(infologLength);
glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
};
#endif