framework2

Форк
0
996 строк · 22.5 Кб
1
#include "ofGLUtils.h"
2
#include "ofGLProgrammableRenderer.h"
3
#include "ofGraphics.h"
4
#include "ofShader.h"
5
#include "ofGraphicsBaseTypes.h"
6
#include "ofRendererCollection.h"
7
#include "ofGLRenderer.h"
8
#include "ofPixels.h"
9
#include "ofLog.h"
10
#include "ofGraphicsConstants.h"
11
#include <set>
12

13
using std::shared_ptr;
14
using std::vector;
15
using std::string;
16

17
//---------------------------------
18
// deprecations
19

20
int ofGetGlInternalFormat(const ofPixels& pixels) {
21
    return ofGetGLInternalFormat(pixels);
22
}
23

24
int ofGetGlInternalFormat(const ofShortPixels& pixels) {
25
    return ofGetGLInternalFormat(pixels);
26
}
27

28
int ofGetGlInternalFormat(const ofFloatPixels& pixels) {
29
    return ofGetGLInternalFormat(pixels);
30
}
31

32
std::string ofGetGlInternalFormatName(int glInternalFormat) {
33
    return ofGetGLInternalFormatName(glInternalFormat);
34
}
35

36
int ofGetGlTypeFromInternal(int glInternalFormat) {
37
    return ofGetGLTypeFromInternal(glInternalFormat);
38
}
39

40
int ofGetGlType(const ofPixels & pixels) {
41
    return ofGetGLType(pixels);
42
}
43

44
int ofGetGlType(const ofShortPixels & pixels) {
45
    return ofGetGLType(pixels);
46
}
47

48
int ofGetGlType(const ofFloatPixels & pixels) {
49
    return ofGetGLType(pixels);
50
}
51

52
//---------------------------------
53
int ofGetGLInternalFormat(const ofPixels& pixels) {
54
	return ofGetGLInternalFormatFromPixelFormat(pixels.getPixelFormat());
55
}
56

57
//---------------------------------
58
int ofGetGLInternalFormat(const ofShortPixels& pixels) {
59
#ifndef TARGET_OPENGLES
60
	switch(pixels.getNumChannels()) {
61
		case 3: return GL_RGB16;
62
		case 4: return GL_RGBA16;
63
		case 2:
64
			if(ofIsGLProgrammableRenderer()){
65
				return GL_RG16;
66
			}else{
67
				return GL_LUMINANCE16_ALPHA16;
68
			}
69
		default:
70
			if(ofIsGLProgrammableRenderer()){
71
				return GL_R16;
72
			}else{
73
				return GL_LUMINANCE16;
74
			}
75
	}
76
#else
77
	ofLogWarning("ofGLUtils") << "ofGetGLInternalFormat(): 16bit textures are not supported in OpenGL ES";
78
	switch(pixels.getNumChannels()) {
79
		case 3: return GL_RGB;
80
		case 4: return GL_RGBA;
81
		case 2:
82
			return GL_LUMINANCE_ALPHA;
83
		default:
84
			return GL_LUMINANCE;
85
	}
86
#endif
87
}
88

89
//---------------------------------
90
int ofGetGLInternalFormat(const ofFloatPixels& pixels) {
91
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
92
	switch(pixels.getNumChannels()) {
93
		case 3: return GL_RGB32F;
94
		case 4: return GL_RGBA32F;
95
		case 2:
96
			if(ofIsGLProgrammableRenderer()){
97
				return GL_RG32F;
98
			}else{
99
				return GL_LUMINANCE_ALPHA32F_ARB;
100
			}
101
		default:
102
			if(ofIsGLProgrammableRenderer()){
103
				return GL_R32F;
104
			}else{
105
				return GL_LUMINANCE32F_ARB;
106
			}
107
	}
108
#else
109
	ofLogWarning("ofGLUtils") << "ofGetGLInternalFormat(): float textures not supported in OpenGL ES";
110
	switch(pixels.getNumChannels()) {
111
		case 3: return GL_RGB;
112
		case 4: return GL_RGBA;
113
		case 2:
114
			return GL_LUMINANCE_ALPHA;
115
		default:
116
			return GL_LUMINANCE;
117
	}
118
#endif
119
}
120

