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 class RenderState
|
37
|
{
|
38
|
private int colorMaskR, colorMaskG, colorMaskB, colorMaskA;
|
39
|
private int depthMask;
|
40
|
private int stencilMask;
|
41
|
private int depthTest;
|
42
|
private int stencilTest;
|
43
|
private int stencilFuncFunc, stencilFuncRef, stencilFuncMask;
|
44
|
private int stencilOpSfail, stencilOpDpfail, stencilOpDppass;
|
45
|
private int depthFunc;
|
46
|
private int blend;
|
47
|
private int blendSrc, blendDst;
|
48
|
}
|
49
|
|
50
|
private RenderState mState; // state the current object wants to have
|
51
|
static private RenderState cState = new RenderState(); // current OpenGL Stave
|
52
|
static private RenderState sState = new RenderState(); // saved OpenGL state
|
53
|
|
54
|
private int mClear;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
// default: color writes on, depth test and writes on, blending on, stencil off.
|
58
|
|
59
|
DistortedRenderState()
|
60
|
{
|
61
|
mState = new RenderState();
|
62
|
|
63
|
mState.colorMaskR = 1;
|
64
|
mState.colorMaskG = 1;
|
65
|
mState.colorMaskB = 1;
|
66
|
mState.colorMaskA = 1;
|
67
|
|
68
|
mState.depthTest = 1;
|
69
|
mState.depthMask = 1;
|
70
|
mState.depthFunc = GLES30.GL_LEQUAL;
|
71
|
|
72
|
mState.blend = 1;
|
73
|
mState.blendSrc = GLES30.GL_SRC_ALPHA;
|
74
|
mState.blendDst = GLES30.GL_ONE_MINUS_SRC_ALPHA;
|
75
|
|
76
|
mState.stencilTest = 0;
|
77
|
mState.stencilMask = STENCIL_MASK;
|
78
|
mState.stencilFuncFunc = GLES30.GL_NEVER;
|
79
|
mState.stencilFuncRef = 0;
|
80
|
mState.stencilFuncMask = STENCIL_MASK;
|
81
|
mState.stencilOpSfail = GLES30.GL_KEEP;
|
82
|
mState.stencilOpDpfail = GLES30.GL_KEEP;
|
83
|
mState.stencilOpDppass = GLES30.GL_KEEP;
|
84
|
|
85
|
mClear = 0;
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
// reset state of everything to 'unknown'
|
90
|
|
91
|
static void reset()
|
92
|
{
|
93
|
cState.colorMaskR = -1;
|
94
|
cState.colorMaskG = -1;
|
95
|
cState.colorMaskB = -1;
|
96
|
cState.colorMaskA = -1;
|
97
|
|
98
|
cState.depthTest = -1;
|
99
|
cState.depthMask = -1;
|
100
|
cState.depthFunc = -1;
|
101
|
|
102
|
cState.blend = -1;
|
103
|
cState.blendSrc = -1;
|
104
|
cState.blendDst = -1;
|
105
|
|
106
|
cState.stencilTest = -1;
|
107
|
cState.stencilMask = -1;
|
108
|
cState.stencilFuncFunc = -1;
|
109
|
cState.stencilFuncRef = -1;
|
110
|
cState.stencilFuncMask = -1;
|
111
|
cState.stencilOpSfail = -1;
|
112
|
cState.stencilOpDpfail = -1;
|
113
|
cState.stencilOpDppass = -1;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
static void colorDepthStencilOn()
|
119
|
{
|
120
|
if( cState.colorMaskR!=1 || cState.colorMaskG!=1 || cState.colorMaskB!=1 || cState.colorMaskA!=1 )
|
121
|
{
|
122
|
cState.colorMaskR = 1;
|
123
|
cState.colorMaskG = 1;
|
124
|
cState.colorMaskB = 1;
|
125
|
cState.colorMaskA = 1;
|
126
|
GLES30.glColorMask(true,true,true,true);
|
127
|
}
|
128
|
if( cState.depthMask!=1 )
|
129
|
{
|
130
|
cState.depthMask = 1;
|
131
|
GLES30.glDepthMask(true);
|
132
|
}
|
133
|
if( cState.stencilMask!= STENCIL_MASK )
|
134
|
{
|
135
|
cState.stencilMask = STENCIL_MASK;
|
136
|
GLES30.glStencilMask(cState.stencilMask);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
static void switchOffDrawing()
|
143
|
{
|
144
|
GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
|
145
|
GLES30.glScissor(0,0,0,0);
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
|
150
|
static void restoreDrawing()
|
151
|
{
|
152
|
GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
|
157
|
static void setUpStencilMark()
|
158
|
{
|
159
|
if( cState.stencilTest!=1 )
|
160
|
{
|
161
|
cState.stencilTest = 1;
|
162
|
//android.util.Log.d("State", "stencil test on");
|
163
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
164
|
}
|
165
|
if( cState.stencilFuncFunc!=GLES30.GL_ALWAYS || cState.stencilFuncRef!=1 || cState.stencilFuncMask!=STENCIL_MASK )
|
166
|
{
|
167
|
cState.stencilFuncFunc = GLES30.GL_ALWAYS;
|
168
|
cState.stencilFuncRef = 1;
|
169
|
cState.stencilFuncMask = STENCIL_MASK;
|
170
|
//android.util.Log.d("State", "stencil func on");
|
171
|
GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
|
172
|
}
|
173
|
if( cState.stencilOpSfail!=GLES30.GL_KEEP || cState.stencilOpDpfail!=GLES30.GL_KEEP || cState.stencilOpDppass!=GLES30.GL_REPLACE )
|
174
|
{
|
175
|
cState.stencilOpSfail = GLES30.GL_KEEP;
|
176
|
cState.stencilOpDpfail= GLES30.GL_KEEP;
|
177
|
cState.stencilOpDppass= GLES30.GL_REPLACE;
|
178
|
//android.util.Log.d("State", "stencil op on");
|
179
|
GLES30.glStencilOp(cState.stencilOpSfail,cState.stencilOpDpfail,cState.stencilOpDppass);
|
180
|
}
|
181
|
if( cState.colorMaskR!=0 || cState.colorMaskG!=0 || cState.colorMaskB!=0 || cState.colorMaskA!=0 )
|
182
|
{
|
183
|
cState.colorMaskR = 0;
|
184
|
cState.colorMaskG = 0;
|
185
|
cState.colorMaskB = 0;
|
186
|
cState.colorMaskA = 0;
|
187
|
//android.util.Log.d("State", "switch off color writing");
|
188
|
GLES30.glColorMask(false,false,false,false);
|
189
|
}
|
190
|
if( cState.depthMask!=0 )
|
191
|
{
|
192
|
cState.depthMask = 0;
|
193
|
//android.util.Log.d("State", "switch off depth writing");
|
194
|
GLES30.glDepthMask(false);
|
195
|
}
|
196
|
if( cState.stencilMask!= STENCIL_MASK )
|
197
|
{
|
198
|
cState.stencilMask = STENCIL_MASK;
|
199
|
//android.util.Log.d("State", "stencil mask on");
|
200
|
GLES30.glStencilMask(cState.stencilMask);
|
201
|
}
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
static void useStencilMark()
|
207
|
{
|
208
|
if( cState.stencilTest!=1 )
|
209
|
{
|
210
|
sState.stencilTest = cState.stencilTest;
|
211
|
cState.stencilTest = 1;
|
212
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
213
|
}
|
214
|
if( cState.stencilFuncFunc!=GLES30.GL_EQUAL || cState.stencilFuncRef!=1 || cState.stencilFuncMask!=STENCIL_MASK )
|
215
|
{
|
216
|
sState.stencilFuncFunc = cState.stencilFuncFunc;
|
217
|
sState.stencilFuncRef = cState.stencilFuncRef;
|
218
|
sState.stencilFuncMask = cState.stencilFuncMask;
|
219
|
cState.stencilFuncFunc = GLES30.GL_EQUAL;
|
220
|
cState.stencilFuncRef = 1;
|
221
|
cState.stencilFuncMask = STENCIL_MASK;
|
222
|
GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
|
223
|
}
|
224
|
|
225
|
// TODO: Debatable
|
226
|
if( cState.stencilMask!= 0x00 )
|
227
|
{
|
228
|
sState.stencilMask = cState.stencilMask;
|
229
|
cState.stencilMask = 0x00;
|
230
|
GLES30.glStencilMask(cState.stencilMask);
|
231
|
}
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
|
236
|
static void unuseStencilMark()
|
237
|
{
|
238
|
if( sState.stencilTest!=cState.stencilTest )
|
239
|
{
|
240
|
cState.stencilTest = sState.stencilTest;
|
241
|
|
242
|
if (cState.stencilTest == 0)
|
243
|
{
|
244
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
245
|
}
|
246
|
else
|
247
|
{
|
248
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
249
|
}
|
250
|
}
|
251
|
|
252
|
if( sState.stencilFuncFunc!=cState.stencilFuncFunc || sState.stencilFuncRef!=cState.stencilFuncRef || sState.stencilFuncMask!=cState.stencilFuncMask )
|
253
|
{
|
254
|
cState.stencilFuncFunc = sState.stencilFuncFunc;
|
255
|
cState.stencilFuncRef = sState.stencilFuncRef ;
|
256
|
cState.stencilFuncMask = sState.stencilFuncMask;
|
257
|
GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
|
258
|
}
|
259
|
|
260
|
if( sState.stencilMask!=cState.stencilMask )
|
261
|
{
|
262
|
cState.stencilMask = sState.stencilMask;
|
263
|
GLES30.glStencilMask(cState.stencilMask);
|
264
|
}
|
265
|
}
|
266
|
|
267
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268
|
|
269
|
void apply()
|
270
|
{
|
271
|
//android.util.Log.e("State", "APPLYING STATE");
|
272
|
|
273
|
/////////////////////////////////////////////////////
|
274
|
// 1. Write to color buffer?
|
275
|
if( mState.colorMaskR!=cState.colorMaskR || mState.colorMaskG!=cState.colorMaskG || mState.colorMaskB!=cState.colorMaskB || mState.colorMaskA!=cState.colorMaskA)
|
276
|
{
|
277
|
//android.util.Log.d("State", "setting color mask");
|
278
|
cState.colorMaskR = mState.colorMaskR;
|
279
|
cState.colorMaskG = mState.colorMaskG;
|
280
|
cState.colorMaskB = mState.colorMaskB;
|
281
|
cState.colorMaskA = mState.colorMaskA;
|
282
|
GLES30.glColorMask(cState.colorMaskR==1,cState.colorMaskG==1,cState.colorMaskB==1,cState.colorMaskA==1);
|
283
|
}
|
284
|
|
285
|
/////////////////////////////////////////////////////
|
286
|
// 2. Enable Depth test?
|
287
|
if( mState.depthTest!=cState.depthTest )
|
288
|
{
|
289
|
cState.depthTest = mState.depthTest;
|
290
|
|
291
|
if (cState.depthTest == 0)
|
292
|
{
|
293
|
//android.util.Log.d("State", "disabling depth test");
|
294
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
295
|
}
|
296
|
else
|
297
|
{
|
298
|
//android.util.Log.d("State", "enable depth test");
|
299
|
GLES30.glEnable(GLES30.GL_DEPTH_TEST);
|
300
|
}
|
301
|
}
|
302
|
|
303
|
/////////////////////////////////////////////////////
|
304
|
// 3. Change Depth Function?
|
305
|
if( mState.depthFunc!=cState.depthFunc )
|
306
|
{
|
307
|
//android.util.Log.d("State", "setting depth func");
|
308
|
cState.depthFunc = mState.depthFunc;
|
309
|
GLES30.glDepthFunc(cState.depthFunc);
|
310
|
}
|
311
|
|
312
|
/////////////////////////////////////////////////////
|
313
|
// 4. Write to Depth buffer?
|
314
|
if( mState.depthMask!=cState.depthMask )
|
315
|
{
|
316
|
//android.util.Log.d("State", "setting depth mask");
|
317
|
cState.depthMask = mState.depthMask;
|
318
|
GLES30.glDepthMask(cState.depthMask==1);
|
319
|
}
|
320
|
|
321
|
/////////////////////////////////////////////////////
|
322
|
// 5. Enable Blending?
|
323
|
if( mState.blend!=cState.blend )
|
324
|
{
|
325
|
cState.blend = mState.blend;
|
326
|
|
327
|
if (cState.blend == 0)
|
328
|
{
|
329
|
//android.util.Log.d("State", "disabling blending");
|
330
|
GLES30.glDisable(GLES30.GL_BLEND);
|
331
|
}
|
332
|
else
|
333
|
{
|
334
|
//android.util.Log.d("State", "enabling blending");
|
335
|
GLES30.glEnable(GLES30.GL_BLEND);
|
336
|
}
|
337
|
}
|
338
|
|
339
|
/////////////////////////////////////////////////////
|
340
|
// 6. Change Blend function?
|
341
|
if( mState.blendSrc!=cState.blendSrc || mState.blendDst!=cState.blendDst )
|
342
|
{
|
343
|
//android.util.Log.d("State", "setting blend function");
|
344
|
cState.blendSrc = mState.blendSrc;
|
345
|
cState.blendDst = mState.blendDst;
|
346
|
GLES30.glBlendFunc(cState.blendSrc,cState.blendDst);
|
347
|
}
|
348
|
|
349
|
/////////////////////////////////////////////////////
|
350
|
// 7. Enable/Disable Stencil Test?
|
351
|
if( mState.stencilTest!=cState.stencilTest )
|
352
|
{
|
353
|
cState.stencilTest = mState.stencilTest;
|
354
|
|
355
|
if (cState.stencilTest == 0)
|
356
|
{
|
357
|
//android.util.Log.d("State", "disabling stencil test");
|
358
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
359
|
}
|
360
|
else
|
361
|
{
|
362
|
//android.util.Log.d("State", "enabling stencil test");
|
363
|
GLES30.glEnable(GLES30.GL_STENCIL_TEST);
|
364
|
}
|
365
|
}
|
366
|
|
367
|
/////////////////////////////////////////////////////
|
368
|
// 8. Adjust Stencil function?
|
369
|
if( mState.stencilFuncFunc!=cState.stencilFuncFunc || mState.stencilFuncRef!=cState.stencilFuncRef || mState.stencilFuncMask!=cState.stencilFuncMask )
|
370
|
{
|
371
|
//android.util.Log.d("State", "setting stencil function");
|
372
|
cState.stencilFuncFunc = mState.stencilFuncFunc;
|
373
|
cState.stencilFuncRef = mState.stencilFuncRef ;
|
374
|
cState.stencilFuncMask = mState.stencilFuncMask;
|
375
|
GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
|
376
|
}
|
377
|
|
378
|
/////////////////////////////////////////////////////
|
379
|
// 9. Adjust Stencil operation?
|
380
|
if( mState.stencilOpSfail!=cState.stencilOpSfail || mState.stencilOpDpfail!=cState.stencilOpDpfail || mState.stencilOpDppass!=cState.stencilOpDppass )
|
381
|
{
|
382
|
//android.util.Log.d("State", "setting stencil op");
|
383
|
cState.stencilOpSfail = mState.stencilOpSfail;
|
384
|
cState.stencilOpDpfail= mState.stencilOpDpfail;
|
385
|
cState.stencilOpDppass= mState.stencilOpDppass;
|
386
|
GLES30.glStencilOp(cState.stencilOpSfail,cState.stencilOpDpfail,cState.stencilOpDppass);
|
387
|
}
|
388
|
|
389
|
/////////////////////////////////////////////////////
|
390
|
// 10. Write to Stencil buffer?
|
391
|
if( mState.stencilMask!=cState.stencilMask )
|
392
|
{
|
393
|
//android.util.Log.d("State", "setting stencil mask");
|
394
|
cState.stencilMask = mState.stencilMask;
|
395
|
GLES30.glStencilMask(cState.stencilMask);
|
396
|
}
|
397
|
|
398
|
/////////////////////////////////////////////////////
|
399
|
// 11. Clear buffers?
|
400
|
if( mClear!=0 )
|
401
|
{
|
402
|
//android.util.Log.d("State", "clearing buffer");
|
403
|
GLES30.glClear(mClear);
|
404
|
}
|
405
|
}
|
406
|
|
407
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
408
|
|
409
|
void glColorMask(boolean r, boolean g, boolean b, boolean a)
|
410
|
{
|
411
|
mState.colorMaskR = (r ? 1:0);
|
412
|
mState.colorMaskG = (g ? 1:0);
|
413
|
mState.colorMaskB = (b ? 1:0);
|
414
|
mState.colorMaskA = (a ? 1:0);
|
415
|
}
|
416
|
|
417
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
418
|
|
419
|
void glDepthMask(boolean mask)
|
420
|
{
|
421
|
mState.depthMask = (mask ? 1:0);
|
422
|
}
|
423
|
|
424
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
425
|
|
426
|
void glStencilMask(int mask)
|
427
|
{
|
428
|
mState.stencilMask = mask;
|
429
|
}
|
430
|
|
431
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
432
|
|
433
|
void glEnable(int test)
|
434
|
{
|
435
|
if( test==GLES30.GL_DEPTH_TEST ) mState.depthTest = 1;
|
436
|
else if( test==GLES30.GL_STENCIL_TEST ) mState.stencilTest = 1;
|
437
|
else if( test==GLES30.GL_BLEND ) mState.blend = 1;
|
438
|
}
|
439
|
|
440
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
441
|
|
442
|
void glDisable(int test)
|
443
|
{
|
444
|
if( test==GLES30.GL_DEPTH_TEST ) mState.depthTest = 0;
|
445
|
else if( test==GLES30.GL_STENCIL_TEST ) mState.stencilTest = 0;
|
446
|
else if( test==GLES30.GL_BLEND ) mState.blend = 0;
|
447
|
}
|
448
|
|
449
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
450
|
|
451
|
void glStencilFunc(int func, int ref, int mask)
|
452
|
{
|
453
|
mState.stencilFuncFunc = func;
|
454
|
mState.stencilFuncRef = ref;
|
455
|
mState.stencilFuncMask = mask;
|
456
|
}
|
457
|
|
458
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
459
|
|
460
|
void glStencilOp(int sfail, int dpfail, int dppass)
|
461
|
{
|
462
|
mState.stencilOpSfail = sfail;
|
463
|
mState.stencilOpDpfail= dpfail;
|
464
|
mState.stencilOpDppass= dppass;
|
465
|
}
|
466
|
|
467
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
468
|
|
469
|
void glDepthFunc(int func)
|
470
|
{
|
471
|
mState.depthFunc = func;
|
472
|
}
|
473
|
|
474
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
475
|
|
476
|
void glBlendFunc(int src, int dst)
|
477
|
{
|
478
|
mState.blendSrc = src;
|
479
|
mState.blendDst = dst;
|
480
|
}
|
481
|
|
482
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
483
|
|
484
|
void glClear(int mask)
|
485
|
{
|
486
|
mClear = mask;
|
487
|
}
|
488
|
}
|