Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ c1a38ba3

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 java.util.ArrayList;
25
import java.util.Collections;
26
import java.util.HashMap;
27

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

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

    
49
  private class Job
50
    {
51
    int type;
52
    DistortedNode node;
53

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

    
61
  private ArrayList<Job> mJobs = new ArrayList<>();
62

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

    
66
  private boolean mRenderWayOIT;
67
  private DistortedNode mParent;
68
  private DistortedOutputSurface mSurfaceParent;
69
  private MeshObject mMesh;
70
  private DistortedEffects mEffects;
71
  private DistortedInputSurface mSurface;
72
  private DistortedRenderState mState;
73
  private NodeData mData;
74
  private int mFboW, mFboH, mFboDepthStencil;
75

    
76
  private class NodeData
77
    {
78
    long ID;
79
    int numPointingNodes;
80
    long currTime;
81
    ArrayList<Long> key;
82
    DistortedFramebuffer mFBO;
83

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

    
96
  static synchronized void onPause()
97
    {
98
    NodeData data;
99

    
100
    for (HashMap.Entry<ArrayList<Long>,NodeData> entry : mMapNodeID.entrySet())
101
      {
102
      data = entry.getValue();
103

    
104
      if( data.mFBO != null )
105
        {
106
        data.mFBO.markForDeletion();
107
        data.mFBO = null;
108
        }
109
      }
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  static synchronized void onDestroy()
115
    {
116
    mNextNodeID = 0;
117
    mMapNodeID.clear();
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private ArrayList<Long> generateIDList()
123
    {
124
    ArrayList<Long> ret = new ArrayList<>();
125

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

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

    
159
    ret.add( 0, mSurface.getID() );
160

    
161
    return ret;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// Debug - print all the Node IDs
166

    
167
  @SuppressWarnings("unused")
168
  void debug(int depth)
169
    {
170
    String tmp="";
171
    int i;
172

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

    
178
    android.util.Log.e("NODE", tmp);
179

    
180
    for(i=0; i<mNumChildren[0]; i++)
181
      mChildren.get(i).debug(depth+1);
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// Debug - print contents of the HashMap
186

    
187
  @SuppressWarnings("unused")
188
  static void debugMap()
189
    {
190
    NodeData tmp;
191

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
// tree isomorphism algorithm
201

    
202
  private void adjustIsomorphism()
203
    {
204
    ArrayList<Long> newList = generateIDList();
205
    NodeData newData = mMapNodeID.get(newList);
206

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

    
217
    boolean deleteOldFBO = false;
218
    boolean createNewFBO = false;
219

    
220
    if( --mData.numPointingNodes==0 )
221
      {
222
      mMapNodeID.remove(mData.key);
223
      if( mData.mFBO!=null ) deleteOldFBO=true;
224
      }
225
    if( mNumChildren[0]>0 && newData.mFBO==null )
226
      {
227
      createNewFBO = true;
228
      }
229
    if( mNumChildren[0]==0 && newData.mFBO!=null )
230
      {
231
      newData.mFBO.markForDeletion();
232
      android.util.Log.e("NODE", "ERROR!! this NodeData cannot possibly contain a non-null FBO!! "+newData.mFBO.getID() );
233
      newData.mFBO = null;
234
      }
235

    
236
    if( deleteOldFBO && createNewFBO )
237
      {
238
      newData.mFBO = mData.mFBO;  // just copy over
239
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
240
      }
241
    else if( deleteOldFBO )
242
      {
243
      mData.mFBO.markForDeletion();
244
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
245
      mData.mFBO = null;
246
      }
247
    else if( createNewFBO )
248
      {
249
      int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
250
      int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
251
      newData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
252
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
253
      }
254

    
255
    mData = newData;
256

    
257
    if( mParent!=null ) mParent.adjustIsomorphism();
258
    }
259

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

    
262
  int markStencilAndDepth(long currTime, DistortedOutputSurface surface, EffectQueuePostprocess queue)
263
    {
264
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
265

    
266
    if( input.setAsInput() )
267
      {
268
      DistortedRenderState.setUpStencilMark();
269
      mEffects.drawPriv(mSurface.getWidth() /2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, queue.getHalo()*surface.mMipmap);
270
      DistortedRenderState.unsetUpStencilMark();
271

    
272
      return 1;
273
      }
274
    return 0;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// return the total number of render calls issued
279

    
280
  int drawNoBlend(long currTime, DistortedOutputSurface surface)
281
    {
282
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
283

    
284
    if( input.setAsInput() )
285
      {
286
      mState.apply();
287
      GLES31.glDisable(GLES31.GL_BLEND);
288
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
289
      GLES31.glEnable(GLES31.GL_BLEND);
290
      return 1;
291
      }
292

    
293
    return 0;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
// Use the Order Independent Transparency method to draw a non-postprocessed child.
298

    
299
  int drawOIT(long currTime, DistortedOutputSurface surface)
300
    {
301
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
302

    
303
    if( input.setAsInput() )
304
      {
305
      mState.apply();
306
      mEffects.drawPrivOIT(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
307
      return 1;
308
      }
309

    
310
    return 0;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
// return the total number of render calls issued
315

    
316
  int draw(long currTime, DistortedOutputSurface surface)
317
    {
318
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
319

    
320
    if( input.setAsInput() )
321
      {
322
      mState.apply();
323
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
324
      return 1;
325
      }
326

    
327
    return 0;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// return the total number of render calls issued
332

    
333
  int renderRecursive(long currTime)
334
    {
335
    int numRenders = 0;
336

    
337
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
338
      {
339
      mData.currTime = currTime;
340

    
341
      for (int i=0; i<mNumChildren[0]; i++)
342
        {
343
        numRenders += mChildren.get(i).renderRecursive(currTime);
344
        }
345

    
346
      if( mData.mFBO==null )
347
        {
348
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
349
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
350
        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
351
        }
352

    
353
      mData.mFBO.setAsOutput(currTime);
354

    
355
      if( mSurface.setAsInput() )
356
        {
357
        numRenders++;
358
        DistortedEffects.blitPriv(mData.mFBO);
359
        }
360

    
361
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren,0, mRenderWayOIT);
362
      }
363

    
364
    return numRenders;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  void setSurfaceParent(DistortedOutputSurface dep)
370
    {
371
    mSurfaceParent = dep;
372
    mParent = null;
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  void sort()
378
    {
379
    if( mParent!=null )
380
      {
381
      mParent.mChildren.remove(this);
382
      DistortedMaster.addSortingByBuckets(mParent.mChildren,this);
383
      }
384
    else if( mSurfaceParent!=null )
385
      {
386
      ArrayList<DistortedNode> children = mSurfaceParent.getChildren();
387
      children.remove(this);
388
      DistortedMaster.addSortingByBuckets(children,this);
389
      }
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
/**
394
 * Not part of the Public API.
395
 *
396
 * @y.exclude
397
 */
398
  public EffectQueuePostprocess getPostprocessQueue()
399
    {
400
    return mEffects.getPostprocess();
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404
// PUBLIC API
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
/**
407
 * Constructs new Node.
408
 *     
409
 * @param surface InputSurface to put into the new Node.
410
 * @param effects DistortedEffects to put into the new Node.
411
 * @param mesh MeshObject to put into the new Node.
412
 */
413
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
414
    {
415
    mSurface       = surface;
416
    mEffects       = effects;
417
    mMesh          = mesh;
418
    mState         = new DistortedRenderState();
419
    mChildren      = null;
420
    mNumChildren   = new int[1];
421
    mNumChildren[0]= 0;
422
    mParent        = null;
423
    mSurfaceParent = null;
424
    mRenderWayOIT  = false;
425

    
426
    mFboW            = 0;  // i.e. take this from
427
    mFboH            = 0;  // mSurface's dimensions
428
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
429

    
430
    ArrayList<Long> list = new ArrayList<>();
431
    list.add(mSurface.getID());
432
    list.add(-mEffects.getID());
433

    
434
    mData = mMapNodeID.get(list);
435
   
436
    if( mData!=null )
437
      {
438
      mData.numPointingNodes++;
439
      }
440
    else
441
      {
442
      mData = new NodeData(++mNextNodeID,list);
443
      mMapNodeID.put(list, mData);
444
      }
445

    
446
    mEffects.newNode(this);
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////  
450
/**
451
 * Copy-constructs new Node from another Node.
452
 *     
453
 * @param node The DistortedNode to copy data from.
454
 * @param flags bit field composed of a subset of the following:
455
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
456
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
457
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
458
 */
459
  public DistortedNode(DistortedNode node, int flags)
460
    {
461
    mEffects      = new DistortedEffects(node.mEffects,flags);
462
    mMesh         = node.mMesh;
463
    mState        = new DistortedRenderState();
464
    mParent       = null;
465
    mSurfaceParent= null;
466
    mRenderWayOIT = false;
467

    
468
    mFboW            = node.mFboW;
469
    mFboH            = node.mFboH;
470
    mFboDepthStencil = node.mFboDepthStencil;
471

    
472
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
473
      {
474
      mSurface = node.mSurface;
475
      }
476
    else
477
      {
478
      int w = node.mSurface.getWidth();
479
      int h = node.mSurface.getHeight();
480

    
481
      if( node.mSurface instanceof DistortedTexture )
482
        {
483
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
484
        }
485
      else if( node.mSurface instanceof DistortedFramebuffer )
486
        {
487
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
488

    
489
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
490
          {
491
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
492
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
493
          }
494

    
495
        mSurface = new DistortedFramebuffer(1,depthStencil,DistortedSurface.TYPE_TREE,w,h);
496
        }
497
      }
498
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
499
      {
500
      if( node.mChildren==null )     // do NOT copy over the NULL!
501
        {
502
        node.mChildren = new ArrayList<>(2);
503
        }
504

    
505
      mChildren = node.mChildren;
506
      mNumChildren = node.mNumChildren;
507
      }
508
    else
509
      {
510
      mChildren = null;
511
      mNumChildren = new int[1];
512
      mNumChildren[0] = 0;
513
      }
514
   
515
    ArrayList<Long> list = generateIDList();
516
   
517
    mData = mMapNodeID.get(list);
518
   
519
    if( mData!=null )
520
      {
521
      mData.numPointingNodes++;
522
      }
523
    else
524
      {
525
      mData = new NodeData(++mNextNodeID,list);
526
      mMapNodeID.put(list, mData);
527
      }
528

    
529
    mEffects.newNode(this);
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533
  /**
534
   * When rendering this Node, should we use the Order Independent Transparency render more?
535
   * <p>
536
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
537
   * fragments in different ways depending on which fragments get rendered first, or the slower
538
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
539
   *
540
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
541
   */
542
  public void setOrderIndependentTransparency(boolean oit)
543
    {
544
    mRenderWayOIT = oit;
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548
/**
549
 * Adds a new child to the last position in the list of our Node's children.
550
 * <p>
551
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
552
 * DistortedMaster (by calling doWork())
553
 *
554
 * @param node The new Node to add.
555
 */
556
  public void attach(DistortedNode node)
557
    {
558
    mJobs.add(new Job(ATTACH,node));
559
    DistortedMaster.newSlave(this);
560
    }
561

    
562
///////////////////////////////////////////////////////////////////////////////////////////////////
563
/**
564
 * Adds a new child to the last position in the list of our Node's children.
565
 * <p>
566
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
567
 * DistortedMaster (by calling doWork())
568
 *
569
 * @param surface InputSurface to initialize our child Node with.
570
 * @param effects DistortedEffects to initialize our child Node with.
571
 * @param mesh MeshObject to initialize our child Node with.
572
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
573
 */
574
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
575
    {
576
    DistortedNode node = new DistortedNode(surface,effects,mesh);
577
    mJobs.add(new Job(ATTACH,node));
578
    DistortedMaster.newSlave(this);
579
    return node;
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
/**
584
 * Removes the first occurrence of a specified child from the list of children of our Node.
585
 * <p>
586
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
587
 * DistortedMaster (by calling doWork())
588
 *
589
 * @param node The Node to remove.
590
 */
591
  public void detach(DistortedNode node)
592
    {
593
    mJobs.add(new Job(DETACH,node));
594
    DistortedMaster.newSlave(this);
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
/**
599
 * Removes the first occurrence of a specified child from the list of children of our Node.
600
 * <p>
601
 * A bit questionable method as there can be many different Nodes attached as children, some
602
 * of them having the same Effects but - for instance - different Mesh. Use with care.
603
 * <p>
604
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
605
 * DistortedMaster (by calling doWork())
606
 *
607
 * @param effects DistortedEffects to remove.
608
 */
609
  public void detach(DistortedEffects effects)
610
    {
611
    long id = effects.getID();
612
    DistortedNode node;
613
    boolean detached = false;
614

    
615
    for(int i=0; i<mNumChildren[0]; i++)
616
      {
617
      node = mChildren.get(i);
618

    
619
      if( node.getEffects().getID()==id )
620
        {
621
        detached = true;
622
        mJobs.add(new Job(DETACH,node));
623
        DistortedMaster.newSlave(this);
624
        break;
625
        }
626
      }
627

    
628
    if( !detached )
629
      {
630
      // if we failed to detach any, it still might be the case that
631
      // there's an ATTACH job that we need to cancel.
632
      int num = mJobs.size();
633
      Job job;
634

    
635
      for(int i=0; i<num; i++)
636
        {
637
        job = mJobs.get(i);
638

    
639
        if( job.type==ATTACH && job.node.getEffects()==effects )
640
          {
641
          mJobs.remove(i);
642
          break;
643
          }
644
        }
645
      }
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
/**
650
 * Removes all children Nodes.
651
 * <p>
652
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
653
 * DistortedMaster (by calling doWork())
654
 */
655
  public void detachAll()
656
    {
657
    mJobs.add(new Job(DETALL,null));
658
    DistortedMaster.newSlave(this);
659
    }
660

    
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662
/**
663
 * This is not really part of the public API. Has to be public only because it is a part of the
664
 * DistortedSlave interface, which should really be a class that we extend here instead but
665
 * Java has no multiple inheritance.
666
 *
667
 * @y.exclude
668
 */
669
  public void doWork()
670
    {
671
    int num = mJobs.size();
672
    Job job;
673

    
674
    int numChanges=0;
675

    
676
    for(int i=0; i<num; i++)
677
      {
678
      job = mJobs.remove(0);
679

    
680
      switch(job.type)
681
        {
682
        case ATTACH: numChanges++;
683
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
684
                     job.node.mParent = this;
685
                     job.node.mSurfaceParent = null;
686
                     DistortedMaster.addSortingByBuckets(mChildren,job.node);
687
                     mNumChildren[0]++;
688
                     break;
689
        case DETACH: numChanges++;
690
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
691
                       {
692
                       job.node.mParent = null;
693
                       job.node.mSurfaceParent = null;
694
                       mNumChildren[0]--;
695
                       }
696
                     break;
697
        case DETALL: numChanges++;
698
                     if( mNumChildren[0]>0 )
699
                       {
700
                       DistortedNode tmp;
701

    
702
                       for(int j=mNumChildren[0]-1; j>=0; j--)
703
                         {
704
                         tmp = mChildren.remove(j);
705
                         tmp.mParent = null;
706
                         tmp.mSurfaceParent = null;
707
                         }
708

    
709
                       mNumChildren[0] = 0;
710
                       }
711
                     break;
712
        case SORT  : mChildren.remove(job.node);
713
                     DistortedMaster.addSortingByBuckets(mChildren,job.node);
714
                     break;
715
        }
716
      }
717

    
718
    if( numChanges>0 ) adjustIsomorphism();
719
    }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722
/**
723
 * Returns the DistortedEffects object that's in the Node.
724
 * 
725
 * @return The DistortedEffects contained in the Node.
726
 */
727
  public DistortedEffects getEffects()
728
    {
729
    return mEffects;
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733
/**
734
 * Returns the DistortedInputSurface object that's in the Node.
735
 *
736
 * @return The DistortedInputSurface contained in the Node.
737
 */
738
  public DistortedInputSurface getSurface()
739
    {
740
    return mSurface;
741
    }
742

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744
/**
745
 * Returns the Mesh object that's in the Node.
746
 *
747
 * @return Mesh contained in the Node.
748
 */
749
  public MeshObject getMesh()
750
    {
751
    return mMesh;
752
    }
753

    
754
///////////////////////////////////////////////////////////////////////////////////////////////////
755
/**
756
 * Resizes the DistortedFramebuffer object that we render this Node to.
757
 */
758
  public void resize(int width, int height)
759
    {
760
    mFboW = width;
761
    mFboH = height;
762

    
763
    if ( mData.mFBO !=null )
764
      {
765
      // TODO: potentially allocate a new NodeData if we have to
766
      mData.mFBO.resize(width,height);
767
      }
768
    }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771
/**
772
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
773
 */
774
  public void enableDepthStencil(int depthStencil)
775
    {
776
    mFboDepthStencil = depthStencil;
777

    
778
    if ( mData.mFBO !=null )
779
      {
780
      // TODO: potentially allocate a new NodeData if we have to
781
      mData.mFBO.enableDepthStencil(depthStencil);
782
      }
783
    }
784

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

    
802
///////////////////////////////////////////////////////////////////////////////////////////////////
803
/**
804
 * When rendering this Node, switch on writing to Depth buffer?
805
 *
806
 * @param mask Write to the Depth buffer when rendering this Node?
807
 */
808
  @SuppressWarnings("unused")
809
  public void glDepthMask(boolean mask)
810
    {
811
    mState.glDepthMask(mask);
812
    }
813

    
814
///////////////////////////////////////////////////////////////////////////////////////////////////
815
/**
816
 * When rendering this Node, which bits of the Stencil buffer to write to?
817
 *
818
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
819
 */
820
  @SuppressWarnings("unused")
821
  public void glStencilMask(int mask)
822
    {
823
    mState.glStencilMask(mask);
824
    }
825

    
826
///////////////////////////////////////////////////////////////////////////////////////////////////
827
/**
828
 * When rendering this Node, which Tests to enable?
829
 *
830
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
831
 */
832
  @SuppressWarnings("unused")
833
  public void glEnable(int test)
834
    {
835
    mState.glEnable(test);
836
    }
837

    
838
///////////////////////////////////////////////////////////////////////////////////////////////////
839
/**
840
 * When rendering this Node, which Tests to enable?
841
 *
842
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
843
 */
844
  @SuppressWarnings("unused")
845
  public void glDisable(int test)
846
    {
847
    mState.glDisable(test);
848
    }
849

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

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

    
880
///////////////////////////////////////////////////////////////////////////////////////////////////
881
/**
882
 * When rendering this Node, use the following DepthFunc.
883
 *
884
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
885
 */
886
  @SuppressWarnings("unused")
887
  public void glDepthFunc(int func)
888
    {
889
    mState.glDepthFunc(func);
890
    }
891

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

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