121
//---------------------------------
122
// this is helpful for debugging ofTexture
123
string ofGetGLInternalFormatName(int glInternalFormat) {
124
	switch(glInternalFormat) {
125
		case GL_RGBA: return "GL_RGBA";
126
#ifndef TARGET_OPENGLES
127
		case GL_RGBA8: return "GL_RGBA8";
128
#endif
129
		case GL_RGB: return "GL_RGB";
130
#ifndef TARGET_OPENGLES
131
		case GL_RGB8: return "GL_RGB8";
132
#endif
133
		case GL_LUMINANCE: return "GL_LUMINANCE";
134
#ifndef TARGET_OPENGLES
135
		case GL_LUMINANCE8: return "GL_LUMINANCE8";
136
		case GL_RGBA16: return "GL_RGBA16";
137
		case GL_RGB16: return "GL_RGB16";
138
		case GL_LUMINANCE16: return "GL_LUMINANCE16";
139
		case GL_RGBA32F_ARB: return "GL_RGBA32F_ARB";
140
		case GL_RGB32F_ARB: return "GL_RGB32F_ARB";
141
		case GL_LUMINANCE32F_ARB: return "GL_LUMINANCE32F_ARB";
142
#endif
143
		case GL_LUMINANCE_ALPHA: return "GL_LUMINANCE_ALPHA";
144
#ifndef TARGET_OPENGLES
145
		case GL_LUMINANCE8_ALPHA8: return "GL_LUMINANCE8_ALPHA8";
146
#endif
147
		default: return "unknown glInternalFormat";
148
	}
149
}
150

151
int ofGetGLFormatFromInternal(int glInternalFormat){
152
	switch(glInternalFormat) {
153
			case GL_RGBA:
154
	#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
155
			case GL_RGBA8:
156
			case GL_RGBA16:
157
			case GL_RGBA16F:
158
		    case GL_RGBA16I:
159
		    case GL_RGBA16UI:
160
		    case GL_RGBA32F:
161
		    case GL_RGBA32I:
162
		    case GL_RGBA32UI:
163
	#endif
164
				 return GL_RGBA;
165
#ifdef TARGET_OF_IOS
166
			case GL_BGRA:
167
			return GL_BGRA;
168
#endif
169

170

171
			case GL_RGB:
172
	#ifndef TARGET_OPENGLES
173
			case GL_RGB8:
174
			case GL_RGB16:
175
		    case GL_RGB16F:
176
		    case GL_RGB16I:
177
		    case GL_RGB16UI:
178
		    case GL_RGB32F:
179
		    case GL_RGB32I:
180
		    case GL_RGB32UI:
181
	#endif
182
				return GL_RGB;
183

184

185
			case GL_LUMINANCE:
186
	#ifndef TARGET_OPENGLES
187
			case GL_LUMINANCE8:
188
			case GL_LUMINANCE16:
189
			case GL_LUMINANCE32F_ARB:
190
	#endif
191
				 return GL_LUMINANCE;
192

193
			case GL_LUMINANCE_ALPHA:
194
	#ifndef TARGET_OPENGLES
195
			case GL_LUMINANCE8_ALPHA8:
196
			case GL_LUMINANCE16_ALPHA16:
197
			case GL_LUMINANCE_ALPHA32F_ARB:
198
	#endif
199
				return GL_LUMINANCE_ALPHA;
200

201

202
			case GL_DEPTH_STENCIL:
203
				 return GL_DEPTH_STENCIL;
204

205
			case GL_DEPTH_COMPONENT:
206
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
207
			case GL_DEPTH_COMPONENT16:
208
			case GL_DEPTH_COMPONENT24:
209
			case GL_DEPTH_COMPONENT32:
210
#endif
211
				return GL_DEPTH_COMPONENT;
212

213
			case GL_STENCIL_INDEX:
214
				return GL_STENCIL_INDEX;
215

216
#ifndef TARGET_OPENGLES
217
			case GL_R8:
218
			case GL_R16:
219
		    case GL_R16I:
220
		    case GL_R16UI:
221
			case GL_R16F:
222
			case GL_R32F:
223
		    case GL_R32I:
224
		    case GL_R32UI:
225
				return GL_RED;
226

227
			case GL_RG8:
228
			case GL_RG16:
229
		    case GL_RG16I:
230
		    case GL_RG16UI:
231
			case GL_RG16F:
232
			case GL_RG32F:
233
		    case GL_RG32I:
234
		    case GL_RG32UI:
235
				return GL_RG;
236
#endif
237

238
#ifndef TARGET_OPENGLES
239
			case GL_ALPHA8:
240
#endif
241
			case GL_ALPHA:
242
				return GL_ALPHA;
243

244
			default:
245
				ofLogError("ofGLUtils") << "ofGetGLFormatFromInternal(): unknown internal format " << glInternalFormat << ", returning GL_RGBA";
246
				return GL_RGBA;
247

248
		}
249
}
250

