Project

General

Profile

« Previous | Next » 

Revision 9dba4df1

Added by Leszek Koltunski over 7 years ago

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

View differences:

src/main/java/org/distorted/library/DistortedRenderState.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.library;
21

  
22
import android.opengl.GLES30;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Remember the OpenGL state.
27
 */
28
class DistortedRenderState
29
{
30
  private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA;
31
  private static int sDepthMask;
32
  private static int sStencilMask;
33
  private static int sDepthTest;
34
  private static int sStencilTest;
35
  private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask;
36
  private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass;
37
  private static int sDepthFunc;
38

  
39
  private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA;
40
  private int mDepthMask;
41
  private int mStencilMask;
42
  private int mDepthTest;
43
  private int mStencilTest;
44
  private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask;
45
  private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass;
46
  private int mDepthFunc;
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
  DistortedRenderState()
51
    {
52
    mColorMaskR = 1;
53
    mColorMaskG = 1;
54
    mColorMaskB = 1;
55
    mColorMaskA = 1;
56
    mDepthMask  = 1;
57
    mStencilMask= 0x11111111;
58
    mDepthTest  = 1;
59
    mStencilTest= 0;
60

  
61
    mStencilFuncFunc = GLES30.GL_NEVER;
62
    mStencilFuncRef  = 0;
63
    mStencilFuncMask = 0x11111111;
64
    mStencilOpSfail  = GLES30.GL_KEEP;
65
    mStencilOpDpfail = GLES30.GL_KEEP;
66
    mStencilOpDppass = GLES30.GL_KEEP;
67
    mDepthFunc       = GLES30.GL_LEQUAL;
68
    }
69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  static void reset()
73
    {
74
    sColorMaskR = -1;
75
    sColorMaskG = -1;
76
    sColorMaskB = -1;
77
    sColorMaskA = -1;
78
    sDepthMask  = -1;
79
    sStencilMask= -1;
80
    sDepthTest  = -1;
81
    sStencilTest= -1;
82

  
83
    sStencilFuncFunc = -1;
84
    sStencilFuncRef  = -1;
85
    sStencilFuncMask = -1;
86
    sStencilOpSfail  = -1;
87
    sStencilOpDpfail = -1;
88
    sStencilOpDppass = -1;
89
    sDepthFunc       = -1;
90
    }
91

  
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

  
94
  void apply()
95
    {
96
    if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
97
      {
98
      sColorMaskR = mColorMaskR;
99
      sColorMaskG = mColorMaskG;
100
      sColorMaskB = mColorMaskB;
101
      sColorMaskA = mColorMaskA;
102
      GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
103
      }
104

  
105
    if( mDepthTest!=sDepthTest )
106
      {
107
      sDepthTest = mDepthTest;
108
      if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
109
      else
110
        {
111
        GLES30.glEnable (GLES30.GL_DEPTH_TEST);
112

  
113
        if( mDepthMask!=sDepthMask )
114
          {
115
          sDepthMask = mDepthMask;
116
          GLES30.glDepthMask(sDepthMask==1);
117
          }
118

  
119
        if( mDepthFunc!=sDepthFunc )
120
          {
121
          sDepthFunc = mDepthFunc;
122
          GLES30.glDepthFunc(sDepthFunc);
123
          }
124
        }
125
      }
126

  
127
    if( mStencilTest!=sStencilTest )
128
      {
129
      sStencilTest = mStencilTest;
130
      if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
131
      else
132
        {
133
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
134

  
135
        if( mStencilMask!=sStencilMask )
136
          {
137
          sStencilMask = mStencilMask;
138
          GLES30.glStencilMask(sStencilMask);
139
          }
140

  
141
        if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
142
          {
143
          sStencilFuncFunc = mStencilFuncFunc;
144
          sStencilFuncRef  = mStencilFuncRef ;
145
          sStencilFuncMask = mStencilFuncMask;
146
          GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
147
          }
148

  
149
        if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
150
          {
151
          sStencilOpSfail = mStencilOpSfail;
152
          sStencilOpDpfail= mStencilOpDpfail;
153
          sStencilOpDppass= mStencilOpDppass;
154
          GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
155
          }
156
        }
157
      }
158
    }
159

  
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

  
162
  void glColorMask(boolean r, boolean g, boolean b, boolean a)
163
    {
164
    mColorMaskR = (r ? 1:0);
165
    mColorMaskG = (g ? 1:0);
166
    mColorMaskB = (b ? 1:0);
167
    mColorMaskA = (a ? 1:0);
168
    }
169

  
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

  
172
  void glDepthMask(boolean mask)
173
    {
174
    mDepthMask = (mask ? 1:0);
175
    }
176

  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

  
179
  void glStencilMask(int mask)
180
    {
181
    mStencilMask = mask;
182
    }
183

  
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

  
186
  void glEnable(int test)
187
    {
188
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 1;
189
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
190
    }
191

  
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

  
194
  void glDisable(int test)
195
    {
196
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 0;
197
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
198
    }
199

  
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

  
202
  void glStencilFunc(int func, int ref, int mask)
203
    {
204
    mStencilFuncFunc = func;
205
    mStencilFuncRef  = ref;
206
    mStencilFuncMask = mask;
207
    }
208

  
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

  
211
  void glStencilOp(int sfail, int dpfail, int dppass)
212
    {
213
    mStencilOpSfail = sfail;
214
    mStencilOpDpfail= dpfail;
215
    mStencilOpDppass= dppass;
216
    }
217

  
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

  
220
  void glDepthFunc(int func)
221
    {
222
    mDepthFunc = func;
223
    }
224
}

Also available in: Unified diff