Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedRenderState.java @ 02ab6f9d

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

    
169
    // 4. Enable Blending?
170

    
171
    if( mBlend!=sBlend )
172
      {
173
      sBlend = mBlend;
174

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

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

    
189
    // 5. Enable Stencil Test?
190

    
191
    if( mStencilTest!=sStencilTest )
192
      {
193
      sStencilTest = mStencilTest;
194

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

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

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

    
218
    // 6. Write to Stencil buffer?
219

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  void glStencilMask(int mask)
247
    {
248
    mStencilMask = mask;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  void glDepthFunc(int func)
290
    {
291
    mDepthFunc = func;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

    
302
}
(9-9/23)