251
int ofGetGLTypeFromInternal(int glInternalFormat){
252

253
	switch(glInternalFormat) {
254
		case GL_RGB:
255
		case GL_RGBA:
256
		case GL_LUMINANCE:
257
		case GL_LUMINANCE_ALPHA:
258
		case GL_ALPHA:
259
#ifndef TARGET_OPENGLES
260
		case GL_LUMINANCE8:
261
		case GL_LUMINANCE8_ALPHA8:
262
		case GL_R8:
263
		case GL_RG8:
264
		case GL_RGB8:
265
		case GL_RGBA8:
266
		case GL_ALPHA8:
267
#endif
268
			 return GL_UNSIGNED_BYTE;
269

270

271
#if !defined(TARGET_OPENGLES) && defined(GL_RGB565)
272
		case GL_RGB565:
273
			return GL_UNSIGNED_SHORT_5_6_5;
274
		break;
275
#endif
276

277
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
278
		case GL_LUMINANCE32F_ARB:
279
		case GL_LUMINANCE_ALPHA32F_ARB:
280
		case GL_R32F:
281
		case GL_RG32F:
282
		case GL_RGB32F:
283
		case GL_RGBA32F:
284
			return GL_FLOAT;
285

286
		case GL_R16F:
287
		case GL_RG16F:
288
		case GL_RGB16F:
289
		case GL_RGBA16F:
290
		case GL_LUMINANCE16:
291
		case GL_LUMINANCE16_ALPHA16:
292
		case GL_R16:
293
		case GL_RG16:
294
		case GL_RGB16:
295
		case GL_RGBA16:
296
			return GL_HALF_FLOAT;
297
#endif
298

299
		case GL_DEPTH_STENCIL:
300
			 return GL_UNSIGNED_INT_24_8;
301

302
		case GL_DEPTH_COMPONENT:
303
#ifndef TARGET_OPENGLES
304
		case GL_DEPTH_COMPONENT16:
305
		case GL_R16UI:
306
		case GL_RG16UI:
307
		case GL_RGB16UI:
308
		case GL_RGBA16UI:
309
#endif
310
			return GL_UNSIGNED_SHORT;
311

312
#ifndef TARGET_OPENGLES
313
		case GL_R16I:
314
		case GL_RG16I:
315
		case GL_RGB16I:
316
		case GL_RGBA16I:
317
		    return GL_SHORT;
318
#endif
319

320
#ifndef TARGET_OPENGLES
321
		case GL_DEPTH_COMPONENT24:
322
		case GL_DEPTH_COMPONENT32:
323
		case GL_R32UI:
324
		case GL_RG32UI:
325
		case GL_RGB32UI:
326
		case GL_RGBA32UI:
327
			return GL_UNSIGNED_INT;
328
#endif
329

330
#ifndef TARGET_OPENGLES
331
		case GL_R32I:
332
		case GL_RG32I:
333
		case GL_RGB32I:
334
		case GL_RGBA32I:
335
		    return GL_INT;
336
#endif
337

338
		case GL_STENCIL_INDEX:
339
			return GL_UNSIGNED_BYTE;
340

341
		default:
342
			ofLogError("ofGLUtils") << "ofGetGLTypeFromInternal(): unknown internal format " << glInternalFormat << ", returning GL_UNSIGNED_BYTE";
343
			return GL_UNSIGNED_BYTE;
344

345
	}
346
}
347

348
//---------------------------------
349
int ofGetGLType(const ofPixels & pixels) {
350
#ifndef TARGET_OPENGLES
351
	if(pixels.getPixelFormat() == OF_PIXELS_RGB565){
352
		return GL_UNSIGNED_SHORT_5_6_5;
353
	}else{
354
#endif
355
		return GL_UNSIGNED_BYTE;
356
#ifndef TARGET_OPENGLES
357
	}
358
#endif
359
}
360

361
//---------------------------------
362
int ofGetGLType(const ofShortPixels & pixels) {
363
	return GL_UNSIGNED_SHORT;
364
}
365

366
//---------------------------------
367
int ofGetGLType(const ofFloatPixels & pixels) {
368
	return GL_FLOAT;
369
}
370

