commit ad16ed3b4480e7d3d434058eccad2b12fe5828f1
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Fri May 12 14:51:20 2017 +0100

    Progress with Stencil App; should be working now AFAIK but doesn't.

diff --git a/src/main/java/org/distorted/library/DistortedNode.java b/src/main/java/org/distorted/library/DistortedNode.java
index b0f2e76..7088d50 100644
--- a/src/main/java/org/distorted/library/DistortedNode.java
+++ b/src/main/java/org/distorted/library/DistortedNode.java
@@ -685,7 +685,8 @@ public class DistortedNode implements DistortedSlave
     return mData.mFBO;
     }
 
-
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// APIs that control how to set the OpenGL state just before rendering this Node.
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * When rendering this Node, use ColorMask (r,g,b,a).
@@ -807,4 +808,20 @@ public class DistortedNode implements DistortedSlave
     {
     mState.glBlendFunc(src,dst);
     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Before rendering this Node, clear the following buffers.
+ * <p>
+ * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
+ *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
+ * Default: 0
+ *
+ * @param mask bitwise OR of BUFFER_BITs to clear.
+ */
+  @SuppressWarnings("unused")
+  public void glClear(int mask)
+    {
+    mState.glClear(mask);
+    }
   }
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index 3778075..b9bb335 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -64,6 +64,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
   private float mClearR, mClearG, mClearB, mClearA;
   private float mClearDepth;
+  private int mClear;
 
 //private String sNew="", sOld="";
 
@@ -92,6 +93,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     mClearA = 0.0f;
 
     mClearDepth = 1.0f;
+    mClear = GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT;
 
     mBuffer1 = new DistortedFramebuffer[EffectQuality.LENGTH];
     mBuffer2 = new DistortedFramebuffer[EffectQuality.LENGTH];
@@ -295,7 +297,7 @@ if( !sNew.equals(sOld) )
       DistortedRenderState.colorDepthOn();
       GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
       GLES30.glClearDepthf(mClearDepth);
-      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
+      GLES30.glClear(mClear);
       }
     }
 
@@ -326,7 +328,7 @@ if( !sNew.equals(sOld) )
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
- * this Surface before each render.
+ * this Surface at the beginning of each frame.
  *
  * @param r the Red component. Default: 0.0f
  * @param g the Green component. Default: 0.0f
@@ -344,7 +346,7 @@ if( !sNew.equals(sOld) )
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
- * the Depth buffer of Surface before each render.
+ * the Depth buffer of Surface at the beginning of each frame.
  *
  * @param d the Depth. Default: 1.0f
  */
@@ -353,6 +355,21 @@ if( !sNew.equals(sOld) )
     mClearDepth = d;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Which buffers to Clear at the beginning of each frame?
+ * <p>
+ * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
+ *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
+ * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
+ *
+ * @param mask bitwise OR of BUFFER_BITs to clear.
+ */
+  public void glClear(int mask)
+    {
+    mClear = mask;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Create new Projection matrix.
diff --git a/src/main/java/org/distorted/library/DistortedRenderState.java b/src/main/java/org/distorted/library/DistortedRenderState.java
index 7632319..a5f9829 100644
--- a/src/main/java/org/distorted/library/DistortedRenderState.java
+++ b/src/main/java/org/distorted/library/DistortedRenderState.java
@@ -24,30 +24,34 @@ import android.opengl.GLES30;
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Remember the OpenGL state.
+ * <p>
+ * This is a member of DistortedNode. Remembers the OpenGL state we want to set just before rendering
+ * the Node.
  */
 class DistortedRenderState
 {
-  private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA;
-  private static int sDepthMask;
-  private static int sStencilMask;
-  private static int sDepthTest;
-  private static int sStencilTest;
-  private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask;
-  private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass;
-  private static int sDepthFunc;
-  private static int sBlend;
-  private static int sBlendSrc, sBlendDst;
-
-  private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA;
-  private int mDepthMask;
-  private int mStencilMask;
-  private int mDepthTest;
-  private int mStencilTest;
-  private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask;
-  private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass;
-  private int mDepthFunc;
-  private int mBlend;
-  private int mBlendSrc, mBlendDst;
+  private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA;   //
+  private static int sDepthMask;                                           //
+  private static int sStencilMask;                                         //
+  private static int sDepthTest;                                           //
+  private static int sStencilTest;                                         //
+  private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask;  // current OpenGL state
+  private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass;  //
+  private static int sDepthFunc;                                           //
+  private static int sBlend;                                               //
+  private static int sBlendSrc, sBlendDst;                                 //
+
+  private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA;          //
+  private int mDepthMask;                                                  //
+  private int mStencilMask;                                                //
+  private int mDepthTest;                                                  //
+  private int mStencilTest;                                                //
+  private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask;         // The state we want to have
+  private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass;         //
+  private int mDepthFunc;                                                  //
+  private int mBlend;                                                      //
+  private int mBlendSrc, mBlendDst;                                        //
+  private int mClear;                                                      // This does not have a 'static' compatriot
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // default: color writes on, depth test and writes on, blending on, stencil off.
@@ -75,6 +79,8 @@ class DistortedRenderState
     mStencilOpSfail  = GLES30.GL_KEEP;
     mStencilOpDpfail = GLES30.GL_KEEP;
     mStencilOpDppass = GLES30.GL_KEEP;
+
+    mClear = 0;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -237,6 +243,13 @@ class DistortedRenderState
       sStencilMask = mStencilMask;
       GLES30.glStencilMask(sStencilMask);
       }
+
+    // 7. Clear buffers?
+
+    if( mClear!=0 )
+      {
+      GLES30.glClear(mClear);
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -314,4 +327,10 @@ class DistortedRenderState
     mBlendDst = dst;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glClear(int mask)
+    {
+    mClear = mask;
+    }
 }
