Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ 9cae4322

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.main;
21

    
22
import android.opengl.GLES31;
23

    
24
import org.distorted.library.mesh.MeshBase;
25

    
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.HashMap;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
/**
32
 * Class which represents a Node in a Tree of (InputSurface,Mesh,Effects) triplets.
33
 * <p>
34
 * Having organized such sets into a Tree, we can then render any Node to any OutputSurface.
35
 * That recursively renders the set held in the Node and all its children.
36
 * <p>
37
 * The class takes special care to only render identical sub-trees once. Each Node holds a reference
38
 * to sub-class 'NodeData'. Two identical sub-trees attached at different points of the main tree
39
 * will point to the same NodeData; only the first of this is rendered (mData.numRender!).
40
 */
41
public class DistortedNode implements DistortedMaster.Slave
42
  {
43
  private static final int ATTACH = 0;
44
  private static final int DETACH = 1;
45
  private static final int DETALL = 2;
46
  private static final int SORT   = 3;
47

    
48
  private ArrayList<DistortedNode> mChildren;
49
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
50

    
51
  private class Job
52
    {
53
    int type;
54
    DistortedNode node;
55

    
56
    Job(int t, DistortedNode n)
57
      {
58
      type = t;
59
      node = n;
60
      }
61
    }
62

    
63
  private ArrayList<Job> mJobs = new ArrayList<>();
64

    
65
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
66
  private static long mNextNodeID =0;
67

    
68
  private boolean mRenderWayOIT;
69
  private DistortedNode mParent;
70
  private DistortedOutputSurface mSurfaceParent;
71
  private MeshBase mMesh;
72
  private DistortedEffects mEffects;
73
  private DistortedSurface mSurface;
74
  private DistortedRenderState mState;
75
  private NodeData mData;
76
  private int mFboW, mFboH, mFboDepthStencil;
77

    
78
  private class NodeData
79
    {
80
    final long ID;
81
    final ArrayList<Long> key;
82

    
83
    int numPointingNodes;
84
    long currTime;
85
    DistortedFramebuffer mFBO;
86

    
87
    NodeData(long id, ArrayList<Long> k)
88
      {
89
      ID              = id;
90
      key             = k;
91
      numPointingNodes= 1;
92
      currTime        =-1;
93
      mFBO            = null;
94
      }
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  static synchronized void onDestroy()
100
    {
101
    mNextNodeID = 0;
102
    mMapNodeID.clear();
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void markForDeletion()
108
    {
109
    if( --mData.numPointingNodes==0 )
110
      {
111
      mMapNodeID.remove(mData.key);
112

    
113
      if( mData.mFBO!=null )
114
        {
115
        mData.mFBO.markForDeletion();
116
        mData.mFBO = null;
117
        }
118
      }
119

    
120
    mEffects.removeNode(this);
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private ArrayList<Long> generateIDList()
126
    {
127
    ArrayList<Long> ret = new ArrayList<>();
128

    
129
    if( mNumChildren[0]==0 )
130
      {
131
      // add a negative number so this leaf never gets confused with a internal node
132
      // with a single child that happens to have ID identical to some leaf's Effects ID.
133
      ret.add(-mEffects.getID());
134
      }
135
    else
136
      {
137
      DistortedNode node;
138
   
139
      for(int i=0; i<mNumChildren[0]; i++)
140
        {
141
        node = mChildren.get(i);
142
        ret.add(node.mData.ID);
143
        }
144

    
145
      // A bit questionable decision here - we are sorting the children IDs, which means
146
      // that order in which we draw the children is going to be undefined (well, this is not
147
      // strictly speaking true - when rendering, if no postprocessing and isomorphism are
148
      // involved, we *DO* render the children in order they were added; if however there
149
      // are two internal nodes with the same list of identical children, just added in a
150
      // different order each time, then we consider them isomorphic, i.e. identical and only
151
      // render the first one. If then two children of such 'pseudo-isomorphic' nodes are at
152
      // exactly the same Z-height this might result in some unexpected sights).
153
      //
154
      // Reason: with the children being sorted by postprocessing buckets, the order is
155
      // undefined anyway (although only when postprocessing is applied).
156
      //
157
      // See the consequences in the 'Olympic' app - remove a few leaves and add them back in
158
      // different order. You will see the number of renders go back to the original 15.
159
      Collections.sort(ret);
160
      }
161

    
162
    ret.add( 0, mSurface.getID() );
163

    
164
    return ret;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// Debug - print all the Node IDs
169

    
170
  @SuppressWarnings("unused")
171
  void debug(int depth)
172
    {
173
    String tmp="";
174
    int i;
175

    
176
    for(i=0; i<depth; i++) tmp +="   ";
177
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
178
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
179
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
180

    
181
    android.util.Log.e("NODE", tmp);
182

    
183
    for(i=0; i<mNumChildren[0]; i++)
184
      mChildren.get(i).debug(depth+1);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// Debug - print contents of the HashMap
189

    
190
  @SuppressWarnings("unused")
191
  static void debugMap()
192
    {
193
    NodeData tmp;
194

    
195
    for(ArrayList<Long> key: mMapNodeID.keySet())
196
      {
197
      tmp = mMapNodeID.get(key);
198
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
199
      }
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  private NodeData retData()
205
    {
206
    ArrayList<Long> newList = generateIDList();
207
    NodeData newData = mMapNodeID.get(newList);
208

    
209
    if( newData!=null )
210
      {
211
      newData.numPointingNodes++;
212
      }
213
    else
214
      {
215
      newData = new NodeData(++mNextNodeID,newList);
216
      mMapNodeID.put(newList,newData);
217
      }
218

    
219
    return newData;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
// tree isomorphism algorithm
224

    
225
  private void adjustIsomorphism()
226
    {
227
    NodeData newData = retData();
228
    boolean deleteOldFBO = false;
229
    boolean createNewFBO = false;
230

    
231
    if( --mData.numPointingNodes==0 )
232
      {
233
      mMapNodeID.remove(mData.key);
234
      if( mData.mFBO!=null ) deleteOldFBO=true;
235
      }
236
    if( mNumChildren[0]>0 && newData.mFBO==null )
237
      {
238
      createNewFBO = true;
239
      }
240
    if( mNumChildren[0]==0 && newData.mFBO!=null )
241
      {
242
      newData.mFBO.markForDeletion();
243
      android.util.Log.e("NODE", "ERROR!! this NodeData cannot possibly contain a non-null FBO!! "+newData.mFBO.getID() );
244
      newData.mFBO = null;
245
      }
246

    
247
    if( deleteOldFBO && createNewFBO )
248
      {
249
      newData.mFBO = mData.mFBO;  // just copy over
250
      }
251
    else if( deleteOldFBO )
252
      {
253
      mData.mFBO.markForDeletion();
254
      mData.mFBO = null;
255
      }
256
    else if( createNewFBO )
257
      {
258
      int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
259
      int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
260
      newData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
261
      }
262

    
263
    mData = newData;
264

    
265
    if( mParent!=null ) mParent.adjustIsomorphism();
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
// return the total number of render calls issued
270

    
271
  int drawNoBlend(long currTime, DistortedOutputSurface surface)
272
    {
273
    DistortedSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
274

    
275
    if( input.setAsInput() )
276
      {
277
      mState.apply();
278
      GLES31.glDisable(GLES31.GL_BLEND);
279
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
280
      GLES31.glEnable(GLES31.GL_BLEND);
281
      return 1;
282
      }
283

    
284
    return 0;
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
// Use the Order Independent Transparency method to draw a non-postprocessed child.
289

    
290
  int drawOIT(long currTime, DistortedOutputSurface surface)
291
    {
292
    DistortedSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
293

    
294
    if( input.setAsInput() )
295
      {
296
      mState.apply();
297
      mEffects.drawPrivOIT(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
298
      return 1;
299
      }
300

    
301
    return 0;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
// return the total number of render calls issued
306

    
307
  int draw(long currTime, DistortedOutputSurface surface)
308
    {
309
    DistortedSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
310

    
311
    if( input.setAsInput() )
312
      {
313
      mState.apply();
314
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
315
      return 1;
316
      }
317

    
318
    return 0;
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
// return the total number of render calls issued
323

    
324
  int renderRecursive(long currTime)
325
    {
326
    int numRenders = 0;
327

    
328
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
329
      {
330
      mData.currTime = currTime;
331

    
332
      for (int i=0; i<mNumChildren[0]; i++)
333
        {
334
        numRenders += mChildren.get(i).renderRecursive(currTime);
335
        }
336

    
337
      if( mData.mFBO==null )
338
        {
339
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
340
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
341
        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
342
        }
343

    
344
      mData.mFBO.setAsOutput(currTime);
345

    
346
      if( mSurface.setAsInput() )
347
        {
348
        numRenders++;
349
        DistortedEffects.blitPriv(mData.mFBO);
350
        }
351

    
352
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren,0, mRenderWayOIT);
353
      }
354

    
355
    return numRenders;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
  void setSurfaceParent(DistortedOutputSurface dep)
361
    {
362
    mSurfaceParent = dep;
363
    mParent = null;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  void sort()
369
    {
370
    if( mParent!=null )
371
      {
372
      mParent.mChildren.remove(this);
373
      DistortedMaster.addSortingByBuckets(mParent.mChildren,this);
374
      }
375
    else if( mSurfaceParent!=null )
376
      {
377
      ArrayList<DistortedNode> children = mSurfaceParent.getChildren();
378
      children.remove(this);
379
      DistortedMaster.addSortingByBuckets(children,this);
380
      }
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
  EffectQueuePostprocess getPostprocessQueue()
386
    {
387
    return mEffects.getPostprocess();
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
// PUBLIC API
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
/**
394
 * Constructs new Node.
395
 *     
396
 * @param surface InputSurface to put into the new Node.
397
 * @param effects DistortedEffects to put into the new Node.
398
 * @param mesh MeshBase to put into the new Node.
399
 */
400
  public DistortedNode(DistortedSurface surface, DistortedEffects effects, MeshBase mesh)
401
    {
402
    mSurface       = surface;
403
    mEffects       = effects;
404
    mMesh          = mesh;
405
    mState         = new DistortedRenderState();
406
    mChildren      = null;
407
    mNumChildren   = new int[1];
408
    mParent        = null;
409
    mSurfaceParent = null;
410
    mRenderWayOIT  = false;
411

    
412
    mFboW            = 0;  // i.e. take this from
413
    mFboH            = 0;  // mSurface's dimensions
414
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
415

    
416
    mData = retData();
417
    mEffects.newNode(this);
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////  
421
/**
422
 * Copy-constructs new Node from another Node.
423
 *     
424
 * @param node The DistortedNode to copy data from.
425
 * @param flags bit field composed of a subset of the following:
426
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
427
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
428
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
429
 */
430
  public DistortedNode(DistortedNode node, int flags)
431
    {
432
    mEffects      = new DistortedEffects(node.mEffects,flags);
433
    mMesh         = node.mMesh;
434
    mState        = new DistortedRenderState();
435
    mParent       = null;
436
    mSurfaceParent= null;
437
    mRenderWayOIT = false;
438

    
439
    mFboW            = node.mFboW;
440
    mFboH            = node.mFboH;
441
    mFboDepthStencil = node.mFboDepthStencil;
442

    
443
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
444
      {
445
      mSurface = node.mSurface;
446
      }
447
    else
448
      {
449
      int w = node.mSurface.getWidth();
450
      int h = node.mSurface.getHeight();
451

    
452
      if( node.mSurface instanceof DistortedTexture )
453
        {
454
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
455
        }
456
      else if( node.mSurface instanceof DistortedFramebuffer )
457
        {
458
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
459

    
460
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
461
          {
462
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
463
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
464
          }
465

    
466
        mSurface = new DistortedFramebuffer(1,depthStencil,DistortedSurface.TYPE_TREE,w,h);
467
        }
468
      }
469
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
470
      {
471
      if( node.mChildren==null )     // do NOT copy over the NULL!
472
        {
473
        node.mChildren = new ArrayList<>(2);
474
        }
475

    
476
      mChildren = node.mChildren;
477
      mNumChildren = node.mNumChildren;
478
      }
479
    else
480
      {
481
      mChildren = null;
482
      mNumChildren = new int[1];
483
      }
484

    
485
    mData = retData();
486
    mEffects.newNode(this);
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490
  /**
491
   * When rendering this Node, should we use the Order Independent Transparency render more?
492
   * <p>
493
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
494
   * fragments in different ways depending on which fragments get rendered first, or the slower
495
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
496
   *
497
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
498
   */
499
  public void setOrderIndependentTransparency(boolean oit)
500
    {
501
    mRenderWayOIT = oit;
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505
  /**
506
   * When rendering this Node, should we use the Order Independent Transparency render more?
507
   * <p>
508
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
509
   * fragments in different ways depending on which fragments get rendered first, or the slower
510
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
511
   *
512
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
513
   * @param initialSize Initial number of transparent fragments we expect, in screenfulls.
514
   *                    I.e '1.0' means 'the scene we are going to render contains about 1 screen
515
   *                    worth of transparent fragments'. Valid values: 0.0 &lt; initialSize &lt; 10.0
516
   *                    Even if you get this wrong, the library will detect that there are more
517
   *                    transparent fragments than it has space for and readjust its internal buffers,
518
   *                    but only after a few frames during which one will probably see missing objects.
519
   */
520
  public void setOrderIndependentTransparency(boolean oit, float initialSize)
521
    {
522
    mRenderWayOIT = oit;
523

    
524
    if( initialSize>0.0f && initialSize<10.0f )
525
      DistortedEffects.setSSBOSize(initialSize);
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Adds a new child to the last position in the list of our Node's children.
531
 * <p>
532
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
533
 * DistortedMaster (by calling doWork())
534
 *
535
 * @param node The new Node to add.
536
 */
537
  public void attach(DistortedNode node)
538
    {
539
    mJobs.add(new Job(ATTACH,node));
540
    DistortedMaster.newSlave(this);
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
/**
545
 * Adds a new child to the last position in the list of our Node's children.
546
 * <p>
547
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
548
 * DistortedMaster (by calling doWork())
549
 *
550
 * @param surface InputSurface to initialize our child Node with.
551
 * @param effects DistortedEffects to initialize our child Node with.
552
 * @param mesh MeshBase to initialize our child Node with.
553
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
554
 */
555
  public DistortedNode attach(DistortedSurface surface, DistortedEffects effects, MeshBase mesh)
556
    {
557
    DistortedNode node = new DistortedNode(surface,effects,mesh);
558
    mJobs.add(new Job(ATTACH,node));
559
    DistortedMaster.newSlave(this);
560
    return node;
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
/**
565
 * Removes the first occurrence of a specified child from the list of children of our Node.
566
 * <p>
567
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
568
 * DistortedMaster (by calling doWork())
569
 *
570
 * @param node The Node to remove.
571
 */
572
  public void detach(DistortedNode node)
573
    {
574
    mJobs.add(new Job(DETACH,node));
575
    DistortedMaster.newSlave(this);
576
    }
577

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579
/**
580
 * Removes the first occurrence of a specified child from the list of children of our Node.
581
 * <p>
582
 * A bit questionable method as there can be many different Nodes attached as children, some
583
 * of them having the same Effects but - for instance - different Mesh. Use with care.
584
 * <p>
585
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
586
 * DistortedMaster (by calling doWork())
587
 *
588
 * @param effects DistortedEffects to remove.
589
 */
590
  public void detach(DistortedEffects effects)
591
    {
592
    long id = effects.getID();
593
    DistortedNode node;
594
    boolean detached = false;
595

    
596
    for(int i=0; i<mNumChildren[0]; i++)
597
      {
598
      node = mChildren.get(i);
599

    
600
      if( node.getEffects().getID()==id )
601
        {
602
        detached = true;
603
        mJobs.add(new Job(DETACH,node));
604
        DistortedMaster.newSlave(this);
605
        break;
606
        }
607
      }
608

    
609
    if( !detached )
610
      {
611
      // if we failed to detach any, it still might be the case that
612
      // there's an ATTACH job that we need to cancel.
613
      int num = mJobs.size();
614
      Job job;
615

    
616
      for(int i=0; i<num; i++)
617
        {
618
        job = mJobs.get(i);
619

    
620
        if( job.type==ATTACH && job.node.getEffects()==effects )
621
          {
622
          mJobs.remove(i);
623
          break;
624
          }
625
        }
626
      }
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630
/**
631
 * Removes all children Nodes.
632
 * <p>
633
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
634
 * DistortedMaster (by calling doWork())
635
 */
636
  public void detachAll()
637
    {
638
    mJobs.add(new Job(DETALL,null));
639
    DistortedMaster.newSlave(this);
640
    }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643
/**
644
 * This is not really part of the public API. Has to be public only because it is a part of the
645
 * DistortedSlave interface, which should really be a class that we extend here instead but
646
 * Java has no multiple inheritance.
647
 *
648
 * @y.exclude
649
 */
650
  public void doWork()
651
    {
652
    int num = mJobs.size();
653

    
654
    if( num>0 )
655
      {
656
      Job job;
657
      int numChanges=0;
658

    
659
      for(int i=0; i<num; i++)
660
        {
661
        job = mJobs.remove(0);
662

    
663
        switch(job.type)
664
          {
665
          case ATTACH: numChanges++;
666
                       if( mChildren==null ) mChildren = new ArrayList<>(2);
667
                       job.node.mParent = this;
668
                       job.node.mSurfaceParent = null;
669
                       DistortedMaster.addSortingByBuckets(mChildren,job.node);
670
                       mNumChildren[0]++;
671
                       break;
672
          case DETACH: numChanges++;
673
                       if( mNumChildren[0]>0 && mChildren.remove(job.node) )
674
                         {
675
                         job.node.mParent = null;
676
                         job.node.mSurfaceParent = null;
677
                         mNumChildren[0]--;
678
                         }
679
                       break;
680
          case DETALL: numChanges++;
681
                       if( mNumChildren[0]>0 )
682
                         {
683
                         DistortedNode tmp;
684

    
685
                         for(int j=mNumChildren[0]-1; j>=0; j--)
686
                           {
687
                           tmp = mChildren.remove(j);
688
                           tmp.mParent = null;
689
                           tmp.mSurfaceParent = null;
690
                           }
691

    
692
                         mNumChildren[0] = 0;
693
                         }
694
                       break;
695
          case SORT  : mChildren.remove(job.node);
696
                       DistortedMaster.addSortingByBuckets(mChildren,job.node);
697
                       break;
698
          }
699
        }
700
      if( numChanges>0 ) adjustIsomorphism();
701
      }
702
    }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705
/**
706
 * Returns the DistortedEffects object that's in the Node.
707
 * 
708
 * @return The DistortedEffects contained in the Node.
709
 */
710
  public DistortedEffects getEffects()
711
    {
712
    return mEffects;
713
    }
714

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716
/**
717
 * Returns the DistortedSurface object that's in the Node.
718
 *
719
 * @return The DistortedSurface contained in the Node.
720
 */
721
  public DistortedSurface getSurface()
722
    {
723
    return mSurface;
724
    }
725

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727
  /**
728
   * Returns the DistortedSurface object that's in the Node.
729
   *
730
   * @return The DistortedSurface contained in the Node (if a leaf), or the FBO (if an internal Node)
731
   */
732
  public DistortedSurface getInternalSurface()
733
    {
734
    return mNumChildren[0]==0 ? mSurface : mData.mFBO;
735
    }
736

    
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738
/**
739
 * Returns the Mesh object that's in the Node.
740
 *
741
 * @return Mesh contained in the Node.
742
 */
743
  public MeshBase getMesh()
744
    {
745
    return mMesh;
746
    }
747

    
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749
/**
750
 * Resizes the DistortedFramebuffer object that we render this Node to.
751
 */
752
  public void resize(int width, int height)
753
    {
754
    mFboW = width;
755
    mFboH = height;
756

    
757
    if ( mData.mFBO !=null )
758
      {
759
      // TODO: potentially allocate a new NodeData if we have to
760
      mData.mFBO.resize(width,height);
761
      }
762
    }
763

    
764
///////////////////////////////////////////////////////////////////////////////////////////////////
765
/**
766
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
767
 */
768
  public void enableDepthStencil(int depthStencil)
769
    {
770
    mFboDepthStencil = depthStencil;
771

    
772
    if ( mData.mFBO !=null )
773
      {
774
      // TODO: potentially allocate a new NodeData if we have to
775
      mData.mFBO.enableDepthStencil(depthStencil);
776
      }
777
    }
778

    
779
///////////////////////////////////////////////////////////////////////////////////////////////////
780
// APIs that control how to set the OpenGL state just before rendering this Node.
781
///////////////////////////////////////////////////////////////////////////////////////////////////
782
/**
783
 * When rendering this Node, use ColorMask (r,g,b,a).
784
 *
785
 * @param r Write to the RED color channel when rendering this Node?
786
 * @param g Write to the GREEN color channel when rendering this Node?
787
 * @param b Write to the BLUE color channel when rendering this Node?
788
 * @param a Write to the ALPHA channel when rendering this Node?
789
 */
790
  @SuppressWarnings("unused")
791
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
792
    {
793
    mState.glColorMask(r,g,b,a);
794
    }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797
/**
798
 * When rendering this Node, switch on writing to Depth buffer?
799
 *
800
 * @param mask Write to the Depth buffer when rendering this Node?
801
 */
802
  @SuppressWarnings("unused")
803
  public void glDepthMask(boolean mask)
804
    {
805
    mState.glDepthMask(mask);
806
    }
807

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810
 * When rendering this Node, which bits of the Stencil buffer to write to?
811
 *
812
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
813
 */
814
  @SuppressWarnings("unused")
815
  public void glStencilMask(int mask)
816
    {
817
    mState.glStencilMask(mask);
818
    }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821
/**
822
 * When rendering this Node, which Tests to enable?
823
 *
824
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
825
 */
826
  @SuppressWarnings("unused")
827
  public void glEnable(int test)
828
    {
829
    mState.glEnable(test);
830
    }
831

    
832
///////////////////////////////////////////////////////////////////////////////////////////////////
833
/**
834
 * When rendering this Node, which Tests to enable?
835
 *
836
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
837
 */
838
  @SuppressWarnings("unused")
839
  public void glDisable(int test)
840
    {
841
    mState.glDisable(test);
842
    }
843

    
844
///////////////////////////////////////////////////////////////////////////////////////////////////
845
/**
846
 * When rendering this Node, use the following StencilFunc.
847
 *
848
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
849
 * @param ref  Reference valut to compare our stencil with.
850
 * @param mask Mask used when comparing.
851
 */
852
  @SuppressWarnings("unused")
853
  public void glStencilFunc(int func, int ref, int mask)
854
    {
855
    mState.glStencilFunc(func,ref,mask);
856
    }
857

    
858
///////////////////////////////////////////////////////////////////////////////////////////////////
859
/**
860
 * When rendering this Node, use the following StencilOp.
861
 * <p>
862
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
863
 *
864
 * @param sfail  What to do when Stencil Test fails.
865
 * @param dpfail What to do when Depth Test fails.
866
 * @param dppass What to do when Depth Test passes.
867
 */
868
  @SuppressWarnings("unused")
869
  public void glStencilOp(int sfail, int dpfail, int dppass)
870
    {
871
    mState.glStencilOp(sfail,dpfail,dppass);
872
    }
873

    
874
///////////////////////////////////////////////////////////////////////////////////////////////////
875
/**
876
 * When rendering this Node, use the following DepthFunc.
877
 *
878
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
879
 */
880
  @SuppressWarnings("unused")
881
  public void glDepthFunc(int func)
882
    {
883
    mState.glDepthFunc(func);
884
    }
885

    
886
///////////////////////////////////////////////////////////////////////////////////////////////////
887
/**
888
 * When rendering this Node, use the following Blending mode.
889
 * <p>
890
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
891
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
892
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
893
 *
894
 * @param src Source Blend function
895
 * @param dst Destination Blend function
896
 */
897
  @SuppressWarnings("unused")
898
  public void glBlendFunc(int src, int dst)
899
    {
900
    mState.glBlendFunc(src,dst);
901
    }
902

    
903
///////////////////////////////////////////////////////////////////////////////////////////////////
904
/**
905
 * Before rendering this Node, clear the following buffers.
906
 * <p>
907
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
908
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
909
 * Default: 0
910
 *
911
 * @param mask bitwise OR of BUFFER_BITs to clear.
912
 */
913
  @SuppressWarnings("unused")
914
  public void glClear(int mask)
915
    {
916
    mState.glClear(mask);
917
    }
918
  }
(6-6/17)