commit 9dba4df1c882bf8dd93463569899f87de2966a09
Author: leszek <leszek@koltunski.pl>
Date:   Thu Feb 23 22:21:19 2017 +0000

    New 'RenderState' class which will be used to change OpenGL state when rendering Nodes.

diff --git a/src/main/java/org/distorted/library/DistortedRenderState.java b/src/main/java/org/distorted/library/DistortedRenderState.java
new file mode 100644
index 0000000..e6544b3
--- /dev/null
+++ b/src/main/java/org/distorted/library/DistortedRenderState.java
@@ -0,0 +1,224 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2016 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Distorted.                                                               //
+//                                                                                               //
+// Distorted is free software: you can redistribute it and/or modify                             //
+// it under the terms of the GNU General Public License as published by                          //
+// the Free Software Foundation, either version 2 of the License, or                             //
+// (at your option) any later version.                                                           //
+//                                                                                               //
+// Distorted 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 General Public License for more details.                                                  //
+//                                                                                               //
+// You should have received a copy of the GNU General Public License                             //
+// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.library;
+
+import android.opengl.GLES30;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Remember the OpenGL state.
+ */
+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 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;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  DistortedRenderState()
+    {
+    mColorMaskR = 1;
+    mColorMaskG = 1;
+    mColorMaskB = 1;
+    mColorMaskA = 1;
+    mDepthMask  = 1;
+    mStencilMask= 0x11111111;
+    mDepthTest  = 1;
+    mStencilTest= 0;
+
+    mStencilFuncFunc = GLES30.GL_NEVER;
+    mStencilFuncRef  = 0;
+    mStencilFuncMask = 0x11111111;
+    mStencilOpSfail  = GLES30.GL_KEEP;
+    mStencilOpDpfail = GLES30.GL_KEEP;
+    mStencilOpDppass = GLES30.GL_KEEP;
+    mDepthFunc       = GLES30.GL_LEQUAL;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static void reset()
+    {
+    sColorMaskR = -1;
+    sColorMaskG = -1;
+    sColorMaskB = -1;
+    sColorMaskA = -1;
+    sDepthMask  = -1;
+    sStencilMask= -1;
+    sDepthTest  = -1;
+    sStencilTest= -1;
+
+    sStencilFuncFunc = -1;
+    sStencilFuncRef  = -1;
+    sStencilFuncMask = -1;
+    sStencilOpSfail  = -1;
+    sStencilOpDpfail = -1;
+    sStencilOpDppass = -1;
+    sDepthFunc       = -1;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void apply()
+    {
+    if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
+      {
+      sColorMaskR = mColorMaskR;
+      sColorMaskG = mColorMaskG;
+      sColorMaskB = mColorMaskB;
+      sColorMaskA = mColorMaskA;
+      GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
+      }
+
+    if( mDepthTest!=sDepthTest )
+      {
+      sDepthTest = mDepthTest;
+      if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
+      else
+        {
+        GLES30.glEnable (GLES30.GL_DEPTH_TEST);
+
+        if( mDepthMask!=sDepthMask )
+          {
+          sDepthMask = mDepthMask;
+          GLES30.glDepthMask(sDepthMask==1);
+          }
+
+        if( mDepthFunc!=sDepthFunc )
+          {
+          sDepthFunc = mDepthFunc;
+          GLES30.glDepthFunc(sDepthFunc);
+          }
+        }
+      }
+
+    if( mStencilTest!=sStencilTest )
+      {
+      sStencilTest = mStencilTest;
+      if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
+      else
+        {
+        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
+
+        if( mStencilMask!=sStencilMask )
+          {
+          sStencilMask = mStencilMask;
+          GLES30.glStencilMask(sStencilMask);
+          }
+
+        if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
+          {
+          sStencilFuncFunc = mStencilFuncFunc;
+          sStencilFuncRef  = mStencilFuncRef ;
+          sStencilFuncMask = mStencilFuncMask;
+          GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
+          }
+
+        if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
+          {
+          sStencilOpSfail = mStencilOpSfail;
+          sStencilOpDpfail= mStencilOpDpfail;
+          sStencilOpDppass= mStencilOpDppass;
+          GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
+          }
+        }
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glColorMask(boolean r, boolean g, boolean b, boolean a)
+    {
+    mColorMaskR = (r ? 1:0);
+    mColorMaskG = (g ? 1:0);
+    mColorMaskB = (b ? 1:0);
+    mColorMaskA = (a ? 1:0);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glDepthMask(boolean mask)
+    {
+    mDepthMask = (mask ? 1:0);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glStencilMask(int mask)
+    {
+    mStencilMask = mask;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glEnable(int test)
+    {
+         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 1;
+    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glDisable(int test)
+    {
+         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 0;
+    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glStencilFunc(int func, int ref, int mask)
+    {
+    mStencilFuncFunc = func;
+    mStencilFuncRef  = ref;
+    mStencilFuncMask = mask;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glStencilOp(int sfail, int dpfail, int dppass)
+    {
+    mStencilOpSfail = sfail;
+    mStencilOpDpfail= dpfail;
+    mStencilOpDppass= dppass;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void glDepthFunc(int func)
+    {
+    mDepthFunc = func;
+    }
+}
