Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedRenderState.java @ 23eecbd9

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
  // TODO: figure this out dynamically; this assumes 8 bit stencil buffer.
34
  private static final int STENCIL_MASK = (1<<8)-1;
35

    
36
  private static int sColorMaskR, sColorMaskG, sColorMaskB, sColorMaskA;   //
37
  private static int sDepthMask;                                           //
38
  private static int sStencilMask;                                         //
39
  private static int sDepthTest;                                           //
40
  private static int sStencilTest;                                         //
41
  private static int sStencilFuncFunc, sStencilFuncRef, sStencilFuncMask;  // current OpenGL state
42
  private static int sStencilOpSfail, sStencilOpDpfail, sStencilOpDppass;  //
43
  private static int sDepthFunc;                                           //
44
  private static int sBlend;                                               //
45
  private static int sBlendSrc, sBlendDst;                                 //
46

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// default: color writes on, depth test and writes on, blending on, stencil off.
61

    
62
  DistortedRenderState()
63
    {
64
    mColorMaskR = 1;
65
    mColorMaskG = 1;
66
    mColorMaskB = 1;
67
    mColorMaskA = 1;
68

    
69
    mDepthTest  = 1;
70
    mDepthMask  = 1;
71
    mDepthFunc  = GLES30.GL_LEQUAL;
72

    
73
    mBlend      = 1;
74
    mBlendSrc   = GLES30.GL_SRC_ALPHA;
75
    mBlendDst   = GLES30.GL_ONE_MINUS_SRC_ALPHA;
76

    
77
    mStencilTest     = 0;
78
    mStencilMask     = STENCIL_MASK;
79
    mStencilFuncFunc = GLES30.GL_NEVER;
80
    mStencilFuncRef  = 0;
81
    mStencilFuncMask = STENCIL_MASK;
82
    mStencilOpSfail  = GLES30.GL_KEEP;
83
    mStencilOpDpfail = GLES30.GL_KEEP;
84
    mStencilOpDppass = GLES30.GL_KEEP;
85

    
86
    mClear = 0;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// reset state of everything to 'unknown'
91

    
92
  static void reset()
93
    {
94
    sColorMaskR = -1;
95
    sColorMaskG = -1;
96
    sColorMaskB = -1;
97
    sColorMaskA = -1;
98

    
99
    sDepthTest  = -1;
100
    sDepthMask  = -1;
101
    sDepthFunc  = -1;
102

    
103
    sBlend      = -1;
104
    sBlendSrc   = -1;
105
    sBlendDst   = -1;
106

    
107
    sStencilTest     = -1;
108
    sStencilMask     = -1;
109
    sStencilFuncFunc = -1;
110
    sStencilFuncRef  = -1;
111
    sStencilFuncMask = -1;
112
    sStencilOpSfail  = -1;
113
    sStencilOpDpfail = -1;
114
    sStencilOpDppass = -1;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  static void colorDepthStencilOn()
120
    {
121
    if( sColorMaskR!=1 || sColorMaskG!=1 || sColorMaskB!=1 || sColorMaskA!=1 )
122
      {
123
      sColorMaskR = 1;
124
      sColorMaskG = 1;
125
      sColorMaskB = 1;
126
      sColorMaskA = 1;
127
      GLES30.glColorMask(true,true,true,true);
128
      }
129
    if( sDepthMask!=1 )
130
      {
131
      sDepthMask = 1;
132
      GLES30.glDepthMask(true);
133
      }
134
    if( sStencilMask!= STENCIL_MASK )
135
      {
136
      sStencilMask = STENCIL_MASK;
137
      GLES30.glStencilMask(sStencilMask);
138
      }
139
    }
140

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

    
143
  static void switchOffDrawing()
144
    {
145
    GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
146
    GLES30.glScissor(0,0,0,0);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  static void restoreDrawing()
152
    {
153
    GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  void apply()
159
    {
160
    //android.util.Log.e("State", "APPLYING STATE");
161

    
162
    /////////////////////////////////////////////////////
163
    // 1. Write to color buffer?
164
    if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
165
      {
166
      //android.util.Log.d("State", "setting color mask");
167
      sColorMaskR = mColorMaskR;
168
      sColorMaskG = mColorMaskG;
169
      sColorMaskB = mColorMaskB;
170
      sColorMaskA = mColorMaskA;
171
      GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
172
      }
173

    
174
    /////////////////////////////////////////////////////
175
    // 2. Enable Depth test?
176
    if( mDepthTest!=sDepthTest )
177
      {
178
      sDepthTest = mDepthTest;
179

    
180
      if (sDepthTest == 0)
181
        {
182
        //android.util.Log.d("State", "disabling depth test");
183
        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
184
        }
185
      else
186
        {
187
        //android.util.Log.d("State", "enable depth test");
188
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
189
        }
190
      }
191

    
192
    /////////////////////////////////////////////////////
193
    // 3. Change Depth Function?
194
    if( mDepthFunc!=sDepthFunc )
195
      {
196
      //android.util.Log.d("State", "setting depth func");
197
      sDepthFunc = mDepthFunc;
198
      GLES30.glDepthFunc(sDepthFunc);
199
      }
200

    
201
    /////////////////////////////////////////////////////
202
    // 4. Write to Depth buffer?
203
    if( mDepthMask!=sDepthMask )
204
      {
205
      //android.util.Log.d("State", "setting depth mask");
206
      sDepthMask = mDepthMask;
207
      GLES30.glDepthMask(sDepthMask==1);
208
      }
209

    
210
    /////////////////////////////////////////////////////
211
    // 5. Enable Blending?
212
    if( mBlend!=sBlend )
213
      {
214
      sBlend = mBlend;
215

    
216
      if (sBlend == 0)
217
        {
218
        //android.util.Log.d("State", "disabling blending");
219
        GLES30.glDisable(GLES30.GL_BLEND);
220
        }
221
      else
222
        {
223
        //android.util.Log.d("State", "enabling blending");
224
        GLES30.glEnable(GLES30.GL_BLEND);
225
        }
226
      }
227

    
228
    /////////////////////////////////////////////////////
229
    // 6. Change Blend function?
230
    if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
231
      {
232
      //android.util.Log.d("State", "setting blend function");
233
      sBlendSrc = mBlendSrc;
234
      sBlendDst = mBlendDst;
235
      GLES30.glBlendFunc(sBlendSrc,sBlendDst);
236
      }
237

    
238
    /////////////////////////////////////////////////////
239
    // 7. Enable/Disable Stencil Test?
240
    if( mStencilTest!=sStencilTest )
241
      {
242
      sStencilTest = mStencilTest;
243

    
244
      if (sStencilTest == 0)
245
        {
246
        //android.util.Log.d("State", "disabling stencil test");
247
        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
248
        }
249
      else
250
        {
251
        //android.util.Log.d("State", "enabling stencil test");
252
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
253
        }
254
      }
255

    
256
    /////////////////////////////////////////////////////
257
    // 8. Adjust Stencil function?
258
    if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
259
      {
260
      //android.util.Log.d("State", "setting stencil function");
261
      sStencilFuncFunc = mStencilFuncFunc;
262
      sStencilFuncRef  = mStencilFuncRef ;
263
      sStencilFuncMask = mStencilFuncMask;
264
      GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
265
      }
266

    
267
    /////////////////////////////////////////////////////
268
    // 9. Adjust Stencil operation?
269
    if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
270
      {
271
      //android.util.Log.d("State", "setting stencil op");
272
      sStencilOpSfail = mStencilOpSfail;
273
      sStencilOpDpfail= mStencilOpDpfail;
274
      sStencilOpDppass= mStencilOpDppass;
275
      GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
276
      }
277

    
278
    /////////////////////////////////////////////////////
279
    // 10. Write to Stencil buffer?
280
    if( mStencilMask!=sStencilMask )
281
      {
282
      //android.util.Log.d("State", "setting stencil mask");
283
      sStencilMask = mStencilMask;
284
      GLES30.glStencilMask(sStencilMask);
285
      }
286

    
287
    /////////////////////////////////////////////////////
288
    // 11. Clear buffers?
289
    if( mClear!=0 )
290
      {
291
      //android.util.Log.d("State", "clearing buffer");
292
      GLES30.glClear(mClear);
293
      }
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  void glColorMask(boolean r, boolean g, boolean b, boolean a)
299
    {
300
    mColorMaskR = (r ? 1:0);
301
    mColorMaskG = (g ? 1:0);
302
    mColorMaskB = (b ? 1:0);
303
    mColorMaskA = (a ? 1:0);
304
    }
305

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

    
308
  void glDepthMask(boolean mask)
309
    {
310
    mDepthMask = (mask ? 1:0);
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  void glStencilMask(int mask)
316
    {
317
    mStencilMask = mask;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  void glEnable(int test)
323
    {
324
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 1;
325
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
326
    else if( test==GLES30.GL_BLEND        ) mBlend       = 1;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  void glDisable(int test)
332
    {
333
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 0;
334
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
335
    else if( test==GLES30.GL_BLEND        ) mBlend       = 0;
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  void glStencilFunc(int func, int ref, int mask)
341
    {
342
    mStencilFuncFunc = func;
343
    mStencilFuncRef  = ref;
344
    mStencilFuncMask = mask;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
  void glStencilOp(int sfail, int dpfail, int dppass)
350
    {
351
    mStencilOpSfail = sfail;
352
    mStencilOpDpfail= dpfail;
353
    mStencilOpDppass= dppass;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  void glDepthFunc(int func)
359
    {
360
    mDepthFunc = func;
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  void glBlendFunc(int src, int dst)
366
    {
367
    mBlendSrc = src;
368
    mBlendDst = dst;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  void glClear(int mask)
374
    {
375
    mClear = mask;
376
    }
377
}
(10-10/26)