Project

General

Profile

Download (11 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedRenderState.java @ ad16ed3b

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
 * <p>
28
 * This is a member of DistortedNode. Remembers the OpenGL state we want to set just before rendering
29
 * the Node.
30
 */
31
class DistortedRenderState
32
{
33
  private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA;   //
34
  private static int sDepthMask;                                           //
35
  private static int sStencilMask;                                         //
36
  private static int sDepthTest;                                           //
37
  private static int sStencilTest;                                         //
38
  private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask;  // current OpenGL state
39
  private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass;  //
40
  private static int sDepthFunc;                                           //
41
  private static int sBlend;                                               //
42
  private static int sBlendSrc, sBlendDst;                                 //
43

    
44
  private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA;          //
45
  private int mDepthMask;                                                  //
46
  private int mStencilMask;                                                //
47
  private int mDepthTest;                                                  //
48
  private int mStencilTest;                                                //
49
  private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask;         // The state we want to have
50
  private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass;         //
51
  private int mDepthFunc;                                                  //
52
  private int mBlend;                                                      //
53
  private int mBlendSrc, mBlendDst;                                        //
54
  private int mClear;                                                      // This does not have a 'static' compatriot
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
// default: color writes on, depth test and writes on, blending on, stencil off.
58

    
59
  DistortedRenderState()
60
    {
61
    mColorMaskR = 1;
62
    mColorMaskG = 1;
63
    mColorMaskB = 1;
64
    mColorMaskA = 1;
65

    
66
    mDepthTest  = 1;
67
    mDepthMask  = 1;
68
    mDepthFunc  = GLES30.GL_LEQUAL;
69

    
70
    mBlend      = 1;
71
    mBlendSrc   = GLES30.GL_SRC_ALPHA;
72
    mBlendDst   = GLES30.GL_ONE_MINUS_SRC_ALPHA;
73

    
74
    mStencilTest     = 0;
75
    mStencilMask     = 0x11111111;
76
    mStencilFuncFunc = GLES30.GL_NEVER;
77
    mStencilFuncRef  = 0;
78
    mStencilFuncMask = 0x11111111;
79
    mStencilOpSfail  = GLES30.GL_KEEP;
80
    mStencilOpDpfail = GLES30.GL_KEEP;
81
    mStencilOpDppass = GLES30.GL_KEEP;
82

    
83
    mClear = 0;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
// reset state of everything to 'unknown'
88

    
89
  static void reset()
90
    {
91
    sColorMaskR = -1;
92
    sColorMaskG = -1;
93
    sColorMaskB = -1;
94
    sColorMaskA = -1;
95

    
96
    sDepthTest  = -1;
97
    sDepthMask  = -1;
98
    sDepthFunc  = -1;
99

    
100
    sBlend      = -1;
101
    sBlendSrc   = -1;
102
    sBlendDst   = -1;
103

    
104
    sStencilTest     = -1;
105
    sStencilMask     = -1;
106
    sStencilFuncFunc = -1;
107
    sStencilFuncRef  = -1;
108
    sStencilFuncMask = -1;
109
    sStencilOpSfail  = -1;
110
    sStencilOpDpfail = -1;
111
    sStencilOpDppass = -1;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  static void colorDepthOn()
117
    {
118
    if( sColorMaskR!=1 || sColorMaskG!=1 || sColorMaskB!=1 || sColorMaskA!=1 )
119
      {
120
      sColorMaskR = 1;
121
      sColorMaskG = 1;
122
      sColorMaskB = 1;
123
      sColorMaskA = 1;
124
      GLES30.glColorMask(true,true,true,true);
125
      }
126
    if( sDepthMask!=1 )
127
      {
128
      sDepthMask = 1;
129
      GLES30.glDepthMask(true);
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  static void switchOffDrawing()
136
    {
137
    GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
138
    GLES30.glScissor(0,0,0,0);
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  static void restoreDrawing()
144
    {
145
    GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  void apply()
151
    {
152
    // 1. Write to color buffer?
153

    
154
    if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
155
      {
156
      sColorMaskR = mColorMaskR;
157
      sColorMaskG = mColorMaskG;
158
      sColorMaskB = mColorMaskB;
159
      sColorMaskA = mColorMaskA;
160
      GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
161
      }
162

    
163
    // 2. Enable Depth test?
164

    
165
    if( mDepthTest!=sDepthTest )
166
      {
167
      sDepthTest = mDepthTest;
168

    
169
      if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
170
      else
171
        {
172
        GLES30.glEnable (GLES30.GL_DEPTH_TEST);
173

    
174
        if( mDepthFunc!=sDepthFunc )
175
          {
176
          sDepthFunc = mDepthFunc;
177
          GLES30.glDepthFunc(sDepthFunc);
178
          }
179
        }
180
      }
181

    
182
    // 3. Write to Depth buffer?
183

    
184
    if( mDepthMask!=sDepthMask )
185
      {
186
      sDepthMask = mDepthMask;
187
      GLES30.glDepthMask(sDepthMask==1);
188
      }
189

    
190
    // 4. Enable Blending?
191

    
192
    if( mBlend!=sBlend )
193
      {
194
      sBlend = mBlend;
195

    
196
      if( sBlend==0 ) GLES30.glDisable(GLES30.GL_BLEND);
197
      else
198
        {
199
        GLES30.glEnable(GLES30.GL_BLEND);
200

    
201
        if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
202
          {
203
          sBlendSrc = mBlendSrc;
204
          sBlendDst = mBlendDst;
205
          GLES30.glBlendFunc(sBlendSrc,sBlendDst);
206
          }
207
        }
208
      }
209

    
210
    // 5. Enable Stencil Test?
211

    
212
    if( mStencilTest!=sStencilTest )
213
      {
214
      sStencilTest = mStencilTest;
215

    
216
      if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
217
      else
218
        {
219
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
220

    
221
        if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
222
          {
223
          sStencilFuncFunc = mStencilFuncFunc;
224
          sStencilFuncRef  = mStencilFuncRef ;
225
          sStencilFuncMask = mStencilFuncMask;
226
          GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
227
          }
228

    
229
        if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
230
          {
231
          sStencilOpSfail = mStencilOpSfail;
232
          sStencilOpDpfail= mStencilOpDpfail;
233
          sStencilOpDppass= mStencilOpDppass;
234
          GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
235
          }
236
        }
237
      }
238

    
239
    // 6. Write to Stencil buffer?
240

    
241
    if( mStencilMask!=sStencilMask )
242
      {
243
      sStencilMask = mStencilMask;
244
      GLES30.glStencilMask(sStencilMask);
245
      }
246

    
247
    // 7. Clear buffers?
248

    
249
    if( mClear!=0 )
250
      {
251
      GLES30.glClear(mClear);
252
      }
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  void glColorMask(boolean r, boolean g, boolean b, boolean a)
258
    {
259
    mColorMaskR = (r ? 1:0);
260
    mColorMaskG = (g ? 1:0);
261
    mColorMaskB = (b ? 1:0);
262
    mColorMaskA = (a ? 1:0);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  void glDepthMask(boolean mask)
268
    {
269
    mDepthMask = (mask ? 1:0);
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  void glStencilMask(int mask)
275
    {
276
    mStencilMask = mask;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  void glEnable(int test)
282
    {
283
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 1;
284
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
285
    else if( test==GLES30.GL_BLEND        ) mBlend       = 1;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  void glDisable(int test)
291
    {
292
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 0;
293
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
294
    else if( test==GLES30.GL_BLEND        ) mBlend       = 0;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  void glStencilFunc(int func, int ref, int mask)
300
    {
301
    mStencilFuncFunc = func;
302
    mStencilFuncRef  = ref;
303
    mStencilFuncMask = mask;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  void glStencilOp(int sfail, int dpfail, int dppass)
309
    {
310
    mStencilOpSfail = sfail;
311
    mStencilOpDpfail= dpfail;
312
    mStencilOpDppass= dppass;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  void glDepthFunc(int func)
318
    {
319
    mDepthFunc = func;
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  void glBlendFunc(int src, int dst)
325
    {
326
    mBlendSrc = src;
327
    mBlendDst = dst;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  void glClear(int mask)
333
    {
334
    mClear = mask;
335
    }
336
}
(10-10/26)