Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalRenderState.java @ 8c57d77b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.main;
22

    
23
import android.opengl.GLES30;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27
 * Remember the OpenGL state.
28
 * <p>
29
 * This is a member of DistortedNode. Remembers the OpenGL state we want to set just before rendering
30
 * the Node.
31
 * <p>
32
 * Only for use by the library itself.
33
 *
34
 * @y.exclude
35
 */
36
public class InternalRenderState
37
{
38
  // TODO: figure this out dynamically; this assumes 8 bit stencil buffer.
39
  private static final int STENCIL_MASK = (1<<8)-1;
40

    
41
  private static class RenderState
42
    {
43
    private int colorMaskR, colorMaskG, colorMaskB, colorMaskA;
44
    private int depthMask;
45
    private int stencilMask;
46
    private int depthTest;
47
    private int stencilTest;
48
    private int stencilFuncFunc, stencilFuncRef, stencilFuncMask;
49
    private int stencilOpSfail, stencilOpDpfail, stencilOpDppass;
50
    private int depthFunc;
51
    private int blend;
52
    private int blendSrc, blendDst;
53
    }
54

    
55
  private final RenderState mState;          // state the current object wants to have
56
  static private final RenderState cState = new RenderState();   // current OpenGL Stave
57
  static private final RenderState sState = new RenderState();   // saved OpenGL state
58

    
59
  private int mClear;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
// default: color writes on, depth test and writes on, blending on, stencil off.
63

    
64
  InternalRenderState()
65
    {
66
    mState = new RenderState();
67

    
68
    mState.colorMaskR = 1;
69
    mState.colorMaskG = 1;
70
    mState.colorMaskB = 1;
71
    mState.colorMaskA = 1;
72

    
73
    mState.depthTest  = 1;
74
    mState.depthMask  = 1;
75
    mState.depthFunc  = GLES30.GL_LEQUAL;
76

    
77
    mState.blend      = 1;
78
    mState.blendSrc   = GLES30.GL_SRC_ALPHA;
79
    mState.blendDst   = GLES30.GL_ONE_MINUS_SRC_ALPHA;
80

    
81
    mState.stencilTest     = 0;
82
    mState.stencilMask     = STENCIL_MASK;
83
    mState.stencilFuncFunc = GLES30.GL_NEVER;
84
    mState.stencilFuncRef  = 0;
85
    mState.stencilFuncMask = STENCIL_MASK;
86
    mState.stencilOpSfail  = GLES30.GL_KEEP;
87
    mState.stencilOpDpfail = GLES30.GL_KEEP;
88
    mState.stencilOpDppass = GLES30.GL_KEEP;
89

    
90
    mClear = 0;
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
// reset state of everything to a known state.
95

    
96
  static void reset()
97
    {
98
    cState.colorMaskR = 1;
99
    cState.colorMaskG = 1;
100
    cState.colorMaskB = 1;
101
    cState.colorMaskA = 1;
102
    GLES30.glColorMask(true,true,true,true);
103

    
104
    cState.depthTest = 1;
105
    cState.depthMask = 1;
106
    cState.depthFunc = GLES30.GL_LEQUAL;
107
    GLES30.glEnable(GLES30.GL_DEPTH_TEST);
108
    GLES30.glDepthMask(true);
109
    GLES30.glDepthFunc(cState.depthFunc);
110

    
111
    cState.stencilTest     = 0;
112
    cState.stencilMask     = STENCIL_MASK;
113
    cState.stencilFuncFunc = GLES30.GL_NEVER;
114
    cState.stencilFuncRef  = 0;
115
    cState.stencilFuncMask = STENCIL_MASK;
116
    cState.stencilOpSfail  = GLES30.GL_KEEP;
117
    cState.stencilOpDpfail = GLES30.GL_KEEP;
118
    cState.stencilOpDppass = GLES30.GL_KEEP;
119
    GLES30.glDisable(GLES30.GL_STENCIL_TEST);
120
    GLES30.glStencilMask(cState.stencilMask);
121

    
122
    cState.blend      = 1;
123
    cState.blendSrc   = GLES30.GL_SRC_ALPHA;
124
    cState.blendDst   = GLES30.GL_ONE_MINUS_SRC_ALPHA;
125
    GLES30.glEnable(GLES30.GL_BLEND);
126
    GLES30.glBlendFunc(cState.blendSrc,cState.blendDst);
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  static void colorDepthStencilOn()
132
    {
133
    sState.colorMaskR = cState.colorMaskR;
134
    sState.colorMaskG = cState.colorMaskG;
135
    sState.colorMaskB = cState.colorMaskB;
136
    sState.colorMaskA = cState.colorMaskA;
137

    
138
    if( cState.colorMaskR!=1 || cState.colorMaskG!=1 || cState.colorMaskB!=1 || cState.colorMaskA!=1 )
139
      {
140
      cState.colorMaskR = 1;
141
      cState.colorMaskG = 1;
142
      cState.colorMaskB = 1;
143
      cState.colorMaskA = 1;
144
      GLES30.glColorMask(true,true,true,true);
145
      }
146

    
147
    sState.depthMask = cState.depthMask;
148

    
149
    if( cState.depthMask!=1 )
150
      {
151
      cState.depthMask = 1;
152
      GLES30.glDepthMask(true);
153
      }
154

    
155
    sState.stencilMask = cState.stencilMask;
156

    
157
    if( cState.stencilMask!= STENCIL_MASK )
158
      {
159
      cState.stencilMask = STENCIL_MASK;
160
      GLES30.glStencilMask(cState.stencilMask);
161
      }
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  static void colorDepthStencilRestore()
167
    {
168
    if( sState.colorMaskR!=cState.colorMaskR || sState.colorMaskG!=cState.colorMaskG || sState.colorMaskB!=cState.colorMaskB || sState.colorMaskA!=cState.colorMaskA)
169
      {
170
      cState.colorMaskR = sState.colorMaskR;
171
      cState.colorMaskG = sState.colorMaskG;
172
      cState.colorMaskB = sState.colorMaskB;
173
      cState.colorMaskA = sState.colorMaskA;
174
      GLES30.glColorMask(cState.colorMaskR==1,cState.colorMaskG==1,cState.colorMaskB==1,cState.colorMaskA==1);
175
      }
176
    if( sState.depthMask!=cState.depthMask )
177
      {
178
      cState.depthMask = sState.depthMask;
179
      GLES30.glDepthMask(cState.depthMask==1);
180
      }
181
    if( sState.stencilMask!=cState.stencilMask )
182
      {
183
      cState.stencilMask = sState.stencilMask;
184
      GLES30.glStencilMask(cState.stencilMask);
185
      }
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  public static void disableBlending()
191
    {
192
    sState.blend = cState.blend;
193

    
194
    if (cState.blend != 0)
195
      {
196
      cState.blend = 0;
197
      GLES30.glDisable(GLES30.GL_BLEND);
198
      }
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  public static void restoreBlending()
204
    {
205
    if (sState.blend != cState.blend)
206
      {
207
      cState.blend = sState.blend;
208

    
209
      if (cState.blend == 0)
210
        {
211
        GLES30.glDisable(GLES30.GL_BLEND);
212
        }
213
      else
214
        {
215
        GLES30.glEnable(GLES30.GL_BLEND);
216
        }
217
      }
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  static void enableDepthTest()
223
    {
224
    sState.depthTest = cState.depthTest;
225

    
226
    if (cState.depthTest != 1)
227
      {
228
      cState.depthTest = 1;
229
      GLES30.glEnable(GLES30.GL_DEPTH_TEST);
230
      }
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  static void restoreDepthTest()
236
    {
237
    if (sState.depthTest != cState.depthTest)
238
      {
239
      cState.depthTest = sState.depthTest;
240

    
241
      if (cState.depthTest == 0)
242
        {
243
        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
244
        }
245
      else
246
        {
247
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
248
        }
249
      }
250
    }
251

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

    
254
  public static void switchOffDrawing()
255
    {
256
    GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
257
    GLES30.glScissor(0,0,0,0);
258
    }
259

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

    
262
  public static void restoreDrawing()
263
    {
264
    GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  static void switchOffColorDepthStencil()
270
    {
271
    sState.stencilTest = cState.stencilTest;
272

    
273
    if( cState.stencilTest!=0 )
274
      {
275
      cState.stencilTest = 0;
276
      //DistortedLibrary.logMessage("InternalRenderState: stencil test off");
277
      GLES30.glDisable(GLES30.GL_STENCIL_TEST);
278
      }
279

    
280
    sState.depthTest = cState.depthTest;
281

    
282
    if( cState.depthTest!=0 )
283
      {
284
      cState.depthTest = 0;
285
      //DistortedLibrary.logMessage("InternalRenderState: depth test off");
286
      GLES30.glDisable(GLES30.GL_DEPTH_TEST);
287
      }
288

    
289
    sState.colorMaskR = cState.colorMaskR;
290
    sState.colorMaskG = cState.colorMaskG;
291
    sState.colorMaskB = cState.colorMaskB;
292
    sState.colorMaskA = cState.colorMaskA;
293

    
294
    if( cState.colorMaskR!=0 || cState.colorMaskG!=0 || cState.colorMaskB!=0 || cState.colorMaskA!=0 )
295
      {
296
      cState.colorMaskR = 0;
297
      cState.colorMaskG = 0;
298
      cState.colorMaskB = 0;
299
      cState.colorMaskA = 0;
300
      //DistortedLibrary.logMessage("InternalRenderState: switch off color writing");
301
      GLES30.glColorMask(false,false,false,false);
302
      }
303

    
304
    sState.depthMask = cState.depthMask;
305

    
306
    if( cState.depthMask!=0 )
307
      {
308
      cState.depthMask = 0;
309
      //DistortedLibrary.logMessage("InternalRenderState: switch off depth writing");
310
      GLES30.glDepthMask(false);
311
      }
312

    
313
    sState.stencilMask = cState.stencilMask;
314

    
315
    if( cState.stencilMask!= 0x00 )
316
      {
317
      cState.stencilMask = 0x00;
318
      //DistortedLibrary.logMessage("InternalRenderState: stencil mask off");
319
      GLES30.glStencilMask(cState.stencilMask);
320
      }
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  static void switchColorDepthOnStencilOff()
326
    {
327
    sState.stencilTest = cState.stencilTest;
328

    
329
    if( cState.stencilTest!=0 )
330
      {
331
      cState.stencilTest = 0;
332
      //DistortedLibrary.logMessage("InternalRenderState: stencil test off");
333
      GLES30.glDisable(GLES30.GL_STENCIL_TEST);
334
      }
335

    
336
    sState.depthTest = cState.depthTest;
337

    
338
    if( cState.depthTest!=0 )
339
      {
340
      cState.depthTest = 0;
341
      //DistortedLibrary.logMessage("InternalRenderState: depth test off");
342
      GLES30.glDisable(GLES30.GL_DEPTH_TEST);
343
      }
344

    
345
    sState.colorMaskR = cState.colorMaskR;
346
    sState.colorMaskG = cState.colorMaskG;
347
    sState.colorMaskB = cState.colorMaskB;
348
    sState.colorMaskA = cState.colorMaskA;
349

    
350
    if( cState.colorMaskR!=1 || cState.colorMaskG!=1 || cState.colorMaskB!=1 || cState.colorMaskA!=1 )
351
      {
352
      cState.colorMaskR = 1;
353
      cState.colorMaskG = 1;
354
      cState.colorMaskB = 1;
355
      cState.colorMaskA = 1;
356
      //DistortedLibrary.logMessage("InternalRenderState: switch on color writing");
357
      GLES30.glColorMask(true,true,true,true);
358
      }
359

    
360
    sState.depthMask = cState.depthMask;
361

    
362
    if( cState.depthMask!=1 )
363
      {
364
      cState.depthMask = 1;
365
      //DistortedLibrary.logMessage("InternalRenderState: switch on depth writing");
366
      GLES30.glDepthMask(true);
367
      }
368

    
369
    sState.stencilMask = cState.stencilMask;
370

    
371
    if( cState.stencilMask!= 0x00 )
372
      {
373
      cState.stencilMask = 0x00;
374
      //DistortedLibrary.logMessage("InternalRenderState: stencil mask off");
375
      GLES30.glStencilMask(cState.stencilMask);
376
      }
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  static void restoreColorDepthStencil()
382
    {
383
    if( sState.stencilTest!=cState.stencilTest )
384
      {
385
      cState.stencilTest = sState.stencilTest;
386

    
387
      if (cState.stencilTest == 0)
388
        {
389
        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
390
        }
391
      else
392
        {
393
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
394
        }
395
      }
396
    if( sState.depthTest!=cState.depthTest )
397
      {
398
      cState.depthTest = sState.depthTest;
399

    
400
      if (cState.depthTest == 0)
401
        {
402
        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
403
        }
404
      else
405
        {
406
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
407
        }
408
      }
409
    if( sState.colorMaskR!=cState.colorMaskR || sState.colorMaskG!=cState.colorMaskG || sState.colorMaskB!=cState.colorMaskB || sState.colorMaskA!=cState.colorMaskA)
410
      {
411
      cState.colorMaskR = sState.colorMaskR;
412
      cState.colorMaskG = sState.colorMaskG;
413
      cState.colorMaskB = sState.colorMaskB;
414
      cState.colorMaskA = sState.colorMaskA;
415
      GLES30.glColorMask(cState.colorMaskR==1,cState.colorMaskG==1,cState.colorMaskB==1,cState.colorMaskA==1);
416
      }
417
    if( sState.depthMask!=cState.depthMask )
418
      {
419
      cState.depthMask = sState.depthMask;
420
      GLES30.glDepthMask(cState.depthMask==1);
421
      }
422
    if( sState.stencilMask!=cState.stencilMask )
423
      {
424
      cState.stencilMask = sState.stencilMask;
425
      GLES30.glStencilMask(cState.stencilMask);
426
      }
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  public static void setUpStencilMark(boolean color)
432
    {
433
    sState.stencilTest = cState.stencilTest;
434

    
435
    if( cState.stencilTest!=1 )
436
      {
437
      cState.stencilTest = 1;
438
      //DistortedLibrary.logMessage("InternalRenderState: stencil test on");
439
      GLES30.glEnable(GLES30.GL_STENCIL_TEST);
440
      }
441

    
442
    sState.stencilFuncFunc = cState.stencilFuncFunc;
443
    sState.stencilFuncRef  = cState.stencilFuncRef;
444
    sState.stencilFuncMask = cState.stencilFuncMask;
445

    
446
    if( cState.stencilFuncFunc!=GLES30.GL_ALWAYS || cState.stencilFuncRef!=1 || cState.stencilFuncMask!=STENCIL_MASK )
447
      {
448
      cState.stencilFuncFunc = GLES30.GL_ALWAYS;
449
      cState.stencilFuncRef  = 1;
450
      cState.stencilFuncMask = STENCIL_MASK;
451
      //DistortedLibrary.logMessage("InternalRenderState: stencil func on");
452
      GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
453
      }
454

    
455
    sState.stencilOpSfail = cState.stencilOpSfail;
456
    sState.stencilOpDpfail= cState.stencilOpDpfail;
457
    sState.stencilOpDppass= cState.stencilOpDppass;
458

    
459
    if( cState.stencilOpSfail!=GLES30.GL_KEEP || cState.stencilOpDpfail!=GLES30.GL_KEEP || cState.stencilOpDppass!=GLES30.GL_REPLACE )
460
      {
461
      cState.stencilOpSfail = GLES30.GL_KEEP;
462
      cState.stencilOpDpfail= GLES30.GL_KEEP;
463
      cState.stencilOpDppass= GLES30.GL_REPLACE;
464
      //DistortedLibrary.logMessage("InternalRenderState: stencil op on");
465
      GLES30.glStencilOp(cState.stencilOpSfail,cState.stencilOpDpfail,cState.stencilOpDppass);
466
      }
467

    
468
    sState.colorMaskR = cState.colorMaskR;
469
    sState.colorMaskG = cState.colorMaskG;
470
    sState.colorMaskB = cState.colorMaskB;
471
    sState.colorMaskA = cState.colorMaskA;
472

    
473
    int clr = color ? 1:0;
474

    
475
    if( cState.colorMaskR!=clr || cState.colorMaskG!=clr || cState.colorMaskB!=clr || cState.colorMaskA!=clr )
476
      {
477
      cState.colorMaskR = clr;
478
      cState.colorMaskG = clr;
479
      cState.colorMaskB = clr;
480
      cState.colorMaskA = clr;
481
      //DistortedLibrary.logMessage("InternalRenderState: switch off color writing");
482
      GLES30.glColorMask(color,color,color,color);
483
      }
484

    
485
    sState.depthMask = cState.depthMask;
486

    
487
    if( cState.depthMask!=1 )
488
      {
489
      cState.depthMask = 1;
490
      //DistortedLibrary.logMessage("InternalRenderState: switch on depth writing");
491
      GLES30.glDepthMask(true);
492
      }
493

    
494
    sState.stencilMask = cState.stencilMask;
495

    
496
    if( cState.stencilMask!= STENCIL_MASK )
497
      {
498
      cState.stencilMask = STENCIL_MASK;
499
      //DistortedLibrary.logMessage("InternalRenderState: stencil mask on");
500
      GLES30.glStencilMask(cState.stencilMask);
501
      }
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public static void unsetUpStencilMark()
507
    {
508
    if( sState.stencilTest!=cState.stencilTest )
509
      {
510
      cState.stencilTest = sState.stencilTest;
511

    
512
      if (cState.stencilTest == 0)
513
        {
514
        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
515
        }
516
      else
517
        {
518
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
519
        }
520
      }
521
    if( sState.colorMaskR!=cState.colorMaskR || sState.colorMaskG!=cState.colorMaskG || sState.colorMaskB!=cState.colorMaskB || sState.colorMaskA!=cState.colorMaskA)
522
      {
523
      cState.colorMaskR = sState.colorMaskR;
524
      cState.colorMaskG = sState.colorMaskG;
525
      cState.colorMaskB = sState.colorMaskB;
526
      cState.colorMaskA = sState.colorMaskA;
527
      GLES30.glColorMask(cState.colorMaskR==1,cState.colorMaskG==1,cState.colorMaskB==1,cState.colorMaskA==1);
528
      }
529
    if( sState.depthMask!=cState.depthMask )
530
      {
531
      cState.depthMask = sState.depthMask;
532
      GLES30.glDepthMask(cState.depthMask==1);
533
      }
534
    if( sState.stencilMask!=cState.stencilMask )
535
      {
536
      cState.stencilMask = sState.stencilMask;
537
      GLES30.glStencilMask(cState.stencilMask);
538
      }
539
    }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542
/**
543
 * Only for use by the library itself.
544
 *
545
 * @y.exclude
546
 */
547
  public static void useStencilMark()
548
    {
549
    sState.stencilMask = cState.stencilMask;
550

    
551
    if( cState.stencilTest!=1 )
552
      {
553
      cState.stencilTest = 1;
554
      GLES30.glEnable(GLES30.GL_STENCIL_TEST);
555
      }
556

    
557
    sState.stencilFuncFunc = cState.stencilFuncFunc;
558
    sState.stencilFuncRef  = cState.stencilFuncRef;
559
    sState.stencilFuncMask = cState.stencilFuncMask;
560

    
561
    if( cState.stencilFuncFunc!=GLES30.GL_EQUAL || cState.stencilFuncRef!=1 || cState.stencilFuncMask!=STENCIL_MASK )
562
      {
563
      cState.stencilFuncFunc = GLES30.GL_EQUAL;
564
      cState.stencilFuncRef  = 1;
565
      cState.stencilFuncMask = STENCIL_MASK;
566
      GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
567
      }
568

    
569
    sState.stencilMask = cState.stencilMask;
570

    
571
    if( cState.stencilMask!= 0x00 )
572
      {
573
      cState.stencilMask = 0x00;
574
      GLES30.glStencilMask(cState.stencilMask);
575
      }
576

    
577
    sState.depthMask = cState.depthMask;
578

    
579
    if( cState.depthMask!=0 )
580
      {
581
      cState.depthMask = 0;
582
      GLES30.glDepthMask(false);
583
      }
584

    
585
    sState.depthTest = cState.depthTest;
586

    
587
    if( cState.depthTest!=0 )
588
      {
589
      cState.depthTest = 0;
590
      GLES30.glDisable(GLES30.GL_DEPTH_TEST);
591
      }
592
    }
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595
/**
596
 * Only for use by the library itself.
597
 *
598
 * @y.exclude
599
 */
600
  public static void unuseStencilMark()
601
    {
602
    if( sState.stencilTest!=cState.stencilTest )
603
      {
604
      cState.stencilTest = sState.stencilTest;
605

    
606
      if (cState.stencilTest == 0)
607
        {
608
        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
609
        }
610
      else
611
        {
612
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
613
        }
614
      }
615
    if( sState.stencilFuncFunc!=cState.stencilFuncFunc || sState.stencilFuncRef!=cState.stencilFuncRef || sState.stencilFuncMask!=cState.stencilFuncMask )
616
      {
617
      cState.stencilFuncFunc = sState.stencilFuncFunc;
618
      cState.stencilFuncRef  = sState.stencilFuncRef ;
619
      cState.stencilFuncMask = sState.stencilFuncMask;
620
      GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
621
      }
622
    if( sState.stencilMask!=cState.stencilMask )
623
      {
624
      cState.stencilMask = sState.stencilMask;
625
      GLES30.glStencilMask(cState.stencilMask);
626
      }
627
    if( sState.depthMask!=cState.depthMask )
628
      {
629
      cState.depthMask = sState.depthMask;
630
      GLES30.glDepthMask(cState.depthMask==1);
631
      }
632
    if( sState.depthTest!=cState.depthTest )
633
      {
634
      cState.depthTest = sState.depthTest;
635

    
636
      if (cState.depthTest == 0)
637
        {
638
        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
639
        }
640
      else
641
        {
642
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
643
        }
644
      }
645
    }
646

    
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648

    
649
  void apply()
650
    {
651
    //DistortedLibrary.logMessage("InternalRenderState: APPLYING STATE");
652

    
653
    /////////////////////////////////////////////////////
654
    // 1. Write to color buffer?
655
    if( mState.colorMaskR!=cState.colorMaskR || mState.colorMaskG!=cState.colorMaskG || mState.colorMaskB!=cState.colorMaskB || mState.colorMaskA!=cState.colorMaskA)
656
      {
657
      //DistortedLibrary.logMessage("InternalRenderState: setting color mask");
658
      cState.colorMaskR = mState.colorMaskR;
659
      cState.colorMaskG = mState.colorMaskG;
660
      cState.colorMaskB = mState.colorMaskB;
661
      cState.colorMaskA = mState.colorMaskA;
662
      GLES30.glColorMask(cState.colorMaskR==1,cState.colorMaskG==1,cState.colorMaskB==1,cState.colorMaskA==1);
663
      }
664

    
665
    /////////////////////////////////////////////////////
666
    // 2. Enable Depth test?
667
    if( mState.depthTest!=cState.depthTest )
668
      {
669
      cState.depthTest = mState.depthTest;
670

    
671
      if (cState.depthTest == 0)
672
        {
673
        //DistortedLibrary.logMessage("InternalRenderState: disabling depth test");
674
        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
675
        }
676
      else
677
        {
678
        //DistortedLibrary.logMessage("InternalRenderState: enable depth test");
679
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
680
        }
681
      }
682

    
683
    /////////////////////////////////////////////////////
684
    // 3. Change Depth Function?
685
    if( mState.depthFunc!=cState.depthFunc )
686
      {
687
      //DistortedLibrary.logMessage("InternalRenderState: setting depth func");
688
      cState.depthFunc = mState.depthFunc;
689
      GLES30.glDepthFunc(cState.depthFunc);
690
      }
691

    
692
    /////////////////////////////////////////////////////
693
    // 4. Write to Depth buffer?
694
    if( mState.depthMask!=cState.depthMask )
695
      {
696
      //DistortedLibrary.logMessage("InternalRenderState: setting depth mask");
697
      cState.depthMask = mState.depthMask;
698
      GLES30.glDepthMask(cState.depthMask==1);
699
      }
700

    
701
    /////////////////////////////////////////////////////
702
    // 5. Enable Blending?
703
    if( mState.blend!=cState.blend )
704
      {
705
      cState.blend = mState.blend;
706

    
707
      if (cState.blend == 0)
708
        {
709
        //DistortedLibrary.logMessage("InternalRenderState: disabling blending");
710
        GLES30.glDisable(GLES30.GL_BLEND);
711
        }
712
      else
713
        {
714
        //DistortedLibrary.logMessage("InternalRenderState: enabling blending");
715
        GLES30.glEnable(GLES30.GL_BLEND);
716
        }
717
      }
718

    
719
    /////////////////////////////////////////////////////
720
    // 6. Change Blend function?
721
    if( mState.blendSrc!=cState.blendSrc || mState.blendDst!=cState.blendDst )
722
      {
723
      //DistortedLibrary.logMessage("InternalRenderState: setting blend function");
724
      cState.blendSrc = mState.blendSrc;
725
      cState.blendDst = mState.blendDst;
726
      GLES30.glBlendFunc(cState.blendSrc,cState.blendDst);
727
      }
728

    
729
    /////////////////////////////////////////////////////
730
    // 7. Enable/Disable Stencil Test?
731
    if( mState.stencilTest!=cState.stencilTest )
732
      {
733
      cState.stencilTest = mState.stencilTest;
734

    
735
      if (cState.stencilTest == 0)
736
        {
737
        //DistortedLibrary.logMessage("InternalRenderState: disabling stencil test");
738
        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
739
        }
740
      else
741
        {
742
        //DistortedLibrary.logMessage("InternalRenderState: enabling stencil test");
743
        GLES30.glEnable(GLES30.GL_STENCIL_TEST);
744
        }
745
      }
746

    
747
    /////////////////////////////////////////////////////
748
    // 8. Adjust Stencil function?
749
    if( mState.stencilFuncFunc!=cState.stencilFuncFunc || mState.stencilFuncRef!=cState.stencilFuncRef || mState.stencilFuncMask!=cState.stencilFuncMask )
750
      {
751
      //DistortedLibrary.logMessage("InternalRenderState: setting stencil function");
752
      cState.stencilFuncFunc = mState.stencilFuncFunc;
753
      cState.stencilFuncRef  = mState.stencilFuncRef ;
754
      cState.stencilFuncMask = mState.stencilFuncMask;
755
      GLES30.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
756
      }
757

    
758
    /////////////////////////////////////////////////////
759
    // 9. Adjust Stencil operation?
760
    if( mState.stencilOpSfail!=cState.stencilOpSfail || mState.stencilOpDpfail!=cState.stencilOpDpfail || mState.stencilOpDppass!=cState.stencilOpDppass )
761
      {
762
      //DistortedLibrary.logMessage("InternalRenderState: setting stencil op");
763
      cState.stencilOpSfail = mState.stencilOpSfail;
764
      cState.stencilOpDpfail= mState.stencilOpDpfail;
765
      cState.stencilOpDppass= mState.stencilOpDppass;
766
      GLES30.glStencilOp(cState.stencilOpSfail,cState.stencilOpDpfail,cState.stencilOpDppass);
767
      }
768

    
769
    /////////////////////////////////////////////////////
770
    // 10. Write to Stencil buffer?
771
    if( mState.stencilMask!=cState.stencilMask )
772
      {
773
      //DistortedLibrary.logMessage("InternalRenderState: setting stencil mask");
774
      cState.stencilMask = mState.stencilMask;
775
      GLES30.glStencilMask(cState.stencilMask);
776
      }
777

    
778
    /////////////////////////////////////////////////////
779
    // 11. Clear buffers?
780
    if( mClear!=0 )
781
      {
782
      //DistortedLibrary.logMessage("InternalRenderState: clearing buffer");
783
      GLES30.glClear(mClear);
784
      }
785
    }
786

    
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788

    
789
  void glColorMask(boolean r, boolean g, boolean b, boolean a)
790
    {
791
    mState.colorMaskR = (r ? 1:0);
792
    mState.colorMaskG = (g ? 1:0);
793
    mState.colorMaskB = (b ? 1:0);
794
    mState.colorMaskA = (a ? 1:0);
795
    }
796

    
797
///////////////////////////////////////////////////////////////////////////////////////////////////
798

    
799
  void glDepthMask(boolean mask)
800
    {
801
    mState.depthMask = (mask ? 1:0);
802
    }
803

    
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805

    
806
  void glStencilMask(int mask)
807
    {
808
    mState.stencilMask = mask;
809
    }
810

    
811
///////////////////////////////////////////////////////////////////////////////////////////////////
812

    
813
  void glEnable(int test)
814
    {
815
         if( test==GLES30.GL_DEPTH_TEST   ) mState.depthTest   = 1;
816
    else if( test==GLES30.GL_STENCIL_TEST ) mState.stencilTest = 1;
817
    else if( test==GLES30.GL_BLEND        ) mState.blend       = 1;
818
    }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821

    
822
  void glDisable(int test)
823
    {
824
         if( test==GLES30.GL_DEPTH_TEST   ) mState.depthTest   = 0;
825
    else if( test==GLES30.GL_STENCIL_TEST ) mState.stencilTest = 0;
826
    else if( test==GLES30.GL_BLEND        ) mState.blend       = 0;
827
    }
828

    
829
///////////////////////////////////////////////////////////////////////////////////////////////////
830

    
831
  void glStencilFunc(int func, int ref, int mask)
832
    {
833
    mState.stencilFuncFunc = func;
834
    mState.stencilFuncRef  = ref;
835
    mState.stencilFuncMask = mask;
836
    }
837

    
838
///////////////////////////////////////////////////////////////////////////////////////////////////
839

    
840
  void glStencilOp(int sfail, int dpfail, int dppass)
841
    {
842
    mState.stencilOpSfail = sfail;
843
    mState.stencilOpDpfail= dpfail;
844
    mState.stencilOpDppass= dppass;
845
    }
846

    
847
///////////////////////////////////////////////////////////////////////////////////////////////////
848

    
849
  void glDepthFunc(int func)
850
    {
851
    mState.depthFunc = func;
852
    }
853

    
854
///////////////////////////////////////////////////////////////////////////////////////////////////
855

    
856
  void glBlendFunc(int src, int dst)
857
    {
858
    mState.blendSrc = src;
859
    mState.blendDst = dst;
860
    }
861

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863

    
864
  void glClear(int mask)
865
    {
866
    mClear = mask;
867
    }
868
}
(13-13/17)