-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjzq_gl.h
688 lines (556 loc) · 23.5 KB
/
jzq_gl.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// This software is in the public domain. Where that dedication is not
// recognized, you are granted a perpetual, irrevocable license to copy
// and modify this file as you see fit.
#ifndef JZQ_GL_H_
#define JZQ_GL_H_
#include <vector>
#include "jzq.h"
template<GLenum TARGET,typename GLTexture>
class GLTextureBase
{
public:
GLTexture& operator=(GLTextureBase&& t);
GLTextureBase(const GLTextureBase& t) = delete;
GLTexture& operator=(const GLTextureBase& t) = delete;
GLTexture& bind(GLint unit=0);
GLTexture& setParameter(GLenum name,GLint param);
GLTexture& setParameter(GLenum name,GLfloat param);
GLTexture& setMinFilter(GLint param);
GLTexture& setMagFilter(GLint param);
GLTexture& setMinLod(GLfloat param);
GLTexture& setMaxLod(GLfloat param);
GLTexture& setBaseLevel(GLint param);
GLTexture& setMaxLevel(GLint param);
GLTexture& setLodBias(GLfloat param);
GLTexture& generateMipmap();
GLuint id();
GLint internalFormat(GLint level=0);
protected:
GLTextureBase();
GLTextureBase(GLTextureBase&& t);
~GLTextureBase();
GLint getTexLevelParameteri(GLint level,GLenum pname);
GLfloat getTexLevelParameterf(GLint level,GLenum pname);
static GLenum formatFor(GLint internalFormat);
static GLenum typeFor(GLint internalFormat);
private:
GLuint _id;
void init();
};
class GLTexture1D : public GLTextureBase<GL_TEXTURE_1D,GLTexture1D>
{
public:
GLTexture1D();
GLTexture1D(GLTexture1D&& t);
GLTexture1D& operator=(GLTexture1D&& t);
GLTexture1D(const GLTexture1D& t) = delete;
GLTexture1D& operator=(const GLTexture1D& t) = delete;
GLTexture1D(GLint internalFormat,int width,void* data=0);
GLTexture1D(GLint internalFormat,int width,GLenum format,void* data);
GLTexture1D(GLint internalFormat,int width,GLenum format,GLenum type,void* data);
template<typename T> explicit GLTexture1D(const std::vector<T>& image);
template<typename T> GLTexture1D(GLint internalFormat,const std::vector<T>& image);
template<typename T> GLTexture1D(GLint internalFormat,GLenum format,const std::vector<T>& image);
GLTexture1D& setWrap(GLint wrapS);
GLint width(GLint level=0);
private:
void init1D(GLint internalFormat,int width,GLenum format,GLenum type,void* data);
};
class GLTexture2D : public GLTextureBase<GL_TEXTURE_2D,GLTexture2D>
{
public:
inline GLTexture2D();
inline GLTexture2D(GLTexture2D&& t);
inline GLTexture2D& operator=(GLTexture2D&& t);
inline GLTexture2D(const GLTexture2D& t) = delete;
inline GLTexture2D& operator=(const GLTexture2D& t) = delete;
inline GLTexture2D(GLint internalFormat,int width,int height,void* data=0);
inline GLTexture2D(GLint internalFormat,int width,int height,GLenum format,void* data);
inline GLTexture2D(GLint internalFormat,int width,int height,GLenum format,GLenum type,void* data);
template<typename T> explicit GLTexture2D(const Array2<T>& image);
template<typename T> GLTexture2D(GLint internalFormat,const Array2<T>& image);
template<typename T> GLTexture2D(GLint internalFormat,GLenum format,const Array2<T>& image);
inline GLTexture2D& setWrap(GLint wrapST);
inline GLTexture2D& setWrap(GLint wrapS,GLint wrapT);
inline GLint width(GLint level=0);
inline GLint height(GLint level=0);
private:
inline void init2D(GLint internalFormat,int width,int height,GLenum format,GLenum type,void* data);
};
class GLTexture3D : public GLTextureBase<GL_TEXTURE_3D,GLTexture3D>
{
public:
GLTexture3D();
GLTexture3D(GLTexture3D&& t);
GLTexture3D& operator=(GLTexture3D&& t);
GLTexture3D(const GLTexture3D& t) = delete;
GLTexture3D& operator=(const GLTexture3D& t) = delete;
GLTexture3D(GLint internalFormat,int width,int height,int depth,void* data=0);
GLTexture3D(GLint internalFormat,int width,int height,int depth,GLenum format,void* data);
GLTexture3D(GLint internalFormat,int width,int height,int depth,GLenum format,GLenum type,void* data);
template<typename T> explicit GLTexture3D(const Array3<T>& image);
template<typename T> GLTexture3D(GLint internalFormat,const Array3<T>& image);
template<typename T> GLTexture3D(GLint internalFormat,GLenum format,const Array3<T>& image);
GLTexture3D& setWrap(GLint wrapSTR);
GLTexture3D& setWrap(GLint wrapS,GLint wrapT,GLint wrapR);
GLint width(GLint level=0);
GLint height(GLint level=0);
GLint depth(GLint level=0);
private:
void init3D(GLint internalFormat,int width,int height,int depth,GLenum format,GLenum type,void* data);
};
class GLShader
{
public:
inline GLShader();
inline GLShader(GLShader&& shader);
inline GLShader& operator=(GLShader&& shader);
inline ~GLShader();
inline GLShader(const GLShader& shader) = delete;
inline GLShader& operator=(const GLShader& shader) = delete;
inline GLShader(const std::string& vertexShaderSource,
const std::string& fragmentShaderSource);
inline GLShader& use();
inline GLint linkStatus();
inline std::string infoLog();
inline GLShader& setUniform(const std::string& name,GLint value);
inline GLShader& setUniform(const std::string& name,GLfloat value);
inline GLShader& bindTexture(const std::string& samplerName,GLTexture1D& texture);
inline GLShader& bindTexture(const std::string& samplerName,GLTexture2D& texture);
inline GLShader& bindTexture(const std::string& samplerName,GLTexture3D& texture);
private:
GLuint _id;
GLint _linkStatus;
std::string _infoLog;
inline GLuint compileShader(GLenum type,const std::string& source);
struct Sampler
{
std::string name;
GLenum type;
GLint unit;
Sampler() {}
Sampler(const std::string& name,GLenum type,GLint unit) : name(name), type(type), unit(unit) {}
};
std::vector<Sampler> _samplers;
inline int samplerIndexForName(const std::string& name);
template<typename GLTexture,GLenum SAMPLER_TYPE>
GLShader& _bindTexture(const std::string& samplerName,GLTexture& texture);
};
inline GLShader GLShaderFromFile(const std::string& vertexShaderFileName,
const std::string& fragmentShaderFileName);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<GLenum TARGET,typename GLTexture>
GLTextureBase<TARGET,GLTexture>::GLTextureBase() : _id(0) {}
template<GLenum TARGET,typename GLTexture>
GLTextureBase<TARGET,GLTexture>::GLTextureBase(GLTextureBase<TARGET,GLTexture>&& t)
{
_id = t._id;
t._id = 0;
}
template<GLenum TARGET,typename GLTexture>
GLTexture& GLTextureBase<TARGET,GLTexture>::operator=(GLTextureBase<TARGET,GLTexture>&& t)
{
if (_id!=0) { glDeleteTextures(1,&_id); }
_id = 0;
_id = t._id;
t._id = 0;
return static_cast<GLTexture&>(*this);
}
template<GLenum TARGET,typename GLTexture>
GLTextureBase<TARGET,GLTexture>::~GLTextureBase()
{
if (_id!=0) { glDeleteTextures(1,&_id); }
}
template<GLenum TARGET,typename GLTexture>
void GLTextureBase<TARGET,GLTexture>::init()
{
glGenTextures(1,&_id);
}
template<GLenum TARGET,typename GLTexture>
GLuint GLTextureBase<TARGET,GLTexture>::id()
{
if (_id==0) { init(); }
return _id;
}
template<GLenum TARGET,typename GLTexture>
GLTexture& GLTextureBase<TARGET,GLTexture>::bind(GLint unit)
{
if (_id==0) { init(); }
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(TARGET,_id);
return static_cast<GLTexture&>(*this);
}
template<GLenum TARGET,typename GLTexture>
GLTexture& GLTextureBase<TARGET,GLTexture>::setParameter(GLenum name,GLint param)
{
bind();
glTexParameteri(TARGET,name,param);
return static_cast<GLTexture&>(*this);
}
template<GLenum TARGET,typename GLTexture>
GLTexture& GLTextureBase<TARGET,GLTexture>::setParameter(GLenum name,GLfloat param)
{
bind();
glTexParameterf(TARGET,name,param);
return static_cast<GLTexture&>(*this);
}
template<GLenum TARGET,typename GLTexture>
GLint GLTextureBase<TARGET,GLTexture>::getTexLevelParameteri(GLint level,GLenum pname)
{
bind();
GLint param = GL_INVALID_VALUE;
glGetTexLevelParameteriv(TARGET,level,pname,¶m);
return param;
}
template<GLenum TARGET,typename GLTexture>
GLfloat GLTextureBase<TARGET,GLTexture>::getTexLevelParameterf(GLint level,GLenum pname)
{
bind();
GLfloat param = GL_INVALID_VALUE;
glGetTexLevelParameterfv(TARGET,level,pname,¶m);
return param;
}
template<GLenum TARGET,typename GLTexture>
GLint GLTextureBase<TARGET,GLTexture>::internalFormat(GLint level)
{
return getTexLevelParameteri(level,GL_TEXTURE_INTERNAL_FORMAT);
}
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setMinFilter(GLint param) { return setParameter(GL_TEXTURE_MIN_FILTER,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setMagFilter(GLint param) { return setParameter(GL_TEXTURE_MAG_FILTER,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setMinLod(GLfloat param) { return setParameter(GL_TEXTURE_MIN_LOD,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setMaxLod(GLfloat param) { return setParameter(GL_TEXTURE_MAX_LOD,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setBaseLevel(GLint param) { return setParameter(GL_TEXTURE_BASE_LEVEL,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setMaxLevel(GLint param) { return setParameter(GL_TEXTURE_MAX_LEVEL,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::setLodBias(GLfloat param) { return setParameter(GL_TEXTURE_LOD_BIAS,param); }
template<GLenum TARGET,typename GLTexture> GLTexture& GLTextureBase<TARGET,GLTexture>::generateMipmap()
{
bind();
glGenerateMipMap(TARGET);
return static_cast<GLTexture&>(*this);
}
template<GLenum TARGET,typename GLTexture>
GLenum GLTextureBase<TARGET,GLTexture>::formatFor(GLint internalFormat)
{
switch(internalFormat)
{
case GL_R8: return GL_RED;
case GL_RG8: return GL_RG;
case GL_RGB8: return GL_RGB;
case GL_RGBA8: return GL_RGBA;
case GL_R8I: return GL_RED;
case GL_RG8I: return GL_RG;
case GL_RGB8I: return GL_RGB;
case GL_RGBA8I: return GL_RGBA;
case GL_R8UI: return GL_RED;
case GL_RG8UI: return GL_RG;
case GL_RGB8UI: return GL_RGB;
case GL_RGBA8UI: return GL_RGBA;
case GL_R16I: return GL_RED;
case GL_RG16I: return GL_RG;
case GL_RGB16I: return GL_RGB;
case GL_RGBA16I: return GL_RGBA;
case GL_R16UI: return GL_RED;
case GL_RG16UI: return GL_RG;
case GL_RGB16UI: return GL_RGB;
case GL_RGBA16UI: return GL_RGBA;
case GL_R32I: return GL_RED;
case GL_RG32I: return GL_RG;
case GL_RGB32I: return GL_RGB;
case GL_RGBA32I: return GL_RGBA;
case GL_R32UI: return GL_RED;
case GL_RG32UI: return GL_RG;
case GL_RGB32UI: return GL_RGB;
case GL_RGBA32UI: return GL_RGBA;
case GL_R32F: return GL_RED;
case GL_RG32F: return GL_RG;
case GL_RGB32F: return GL_RGB;
case GL_RGBA32F: return GL_RGBA;
case GL_SRGB8: return GL_RGB;
case GL_SRGB8_ALPHA8: return GL_RGBA;
case GL_DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT;
case GL_DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT;
case GL_DEPTH_COMPONENT32: return GL_DEPTH_COMPONENT;
case GL_DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT;
}
return GL_INVALID_VALUE;
}
template<GLenum TARGET,typename GLTexture>
GLenum GLTextureBase<TARGET,GLTexture>::typeFor(GLint internalFormat)
{
switch(internalFormat)
{
case GL_R8: return GL_UNSIGNED_BYTE;
case GL_RG8: return GL_UNSIGNED_BYTE;
case GL_RGB8: return GL_UNSIGNED_BYTE;
case GL_RGBA8: return GL_UNSIGNED_BYTE;
case GL_R8I: return GL_BYTE;
case GL_RG8I: return GL_BYTE;
case GL_RGB8I: return GL_BYTE;
case GL_RGBA8I: return GL_BYTE;
case GL_R8UI: return GL_UNSIGNED_BYTE;
case GL_RG8UI: return GL_UNSIGNED_BYTE;
case GL_RGB8UI: return GL_UNSIGNED_BYTE;
case GL_RGBA8UI: return GL_UNSIGNED_BYTE;
case GL_R16I: return GL_SHORT;
case GL_RG16I: return GL_SHORT;
case GL_RGB16I: return GL_SHORT;
case GL_RGBA16I: return GL_SHORT;
case GL_R16UI: return GL_UNSIGNED_SHORT;
case GL_RG16UI: return GL_UNSIGNED_SHORT;
case GL_RGB16UI: return GL_UNSIGNED_SHORT;
case GL_RGBA16UI: return GL_UNSIGNED_SHORT;
case GL_R32I: return GL_INT;
case GL_RG32I: return GL_INT;
case GL_RGB32I: return GL_INT;
case GL_RGBA32I: return GL_INT;
case GL_R32UI: return GL_UNSIGNED_INT;
case GL_RG32UI: return GL_UNSIGNED_INT;
case GL_RGB32UI: return GL_UNSIGNED_INT;
case GL_RGBA32UI: return GL_UNSIGNED_INT;
case GL_R32F: return GL_FLOAT;
case GL_RG32F: return GL_FLOAT;
case GL_RGB32F: return GL_FLOAT;
case GL_RGBA32F: return GL_FLOAT;
case GL_SRGB8: return GL_UNSIGNED_BYTE;
case GL_SRGB8_ALPHA8: return GL_UNSIGNED_BYTE;
case GL_DEPTH_COMPONENT16: return GL_UNSIGNED_SHORT;
case GL_DEPTH_COMPONENT24: return GL_UNSIGNED_INT;
case GL_DEPTH_COMPONENT32: return GL_UNSIGNED_INT;
case GL_DEPTH_COMPONENT32F: return GL_FLOAT;
}
return GL_INVALID_VALUE;
}
template<typename T>
struct GLInternalFormatFor { };
template<> struct GLInternalFormatFor<unsigned char> { static const GLint value = GL_R8; };
template<> struct GLInternalFormatFor<Vec2uc> { static const GLint value = GL_RG8; };
template<> struct GLInternalFormatFor<Vec3uc> { static const GLint value = GL_RGB8; };
template<> struct GLInternalFormatFor<Vec4uc> { static const GLint value = GL_RGBA8; };
template<> struct GLInternalFormatFor<int> { static const GLint value = GL_R32I; };
template<> struct GLInternalFormatFor<Vec2i> { static const GLint value = GL_RG32I; };
template<> struct GLInternalFormatFor<Vec3i> { static const GLint value = GL_RGB32I; };
template<> struct GLInternalFormatFor<Vec4i> { static const GLint value = GL_RGBA32I; };
template<> struct GLInternalFormatFor<float> { static const GLint value = GL_R32F; };
template<> struct GLInternalFormatFor<Vec2f> { static const GLint value = GL_RG32F; };
template<> struct GLInternalFormatFor<Vec3f> { static const GLint value = GL_RGB32F; };
template<> struct GLInternalFormatFor<Vec4f> { static const GLint value = GL_RGBA32F; };
inline GLTexture2D::GLTexture2D() {}
inline GLTexture2D::GLTexture2D(GLTexture2D&& t) : GLTextureBase(std::move(t)) {}
inline GLTexture2D& GLTexture2D::operator=(GLTexture2D&& t) { return GLTextureBase::operator=(std::move(t)); }
inline void GLTexture2D::init2D(GLint internalFormat,int width,int height,GLenum format,GLenum type,void* data)
{
bind();
glPixelStorei(GL_UNPACK_SWAP_BYTES,GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST,GL_FALSE);
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,0);
glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
glPixelStorei(GL_UNPACK_SKIP_IMAGES,0);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,internalFormat,width,height,0,format,type,data);
setWrap(GL_CLAMP_TO_EDGE);
setMinFilter(GL_NEAREST);
setMagFilter(GL_NEAREST);
}
inline GLTexture2D::GLTexture2D(GLint internalFormat,int width,int height,void* data)
{
init2D(internalFormat,width,height,formatFor(internalFormat),typeFor(internalFormat),data);
}
inline GLTexture2D::GLTexture2D(GLint internalFormat,int width,int height,GLenum format,void* data)
{
init2D(internalFormat,width,height,format,typeFor(internalFormat),data);
}
inline GLTexture2D::GLTexture2D(GLint internalFormat,int width,int height,GLenum format,GLenum type,void* data)
{
init2D(internalFormat,width,height,format,type,data);
}
template<typename T>
GLTexture2D::GLTexture2D(const Array2<T>& image)
{
init2D(GLInternalFormatFor<T>::value,image.width(),image.height(),formatFor(GLInternalFormatFor<T>::value),typeFor(GLInternalFormatFor<T>::value),(void*)image.data());
}
template<typename T>
GLTexture2D::GLTexture2D(GLint internalFormat,const Array2<T>& image)
{
init2D(internalFormat,image.width(),image.height(),formatFor(GLInternalFormatFor<T>::value),typeFor(GLInternalFormatFor<T>::value),(void*)image.data());
}
template<typename T>
GLTexture2D::GLTexture2D(GLint internalFormat,GLenum format,const Array2<T>& image)
{
init2D(internalFormat,image.width(),image.height(),format,typeFor(GLInternalFormatFor<T>::value),(void*)image.data());
}
inline GLTexture2D& GLTexture2D::setWrap(GLint wrapS,GLint wrapT)
{
setParameter(GL_TEXTURE_WRAP_S,wrapS);
setParameter(GL_TEXTURE_WRAP_T,wrapT);
return *this;
}
inline GLTexture2D& GLTexture2D::setWrap(GLint wrapST)
{
return setWrap(wrapST,wrapST);
}
inline GLint GLTexture2D::width(GLint level)
{
return getTexLevelParameteri(level,GL_TEXTURE_WIDTH);
}
inline GLint GLTexture2D::height(GLint level)
{
return getTexLevelParameteri(level,GL_TEXTURE_HEIGHT);
}
inline GLShader::GLShader() : _id(0),_linkStatus(GL_FALSE),_infoLog("") {}
inline GLShader::GLShader(GLShader&& shader)
{
_id = shader._id;
_linkStatus = shader._linkStatus;
_infoLog = std::move(shader._infoLog);
_samplers = std::move(shader._samplers);
shader._id = 0;
shader._linkStatus = GL_FALSE;
}
inline GLShader& GLShader::operator=(GLShader&& shader)
{
if (_id!=0) { glDeleteProgram(_id); }
_id = 0;
_id = shader._id;
_linkStatus = shader._linkStatus;
_infoLog = std::move(shader._infoLog);
_samplers = std::move(shader._samplers);
shader._id = 0;
shader._linkStatus = GL_FALSE;
return *this;
}
inline GLShader::~GLShader()
{
if (_id!=0) { glDeleteProgram(_id); }
}
inline GLuint GLShader::compileShader(GLenum type,const std::string& source)
{
const GLuint shader = glCreateShader(type);
const GLchar* shaderSource = (const GLchar*)source.c_str();
glShaderSource(shader,1,&shaderSource,0);
glCompileShader(shader);
return shader;
}
inline GLShader::GLShader(const std::string& vertexShaderSource,
const std::string& fragmentShaderSource)
: _id(0),_linkStatus(GL_FALSE),_infoLog("")
{
const GLuint vertexShader = compileShader(GL_VERTEX_SHADER,vertexShaderSource);
const GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER,fragmentShaderSource);
const GLuint program = glCreateProgram();
glAttachShader(program,vertexShader);
glAttachShader(program,fragmentShader);
glLinkProgram(program);
glGetProgramiv(program,GL_LINK_STATUS,&_linkStatus);
GLint logLength = 0;
glGetProgramiv(program,GL_INFO_LOG_LENGTH,&logLength);
if (logLength>0)
{
_infoLog.resize(logLength);
glGetProgramInfoLog(program,logLength,&logLength,&_infoLog[0]);
_infoLog.pop_back();
}
glDetachShader(program,vertexShader);
glDetachShader(program,fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
{
GLint numUniforms = 0;
glGetProgramiv(program,GL_ACTIVE_UNIFORMS,&numUniforms);
GLint maxLength = 0;
glGetProgramiv(program,GL_ACTIVE_UNIFORM_MAX_LENGTH,&maxLength);
GLint unit = 0;
for(int index=0;index<numUniforms;index++)
{
std::string name(maxLength+1,0); // XXX
GLint arraySize = 0;
GLenum type = 0;
GLsizei actualLength = 0;
glGetActiveUniform(program,index,maxLength,&actualLength,&arraySize,&type,&name[0]);
name.resize(actualLength);
if (type==GL_SAMPLER_1D ||
type==GL_SAMPLER_2D ||
type==GL_SAMPLER_3D ||
type==GL_SAMPLER_CUBE)
{
_samplers.push_back(Sampler(name,type,unit));
unit++;
}
}
}
_id = program;
}
inline GLShader& GLShader::use()
{
glUseProgram(_id);
return *this;
}
inline GLint GLShader::linkStatus()
{
return _linkStatus;
}
inline std::string GLShader::infoLog()
{
return _infoLog;
}
inline int GLShader::samplerIndexForName(const std::string& name)
{
for(int i=0;i<_samplers.size();i++)
{
if (_samplers[i].name==name) { return i; }
}
return -1;
}
inline GLShader& GLShader::setUniform(const std::string& name,GLint value)
{
use();
// XXX: check uniform exists & type matches
glUniform1i(glGetUniformLocation(_id,name.c_str()),value);
return *this;
}
inline GLShader& GLShader::setUniform(const std::string& name,GLfloat value)
{
use();
// XXX: check uniform exists & type matches
glUniform1f(glGetUniformLocation(_id,name.c_str()),value);
return *this;
}
template<typename GLTexture,GLenum SAMPLER_TYPE>
GLShader& GLShader::_bindTexture(const std::string& samplerName,GLTexture& texture)
{
const int samplerIndex = samplerIndexForName(samplerName);
if (samplerIndex==-1) { printf("sampler %s does not exist!\n",samplerName.c_str()); return *this; }
const Sampler& sampler = _samplers[samplerIndex];
if (sampler.type!=SAMPLER_TYPE) { printf("wrong sampler type\n"); }
texture.bind(sampler.unit);
setUniform(samplerName,sampler.unit);
return *this;
}
inline GLShader& GLShader::bindTexture(const std::string& samplerName,GLTexture1D& texture) { return _bindTexture<GLTexture1D,GL_SAMPLER_1D>(samplerName,texture); }
inline GLShader& GLShader::bindTexture(const std::string& samplerName,GLTexture2D& texture) { return _bindTexture<GLTexture2D,GL_SAMPLER_2D>(samplerName,texture); }
inline GLShader& GLShader::bindTexture(const std::string& samplerName,GLTexture3D& texture) { return _bindTexture<GLTexture3D,GL_SAMPLER_3D>(samplerName,texture); }
namespace jzq_detail
{
inline std::string stringFromFile(const std::string& fileName)
{
FILE* f = jzq_fopen(fileName.c_str(),"rb");
if (!f) { return std::string(); }
fseek(f,0,SEEK_END);
const int size = ftell(f);
rewind(f);
std::string content(size,'\0');
if (fread(&content[0],sizeof(char),size,f)!=size) { content.clear(); }
fclose(f);
return content;
}
}
inline GLShader GLShaderFromFile(const std::string& vertexShaderFileName,
const std::string& fragmentShaderFileName)
{
return GLShader(jzq_detail::stringFromFile(vertexShaderFileName),
jzq_detail::stringFromFile(fragmentShaderFileName));
}
#endif