371
//---------------------------------
372
ofImageType ofGetImageTypeFromGLType(int glType){
373
	switch(glType){
374
	case GL_LUMINANCE:
375
#ifndef TARGET_OPENGLES
376
	case GL_LUMINANCE8:
377
	case GL_LUMINANCE16:
378
	case GL_LUMINANCE32F_ARB:
379
	case GL_R8:
380
	case GL_R16:
381
	case GL_R16F:
382
	case GL_R16I:
383
	case GL_R16UI:
384
	case GL_R32F:
385
	case GL_R32I:
386
	case GL_R32UI:
387
	case GL_DEPTH_COMPONENT32F:
388
	case GL_DEPTH_COMPONENT32:
389
	case GL_DEPTH_COMPONENT16:
390
	case GL_DEPTH_COMPONENT24:
391
	case GL_DEPTH_COMPONENT:
392
#endif
393
		return OF_IMAGE_GRAYSCALE;
394

395

396
	case GL_RGB:
397
#ifndef TARGET_OPENGLES
398
	case GL_RGB8:
399
	case GL_RGB16:
400
	case GL_RGB16F:
401
	case GL_RGB16I:
402
	case GL_RGB16UI:
403
	case GL_RGB32F:
404
	case GL_RGB32I:
405
	case GL_RGB32UI:
406
#endif
407
		return OF_IMAGE_COLOR;
408

409

410
	case GL_RGBA:
411
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
412
	case GL_RGBA8:
413
	case GL_RGBA16:
414
	case GL_RGBA16F:
415
	case GL_RGBA16I:
416
	case GL_RGBA16UI:
417
	case GL_RGBA32F:
418
	case GL_RGBA32I:
419
	case GL_RGBA32UI:
420
#endif
421
		return OF_IMAGE_COLOR_ALPHA;
422
	}
423
	return OF_IMAGE_UNDEFINED;
424
}
425

426
GLuint ofGetGLPolyMode(ofPolyRenderMode mode){
427
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
428
	switch(mode){
429
		case(OF_MESH_POINTS):
430
			return GL_POINT;
431
			break;
432
		case(OF_MESH_WIREFRAME):
433
			return GL_LINE;
434
			break;
435
		case(OF_MESH_FILL):
436
			return GL_FILL;
437
			break;
438
		default:
439
			ofLogError("ofGLUtils") << "ofGetGLPolyMode(): unknown OF poly mode " << ofToString(mode) << ", returning GL_FILL";
440
			return GL_FILL;
441
			break;
442
	}
443
#else
444
	return 0;
445
#endif
446
}
447

448
ofPolyRenderMode ofGetOFPolyMode(GLuint mode){
449
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
450
	switch(mode){
451
		case(GL_POINT):
452
			return OF_MESH_POINTS;
453
			break;
454
		case(GL_LINE):
455
			return OF_MESH_WIREFRAME;
456
			break;
457
		case(GL_FILL):
458
			return OF_MESH_FILL;
459
			break;
460
		default:
461
			ofLogError("ofGLUtils") << "ofGetOFPolyMode(): unknown GL poly mode " << ofToString(mode) << ", returning OF_MESH_FILL";
462
			return OF_MESH_FILL;
463
			break;
464
	}
465
#else
466
	return OF_MESH_FILL;
467
#endif
468
}
469

470

471
GLuint ofGetGLPrimitiveMode(ofPrimitiveMode mode){
472
	switch(mode){
473
		case OF_PRIMITIVE_TRIANGLES:
474
			return GL_TRIANGLES;
475
			break;
476
		case OF_PRIMITIVE_TRIANGLE_STRIP:
477
			return GL_TRIANGLE_STRIP;
478
			break;
479
		case OF_PRIMITIVE_TRIANGLE_FAN:
480
			return GL_TRIANGLE_FAN;
481
			break;
482
		case OF_PRIMITIVE_LINES:
483
			return GL_LINES;
484
			break;
485
		case OF_PRIMITIVE_LINE_STRIP:
486
			return GL_LINE_STRIP;
487
			break;
488
		case OF_PRIMITIVE_LINE_LOOP:
489
			return GL_LINE_LOOP;
490
			break;
491
		case OF_PRIMITIVE_POINTS:
492
			return GL_POINTS;
493
			break;
494
#ifndef TARGET_OPENGLES
495
		case OF_PRIMITIVE_LINES_ADJACENCY:
496
			return GL_LINES_ADJACENCY;
497
			break;
498
		case OF_PRIMITIVE_LINE_STRIP_ADJACENCY:
499
			return GL_LINE_STRIP_ADJACENCY;
500
			break;
501
		case OF_PRIMITIVE_TRIANGLES_ADJACENCY:
502
			return GL_TRIANGLES_ADJACENCY;
503
			break;
504
		case OF_PRIMITIVE_TRIANGLE_STRIP_ADJACENCY:
505
			return GL_TRIANGLE_STRIP_ADJACENCY;
506
			break;
507
		case OF_PRIMITIVE_PATCHES:
508
			return GL_PATCHES;
509
			break;
510
#endif
511
		default:
512
			ofLogError("ofGLUtils") << "ofGetGLPrimitiveMode(): unknown OF primitive mode " << ofToString(mode) << ", returning GL_TRIANGLES";
513
			return GL_TRIANGLES;
514
			break;
515
	}
516
}
517

