Project

General

Profile

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

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

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
  private static int sBlend;
39
  private static int sBlendSrc, sBlendDst;
40

    
41
  private int mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA;
42
  private int mDepthMask;
43
  private int mStencilMask;
44
  private int mDepthTest;
45
  private int mStencilTest;
46
  private int mStencilFuncFunc, mStencilFuncRef, mStencilFuncMask;
47
  private int mStencilOpSfail, mStencilOpDpfail, mStencilOpDppass;
48
  private int mDepthFunc;
49
  private int mBlend;
50
  private int mBlendSrc, mBlendDst;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
// default: color writes on, depth test and writes on, blending on, stencil off.
54

    
55
  DistortedRenderState()
56
    {
57
    mColorMaskR = 1;
58
    mColorMaskG = 1;
59
    mColorMaskB = 1;
60
    mColorMaskA = 1;
61

    
62
    mDepthTest  = 1;
63
    mDepthMask  = 1;
64
    mDepthFunc  = GLES30.GL_LEQUAL;
65

    
66
    mBlend      = 1;
67
    mBlendSrc   = GLES30.GL_SRC_ALPHA;
68
    mBlendDst   = GLES30.GL_ONE_MINUS_SRC_ALPHA;
69

    
70
    mStencilTest     = 0;
71
    mStencilMask     = 0x11111111;
72
    mStencilFuncFunc = GLES30.GL_NEVER;
73
    mStencilFuncRef  = 0;
74
    mStencilFuncMask = 0x11111111;
75
    mStencilOpSfail  = GLES30.GL_KEEP;
76
    mStencilOpDpfail = GLES30.GL_KEEP;
77
    mStencilOpDppass = GLES30.GL_KEEP;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// reset state of everything to 'unknown'
82

    
83
  static void reset()
84
    {
85
    sColorMaskR = -1;
86
    sColorMaskG = -1;
87
    sColorMaskB = -1;
88
    sColorMaskA = -1;
89

    
90
    sDepthTest  = -1;
91
    sDepthMask  = -1;
92
    sDepthFunc  = -1;
93

    
94
    sBlend      = -1;
95
    sBlendSrc   = -1;
96
    sBlendDst   = -1;
97

    
98
    sStencilTest     = -1;
99
    sStencilMask     = -1;
100
    sStencilFuncFunc = -1;
101
    sStencilFuncRef  = -1;
102
    sStencilFuncMask = -1;
103
    sStencilOpSfail  = -1;
104
    sStencilOpDpfail = -1;
105
    sStencilOpDppass = -1;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  static void colorDepthOn()
111
    {
112
    if( sColorMaskR!=1 || sColorMaskG!=1 || sColorMaskB!=1 || sColorMaskA!=1 )
113
      {
114
      sColorMaskR = 1;
115
      sColorMaskG = 1;
116
      sColorMaskB = 1;
117
      sColorMaskA = 1;
118
      GLES30.glColorMask(true,true,true,true);
119
      }
120
    if( sDepthMask!=1 )
121
      {
122
      sDepthMask = 1;
123
      GLES30.glDepthMask(true);
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  static void switchOffDrawing()
130
    {
131
    GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
132
    GLES30.glScissor(0,0,0,0);
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  static void restoreDrawing()
138
    {
139
    GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  void apply()
145
    {
146
    // 1. Write to color buffer?
147

    
148
    if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
149
      {
150
      sColorMaskR = mColorMaskR;
151
      sColorMaskG = mColorMaskG;
152
      sColorMaskB = mColorMaskB;
153
      sColorMaskA = mColorMaskA;
154
      GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
155
      }
156

    
157
    // 2. Enable Depth test?
158

    
159
    if( mDepthTest!=sDepthTest )
160
      {
161
      sDepthTest = mDepthTest;
162

    
163
      if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
164
      else
165
        {
166
        GLES30.glEnable (GLES30.GL_DEPTH_TEST);
167

    
168
        if( mDepthFunc!=sDepthFunc )
169
          {
170
          sDepthFunc = mDepthFunc;
171
          GLES30.glDepthFunc(sDepthFunc);
172
          }
173
        }
174
      }
175

    
176
    // 3. Write to Depth buffer?
177

    
178
    if( mDepthMask!=sDepthMask )
179
      {
180
      sDepthMask = mDepthMask;
181
      GLES30.glDepthMask(sDepthMask==1);
182
      }
183

    
184
    // 4. Enable Blending?
185

    
186
    if( mBlend!=sBlend )
187
      {
188
      sBlend = mBlend;
189

    
190
      if( sBlend==0 ) GLES30.glDisable(GLES30.GL_BLEND);
191
      else
192
        {
193
        GLES30.glEnable(GLES30.GL_BLEND);
194

    
195
        if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
196
          {
197
          sBlendSrc = mBlendSrc;
198
          sBlendDst = mBlendDst;
199
          GLES30.glBlendFunc(sBlendSrc,sBlendDst);
200
          }
201
        }
202
      }
203

    
204
    // 5. Enable Stencil Test?
205

    
206
    if( mStencilTest!=sStencilTest )
207
      {
208
      sStencilTest = mStencilTest;
209

    
210
      if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
211
      else
212
        {
213
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
214

    
215
        if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
216
          {
217
          sStencilFuncFunc = mStencilFuncFunc;
218
          sStencilFuncRef  = mStencilFuncRef ;
219
          sStencilFuncMask = mStencilFuncMask;
220
          GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
221
          }
222

    
223
        if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
224
          {
225
          sStencilOpSfail = mStencilOpSfail;
226
          sStencilOpDpfail= mStencilOpDpfail;
227
          sStencilOpDppass= mStencilOpDppass;
228
          GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
229
          }
230
        }
231
      }
232

    
233
    // 6. Write to Stencil buffer?
234

    
235
    if( mStencilMask!=sStencilMask )
236
      {
237
      sStencilMask = mStencilMask;
238
      GLES30.glStencilMask(sStencilMask);
239
      }
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  void glColorMask(boolean r, boolean g, boolean b, boolean a)
245
    {
246
    mColorMaskR = (r ? 1:0);
247
    mColorMaskG = (g ? 1:0);
248
    mColorMaskB = (b ? 1:0);
249
    mColorMaskA = (a ? 1:0);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  void glDepthMask(boolean mask)
255
    {
256
    mDepthMask = (mask ? 1:0);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  void glStencilMask(int mask)
262
    {
263
    mStencilMask = mask;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  void glEnable(int test)
269
    {
270
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 1;
271
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
272
    else if( test==GLES30.GL_BLEND        ) mBlend       = 1;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  void glDisable(int test)
278
    {
279
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 0;
280
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
281
    else if( test==GLES30.GL_BLEND        ) mBlend       = 0;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  void glStencilFunc(int func, int ref, int mask)
287
    {
288
    mStencilFuncFunc = func;
289
    mStencilFuncRef  = ref;
290
    mStencilFuncMask = mask;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  void glStencilOp(int sfail, int dpfail, int dppass)
296
    {
297
    mStencilOpSfail = sfail;
298
    mStencilOpDpfail= dpfail;
299
    mStencilOpDppass= dppass;
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  void glDepthFunc(int func)
305
    {
306
    mDepthFunc = func;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  void glBlendFunc(int src, int dst)
312
    {
313
    mBlendSrc = src;
314
    mBlendDst = dst;
315
    }
316

    
317
}
(10-10/26)