-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPGL.java
More file actions
3373 lines (2719 loc) · 108 KB
/
PGL.java
File metadata and controls
3373 lines (2719 loc) · 108 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) 2012-15 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
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.io.IOException;
import java.net.URL;
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 java.util.regex.Pattern;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PGraphics;
/**
* Processing-OpenGL abstraction layer. Needs to be implemented by subclasses
* using specific OpenGL-Java bindings.
*
* It includes a full GLES 2.0 interface.
*
*/
public abstract class PGL {
// ........................................................
// Basic fields
/** The PGraphics and PApplet objects using this interface */
protected PGraphicsOpenGL graphics;
protected PApplet sketch;
/** OpenGL thread */
protected Thread glThread;
/** ID of the GL context associated to the surface **/
protected int glContext;
/** true if this is the GL interface for a primary surface PGraphics */
public boolean primaryPGL;
// ........................................................
// Parameters
public static int REQUESTED_DEPTH_BITS = 24;
public static int REQUESTED_STENCIL_BITS = 8;
public static int REQUESTED_ALPHA_BITS = 8;
/** Switches between the use of regular and direct buffers. */
protected static boolean USE_DIRECT_BUFFERS = true;
protected static int MIN_DIRECT_BUFFER_SIZE = 1;
/** Enables/disables mipmap use. */
protected static boolean MIPMAPS_ENABLED = true;
/** Initial sizes for arrays of input and tessellated data. */
protected static int DEFAULT_IN_VERTICES = 64;
protected static int DEFAULT_IN_EDGES = 128;
protected static int DEFAULT_IN_TEXTURES = 64;
protected static int DEFAULT_TESS_VERTICES = 64;
protected static int DEFAULT_TESS_INDICES = 128;
/** Maximum lights by default is 8, the minimum defined by OpenGL. */
protected static 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 int MAX_VERTEX_INDEX = 32767;
protected static 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 int FLUSH_VERTEX_COUNT = MAX_VERTEX_INDEX1;
/** Minimum/maximum dimensions of a texture used to hold font data. */
protected static int MIN_FONT_TEX_SIZE = 256;
protected static int MAX_FONT_TEX_SIZE = 1024;
/** Minimum stroke weight needed to apply the full path stroking
* algorithm that properly generates caps and joins.
*/
protected static float MIN_CAPS_JOINS_WEIGHT = 2f;
/** Maximum length of linear paths to be stroked with the
* full algorithm that generates accurate caps and joins.
*/
protected static int MAX_CAPS_JOINS_LENGTH = 5000;
/** Minimum array size to use arrayCopy method(). */
protected static int MIN_ARRAYCOPY_SIZE = 2;
/** Factor used to displace the stroke vertices towards the camera in
* order to make sure the lines are always on top of the fill geometry */
protected static float STROKE_DISPLACEMENT = 0.999f;
// ........................................................
// Variables to handle single-buffered situations (i.e.: Android)
protected IntBuffer firstFrame;
protected static boolean SINGLE_BUFFERED = false;
// ........................................................
// FBO layer
protected boolean fboLayerEnabled = false;
protected boolean fboLayerCreated = false;
protected boolean fboLayerEnabledReq = false;
protected boolean fboLayerDisableReq = false;
protected boolean fbolayerResetReq = false;
public int reqNumSamples;
protected int numSamples;
protected IntBuffer glColorFbo;
protected IntBuffer glColorTex;
protected IntBuffer glDepthStencil;
protected IntBuffer glDepth;
protected IntBuffer glStencil;
protected IntBuffer glMultiFbo;
protected IntBuffer glMultiColor;
protected IntBuffer glMultiDepthStencil;
protected IntBuffer glMultiDepth;
protected IntBuffer glMultiStencil;
protected int fboWidth, fboHeight;
protected int backTex, frontTex;
/** Flags used to handle the creation of a separate front texture */
protected boolean usingFrontTex = false;
protected boolean needSepFrontTex = false;
// ........................................................
// Texture rendering
protected boolean loadedTex2DShader = false;
protected int tex2DShaderProgram;
protected int tex2DVertShader;
protected int tex2DFragShader;
protected int tex2DShaderContext;
protected int tex2DVertLoc;
protected int tex2DTCoordLoc;
protected int tex2DSamplerLoc;
protected int tex2DGeoVBO;
protected boolean loadedTexRectShader = false;
protected int texRectShaderProgram;
protected int texRectVertShader;
protected int texRectFragShader;
protected int texRectShaderContext;
protected int texRectVertLoc;
protected int texRectTCoordLoc;
protected int texRectSamplerLoc;
protected int texRectGeoVBO;
protected 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 FloatBuffer texData;
protected static final String SHADER_PREPROCESSOR_DIRECTIVE =
"#ifdef GL_ES\n" +
"precision mediump float;\n" +
"precision mediump int;\n" +
"#endif\n";
protected static String[] texVertShaderSource = {
"attribute vec2 position;",
"attribute vec2 texCoord;",
"varying vec2 vertTexCoord;",
"void main() {",
" gl_Position = vec4(position, 0, 1);",
" vertTexCoord = texCoord;",
"}"
};
protected static String[] tex2DFragShaderSource = {
SHADER_PREPROCESSOR_DIRECTIVE,
"uniform sampler2D texMap;",
"varying vec2 vertTexCoord;",
"void main() {",
" gl_FragColor = texture2D(texMap, vertTexCoord.st);",
"}"
};
protected static String[] texRectFragShaderSource = {
SHADER_PREPROCESSOR_DIRECTIVE,
"uniform sampler2DRect texMap;",
"varying vec2 vertTexCoord;",
"void main() {",
" gl_FragColor = texture2DRect(texMap, vertTexCoord.st);",
"}"
};
/** Which texturing targets are enabled */
protected boolean[] texturingTargets = { false, false };
/** Used to keep track of which textures are bound to each target */
protected int maxTexUnits;
protected int activeTexUnit = 0;
protected int[][] boundTextures;
// ........................................................
// Framerate handling
protected float targetFps = 60;
protected float currentFps = 60;
protected boolean setFps = false;
// ........................................................
// Utility buffers
protected ByteBuffer byteBuffer;
protected IntBuffer intBuffer;
protected IntBuffer viewBuffer;
protected IntBuffer colorBuffer;
protected FloatBuffer depthBuffer;
protected ByteBuffer stencilBuffer;
//........................................................
// Rendering information
/** Used to register amount of geometry rendered in each frame. */
protected int geomCount = 0;
protected int pgeomCount;
/** Used to register calls to background. */
protected boolean clearColor = false;
protected boolean pclearColor;
protected boolean clearDepth = false;
protected boolean pclearDepth;
protected boolean clearStencil = false;
protected boolean pclearStencil;
// ........................................................
// Error messages
public static final String WIKI =
" Read http://wiki.processing.org/w/OpenGL_Issues for help.";
public static final String FRAMEBUFFER_ERROR =
"Framebuffer error (%1$s), rendering will probably not work as expected" + WIKI;
public static final String MISSING_FBO_ERROR =
"Framebuffer objects are not supported by this hardware (or driver)" + WIKI;
public static final String MISSING_GLSL_ERROR =
"GLSL shaders are not supported by this hardware (or driver)" + WIKI;
public static final String MISSING_GLFUNC_ERROR =
"GL function %1$s is not available on this hardware (or driver)" + WIKI;
public static final String UNSUPPORTED_GLPROF_ERROR =
"Unsupported OpenGL profile.";
public static final String TEXUNIT_ERROR =
"Number of texture units not supported by this hardware (or driver)" + WIKI;
public static final String NONPRIMARY_ERROR =
"The renderer is trying to call a PGL function that can only be called on a primary PGL. " +
"This is most likely due to a bug in the renderer's code, please report it with an " +
"issue on Processing's github page https://github.com/processing/processing/issues?state=open " +
"if using any of the built-in OpenGL renderers. If you are using a contributed " +
"library, contact the library's developers.";
protected static final String DEPTH_READING_NOT_ENABLED_ERROR =
"Reading depth and stencil values from this multisampled buffer is not enabled. " +
"You can enable it by calling hint(ENABLE_DEPTH_READING) once. " +
"If your sketch becomes too slow, disable multisampling with noSmooth() instead.";
// ........................................................
// Constants
/** Size of different types in bytes */
protected static int SIZEOF_SHORT = Short.SIZE / 8;
protected static int SIZEOF_INT = Integer.SIZE / 8;
protected static int SIZEOF_FLOAT = Float.SIZE / 8;
protected static int SIZEOF_BYTE = Byte.SIZE / 8;
protected static int SIZEOF_INDEX = SIZEOF_SHORT;
protected static int INDEX_TYPE = 0x1403; // GL_UNSIGNED_SHORT
/** 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;
// ........................................................
// Present mode
// ........................................................
// Present mode
protected boolean presentMode = false;
protected boolean showStopButton = true;
public float presentX;
public float presentY;
protected IntBuffer closeButtonTex;
protected int stopButtonColor;
protected int stopButtonWidth = 28;
protected int stopButtonHeight = 12;
protected int stopButtonX = 21; // The position of the close button is relative to the
protected int closeButtonY = 21; // lower left corner
protected static int[] closeButtonPix = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, -1,
-1, -1, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0,
0, 0, 0, -1, -1, 0, -1, -1, 0, 0, -1, -1, 0, -1, -1, 0, 0, -1, 0, 0, 0, 0, 0,
0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1,
-1, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, -1,
0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1,
0, 0, 0, -1, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, -1, 0, -1,
-1, 0, 0, -1, -1, 0, -1, -1, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, -1, -1, -1,
0, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0};
///////////////////////////////////////////////////////////////
// Initialization, finalization
public PGL() { }
public PGL(PGraphicsOpenGL pg) {
this.graphics = pg;
if (glColorTex == null) {
glColorFbo = allocateIntBuffer(1);
glColorTex = allocateIntBuffer(2);
glDepthStencil = allocateIntBuffer(1);
glDepth = allocateIntBuffer(1);
glStencil = allocateIntBuffer(1);
glMultiFbo = allocateIntBuffer(1);
glMultiColor = allocateIntBuffer(1);
glMultiDepthStencil = allocateIntBuffer(1);
glMultiDepth = allocateIntBuffer(1);
glMultiStencil = allocateIntBuffer(1);
}
byteBuffer = allocateByteBuffer(1);
intBuffer = allocateIntBuffer(1);
viewBuffer = allocateIntBuffer(4);
}
public void dispose() {
destroyFBOLayer();
}
public void setPrimary(boolean primary) {
primaryPGL = primary;
}
static public int smoothToSamples(int smooth) {
if (smooth == 0) {
// smooth(0) is noSmooth(), which is 1x sampling
return 1;
} else if (smooth == 1) {
// smooth(1) means "default smoothing", which is 2x for OpenGL
return 2;
} else {
// smooth(N) can be used for 4x, 8x, etc
return smooth;
}
}
abstract public Object getNative();
abstract protected void setFrameRate(float fps);
abstract protected void initSurface(int antialias);
abstract protected void reinitSurface();
abstract protected void registerListeners();
protected int getReadFramebuffer() {
return fboLayerEnabled ? glColorFbo.get(0) : 0;
}
protected int getDrawFramebuffer() {
if (fboLayerEnabled) return 1 < numSamples ? glMultiFbo.get(0) :
glColorFbo.get(0);
else return 0;
}
protected int getDefaultDrawBuffer() {
return fboLayerEnabled ? COLOR_ATTACHMENT0 : BACK;
}
protected int getDefaultReadBuffer() {
return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT;
}
protected boolean isFBOBacked() {
return fboLayerEnabled;
}
@Deprecated
public void requestFBOLayer() {
enableFBOLayer();
}
public void enableFBOLayer() {
fboLayerEnabledReq = true;
}
public void disableFBOLayer() {
fboLayerDisableReq = true;
}
public void resetFBOLayer() {
fbolayerResetReq = true;
}
protected boolean isMultisampled() {
return 1 < numSamples;
}
abstract protected int getDepthBits();
abstract protected int getStencilBits();
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 texture) {
if (texture == null) {
texture = new Texture(graphics);
texture.init(graphics.width, graphics.height,
glColorTex.get(backTex), TEXTURE_2D, RGBA,
fboWidth, fboHeight, NEAREST, NEAREST,
CLAMP_TO_EDGE, CLAMP_TO_EDGE);
texture.invertedY(true);
texture.colorBuffer(true);
graphics.setCache(graphics, texture);
} else {
texture.glName = glColorTex.get(backTex);
}
return texture;
}
protected Texture wrapFrontTexture(Texture texture) {
if (texture == null) {
texture = new Texture(graphics);
texture.init(graphics.width, graphics.height,
glColorTex.get(frontTex), TEXTURE_2D, RGBA,
fboWidth, fboHeight, NEAREST, NEAREST,
CLAMP_TO_EDGE, CLAMP_TO_EDGE);
texture.invertedY(true);
texture.colorBuffer(true);
} else {
texture.glName = glColorTex.get(frontTex);
}
return texture;
}
protected void bindFrontTexture() {
usingFrontTex = true;
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 (usingFrontTex) needSepFrontTex = true;
if (1 < numSamples) {
bindFramebufferImpl(READ_FRAMEBUFFER, glMultiFbo.get(0));
bindFramebufferImpl(DRAW_FRAMEBUFFER, glColorFbo.get(0));
int mask = COLOR_BUFFER_BIT;
if (graphics.getHint(PConstants.ENABLE_BUFFER_READING)) {
mask |= DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT;
}
blitFramebuffer(0, 0, fboWidth, fboHeight,
0, 0, fboWidth, fboHeight,
mask, NEAREST);
}
}
abstract protected float getPixelScale();
///////////////////////////////////////////////////////////
// Present mode
public void initPresentMode(float x, float y, int stopColor) {
presentMode = true;
showStopButton = stopColor != 0;
stopButtonColor = stopColor;
presentX = x;
presentY = y;
enableFBOLayer();
}
public boolean presentMode() {
return presentMode;
}
public float presentX() {
return presentX;
}
public float presentY() {
return presentY;
}
public boolean insideStopButton(float x, float y) {
if (!showStopButton) return false;
return stopButtonX < x && x < stopButtonX + stopButtonWidth &&
-(closeButtonY + stopButtonHeight) < y && y < -closeButtonY;
}
///////////////////////////////////////////////////////////
// Frame rendering
protected void clearDepthStencil() {
if (!pclearDepth && !pclearStencil) {
depthMask(true);
clearDepth(1);
clearStencil(0);
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT);
} else if (!pclearDepth) {
depthMask(true);
clearDepth(1);
clear(DEPTH_BUFFER_BIT);
} else if (!pclearStencil) {
clearStencil(0);
clear(STENCIL_BUFFER_BIT);
}
}
protected void clearBackground(float r, float g, float b, float a,
boolean depth, boolean stencil) {
clearColor(r, g, b, a);
if (depth && stencil) {
clearDepth(1);
clearStencil(0);
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
if (0 < sketch.frameCount) {
clearDepth = true;
clearStencil = true;
}
} else if (depth) {
clearDepth(1);
clear(DEPTH_BUFFER_BIT | COLOR_BUFFER_BIT);
if (0 < sketch.frameCount) {
clearDepth = true;
}
} else if (stencil) {
clearStencil(0);
clear(STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
if (0 < sketch.frameCount) {
clearStencil = true;
}
} else {
clear(PGL.COLOR_BUFFER_BIT);
}
if (0 < sketch.frameCount) {
clearColor = true;
}
}
protected void beginRender() {
if (sketch == null) {
sketch = graphics.parent;
}
pgeomCount = geomCount;
geomCount = 0;
pclearColor = clearColor;
clearColor = false;
pclearDepth = clearDepth;
clearDepth = false;
pclearStencil = clearStencil;
clearStencil = false;
if (SINGLE_BUFFERED && sketch.frameCount == 1) {
restoreFirstFrame();
}
if (fboLayerEnabledReq) {
fboLayerEnabled = true;
fboLayerEnabledReq = false;
}
if (fboLayerEnabled) {
if (fbolayerResetReq) {
destroyFBOLayer();
fbolayerResetReq = false;
}
if (!fboLayerCreated) {
createFBOLayer();
}
// Draw to the back texture
bindFramebufferImpl(FRAMEBUFFER, glColorFbo.get(0));
framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0,
TEXTURE_2D, glColorTex.get(backTex), 0);
if (1 < numSamples) {
bindFramebufferImpl(FRAMEBUFFER, glMultiFbo.get(0));
}
if (sketch.frameCount == 0) {
// No need to draw back color buffer because we are in the first frame.
int argb = graphics.backgroundColor;
float ba = ((argb >> 24) & 0xff) / 255.0f;
float br = ((argb >> 16) & 0xff) / 255.0f;
float bg = ((argb >> 8) & 0xff) / 255.0f;
float bb = ((argb) & 0xff) / 255.0f;
clearColor(br, bg, bb, ba);
clear(COLOR_BUFFER_BIT);
} else if (!pclearColor || !sketch.isLooping()) {
// Render previous back texture (now is the front) as background,
// because no background() is being used ("incremental drawing")
int x = 0;
int y = 0;
if (presentMode) {
x = (int)presentX;
y = (int)presentY;
}
float scale = getPixelScale();
drawTexture(TEXTURE_2D, glColorTex.get(frontTex), fboWidth, fboHeight,
x, y, graphics.width, graphics.height,
0, 0, (int)(scale * graphics.width), (int)(scale * graphics.height),
0, 0, graphics.width, graphics.height);
}
}
}
protected void endRender(int windowColor) {
if (fboLayerEnabled) {
syncBackTexture();
// Draw the contents of the back texture to the screen framebuffer.
bindFramebufferImpl(FRAMEBUFFER, 0);
if (presentMode) {
float wa = ((windowColor >> 24) & 0xff) / 255.0f;
float wr = ((windowColor >> 16) & 0xff) / 255.0f;
float wg = ((windowColor >> 8) & 0xff) / 255.0f;
float wb = (windowColor & 0xff) / 255.0f;
clearDepth(1);
clearColor(wr, wg, wb, wa);
clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
if (showStopButton) {
if (closeButtonTex == null) {
closeButtonTex = allocateIntBuffer(1);
genTextures(1, closeButtonTex);
bindTexture(TEXTURE_2D, closeButtonTex.get(0));
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, stopButtonWidth, stopButtonHeight, 0, RGBA, UNSIGNED_BYTE, null);
int[] color = new int[closeButtonPix.length];
PApplet.arrayCopy(closeButtonPix, color);
// Multiply the texture by the button color
float ba = ((stopButtonColor >> 24) & 0xFF) / 255f;
float br = ((stopButtonColor >> 16) & 0xFF) / 255f;
float bg = ((stopButtonColor >> 8) & 0xFF) / 255f;
float bb = ((stopButtonColor >> 0) & 0xFF) / 255f;
for (int i = 0; i < color.length; i++) {
int c = closeButtonPix[i];
int a = (int)(ba * ((c >> 24) & 0xFF));
int r = (int)(br * ((c >> 16) & 0xFF));
int g = (int)(bg * ((c >> 8) & 0xFF));
int b = (int)(bb * ((c >> 0) & 0xFF));
color[i] = javaToNativeARGB((a << 24) | (r << 16) | (g << 8) | b);
}
IntBuffer buf = allocateIntBuffer(color);
copyToTexture(TEXTURE_2D, RGBA, closeButtonTex.get(0), 0, 0, stopButtonWidth, stopButtonHeight, buf);
bindTexture(TEXTURE_2D, 0);
}
drawTexture(TEXTURE_2D, closeButtonTex.get(0), stopButtonWidth, stopButtonHeight,
0, 0, stopButtonX + stopButtonWidth, closeButtonY + stopButtonHeight,
0, stopButtonHeight, stopButtonWidth, 0,
stopButtonX, closeButtonY, stopButtonX + stopButtonWidth, closeButtonY + stopButtonHeight);
}
} else {
clearDepth(1);
clearColor(0, 0, 0, 0);
clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
}
// Render current back texture to screen, without blending.
disable(BLEND);
int x = 0;
int y = 0;
if (presentMode) {
x = (int)presentX;
y = (int)presentY;
}
float scale = getPixelScale();
drawTexture(TEXTURE_2D, glColorTex.get(backTex),
fboWidth, fboHeight,
x, y, graphics.width, graphics.height,
0, 0, (int)(scale * graphics.width), (int)(scale * graphics.height),
0, 0, graphics.width, graphics.height);
// Swapping front and back textures.
int temp = frontTex;
frontTex = backTex;
backTex = temp;
if (fboLayerDisableReq) {
fboLayerEnabled = false;
fboLayerDisableReq = false;
}
} else {
if (SINGLE_BUFFERED && sketch.frameCount == 0) {
saveFirstFrame();
}
if (!clearColor && 0 < sketch.frameCount || !sketch.isLooping()) {
enableFBOLayer();
if (SINGLE_BUFFERED) {
createFBOLayer();
}
}
}
}
protected abstract void getGL(PGL pgl);
protected abstract boolean canDraw();
protected abstract void requestFocus();
protected abstract void requestDraw();
protected abstract void swapBuffers();
public boolean threadIsCurrent() {
return Thread.currentThread() == glThread;
}
public void setThread(Thread thread) {
glThread = thread;
}
protected void beginGL() { }
protected void endGL() { }
private void createFBOLayer() {
float scale = getPixelScale();
if (hasNpotTexSupport()) {
fboWidth = (int)(scale * graphics.width);
fboHeight = (int)(scale * graphics.height);
} else {
fboWidth = nextPowerOfTwo((int)(scale * graphics.width));
fboHeight = nextPowerOfTwo((int)(scale * graphics.height));
}
if (hasFboMultisampleSupport()) {
int maxs = maxSamples();
numSamples = PApplet.min(reqNumSamples, maxs);
} else {
numSamples = 1;
}
boolean multisample = 1 < numSamples;
boolean packed = hasPackedDepthStencilSupport();
int depthBits = PApplet.min(REQUESTED_DEPTH_BITS, getDepthBits());
int stencilBits = PApplet.min(REQUESTED_STENCIL_BITS, 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, graphics.backgroundColor);
}
bindTexture(TEXTURE_2D, 0);
backTex = 0;
frontTex = 1;
genFramebuffers(1, glColorFbo);
bindFramebufferImpl(FRAMEBUFFER, glColorFbo.get(0));
framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, TEXTURE_2D,
glColorTex.get(backTex), 0);
if (!multisample || graphics.getHint(PConstants.ENABLE_BUFFER_READING)) {
// If not multisampled, this is the only depth and stencil buffer.
// If multisampled and depth reading enabled, these are going to
// hold downsampled depth and stencil buffers.
createDepthAndStencilBuffer(false, depthBits, stencilBits, packed);
}
if (multisample) {
// Creating multisampled FBO
genFramebuffers(1, glMultiFbo);
bindFramebufferImpl(FRAMEBUFFER, glMultiFbo.get(0));
// color render buffer...
genRenderbuffers(1, glMultiColor);
bindRenderbuffer(RENDERBUFFER, glMultiColor.get(0));
renderbufferStorageMultisample(RENDERBUFFER, numSamples,
RGBA8, fboWidth, fboHeight);
framebufferRenderbuffer(FRAMEBUFFER, COLOR_ATTACHMENT0,
RENDERBUFFER, glMultiColor.get(0));
// Creating multisampled depth and stencil buffers
createDepthAndStencilBuffer(true, depthBits, stencilBits, packed);
}
int status = validateFramebuffer();
if (status == FRAMEBUFFER_INCOMPLETE_MULTISAMPLE && 1 < numSamples) {
System.err.println("Continuing with multisampling disabled");
reqNumSamples = 1;
destroyFBOLayer();
// try again
createFBOLayer();
return;
}
// Clear all buffers.
clearDepth(1);
clearStencil(0);
int argb = graphics.backgroundColor;
float ba = ((argb >> 24) & 0xff) / 255.0f;
float br = ((argb >> 16) & 0xff) / 255.0f;
float bg = ((argb >> 8) & 0xff) / 255.0f;
float bb = ((argb) & 0xff) / 255.0f;
clearColor(br, bg, bb, ba);
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
bindFramebufferImpl(FRAMEBUFFER, 0);
initFBOLayer();
fboLayerCreated = true;
}
protected abstract void initFBOLayer();
protected void saveFirstFrame() {
firstFrame = allocateDirectIntBuffer(graphics.width * graphics.height);
if (hasReadBuffer()) readBuffer(BACK);
readPixelsImpl(0, 0, graphics.width, graphics.height, RGBA, UNSIGNED_BYTE, firstFrame);
}