518
ofPrimitiveMode ofGetOFPrimitiveMode(GLuint mode){
519
	switch(mode){
520
		case GL_TRIANGLES:
521
			return OF_PRIMITIVE_TRIANGLES;
522
			break;
523
		case GL_TRIANGLE_STRIP:
524
			return OF_PRIMITIVE_TRIANGLE_STRIP;
525
			break;
526
		case GL_TRIANGLE_FAN:
527
			return OF_PRIMITIVE_TRIANGLE_FAN;
528
			break;
529
		case GL_LINES:
530
			return OF_PRIMITIVE_LINES;
531
			break;
532
		case GL_LINE_STRIP:
533
			return OF_PRIMITIVE_LINE_STRIP;
534
			break;
535
		case GL_LINE_LOOP:
536
			return OF_PRIMITIVE_LINE_LOOP;
537
			break;
538
		case GL_POINTS:
539
			return OF_PRIMITIVE_POINTS;
540
			break;
541
#ifndef TARGET_OPENGLES
542
		case GL_LINES_ADJACENCY:
543
			return OF_PRIMITIVE_LINES_ADJACENCY;
544
			break;
545
		case GL_LINE_STRIP_ADJACENCY:
546
			return OF_PRIMITIVE_LINE_STRIP_ADJACENCY;
547
			break;
548
		case GL_TRIANGLES_ADJACENCY:
549
			return OF_PRIMITIVE_TRIANGLES_ADJACENCY;
550
			break;
551
		case GL_TRIANGLE_STRIP_ADJACENCY:
552
			return OF_PRIMITIVE_TRIANGLE_STRIP_ADJACENCY;
553
			break;
554
		case GL_PATCHES:
555
			return OF_PRIMITIVE_PATCHES;
556
			break;
557
#endif
558
		default:
559
			ofLogError("ofGLUtils") << "ofGetOFPrimitiveMode(): unknown GL primitive mode " << ofToString(mode) << ", returning OF_PRIMITIVE_TRIANGLES";
560
			return OF_PRIMITIVE_TRIANGLES;
561
			break;
562
	}
563
}
564

565

566
int ofGetGLInternalFormatFromPixelFormat(ofPixelFormat pixelFormat){
567
	switch(pixelFormat){
568
	case OF_PIXELS_BGRA:
569
	case OF_PIXELS_RGBA:
570
#ifndef TARGET_OPENGLES
571
		return GL_RGBA8;
572
#else
573
		return GL_RGBA;
574
#endif
575
	case OF_PIXELS_RGB:
576
	case OF_PIXELS_BGR:
577
#ifndef TARGET_OPENGLES
578
		return GL_RGB8;
579
#else
580
		return GL_RGB;
581
#endif
582
    case OF_PIXELS_RGB565:
583
	#if defined(TARGET_ANDROID) || defined(TARGET_RASPBERRY_PI_LEGACY)
584
		return GL_RGB565_OES;
585
	#elif defined(GL_RGB565)
586
		return GL_RGB565;
587
	#else
588
		return GL_RGB;
589
	#endif
590
    case OF_PIXELS_GRAY:
591
    case OF_PIXELS_NV12:
592
    case OF_PIXELS_NV21:
593
	case OF_PIXELS_YV12:
594
	case OF_PIXELS_I420:
595
	case OF_PIXELS_Y:
596
	case OF_PIXELS_U:
597
	case OF_PIXELS_V:
598
#ifndef TARGET_OPENGLES
599
		if(ofIsGLProgrammableRenderer()){
600
			return GL_R8;
601
		}else{
602
#endif
603
			return GL_LUMINANCE;
604
#ifndef TARGET_OPENGLES
605
		}
606
#endif
607
    case OF_PIXELS_GRAY_ALPHA:
608
	case OF_PIXELS_YUY2:
609
	case OF_PIXELS_UV:
610
	case OF_PIXELS_VU:
611
#ifndef TARGET_OPENGLES
612
		if(ofIsGLProgrammableRenderer()){
613
			return GL_RG8;
614
		}else{
615
#endif
616
			return GL_LUMINANCE_ALPHA;
617
#ifndef TARGET_OPENGLES
618
		}
619
#endif
620
	default:
621
		ofLogError("ofGLUtils") << "ofGetGLInternalFormatFromPixelFormat(): unknown OF pixel format "
622
						<< ofToString(pixelFormat) << ", returning GL_RGBA";
623
		return GL_RGBA;
624
	}
625
}
626

