Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedRenderState.java @ 580f7d10

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
  void apply()
130
    {
131
    // 1. Write to color buffer?
132

    
133
    if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
134
      {
135
      sColorMaskR = mColorMaskR;
136
      sColorMaskG = mColorMaskG;
137
      sColorMaskB = mColorMaskB;
138
      sColorMaskA = mColorMaskA;
139
      GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
140
      }
141

    
142
    // 2. Enable Depth test?
143

    
144
    if( mDepthTest!=sDepthTest )
145
      {
146
      sDepthTest = mDepthTest;
147

    
148
      if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
149
      else
150
        {
151
        GLES30.glEnable (GLES30.GL_DEPTH_TEST);
152

    
153
        if( mDepthFunc!=sDepthFunc )
154
          {
155
          sDepthFunc = mDepthFunc;
156
          GLES30.glDepthFunc(sDepthFunc);
157
          }
158
        }
159
      }
160

    
161
    // 3. Write to Depth buffer?
162

    
163
    if( mDepthMask!=sDepthMask )
164
      {
165
      sDepthMask = mDepthMask;
166
      GLES30.glDepthMask(sDepthMask==1);
167

    
168
      android.util.Log.e("STATE","switching depth mask to "+(sDepthMask==1));
169
      }
170

    
171
    // 4. Enable Blending?
172

    
173
    if( mBlend!=sBlend )
174
      {
175
      sBlend = mBlend;
176

    
177
      if( sBlend==0 ) GLES30.glDisable(GLES30.GL_BLEND);
178
      else
179
        {
180
        GLES30.glEnable(GLES30.GL_BLEND);
181

    
182
        if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
183
          {
184
          sBlendSrc = mBlendSrc;
185
          sBlendDst = mBlendDst;
186
          GLES30.glBlendFunc(sBlendSrc,sBlendDst);
187
          }
188
        }
189
      }
190

    
191
    // 5. Enable Stencil Test?
192

    
193
    if( mStencilTest!=sStencilTest )
194
      {
195
      sStencilTest = mStencilTest;
196

    
197
      if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
198
      else
199
        {
200
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
201

    
202
        if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
203
          {
204
          sStencilFuncFunc = mStencilFuncFunc;
205
          sStencilFuncRef  = mStencilFuncRef ;
206
          sStencilFuncMask = mStencilFuncMask;
207
          GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
208
          }
209

    
210
        if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
211
          {
212
          sStencilOpSfail = mStencilOpSfail;
213
          sStencilOpDpfail= mStencilOpDpfail;
214
          sStencilOpDppass= mStencilOpDppass;
215
          GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
216
          }
217
        }
218
      }
219

    
220
    // 6. Write to Stencil buffer?
221

    
222
    if( mStencilMask!=sStencilMask )
223
      {
224
      sStencilMask = mStencilMask;
225
      GLES30.glStencilMask(sStencilMask);
226
      }
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  void glColorMask(boolean r, boolean g, boolean b, boolean a)
232
    {
233
    mColorMaskR = (r ? 1:0);
234
    mColorMaskG = (g ? 1:0);
235
    mColorMaskB = (b ? 1:0);
236
    mColorMaskA = (a ? 1:0);
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  void glDepthMask(boolean mask)
242
    {
243
    mDepthMask = (mask ? 1:0);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  void glStencilMask(int mask)
249
    {
250
    mStencilMask = mask;
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  void glEnable(int test)
256
    {
257
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 1;
258
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
259
    else if( test==GLES30.GL_BLEND        ) mBlend       = 1;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  void glDisable(int test)
265
    {
266
         if( test==GLES30.GL_DEPTH_TEST   ) mDepthTest   = 0;
267
    else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
268
    else if( test==GLES30.GL_BLEND        ) mBlend       = 0;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  void glStencilFunc(int func, int ref, int mask)
274
    {
275
    mStencilFuncFunc = func;
276
    mStencilFuncRef  = ref;
277
    mStencilFuncMask = mask;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  void glStencilOp(int sfail, int dpfail, int dppass)
283
    {
284
    mStencilOpSfail = sfail;
285
    mStencilOpDpfail= dpfail;
286
    mStencilOpDppass= dppass;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  void glDepthFunc(int func)
292
    {
293
    mDepthFunc = func;
294
    }
295

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

    
298
  void glBlendFunc(int src, int dst)
299
    {
300
    mBlendSrc = src;
301
    mBlendDst = dst;
302
    }
303

    
304
}
(9-9/23)