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
|
//android.util.Log.e("State", "APPLYING STATE");
|
153
|
|
154
|
/////////////////////////////////////////////////////
|
155
|
// 1. Write to color buffer?
|
156
|
if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
|
157
|
{
|
158
|
//android.util.Log.d("State", "setting color mask");
|
159
|
sColorMaskR = mColorMaskR;
|
160
|
sColorMaskG = mColorMaskG;
|
161
|
sColorMaskB = mColorMaskB;
|
162
|
sColorMaskA = mColorMaskA;
|
163
|
GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
|
164
|
}
|
165
|
|
166
|
/////////////////////////////////////////////////////
|
167
|
// 2. Enable Depth test?
|
168
|
if( mDepthTest!=sDepthTest )
|
169
|
{
|
170
|
sDepthTest = mDepthTest;
|
171
|
|
172
|
if (sDepthTest == 0)
|
173
|
{
|
174
|
//android.util.Log.d("State", "disabling depth test");
|
175
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
176
|
}
|
177
|
else
|
178
|
{
|
179
|
//android.util.Log.d("State", "enable depth test");
|
180
|
GLES30.glEnable(GLES30.GL_DEPTH_TEST);
|
181
|
}
|
182
|
}
|
183
|
|
184
|
/////////////////////////////////////////////////////
|
185
|
// 3. Change Depth Function?
|
186
|
if( mDepthFunc!=sDepthFunc )
|
187
|
{
|
188
|
//android.util.Log.d("State", "setting depth func");
|
189
|
sDepthFunc = mDepthFunc;
|
190
|
GLES30.glDepthFunc(sDepthFunc);
|
191
|
}
|
192
|
|
193
|
/////////////////////////////////////////////////////
|
194
|
// 4. Write to Depth buffer?
|
195
|
if( mDepthMask!=sDepthMask )
|
196
|
{
|
197
|
//android.util.Log.d("State", "setting depth mask");
|
198
|
sDepthMask = mDepthMask;
|
199
|
GLES30.glDepthMask(sDepthMask==1);
|
200
|
}
|
201
|
|
202
|
/////////////////////////////////////////////////////
|
203
|
// 5. Enable Blending?
|
204
|
if( mBlend!=sBlend )
|
205
|
{
|
206
|
sBlend = mBlend;
|
207
|
|
208
|
if (sBlend == 0)
|
209
|
{
|
210
|
//android.util.Log.d("State", "disabling blending");
|
211
|
GLES30.glDisable(GLES30.GL_BLEND);
|
212
|
}
|
213
|
else
|
214
|
{
|
215
|
//android.util.Log.d("State", "enabling blending");
|
216
|
GLES30.glEnable(GLES30.GL_BLEND);
|
217
|
}
|
218
|
}
|
219
|
|
220
|
/////////////////////////////////////////////////////
|
221
|
// 6. Change Blend function?
|
222
|
if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
|
223
|
{
|
224
|
//android.util.Log.d("State", "setting blend function");
|
225
|
sBlendSrc = mBlendSrc;
|
226
|
sBlendDst = mBlendDst;
|
227
|
GLES30.glBlendFunc(sBlendSrc,sBlendDst);
|
228
|
}
|
229
|
|
230
|
/////////////////////////////////////////////////////
|
231
|
// 7. Enable/Disable Stencil Test?
|
232
|
if( mStencilTest!=sStencilTest )
|
233
|
{
|
234
|
sStencilTest = mStencilTest;
|
235
|
|
236
|
if (sStencilTest == 0)
|
237
|
{
|
238
|
//android.util.Log.d("State", "disabling stencil test");
|
239
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
240
|
}
|
241
|
else
|
242
|
{
|
243
|
//android.util.Log.d("State", "enabling stencil test");
|
244
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/////////////////////////////////////////////////////
|
249
|
// 8. Adjust Stencil function?
|
250
|
if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
|
251
|
{
|
252
|
//android.util.Log.d("State", "setting stencil function");
|
253
|
sStencilFuncFunc = mStencilFuncFunc;
|
254
|
sStencilFuncRef = mStencilFuncRef ;
|
255
|
sStencilFuncMask = mStencilFuncMask;
|
256
|
GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
|
257
|
}
|
258
|
|
259
|
/////////////////////////////////////////////////////
|
260
|
// 9. Adjust Stencil operation?
|
261
|
if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
|
262
|
{
|
263
|
//android.util.Log.d("State", "setting stencil op");
|
264
|
sStencilOpSfail = mStencilOpSfail;
|
265
|
sStencilOpDpfail= mStencilOpDpfail;
|
266
|
sStencilOpDppass= mStencilOpDppass;
|
267
|
GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
|
268
|
}
|
269
|
|
270
|
/////////////////////////////////////////////////////
|
271
|
// 10. Write to Stencil buffer?
|
272
|
if( mStencilMask!=sStencilMask )
|
273
|
{
|
274
|
//android.util.Log.d("State", "setting stencil mask");
|
275
|
sStencilMask = mStencilMask;
|
276
|
GLES30.glStencilMask(sStencilMask);
|
277
|
}
|
278
|
|
279
|
/////////////////////////////////////////////////////
|
280
|
// 11. Clear buffers?
|
281
|
if( mClear!=0 )
|
282
|
{
|
283
|
//android.util.Log.d("State", "clearing buffer");
|
284
|
GLES30.glClear(mClear);
|
285
|
}
|
286
|
}
|
287
|
|
288
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
289
|
|
290
|
void glColorMask(boolean r, boolean g, boolean b, boolean a)
|
291
|
{
|
292
|
mColorMaskR = (r ? 1:0);
|
293
|
mColorMaskG = (g ? 1:0);
|
294
|
mColorMaskB = (b ? 1:0);
|
295
|
mColorMaskA = (a ? 1:0);
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
void glDepthMask(boolean mask)
|
301
|
{
|
302
|
mDepthMask = (mask ? 1:0);
|
303
|
}
|
304
|
|
305
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306
|
|
307
|
void glStencilMask(int mask)
|
308
|
{
|
309
|
mStencilMask = mask;
|
310
|
}
|
311
|
|
312
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
313
|
|
314
|
void glEnable(int test)
|
315
|
{
|
316
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 1;
|
317
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 1;
|
318
|
else if( test==GLES30.GL_BLEND ) mBlend = 1;
|
319
|
}
|
320
|
|
321
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
322
|
|
323
|
void glDisable(int test)
|
324
|
{
|
325
|
if( test==GLES30.GL_DEPTH_TEST ) mDepthTest = 0;
|
326
|
else if( test==GLES30.GL_STENCIL_TEST ) mStencilTest = 0;
|
327
|
else if( test==GLES30.GL_BLEND ) mBlend = 0;
|
328
|
}
|
329
|
|
330
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
331
|
|
332
|
void glStencilFunc(int func, int ref, int mask)
|
333
|
{
|
334
|
mStencilFuncFunc = func;
|
335
|
mStencilFuncRef = ref;
|
336
|
mStencilFuncMask = mask;
|
337
|
}
|
338
|
|
339
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
340
|
|
341
|
void glStencilOp(int sfail, int dpfail, int dppass)
|
342
|
{
|
343
|
mStencilOpSfail = sfail;
|
344
|
mStencilOpDpfail= dpfail;
|
345
|
mStencilOpDppass= dppass;
|
346
|
}
|
347
|
|
348
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
349
|
|
350
|
void glDepthFunc(int func)
|
351
|
{
|
352
|
mDepthFunc = func;
|
353
|
}
|
354
|
|
355
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
356
|
|
357
|
void glBlendFunc(int src, int dst)
|
358
|
{
|
359
|
mBlendSrc = src;
|
360
|
mBlendDst = dst;
|
361
|
}
|
362
|
|
363
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
364
|
|
365
|
void glClear(int mask)
|
366
|
{
|
367
|
mClear = mask;
|
368
|
}
|
369
|
}
|