627
int ofGetGLFormatFromPixelFormat(ofPixelFormat pixelFormat){
628
	switch(pixelFormat){
629
	case OF_PIXELS_BGRA:
630
#ifdef TARGET_OPENGLES
631
    	return GL_BGRA_EXT;
632
#else
633
        return GL_BGRA;
634
#endif
635
	case OF_PIXELS_RGB:
636
		return GL_RGB;
637
	case OF_PIXELS_BGR:
638
#ifdef TARGET_OPENGLES
639
    	return GL_RGB;
640
#else
641
        return GL_BGR;
642
#endif
643
	case OF_PIXELS_RGBA:
644
		return GL_RGBA;
645
    case OF_PIXELS_RGB565:
646
        return GL_RGB;
647
	case OF_PIXELS_GRAY:
648
    case OF_PIXELS_NV12:
649
    case OF_PIXELS_NV21:
650
	case OF_PIXELS_YV12:
651
	case OF_PIXELS_I420:
652
	case OF_PIXELS_Y:
653
	case OF_PIXELS_U:
654
	case OF_PIXELS_V:
655
#ifndef TARGET_OPENGLES
656
		if(ofIsGLProgrammableRenderer()){
657
			return GL_RED;
658
		}else{
659
#endif
660
			return GL_LUMINANCE;
661
#ifndef TARGET_OPENGLES
662
		}
663
#endif
664
    case OF_PIXELS_GRAY_ALPHA:
665
	case OF_PIXELS_YUY2:
666
	case OF_PIXELS_UV:
667
	case OF_PIXELS_VU:
668
#ifndef TARGET_OPENGLES
669
		if(ofIsGLProgrammableRenderer()){
670
			return GL_RG;
671
		}else{
672
#endif
673
			return GL_LUMINANCE_ALPHA;
674
#ifndef TARGET_OPENGLES
675
		}
676
#endif
677
	default:
678
#ifndef TARGET_OPENGLES
679
		if(ofIsGLProgrammableRenderer()){
680
			ofLogError("ofGLUtils") << "ofGetGLFormatFromPixelFormat(): unknown OF pixel format "
681
				<< ofToString(pixelFormat) << ", returning GL_RED";
682
			return GL_RED;
683
		}else{
684
#endif
685
			ofLogError("ofGLUtils") << "ofGetGLFormatFromPixelFormat(): unknown OF pixel format "
686
				<< ofToString(pixelFormat) << ", returning GL_LUMINANCE";
687
			return GL_LUMINANCE;
688
#ifndef TARGET_OPENGLES
689
		}
690
#endif
691
	}
692
}
693

694

695
int ofGetNumChannelsFromGLFormat(int glFormat){
696
	switch(glFormat){
697
	case GL_RGB:
698
		return 3;
699
#ifdef TARGET_OF_IOS
700
	case GL_BGRA:
701
#endif
702
	case GL_RGBA:
703
		return 4;
704
	case GL_LUMINANCE:
705
		return 1;
706
	case GL_LUMINANCE_ALPHA:
707
		return 2;
708
#ifndef TARGET_OPENGLES
709
	case GL_RED:
710
		return 1;
711
	case GL_RG:
712
		return 2;
713
#endif
714
	default:
715
		return 1;
716
	}
717
}
718

719
int ofGetBytesPerChannelFromGLType(int glType){
720
	switch(glType) {
721
		case GL_UNSIGNED_BYTE:
722
			return 1;
723
		case GL_UNSIGNED_SHORT:
724
			return 2;
725

726
#if !defined(TARGET_OPENGLES) && defined(GL_RGB565)
727
		case GL_UNSIGNED_SHORT_5_6_5:
728
			return 2;
729
#endif
730

731
#if !defined(TARGET_OPENGLES) || defined(TARGET_EMSCRIPTEN)
732
		case GL_FLOAT:
733
			return 4;
734
#endif
735
		case GL_UNSIGNED_INT_24_8:
736
			 return 4;
737

738

739
#ifndef TARGET_OPENGLES
740
		case GL_UNSIGNED_INT:
741
			return 4;
742
#endif
743
		case GL_HALF_FLOAT:
744
			return 2;
745

746
		default:
747
			ofLogError("ofGetBytesPerChannelFromGLType") << "unknown type returning 1";
748
			return 1;
749

750
	}
751
}
752

753
// Rounds an integer value up to the next multiple of 2,4 and 8.
754
#define OF_ROUND_UP_2(num)  (((num)+1)&~1)
755
#define OF_ROUND_UP_4(num)  (((num)+3)&~3)
756
#define OF_ROUND_UP_8(num)  (((num)+7)&~7)
757

758
void ofSetPixelStoreiAlignment(GLenum pname, int w, int bpc, int numChannels){
759
	int stride = w * numChannels * bpc;
760
	ofSetPixelStoreiAlignment(pname,stride);
761
}
762

