forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGL.java
More file actions
2842 lines (2196 loc) · 82.9 KB
/
Copy pathPGL.java
File metadata and controls
2842 lines (2196 loc) · 82.9 KB
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011-12 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.opengl;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.Arrays;
import processing.core.PApplet;
import processing.opengl.tess.PGLU;
import processing.opengl.tess.PGLUtessellator;
import processing.opengl.tess.PGLUtessellatorCallbackAdapter;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.*;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
/**
* Processing-OpenGL abstraction layer.
*
*/
public class PGL {
///////////////////////////////////////////////////////////
// Parameters
public static boolean FORCE_SCREEN_FBO = false;
// The use of indirect buffers creates problems with glBufferSubData because
// the buffer position is ignored:
// http://stackoverflow.com/questions/3380489/glbuffersubdata-with-an-offset-into-buffer-without-causing-garbage
// http://code.google.com/p/android/issues/detail?id=12245
// This doesn't happen with direct buffers.
public static final boolean USE_DIRECT_BUFFERS = true;
public static final int MIN_DIRECT_BUFFER_SIZE = 1;
public static final boolean SAVE_SURFACE_TO_PIXELS = false;
/** Enables/disables mipmap use. **/
protected static final boolean MIPMAPS_ENABLED = false;
/** Initial sizes for arrays of input and tessellated data. */
protected static final int DEFAULT_IN_VERTICES = 16;
protected static final int DEFAULT_IN_EDGES = 32;
protected static final int DEFAULT_IN_TEXTURES = 16;
protected static final int DEFAULT_TESS_VERTICES = 16;
protected static final int DEFAULT_TESS_INDICES = 32;
/** Maximum lights by default is 8, the minimum defined by OpenGL. */
protected static final int MAX_LIGHTS = 8;
/** Maximum index value of a tessellated vertex. GLES restricts the vertex
* indices to be of type unsigned short. Since Java only supports signed
* shorts as primitive type we have 2^15 = 32768 as the maximum number of
* vertices that can be referred to within a single VBO. */
protected static final int MAX_VERTEX_INDEX = 32767;
protected static final int MAX_VERTEX_INDEX1 = MAX_VERTEX_INDEX + 1;
/** Count of tessellated fill, line or point vertices that will
* trigger a flush in the immediate mode. It doesn't necessarily
* be equal to MAX_VERTEX_INDEX1, since the number of vertices can
* be effectively much large since the renderer uses offsets to
* refer to vertices beyond the MAX_VERTEX_INDEX limit.
*/
protected static final int FLUSH_VERTEX_COUNT = MAX_VERTEX_INDEX1;
/** Maximum dimension of a texture used to hold font data. **/
protected static final int MAX_FONT_TEX_SIZE = 512;
/** Minimum stroke weight needed to apply the full path stroking
* algorithm that properly generates caps and joing.
*/
protected static final float MIN_CAPS_JOINS_WEIGHT = 2.f;
/** Maximum length of linear paths to be stroked with the
* full algorithm that generates accurate caps and joins.
*/
protected static final int MAX_CAPS_JOINS_LENGTH = 1000;
/** Minimum array size to use arrayCopy method(). **/
protected static final int MIN_ARRAYCOPY_SIZE = 2;
protected static final int SIZEOF_SHORT = Short.SIZE / 8;
protected static final int SIZEOF_INT = Integer.SIZE / 8;
protected static final int SIZEOF_FLOAT = Float.SIZE / 8;
protected static final int SIZEOF_BYTE = Byte.SIZE / 8;
protected static final int SIZEOF_INDEX = SIZEOF_SHORT;
protected static final int INDEX_TYPE = GLES20.GL_UNSIGNED_SHORT;
/** Error string from framebuffer errors **/
protected static final String FRAMEBUFFER_ERROR_MESSAGE =
"Framebuffer error (%1$s), rendering will probably not work as expected";
/** Machine Epsilon for float precision. **/
protected static float FLOAT_EPS = Float.MIN_VALUE;
// Calculation of the Machine Epsilon for float precision. From:
// http://en.wikipedia.org/wiki/Machine_epsilon#Approximation_using_Java
static {
float eps = 1.0f;
do {
eps /= 2.0f;
} while ((float)(1.0 + (eps / 2.0)) != 1.0);
FLOAT_EPS = eps;
}
/**
* Set to true if the host system is big endian (PowerPC, MIPS, SPARC), false
* if little endian (x86 Intel for Mac or PC).
*/
protected static boolean BIG_ENDIAN =
ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
protected static final String SHADER_PREPROCESSOR_DIRECTIVE =
"#ifdef GL_ES\n" +
"precision mediump float;\n" +
"precision mediump int;\n" +
"#endif\n";
///////////////////////////////////////////////////////////
// OpenGL constants
// The values for constants not defined in the GLES20 interface can be found
// in this file:
// http://code.metager.de/source/xref/android/4.0.3/development/tools/glesv2debugger/src/com/android/glesv2debugger/GLEnum.java
public static final int FALSE = GLES20.GL_FALSE;
public static final int TRUE = GLES20.GL_TRUE;
public static final int LESS = GLES20.GL_LESS;
public static final int LEQUAL = GLES20.GL_LEQUAL;
public static final int CCW = GLES20.GL_CCW;
public static final int CW = GLES20.GL_CW;
public static final int CULL_FACE = GLES20.GL_CULL_FACE;
public static final int FRONT = GLES20.GL_FRONT;
public static final int BACK = GLES20.GL_BACK;
public static final int FRONT_AND_BACK = GLES20.GL_FRONT_AND_BACK;
public static final int VIEWPORT = GLES20.GL_VIEWPORT;
public static final int SCISSOR_TEST = GLES20.GL_SCISSOR_TEST;
public static final int DEPTH_TEST = GLES20.GL_DEPTH_TEST;
public static final int DEPTH_WRITEMASK = GLES20.GL_DEPTH_WRITEMASK;
public static final int COLOR_BUFFER_BIT = GLES20.GL_COLOR_BUFFER_BIT;
public static final int DEPTH_BUFFER_BIT = GLES20.GL_DEPTH_BUFFER_BIT;
public static final int STENCIL_BUFFER_BIT = GLES20.GL_STENCIL_BUFFER_BIT;
public static final int FUNC_ADD = GLES20.GL_FUNC_ADD;
public static final int FUNC_MIN = 0x8007;
public static final int FUNC_MAX = 0x8008;
public static final int FUNC_REVERSE_SUBTRACT =
GLES20.GL_FUNC_REVERSE_SUBTRACT;
public static final int TEXTURE_2D = GLES20.GL_TEXTURE_2D;
public static final int TEXTURE_BINDING_2D = GLES20.GL_TEXTURE_BINDING_2D;
public static final int RGB = GLES20.GL_RGB;
public static final int RGBA = GLES20.GL_RGBA;
public static final int ALPHA = GLES20.GL_ALPHA;
public static final int UNSIGNED_INT = GLES20.GL_UNSIGNED_INT;
public static final int UNSIGNED_BYTE = GLES20.GL_UNSIGNED_BYTE;
public static final int UNSIGNED_SHORT = GLES20.GL_UNSIGNED_SHORT;
public static final int FLOAT = GLES20.GL_FLOAT;
public static final int NEAREST = GLES20.GL_NEAREST;
public static final int LINEAR = GLES20.GL_LINEAR;
public static final int LINEAR_MIPMAP_NEAREST =
GLES20.GL_LINEAR_MIPMAP_NEAREST;
public static final int LINEAR_MIPMAP_LINEAR =
GLES20.GL_LINEAR_MIPMAP_LINEAR;
public static final int TEXTURE_MAX_ANISOTROPY = 0x84FE;
public static final int MAX_TEXTURE_MAX_ANISOTROPY = 0x84FF;
public static final int CLAMP_TO_EDGE = GLES20.GL_CLAMP_TO_EDGE;
public static final int REPEAT = GLES20.GL_REPEAT;
public static final int RGBA8 = -1;
public static final int DEPTH24_STENCIL8 = 0x88F0;
public static final int DEPTH_COMPONENT = GLES20.GL_DEPTH_COMPONENT;
public static final int DEPTH_COMPONENT16 = GLES20.GL_DEPTH_COMPONENT16;
public static final int DEPTH_COMPONENT24 = 0x81A6;
public static final int DEPTH_COMPONENT32 = 0x81A7;
public static final int STENCIL_INDEX = GLES20.GL_STENCIL_INDEX;
public static final int STENCIL_INDEX1 = 0x8D46;
public static final int STENCIL_INDEX4 = 0x8D47;
public static final int STENCIL_INDEX8 = GLES20.GL_STENCIL_INDEX8;
public static final int ARRAY_BUFFER = GLES20.GL_ARRAY_BUFFER;
public static final int ELEMENT_ARRAY_BUFFER = GLES20.GL_ELEMENT_ARRAY_BUFFER;
public static final int SAMPLES = GLES20.GL_SAMPLES;
public static final int FRAMEBUFFER_COMPLETE =
GLES20.GL_FRAMEBUFFER_COMPLETE;
public static final int FRAMEBUFFER_INCOMPLETE_ATTACHMENT =
GLES20.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
public static final int FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT =
GLES20.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
public static final int FRAMEBUFFER_INCOMPLETE_DIMENSIONS =
GLES20.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
public static final int FRAMEBUFFER_INCOMPLETE_FORMATS =
0x8CDA;
public static final int FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = -1;
public static final int FRAMEBUFFER_INCOMPLETE_READ_BUFFER = -1;
public static final int FRAMEBUFFER_UNSUPPORTED =
GLES20.GL_FRAMEBUFFER_UNSUPPORTED;
public static final int STATIC_DRAW = GLES20.GL_STATIC_DRAW;
public static final int DYNAMIC_DRAW = GLES20.GL_DYNAMIC_DRAW;
public static final int STREAM_DRAW = GLES20.GL_STREAM_DRAW;
public static final int READ_ONLY = -1;
public static final int WRITE_ONLY = -1;
public static final int READ_WRITE = -1;
public static final int TRIANGLE_FAN = GLES20.GL_TRIANGLE_FAN;
public static final int TRIANGLE_STRIP = GLES20.GL_TRIANGLE_STRIP;
public static final int TRIANGLES = GLES20.GL_TRIANGLES;
public static final int VENDOR = GLES20.GL_VENDOR;
public static final int RENDERER = GLES20.GL_RENDERER;
public static final int VERSION = GLES20.GL_VERSION;
public static final int EXTENSIONS = GLES20.GL_EXTENSIONS;
public static final int SHADING_LANGUAGE_VERSION =
GLES20.GL_SHADING_LANGUAGE_VERSION;
public static final int MAX_TEXTURE_SIZE = GLES20.GL_MAX_TEXTURE_SIZE;
public static final int MAX_SAMPLES = -1;
public static final int ALIASED_LINE_WIDTH_RANGE =
GLES20.GL_ALIASED_LINE_WIDTH_RANGE;
public static final int ALIASED_POINT_SIZE_RANGE =
GLES20.GL_ALIASED_POINT_SIZE_RANGE;
public static final int DEPTH_BITS = GLES20.GL_DEPTH_BITS;
public static final int STENCIL_BITS = GLES20.GL_STENCIL_BITS;
public static final int TESS_WINDING_NONZERO = PGLU.GLU_TESS_WINDING_NONZERO;
public static final int TESS_WINDING_ODD = PGLU.GLU_TESS_WINDING_ODD;
public static final int TEXTURE0 = GLES20.GL_TEXTURE0;
public static final int TEXTURE1 = GLES20.GL_TEXTURE1;
public static final int TEXTURE2 = GLES20.GL_TEXTURE2;
public static final int TEXTURE3 = GLES20.GL_TEXTURE3;
public static final int TEXTURE_MIN_FILTER = GLES20.GL_TEXTURE_MIN_FILTER;
public static final int TEXTURE_MAG_FILTER = GLES20.GL_TEXTURE_MAG_FILTER;
public static final int TEXTURE_WRAP_S = GLES20.GL_TEXTURE_WRAP_S;
public static final int TEXTURE_WRAP_T = GLES20.GL_TEXTURE_WRAP_T;
public static final int BLEND = GLES20.GL_BLEND;
public static final int ONE = GLES20.GL_ONE;
public static final int ZERO = GLES20.GL_ZERO;
public static final int SRC_ALPHA = GLES20.GL_SRC_ALPHA;
public static final int DST_ALPHA = GLES20.GL_DST_ALPHA;
public static final int ONE_MINUS_SRC_ALPHA = GLES20.GL_ONE_MINUS_SRC_ALPHA;
public static final int ONE_MINUS_DST_COLOR = GLES20.GL_ONE_MINUS_DST_COLOR;
public static final int ONE_MINUS_SRC_COLOR = GLES20.GL_ONE_MINUS_SRC_COLOR;
public static final int DST_COLOR = GLES20.GL_DST_COLOR;
public static final int SRC_COLOR = GLES20.GL_SRC_COLOR;
public static final int FRAMEBUFFER = GLES20.GL_FRAMEBUFFER;
public static final int COLOR_ATTACHMENT0 = GLES20.GL_COLOR_ATTACHMENT0;
public static final int COLOR_ATTACHMENT1 = -1;
public static final int COLOR_ATTACHMENT2 = -1;
public static final int COLOR_ATTACHMENT3 = -1;
public static final int RENDERBUFFER = GLES20.GL_RENDERBUFFER;
public static final int DEPTH_ATTACHMENT = GLES20.GL_DEPTH_ATTACHMENT;
public static final int STENCIL_ATTACHMENT = GLES20.GL_STENCIL_ATTACHMENT;
public static final int READ_FRAMEBUFFER = -1;
public static final int DRAW_FRAMEBUFFER = -1;
public static final int VERTEX_SHADER = GLES20.GL_VERTEX_SHADER;
public static final int FRAGMENT_SHADER = GLES20.GL_FRAGMENT_SHADER;
public static final int INFO_LOG_LENGTH = GLES20.GL_INFO_LOG_LENGTH;
public static final int SHADER_SOURCE_LENGTH = GLES20.GL_SHADER_SOURCE_LENGTH;
public static final int COMPILE_STATUS = GLES20.GL_COMPILE_STATUS;
public static final int LINK_STATUS = GLES20.GL_LINK_STATUS;
public static final int VALIDATE_STATUS = GLES20.GL_VALIDATE_STATUS;
public static final int MULTISAMPLE = -1;
public static final int POINT_SMOOTH = -1;
public static final int LINE_SMOOTH = -1;
public static final int POLYGON_SMOOTH = -1;
// Some EGL constants needed to initialize an GLES2 context.
protected static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
protected static final int EGL_OPENGL_ES2_BIT = 0x0004;
/** Basic GLES 1.0 interface */
public static GL10 gl;
/** GLU interface **/
public static PGLU glu;
/** The current opengl context */
public static EGLContext context;
/** The PGraphics object using this interface */
protected PGraphicsOpenGL pg;
/** The renderer object driving the rendering loop,
* analogous to the GLEventListener in JOGL */
protected static AndroidRenderer renderer;
/** OpenGL thread */
protected static Thread glThread;
/** Which texturing targets are enabled */
protected static boolean[] texturingTargets = { false };
/** Which textures are bound to each target */
protected static int[] boundTextures = { 0 };
///////////////////////////////////////////////////////////
// FBO layer
protected static boolean fboLayerByDefault = FORCE_SCREEN_FBO;
protected static boolean fboLayerCreated = false;
protected static boolean fboLayerInUse = false;
protected static boolean firstFrame = true;
protected static int reqNumSamples;
protected static int numSamples;
protected static IntBuffer glColorFbo;
protected static IntBuffer glMultiFbo;
protected static IntBuffer glColorBuf;
protected static IntBuffer glColorTex;
protected static IntBuffer glDepthStencil;
protected static IntBuffer glDepth;
protected static IntBuffer glStencil;
protected static int fboWidth, fboHeight;
protected static int backTex, frontTex;
///////////////////////////////////////////////////////////
// Texture rendering
protected static boolean loadedTexShader = false;
protected static int texShaderProgram;
protected static int texVertShader;
protected static int texFragShader;
protected static EGLContext texShaderContext;
protected static int texVertLoc;
protected static int texTCoordLoc;
protected static float[] texCoords = {
// X, Y, U, V
-1.0f, -1.0f, 0.0f, 0.0f,
+1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, +1.0f, 0.0f, 1.0f,
+1.0f, +1.0f, 1.0f, 1.0f
};
protected static FloatBuffer texData;
protected static String texVertShaderSource =
"attribute vec2 inVertex;" +
"attribute vec2 inTexcoord;" +
"varying vec2 vertTexcoord;" +
"void main() {" +
" gl_Position = vec4(inVertex, 0, 1);" +
" vertTexcoord = inTexcoord;" +
"}";
protected static String texFragShaderSource =
SHADER_PREPROCESSOR_DIRECTIVE +
"uniform sampler2D textureSampler;" +
"varying vec2 vertTexcoord;" +
"void main() {" +
" gl_FragColor = texture2D(textureSampler, vertTexcoord.st);" +
"}";
///////////////////////////////////////////////////////////
// Utilities
protected ByteBuffer byteBuffer;
protected IntBuffer intBuffer;
protected IntBuffer colorBuffer;
protected FloatBuffer depthBuffer;
protected ByteBuffer stencilBuffer;
///////////////////////////////////////////////////////////
// Intialization, finalization
public PGL(PGraphicsOpenGL pg) {
this.pg = pg;
if (glu == null) {
glu = new PGLU();
}
if (glColorTex == null) {
glColorTex = allocateIntBuffer(2);
glColorFbo = allocateIntBuffer(1);
glMultiFbo = allocateIntBuffer(1);
glColorBuf = allocateIntBuffer(1);
glDepthStencil = allocateIntBuffer(1);
glDepth = allocateIntBuffer(1);
glStencil = allocateIntBuffer(1);
fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = false;
}
byteBuffer = allocateByteBuffer(1);
intBuffer = allocateIntBuffer(1);
}
protected void setFrameRate(float framerate) {
}
protected void initSurface(int antialias) {
reqNumSamples = qualityToSamples(antialias);
fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = true;
}
protected void deleteSurface() {
if (glColorTex != null) {
deleteTextures(2, glColorTex);
deleteFramebuffers(1, glColorFbo);
deleteFramebuffers(1, glMultiFbo);
deleteRenderbuffers(1, glColorBuf);
deleteRenderbuffers(1, glDepthStencil);
deleteRenderbuffers(1, glDepth);
deleteRenderbuffers(1, glStencil);
}
fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = true;
}
protected void update() {
if (!fboLayerCreated) {
String ext = getString(EXTENSIONS);
if (-1 < ext.indexOf("texture_non_power_of_two")) {
fboWidth = pg.width;
fboHeight = pg.height;
} else {
fboWidth = nextPowerOfTwo(pg.width);
fboHeight = nextPowerOfTwo(pg.height);
}
getIntegerv(MAX_SAMPLES, intBuffer);
if (-1 < ext.indexOf("_framebuffer_multisample") &&
1 < intBuffer.get(0)) {
numSamples = reqNumSamples;
} else {
numSamples = 1;
}
boolean multisample = 1 < numSamples;
boolean packed = ext.indexOf("packed_depth_stencil") != -1;
int depthBits = getDepthBits();
int stencilBits = getStencilBits();
genTextures(2, glColorTex);
for (int i = 0; i < 2; i++) {
bindTexture(TEXTURE_2D, glColorTex.get(i));
texParameteri(TEXTURE_2D, TEXTURE_MIN_FILTER, NEAREST);
texParameteri(TEXTURE_2D, TEXTURE_MAG_FILTER, NEAREST);
texParameteri(TEXTURE_2D, TEXTURE_WRAP_S, CLAMP_TO_EDGE);
texParameteri(TEXTURE_2D, TEXTURE_WRAP_T, CLAMP_TO_EDGE);
texImage2D(TEXTURE_2D, 0, RGBA, fboWidth, fboHeight, 0,
RGBA, UNSIGNED_BYTE, null);
initTexture(TEXTURE_2D, RGBA, fboWidth, fboHeight, pg.backgroundColor);
}
bindTexture(TEXTURE_2D, 0);
backTex = 0;
frontTex = 1;
genFramebuffers(1, glColorFbo);
bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0));
framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, TEXTURE_2D,
glColorTex.get(backTex), 0);
if (multisample) {
// Creating multisampled FBO
genFramebuffers(1, glMultiFbo);
bindFramebuffer(FRAMEBUFFER, glMultiFbo.get(0));
// color render buffer...
genRenderbuffers(1, glColorBuf);
bindRenderbuffer(RENDERBUFFER, glColorBuf.get(0));
renderbufferStorageMultisample(RENDERBUFFER, numSamples,
RGBA8, fboWidth, fboHeight);
framebufferRenderbuffer(FRAMEBUFFER, COLOR_ATTACHMENT0,
RENDERBUFFER, glColorBuf.get(0));
}
// Creating depth and stencil buffers
if (packed && depthBits == 24 && stencilBits == 8) {
// packed depth+stencil buffer
genRenderbuffers(1, glDepthStencil);
bindRenderbuffer(RENDERBUFFER, glDepthStencil.get(0));
if (multisample) {
renderbufferStorageMultisample(RENDERBUFFER, numSamples,
DEPTH24_STENCIL8, fboWidth, fboHeight);
} else {
renderbufferStorage(RENDERBUFFER, DEPTH24_STENCIL8,
fboWidth, fboHeight);
}
framebufferRenderbuffer(FRAMEBUFFER, DEPTH_ATTACHMENT, RENDERBUFFER,
glDepthStencil.get(0));
framebufferRenderbuffer(FRAMEBUFFER, STENCIL_ATTACHMENT, RENDERBUFFER,
glDepthStencil.get(0));
} else {
// separate depth and stencil buffers
if (0 < depthBits) {
int depthComponent = DEPTH_COMPONENT16;
if (depthBits == 32) {
depthComponent = DEPTH_COMPONENT32;
} else if (depthBits == 24) {
depthComponent = DEPTH_COMPONENT24;
} else if (depthBits == 16) {
depthComponent = DEPTH_COMPONENT16;
}
genRenderbuffers(1, glDepth);
bindRenderbuffer(RENDERBUFFER, glDepth.get(0));
if (multisample) {
renderbufferStorageMultisample(RENDERBUFFER, numSamples,
depthComponent, fboWidth, fboHeight);
} else {
renderbufferStorage(RENDERBUFFER, depthComponent,
fboWidth, fboHeight);
}
framebufferRenderbuffer(FRAMEBUFFER, DEPTH_ATTACHMENT,
RENDERBUFFER, glDepth.get(0));
}
if (0 < stencilBits) {
int stencilIndex = STENCIL_INDEX1;
if (stencilBits == 8) {
stencilIndex = STENCIL_INDEX8;
} else if (stencilBits == 4) {
stencilIndex = STENCIL_INDEX4;
} else if (stencilBits == 1) {
stencilIndex = STENCIL_INDEX1;
}
genRenderbuffers(1, glStencil);
bindRenderbuffer(RENDERBUFFER, glStencil.get(0));
if (multisample) {
renderbufferStorageMultisample(RENDERBUFFER, numSamples,
stencilIndex, fboWidth, fboHeight);
} else {
renderbufferStorage(RENDERBUFFER, stencilIndex,
fboWidth, fboHeight);
}
framebufferRenderbuffer(FRAMEBUFFER, STENCIL_ATTACHMENT,
RENDERBUFFER, glStencil.get(0));
}
}
validateFramebuffer();
// Clear all buffers.
clearDepth(1);
clearStencil(0);
int argb = pg.backgroundColor;
float a = ((argb >> 24) & 0xff) / 255.0f;
float r = ((argb >> 16) & 0xff) / 255.0f;
float g = ((argb >> 8) & 0xff) / 255.0f;
float b = ((argb) & 0xff) / 255.0f;
clearColor(r, g, b, a);
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
bindFramebuffer(FRAMEBUFFER, 0);
fboLayerCreated = true;
}
}
protected int getReadFramebuffer() {
if (fboLayerInUse) {
return glColorFbo.get(0);
} else {
return 0;
}
}
protected int getDrawFramebuffer() {
if (fboLayerInUse) {
return glColorFbo.get(0);
} else {
return 0;
}
}
protected int getDefaultDrawBuffer() {
if (fboLayerInUse) {
return COLOR_ATTACHMENT0;
} else {
return BACK;
}
}
protected int getDefaultReadBuffer() {
if (fboLayerInUse) {
return COLOR_ATTACHMENT0;
} else {
return FRONT;
}
}
protected boolean isFBOBacked() {
return fboLayerInUse;
}
protected void needFBOLayer() {
FORCE_SCREEN_FBO = true;
}
protected boolean isMultisampled() {
return false;
}
protected int getDepthBits() {
intBuffer.rewind();
getIntegerv(DEPTH_BITS, intBuffer);
return intBuffer.get(0);
}
protected int getStencilBits() {
intBuffer.rewind();
getIntegerv(STENCIL_BITS, intBuffer);
return intBuffer.get(0);
}
protected boolean getDepthTest() {
intBuffer.rewind();
getBooleanv(DEPTH_TEST, intBuffer);
return intBuffer.get(0) == 0 ? false : true;
}
protected boolean getDepthWriteMask() {
intBuffer.rewind();
getBooleanv(DEPTH_WRITEMASK, intBuffer);
return intBuffer.get(0) == 0 ? false : true;
}
protected Texture wrapBackTexture() {
Texture tex = new Texture(pg.parent);
tex.init(pg.width, pg.height,
glColorTex.get(backTex), TEXTURE_2D, RGBA,
fboWidth, fboHeight, NEAREST, NEAREST,
CLAMP_TO_EDGE, CLAMP_TO_EDGE);
tex.invertedY(true);
tex.colorBufferOf(pg);
pg.setCache(pg, tex);
return tex;
}
protected Texture wrapFrontTexture() {
Texture tex = new Texture(pg.parent);
tex.init(pg.width, pg.height,
glColorTex.get(frontTex), TEXTURE_2D, RGBA,
fboWidth, fboHeight, NEAREST, NEAREST,
CLAMP_TO_EDGE, CLAMP_TO_EDGE);
tex.invertedY(true);
tex.colorBufferOf(pg);
return tex;
}
int getBackTextureName() {
return glColorTex.get(backTex);
}
int getFrontTextureName() {
return glColorTex.get(frontTex);
}
protected void bindFrontTexture() {
if (!texturingIsEnabled(TEXTURE_2D)) {
enableTexturing(TEXTURE_2D);
}
bindTexture(TEXTURE_2D, glColorTex.get(frontTex));
}
protected void unbindFrontTexture() {
if (textureIsBound(TEXTURE_2D, glColorTex.get(frontTex))) {
// We don't want to unbind another texture
// that might be bound instead of this one.
if (!texturingIsEnabled(TEXTURE_2D)) {
enableTexturing(TEXTURE_2D);
bindTexture(TEXTURE_2D, 0);
disableTexturing(TEXTURE_2D);
} else {
bindTexture(TEXTURE_2D, 0);
}
}
}
protected void syncBackTexture() {
if (1 < numSamples) {
bindFramebuffer(READ_FRAMEBUFFER, glMultiFbo.get(0));
bindFramebuffer(DRAW_FRAMEBUFFER, glColorFbo.get(0));
blitFramebuffer(0, 0, fboWidth, fboHeight,
0, 0, fboWidth, fboHeight,
COLOR_BUFFER_BIT, NEAREST);
}
}
protected int qualityToSamples(int quality) {
if (quality <= 1) {
return 1;
} else {
// Number of samples is always an even number:
int n = 2 * (quality / 2);
return n;
}
}
///////////////////////////////////////////////////////////
// Frame rendering
protected void beginDraw(boolean clear0) {
if (fboLayerInUse(clear0)) {
bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0));
framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0,
TEXTURE_2D, glColorTex.get(backTex), 0);
if (1 < numSamples) {
bindFramebuffer(FRAMEBUFFER, glMultiFbo.get(0));
}
if (firstFrame) {
// No need to draw back color buffer because we are in the first frame.
int argb = pg.backgroundColor;
float a = ((argb >> 24) & 0xff) / 255.0f;
float r = ((argb >> 16) & 0xff) / 255.0f;
float g = ((argb >> 8) & 0xff) / 255.0f;
float b = ((argb) & 0xff) / 255.0f;
clearColor(r, g, b, a);
clear(COLOR_BUFFER_BIT);
} else if (!clear0) {
// Render previous back texture (now is the front) as background,
// because no background() is being used ("incremental drawing")
drawTexture(TEXTURE_2D, glColorTex.get(frontTex),
fboWidth, fboHeight, 0, 0, pg.width, pg.height,
0, 0, pg.width, pg.height);
}
fboLayerInUse = true;
} else {
fboLayerInUse = false;
}
if (firstFrame) {
firstFrame = false;
}
if (!fboLayerByDefault) {
// The result of this assignment is the following: if the user requested
// at some point the use of the FBO layer, but subsequently didn't do
// request it again, then the rendering won't use the FBO layer if not
// needed, since it is slower than simple onscreen rendering.
FORCE_SCREEN_FBO = false;
}
}
protected void endDraw(boolean clear) {
if (fboLayerInUse) {
syncBackTexture();
// Draw the contents of the back texture to the screen framebuffer.
bindFramebuffer(FRAMEBUFFER, 0);
clearDepth(1);
clearColor(0, 0, 0, 0);
clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
// Render current back texture to screen, without blending.
disable(BLEND);
drawTexture(TEXTURE_2D, glColorTex.get(backTex),
fboWidth, fboHeight, 0, 0, pg.width, pg.height,
0, 0, pg.width, pg.height);
// Swapping front and back textures.
int temp = frontTex;
frontTex = backTex;
backTex = temp;
}
flush();
}
protected boolean canDraw() {
return true;
}
protected void requestDraw() {
pg.parent.andresNeedsBetterAPI();
}
protected boolean threadIsCurrent() {
return Thread.currentThread() == glThread;
}
protected boolean fboLayerInUse(boolean clear0) {
boolean cond = !clear0 || FORCE_SCREEN_FBO || 1 < numSamples;
return cond && glColorFbo.get(0) != 0;
}
protected void beginGL() {
}
protected void endGL() {
}
///////////////////////////////////////////////////////////
// Caps query
public String getString(int name) {
return GLES20.glGetString(name);
}
public void getIntegerv(int name, IntBuffer values) {
if (-1 < name) {
GLES20.glGetIntegerv(name, values);
} else {
fillIntBuffer(values, 0, values.capacity(), 0);
}
}
public void getFloatv(int name, FloatBuffer values) {
if (-1 < name) {
GLES20.glGetFloatv(name, values);
} else {
fillFloatBuffer(values, 0, values.capacity(), 0);
}
}
public void getBooleanv(int name, IntBuffer values) {
if (-1 < name) {
GLES20.glGetBooleanv(name, values);
} else {
fillIntBuffer(values, 0, values.capacity(), 0);
}
}
///////////////////////////////////////////////////////////
// Enable/disable caps
public void enable(int cap) {
if (-1 < cap) {
GLES20.glEnable(cap);
}
}
public void disable(int cap) {
if (-1 < cap) {
GLES20.glDisable(cap);
}
}
///////////////////////////////////////////////////////////
// Render control
public void flush() {
GLES20.glFlush();
}
public void finish() {
GLES20.glFinish();
}
///////////////////////////////////////////////////////////
// Error handling
public int getError() {
return GLES20.glGetError();
}
public String errorString(int err) {
return GLU.gluErrorString(err);
}
///////////////////////////////////////////////////////////
// Rendering options
public void frontFace(int mode) {
GLES20.glFrontFace(mode);
}
public void cullFace(int mode) {
GLES20.glCullFace(mode);
}
public void depthMask(boolean flag) {
GLES20.glDepthMask(flag);
}
public void depthFunc(int func) {
GLES20.glDepthFunc(func);
}
///////////////////////////////////////////////////////////
// Textures