@@ -53,8 +53,9 @@ public abstract class PGL {
5353
5454 // Basic fields
5555
56- /** The PGraphics object using this interface */
57- protected PGraphicsOpenGL pg ;
56+ /** The PGraphics and PApplet objects using this interface */
57+ protected PGraphicsOpenGL graphics ;
58+ protected PApplet sketch ;
5859
5960 /** OpenGL thread */
6061 protected Thread glThread ;
@@ -263,6 +264,10 @@ public abstract class PGL {
263264 protected int geomCount = 0 ;
264265 protected int pgeomCount ;
265266
267+ /** Used to register calls to background. */
268+ protected boolean clearColor = false ;
269+ protected boolean pclearColor ;
270+
266271 // ........................................................
267272
268273 // Error messages
@@ -425,7 +430,7 @@ public PGL() { }
425430
426431
427432 public PGL (PGraphicsOpenGL pg ) {
428- this .pg = pg ;
433+ this .graphics = pg ;
429434 if (glColorTex == null ) {
430435 glColorFbo = allocateIntBuffer (1 );
431436 glColorTex = allocateIntBuffer (2 );
@@ -552,14 +557,14 @@ protected boolean getDepthWriteMask() {
552557
553558 protected Texture wrapBackTexture (Texture texture ) {
554559 if (texture == null ) {
555- texture = new Texture (pg );
556- texture .init (pg .width , pg .height ,
560+ texture = new Texture (graphics );
561+ texture .init (graphics .width , graphics .height ,
557562 glColorTex .get (backTex ), TEXTURE_2D , RGBA ,
558563 fboWidth , fboHeight , NEAREST , NEAREST ,
559564 CLAMP_TO_EDGE , CLAMP_TO_EDGE );
560565 texture .invertedY (true );
561566 texture .colorBuffer (true );
562- pg .setCache (pg , texture );
567+ graphics .setCache (graphics , texture );
563568 } else {
564569 texture .glName = glColorTex .get (backTex );
565570 }
@@ -569,8 +574,8 @@ protected Texture wrapBackTexture(Texture texture) {
569574
570575 protected Texture wrapFrontTexture (Texture texture ) {
571576 if (texture == null ) {
572- texture = new Texture (pg );
573- texture .init (pg .width , pg .height ,
577+ texture = new Texture (graphics );
578+ texture .init (graphics .width , graphics .height ,
574579 glColorTex .get (frontTex ), TEXTURE_2D , RGBA ,
575580 fboWidth , fboHeight , NEAREST , NEAREST ,
576581 CLAMP_TO_EDGE , CLAMP_TO_EDGE );
@@ -613,7 +618,7 @@ protected void syncBackTexture() {
613618 bindFramebufferImpl (READ_FRAMEBUFFER , glMultiFbo .get (0 ));
614619 bindFramebufferImpl (DRAW_FRAMEBUFFER , glColorFbo .get (0 ));
615620 int mask = COLOR_BUFFER_BIT ;
616- if (pg .getHint (PConstants .ENABLE_BUFFER_READING )) {
621+ if (graphics .getHint (PConstants .ENABLE_BUFFER_READING )) {
617622 mask |= DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT ;
618623 }
619624 blitFramebuffer (0 , 0 , fboWidth , fboHeight ,
@@ -662,16 +667,38 @@ public boolean insideCloseButton(float x, float y) {
662667 // Frame rendering
663668
664669
665- protected void beginRender (boolean pclearColor ) {
670+ protected void clearBackground (float r , float g , float b , float a , boolean depth ) {
671+ if (depth ) {
672+ clearDepth (1 );
673+ clear (PGL .DEPTH_BUFFER_BIT );
674+ }
675+ clearColor (r , g , b , a );
676+ clear (PGL .COLOR_BUFFER_BIT );
677+ if (0 < sketch .frameCount ) {
678+ clearColor = true ;
679+ }
680+ }
681+
682+
683+ protected void beginRender () {
684+ if (sketch == null ) {
685+ sketch = graphics .parent ;
686+ }
687+
666688 pgeomCount = geomCount ;
667689 geomCount = 0 ;
668690
691+ pclearColor = clearColor ;
692+ clearColor = false ;
693+
669694 if (requestedFBOLayer ) {
670695 if (requestedFBOLayerReset ) {
671696 destroyFBOLayer ();
672697 requestedFBOLayerReset = false ;
673698 }
674- if (!fboLayerCreated ) createFBOLayer (pclearColor );
699+ if (!fboLayerCreated ) {
700+ createFBOLayer ();
701+ }
675702
676703 // Draw to the back texture
677704 bindFramebufferImpl (FRAMEBUFFER , glColorFbo .get (0 ));
@@ -684,14 +711,14 @@ protected void beginRender(boolean pclearColor) {
684711
685712 if (firstFrame ) {
686713 // No need to draw back color buffer because we are in the first frame.
687- int argb = pg .backgroundColor ;
714+ int argb = graphics .backgroundColor ;
688715 float a = ((argb >> 24 ) & 0xff ) / 255.0f ;
689716 float r = ((argb >> 16 ) & 0xff ) / 255.0f ;
690717 float g = ((argb >> 8 ) & 0xff ) / 255.0f ;
691718 float b = ((argb ) & 0xff ) / 255.0f ;
692719 clearColor (r , g , b , a );
693720 clear (COLOR_BUFFER_BIT );
694- } else if (!pclearColor ) {
721+ } else if (!pclearColor || ! sketch . isLooping () ) {
695722 // Render previous back texture (now is the front) as background,
696723 // because no background() is being used ("incremental drawing")
697724 int x = 0 ;
@@ -700,11 +727,11 @@ protected void beginRender(boolean pclearColor) {
700727 x = (int )presentX ;
701728 y = (int )presentY ;
702729 }
703- float scale = pg .getPixelScale ();
730+ float scale = graphics .getPixelScale ();
704731 drawTexture (TEXTURE_2D , glColorTex .get (frontTex ), fboWidth , fboHeight ,
705- x , y , pg .width , pg .height ,
706- 0 , 0 , (int )(scale * pg .width ), (int )(scale * pg .height ),
707- 0 , 0 , pg .width , pg .height );
732+ x , y , graphics .width , graphics .height ,
733+ 0 , 0 , (int )(scale * graphics .width ), (int )(scale * graphics .height ),
734+ 0 , 0 , graphics .width , graphics .height );
708735 }
709736
710737 fboLayerInUse = true ;
@@ -716,7 +743,7 @@ protected void beginRender(boolean pclearColor) {
716743 }
717744
718745
719- protected void endRender (boolean pclearColor , int windowColor ) {
746+ protected void endRender (int windowColor ) {
720747 if (fboLayerInUse ) {
721748 syncBackTexture ();
722749
@@ -764,18 +791,18 @@ protected void endRender(boolean pclearColor, int windowColor) {
764791 x = (int )presentX ;
765792 y = (int )presentY ;
766793 }
767- float scale = pg .getPixelScale ();
794+ float scale = graphics .getPixelScale ();
768795 drawTexture (TEXTURE_2D , glColorTex .get (backTex ),
769796 fboWidth , fboHeight ,
770- x , y , pg .width , pg .height ,
771- 0 , 0 , (int )(scale * pg .width ), (int )(scale * pg .height ),
772- 0 , 0 , pg .width , pg .height );
797+ x , y , graphics .width , graphics .height ,
798+ 0 , 0 , (int )(scale * graphics .width ), (int )(scale * graphics .height ),
799+ 0 , 0 , graphics .width , graphics .height );
773800
774801 // Swapping front and back textures.
775802 int temp = frontTex ;
776803 frontTex = backTex ;
777804 backTex = temp ;
778- } else if (!pclearColor && 0 < pg . parent . frameCount || !pg . parent .isLooping ()) {
805+ } else if (!clearColor && 0 < sketch . frameCount || !sketch .isLooping ()) {
779806 requestFBOLayer ();
780807 }
781808 }
@@ -812,15 +839,15 @@ protected void beginGL() { }
812839 protected void endGL () { }
813840
814841
815- private void createFBOLayer (boolean pclearColor ) {
816- float scale = pg .getPixelScale ();
842+ private void createFBOLayer () {
843+ float scale = graphics .getPixelScale ();
817844
818845 if (hasNpotTexSupport ()) {
819- fboWidth = (int )(scale * pg .width );
820- fboHeight = (int )(scale * pg .height );
846+ fboWidth = (int )(scale * graphics .width );
847+ fboHeight = (int )(scale * graphics .height );
821848 } else {
822- fboWidth = nextPowerOfTwo ((int )(scale * pg .width ));
823- fboHeight = nextPowerOfTwo ((int )(scale * pg .height ));
849+ fboWidth = nextPowerOfTwo ((int )(scale * graphics .width ));
850+ fboHeight = nextPowerOfTwo ((int )(scale * graphics .height ));
824851 }
825852
826853 int maxs = maxSamples ();
@@ -844,7 +871,7 @@ private void createFBOLayer(boolean pclearColor) {
844871 texParameteri (TEXTURE_2D , TEXTURE_WRAP_T , CLAMP_TO_EDGE );
845872 texImage2D (TEXTURE_2D , 0 , RGBA , fboWidth , fboHeight , 0 ,
846873 RGBA , UNSIGNED_BYTE , null );
847- initTexture (TEXTURE_2D , RGBA , fboWidth , fboHeight , pg .backgroundColor );
874+ initTexture (TEXTURE_2D , RGBA , fboWidth , fboHeight , graphics .backgroundColor );
848875 }
849876 bindTexture (TEXTURE_2D , 0 );
850877
@@ -856,7 +883,7 @@ private void createFBOLayer(boolean pclearColor) {
856883 framebufferTexture2D (FRAMEBUFFER , COLOR_ATTACHMENT0 , TEXTURE_2D ,
857884 glColorTex .get (backTex ), 0 );
858885
859- if (!multisample || pg .getHint (PConstants .ENABLE_BUFFER_READING )) {
886+ if (!multisample || graphics .getHint (PConstants .ENABLE_BUFFER_READING )) {
860887 // If not multisampled, this is the only depth and stencil buffer.
861888 // If multisampled and depth reading enabled, these are going to
862889 // hold downsampled depth and stencil buffers.
@@ -885,7 +912,7 @@ private void createFBOLayer(boolean pclearColor) {
885912 // Clear all buffers.
886913 clearDepth (1 );
887914 clearStencil (0 );
888- int argb = pg .backgroundColor ;
915+ int argb = graphics .backgroundColor ;
889916 float a = ((argb >> 24 ) & 0xff ) / 255.0f ;
890917 float r = ((argb >> 16 ) & 0xff ) / 255.0f ;
891918 float g = ((argb >> 8 ) & 0xff ) / 255.0f ;
@@ -895,13 +922,13 @@ private void createFBOLayer(boolean pclearColor) {
895922
896923 bindFramebufferImpl (FRAMEBUFFER , 0 );
897924
898- if (0 < pg . parent .frameCount ) {
925+ if (0 < sketch .frameCount ) {
899926 // Copy the contents of the front and back screen buffers to the textures
900927 // of the FBO, so they are properly initialized. Note that the front buffer
901928 // of the default framebuffer (the screen) contains the previous frame:
902929 // https://www.opengl.org/wiki/Default_Framebuffer
903930 // so it is copied to the front texture of the FBO layer:
904- if (pclearColor || 0 < pgeomCount || !pg . parent .isLooping ()) {
931+ if (pclearColor || 0 < pgeomCount || !sketch .isLooping ()) {
905932 readBuffer (FRONT );
906933 } else {
907934 // ...except when the previous frame has not been cleared and nothing was
@@ -954,6 +981,7 @@ protected void destroyFBOLayer() {
954981// firstFrame = false;
955982 }
956983
984+
957985 private void createDepthAndStencilBuffer (boolean multisample , int depthBits ,
958986 int stencilBits , boolean packed ) {
959987 // Creating depth and stencil buffers
@@ -1160,7 +1188,7 @@ public void drawTexture(int target, int id,int texW, int texH,
11601188 int viewX , int viewY , int viewW , int viewH ,
11611189 int texX0 , int texY0 , int texX1 , int texY1 ,
11621190 int scrX0 , int scrY0 , int scrX1 , int scrY1 ) {
1163- int viewF = (int )pg .getPixelScale ();
1191+ int viewF = (int )graphics .getPixelScale ();
11641192 drawTexture (target , id , texW , texH ,
11651193 viewX , viewY , viewW , viewH , viewF ,
11661194 texX0 , texY0 , texX1 , texY1 ,
@@ -1187,7 +1215,7 @@ public void drawTexture(int target, int id,int texW, int texH,
11871215
11881216
11891217 protected PGL initTex2DShader () {
1190- PGL ppgl = primaryPGL ? this : pg .getPrimaryPGL ();
1218+ PGL ppgl = primaryPGL ? this : graphics .getPrimaryPGL ();
11911219
11921220 if (!ppgl .loadedTex2DShader || ppgl .tex2DShaderContext != ppgl .glContext ) {
11931221 String [] preprocVertSrc = preprocessVertexSource (texVertShaderSource , getGLSLVersion ());
@@ -1319,7 +1347,7 @@ protected void drawTexture2D(int id, int texW, int texH,
13191347
13201348
13211349 protected PGL initTexRectShader () {
1322- PGL ppgl = primaryPGL ? this : pg .getPrimaryPGL ();
1350+ PGL ppgl = primaryPGL ? this : graphics .getPrimaryPGL ();
13231351
13241352 if (!ppgl .loadedTexRectShader || ppgl .texRectShaderContext != ppgl .glContext ) {
13251353 String [] preprocVertSrc = preprocessVertexSource (texVertShaderSource , getGLSLVersion ());
@@ -1456,7 +1484,7 @@ protected int getColorValue(int scrX, int scrY) {
14561484 colorBuffer = IntBuffer .allocate (1 );
14571485 }
14581486 colorBuffer .rewind ();
1459- readPixels (scrX , pg .height - scrY - 1 , 1 , 1 , RGBA , UNSIGNED_BYTE ,
1487+ readPixels (scrX , graphics .height - scrY - 1 , 1 , 1 , RGBA , UNSIGNED_BYTE ,
14601488 colorBuffer );
14611489 return colorBuffer .get ();
14621490 }
@@ -1467,7 +1495,7 @@ protected float getDepthValue(int scrX, int scrY) {
14671495 depthBuffer = FloatBuffer .allocate (1 );
14681496 }
14691497 depthBuffer .rewind ();
1470- readPixels (scrX , pg .height - scrY - 1 , 1 , 1 , DEPTH_COMPONENT , FLOAT ,
1498+ readPixels (scrX , graphics .height - scrY - 1 , 1 , 1 , DEPTH_COMPONENT , FLOAT ,
14711499 depthBuffer );
14721500 return depthBuffer .get (0 );
14731501 }
@@ -1478,7 +1506,7 @@ protected byte getStencilValue(int scrX, int scrY) {
14781506 stencilBuffer = ByteBuffer .allocate (1 );
14791507 }
14801508 stencilBuffer .rewind ();
1481- readPixels (scrX , pg .height - scrY - 1 , 1 , 1 , STENCIL_INDEX ,
1509+ readPixels (scrX , graphics .height - scrY - 1 , 1 , 1 , STENCIL_INDEX ,
14821510 UNSIGNED_BYTE , stencilBuffer );
14831511 return stencilBuffer .get (0 );
14841512 }
@@ -1751,12 +1779,12 @@ protected int getGLSLVersion() {
17511779
17521780
17531781 protected String [] loadVertexShader (String filename ) {
1754- return pg . parent .loadStrings (filename );
1782+ return sketch .loadStrings (filename );
17551783 }
17561784
17571785
17581786 protected String [] loadFragmentShader (String filename ) {
1759- return pg . parent .loadStrings (filename );
1787+ return sketch .loadStrings (filename );
17601788 }
17611789
17621790
@@ -2925,18 +2953,18 @@ protected interface FontOutline {
29252953 // to glReadPixels() should be done in readPixelsImpl().
29262954
29272955 public void readPixels (int x , int y , int width , int height , int format , int type , Buffer buffer ){
2928- boolean multisampled = isMultisampled () || pg .offscreenMultisample ;
2929- boolean depthReadingEnabled = pg .getHint (PConstants .ENABLE_BUFFER_READING );
2956+ boolean multisampled = isMultisampled () || graphics .offscreenMultisample ;
2957+ boolean depthReadingEnabled = graphics .getHint (PConstants .ENABLE_BUFFER_READING );
29302958 boolean depthRequested = format == STENCIL_INDEX || format == DEPTH_COMPONENT || format == DEPTH_STENCIL ;
29312959
29322960 if (multisampled && depthRequested && !depthReadingEnabled ) {
29332961 PGraphics .showWarning (DEPTH_READING_NOT_ENABLED_ERROR );
29342962 return ;
29352963 }
29362964
2937- pg .beginReadPixels ();
2965+ graphics .beginReadPixels ();
29382966 readPixelsImpl (x , y , width , height , format , type , buffer );
2939- pg .endReadPixels ();
2967+ graphics .endReadPixels ();
29402968 }
29412969
29422970 protected abstract void readPixelsImpl (int x , int y , int width , int height , int format , int type , Buffer buffer );
@@ -3128,9 +3156,9 @@ public void bindTexture(int target, int texture) {
31283156 // Framebuffers Objects
31293157
31303158 public void bindFramebuffer (int target , int framebuffer ) {
3131- pg .beginBindFramebuffer (target , framebuffer );
3159+ graphics .beginBindFramebuffer (target , framebuffer );
31323160 bindFramebufferImpl (target , framebuffer );
3133- pg .endBindFramebuffer (target , framebuffer );
3161+ graphics .endBindFramebuffer (target , framebuffer );
31343162 }
31353163 protected abstract void bindFramebufferImpl (int target , int framebuffer );
31363164
0 commit comments