763
void ofSetPixelStoreiAlignment(GLenum pname, int stride){
764
    if (OF_ROUND_UP_8 (stride) == stride) {
765
        glPixelStorei (pname, 8);
766
    } else if (OF_ROUND_UP_4 (stride) == stride) {
767
        glPixelStorei (pname, 4);
768
    } else if (OF_ROUND_UP_2 (stride) == stride) {
769
        glPixelStorei (pname, 2);
770
    } else {
771
        glPixelStorei (pname, 1);
772
    }
773
}
774

775
vector<string> ofGLSupportedExtensions(){
776
#ifdef TARGET_OPENGLES
777
	char* extensions = (char*)glGetString(GL_EXTENSIONS);
778
	if(extensions){
779
		string extensions_str = extensions;
780
		return ofSplitString(extensions_str," ");
781
	}else{
782
		return vector<string>();
783
	}
784
#else
785
	int numExtensions=0;
786
	glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
787
	std::vector<std::string> extensions;
788
	for(int i=0;i<numExtensions;i++){
789
		char* extension = (char*)glGetStringi(GL_EXTENSIONS, i);
790
		if(extension){
791
			extensions.emplace_back(extension);
792
		}
793
	}
794
	return extensions;
795
#endif
796
}
797

798
bool ofGLCheckExtension(string searchName){
799
#if defined( TARGET_OPENGLES )
800
	vector<string> extensionsList = ofGLSupportedExtensions();
801
	std::set<string> extensionsSet;
802
	extensionsSet.insert(extensionsList.begin(),extensionsList.end());
803
	return extensionsSet.find(searchName)!=extensionsSet.end();
804
#else
805
	return glewIsSupported(searchName.c_str());
806
#endif
807
}
808

809
bool ofGLSupportsNPOTTextures(){
810
#ifndef TARGET_OPENGLES
811
	return GL_ARB_texture_rectangle;
812
#elif !defined(TARGET_EMSCRIPTEN)
813
	static bool npotChecked = false;
814
	static bool npotSupported = false;
815
	if(!npotChecked){
816
		vector<string> extensionsList = ofGLSupportedExtensions();
817
		std::set<string> extensionsSet;
818
		extensionsSet.insert(extensionsList.begin(),extensionsList.end());
819

820
		npotSupported = extensionsSet.find("GL_OES_texture_npot")!=extensionsSet.end() ||
821
				extensionsSet.find("APPLE_texture_2D_limited_npot")!=extensionsSet.end() ||
822
				extensionsSet.find("GL_NV_texture_npot_2D_mipmap")!=extensionsSet.end() ||
823
				extensionsSet.find("GL_IMG_texture_npot")!=extensionsSet.end() ||
824
				extensionsSet.find("GL_ARB_texture_non_power_of_two")!=extensionsSet.end();
825
		npotChecked = true;
826
	}
827

828
	return npotSupported;
829
#else
830
	return true;
831
#endif
832
}
833

834
string ofGLSLVersionFromGL(int major, int minor){
835
#ifdef TARGET_OPENGLES
836
	#ifdef TARGET_EMSCRIPTEN
837
	if( major >= 2 ) { // for emscripten major version refers to WEBGL version
838
		return "300 es";
839
	} else {
840
		return "ES1";
841
	}
842
	#else
843
		return "ES1";
844
	#endif
845
#else
846
	switch(major){
847
	case 3:
848
		if(minor==0){
849
			return "130";
850
		}else if(minor==1){
851
			return "140";
852
		}else if(minor==2){
853
			return "150";
854
		}else{
855
			return ofToString(major*100+minor*10);
856
		}
857
	case 4:
858
		return ofToString(major*100+minor*10);
859
	default:
860
		return "120";
861
	}
862
#endif
863
}
864

865
string ofGLSLVersionFromGL(){
866
    int major = 0;
867
    int minor = 0;
868
    
869
    auto glRenderer = std::dynamic_pointer_cast<ofBaseGLRenderer>(ofGetCurrentRenderer());
870
    if( glRenderer ){
871
        major = glRenderer->getGLVersionMajor();
872
        minor = glRenderer->getGLVersionMinor();
873
    }
874
    return ofGLSLVersionFromGL(major, minor);
875
}
876

877
//TODO: unify this with similar in ofGLProgrammableRenderer and ofMaterial
878
string ofGLSLGetDefaultHeader(){
879
    string header = "";
880

881
    auto glRenderer = std::dynamic_pointer_cast<ofBaseGLRenderer>(ofGetCurrentRenderer());
882
    
883
    if( glRenderer ){
884
        string versionStr = ofGLSLVersionFromGL(glRenderer->getGLVersionMajor(), glRenderer->getGLVersionMinor());
885
        header = "#version "+versionStr+"\n";
886
        
887
        #ifdef TARGET_OPENGLES
888
            if( versionStr != "ES1" ){
889
                header += "#extension GL_OES_standard_derivatives : enable\n";
890
            }
891
            #ifdef TARGET_ANDROID
892
                header += "#extension GL_OES_EGL_image_external : require\n";
893
            #endif
894
            header += "precision mediump float;\n";
895
            header += "precision mediump int;\n";
896
            header += "#define TARGET_OPENGLES\n";
897
        #else
898
            if( ofGetUsingArbTex() ){
899
                if( glRenderer->getGLVersionMajor() < 4 && glRenderer->getGLVersionMinor() < 2 ){
900
                    header += "\n#extension GL_ARB_texture_rectangle : enable";
901
                }
902
            }
903
        #endif
904
    }
905
    return header;
906
}
907

908
#ifndef TARGET_PROGRAMMABLE_GL
909
shared_ptr<ofBaseGLRenderer> ofGetGLRenderer(){
910
	if(ofGetCurrentRenderer()->getType()==ofGLRenderer::TYPE || ofGetCurrentRenderer()->getType()==ofGLProgrammableRenderer::TYPE){
911
		return (shared_ptr<ofBaseGLRenderer>&)ofGetCurrentRenderer();
912
	}else if(ofGetCurrentRenderer()->getType()==ofRendererCollection::TYPE){
913
		return ((shared_ptr<ofRendererCollection>&)ofGetCurrentRenderer())->getGLRenderer();
914
	}else{
915
		return shared_ptr<ofGLRenderer>();
916
	}
917
}
918
#else
919
shared_ptr<ofBaseGLRenderer> ofGetGLRenderer(){
920
	if(ofGetCurrentRenderer()->getType()==ofGLProgrammableRenderer::TYPE){
921
		return (shared_ptr<ofBaseGLRenderer>&)ofGetCurrentRenderer();
922
	}else if(ofGetCurrentRenderer()->getType()==ofRendererCollection::TYPE){
923
		return ((shared_ptr<ofRendererCollection>&)ofGetCurrentRenderer())->getGLRenderer();
924
	}else{
925
		return shared_ptr<ofGLProgrammableRenderer>();
926
	}
927
}
928
#endif
929

930
#ifndef TARGET_OPENGLES
931
namespace{
932
	void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, void * user){
933
		std::ostringstream oss;
934
		oss << "GL Debug: ";
935

936
		ofLogLevel level;
937
		switch (type) {
938
		case GL_DEBUG_TYPE_ERROR:
939
			level = OF_LOG_ERROR;
940
			oss << "ERROR";
941
			break;
942
		case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
943
			level = OF_LOG_WARNING;
944
			oss << "DEPRECATED_BEHAVIOR";
945
			break;
946
		case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
947
			level = OF_LOG_WARNING;
948
			oss << "UNDEFINED_BEHAVIOR";
949
			break;
950
		case GL_DEBUG_TYPE_PORTABILITY:
951
			level = OF_LOG_NOTICE;
952
			oss << "PORTABILITY";
953
			break;
954
		case GL_DEBUG_TYPE_PERFORMANCE:
955
			level = OF_LOG_NOTICE;
956
			oss << "PERFORMANCE";
957
			break;
958
		case GL_DEBUG_TYPE_OTHER:
959
		default:
960
			level = OF_LOG_VERBOSE;
961
			oss << "OTHER";
962
			break;
963
		}
964

965
		switch (severity) {
966
		case GL_DEBUG_SEVERITY_LOW:
967
			oss << " (LOW)";
968
			break;
969
		case GL_DEBUG_SEVERITY_MEDIUM:
970
			oss << " (MEDIUM)";
971
			break;
972
		case GL_DEBUG_SEVERITY_HIGH:
973
			oss << " (HIGH)";
974
			break;
975
		}
976

977
		oss << ", " << id << ": " << message;
978

979
		ofLog(level, oss.str());
980
	}
981
}
982

983
void ofEnableGLDebugLog(){
984
	if(ofGLCheckExtension("GL_KHR_debug") && ofGLCheckExtension("GL_ARB_debug_output")){
985
		glEnable(GL_DEBUG_OUTPUT);
986
		glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
987
		glDebugMessageCallback((GLDEBUGPROC)gl_debug_callback, nullptr);
988
	}
989
}
990

991
void ofDisableGLDebugLog(){
992
	if(ofGLCheckExtension("GL_KHR_debug") && ofGLCheckExtension("GL_ARB_debug_output")){
993
		glDisable(GL_DEBUG_OUTPUT);
994
	}
995
}
996
#endif
997

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.