Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ 12f9e4bb

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 DistortedSurface 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 onDestroy()
97
    {
98
    mNextNodeID = 0;
99
    mMapNodeID.clear();
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private ArrayList<Long> generateIDList()
105
    {
106
    ArrayList<Long> ret = new ArrayList<>();
107

    
108
    if( mNumChildren[0]==0 )
109
      {
110
      // add a negative number so this leaf never gets confused with a internal node
111
      // with a single child that happens to have ID identical to some leaf's Effects ID.
112
      ret.add(-mEffects.getID());
113
      }
114
    else
115
      {
116
      DistortedNode node;
117
   
118
      for(int i=0; i<mNumChildren[0]; i++)
119
        {
120
        node = mChildren.get(i);
121
        ret.add(node.mData.ID);
122
        }
123

    
124
      // A bit questionable decision here - we are sorting the children IDs, which means
125
      // that order in which we draw the children is going to be undefined (well, this is not
126
      // strictly speaking true - when rendering, if no postprocessing and isomorphism are
127
      // involved, we *DO* render the children in order they were added; if however there
128
      // are two internal nodes with the same list of identical children, just added in a
129
      // different order each time, then we consider them isomorphic, i.e. identical and only
130
      // render the first one. If then two children of such 'pseudo-isomorphic' nodes are at
131
      // exactly the same Z-height this might result in some unexpected sights).
132
      //
133
      // Reason: with the children being sorted by postprocessing buckets, the order is
134
      // undefined anyway (although only when postprocessing is applied).
135
      //
136
      // See the consequences in the 'Olympic' app - remove a few leaves and add them back in
137
      // different order. You will see the number of renders go back to the original 15.
138
      Collections.sort(ret);
139
      }
140

    
141
    ret.add( 0, mSurface.getID() );
142

    
143
    return ret;
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
// Debug - print all the Node IDs
148

    
149
  @SuppressWarnings("unused")
150
  void debug(int depth)
151
    {
152
    String tmp="";
153
    int i;
154

    
155
    for(i=0; i<depth; i++) tmp +="   ";
156
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
157
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
158
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
159

    
160
    android.util.Log.e("NODE", tmp);
161

    
162
    for(i=0; i<mNumChildren[0]; i++)
163
      mChildren.get(i).debug(depth+1);
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// Debug - print contents of the HashMap
168

    
169
  @SuppressWarnings("unused")
170
  static void debugMap()
171
    {
172
    NodeData tmp;
173

    
174
    for(ArrayList<Long> key: mMapNodeID.keySet())
175
      {
176
      tmp = mMapNodeID.get(key);
177
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// tree isomorphism algorithm
183

    
184
  private void adjustIsomorphism()
185
    {
186
    ArrayList<Long> newList = generateIDList();
187
    NodeData newData = mMapNodeID.get(newList);
188

    
189
    if( newData!=null )
190
      {
191
      newData.numPointingNodes++;
192
      }
193
    else
194
      {
195
      newData = new NodeData(++mNextNodeID,newList);
196
      mMapNodeID.put(newList,newData);
197
      }
198

    
199
    boolean deleteOldFBO = false;
200
    boolean createNewFBO = false;
201

    
202
    if( --mData.numPointingNodes==0 )
203
      {
204
      mMapNodeID.remove(mData.key);
205
      if( mData.mFBO!=null ) deleteOldFBO=true;
206
      }
207
    if( mNumChildren[0]>0 && newData.mFBO==null )
208
      {
209
      createNewFBO = true;
210
      }
211
    if( mNumChildren[0]==0 && newData.mFBO!=null )
212
      {
213
      newData.mFBO.markForDeletion();
214
      android.util.Log.e("NODE", "ERROR!! this NodeData cannot possibly contain a non-null FBO!! "+newData.mFBO.getID() );
215
      newData.mFBO = null;
216
      }
217

    
218
    if( deleteOldFBO && createNewFBO )
219
      {
220
      newData.mFBO = mData.mFBO;  // just copy over
221
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
222
      }
223
    else if( deleteOldFBO )
224
      {
225
      mData.mFBO.markForDeletion();
226
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
227
      mData.mFBO = null;
228
      }
229
    else if( createNewFBO )
230
      {
231
      int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
232
      int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
233
      newData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
234
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
235
      }
236

    
237
    mData = newData;
238

    
239
    if( mParent!=null ) mParent.adjustIsomorphism();
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  int markStencilAndDepth(long currTime, DistortedOutputSurface surface, EffectQueuePostprocess queue)
245
    {
246
    DistortedSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
247

    
248
    if( input.setAsInput() )
249
      {
250
      DistortedRenderState.setUpStencilMark();
251
      mEffects.drawPriv(mSurface.getWidth() /2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, queue.getHalo()*surface.mMipmap);
252
      DistortedRenderState.unsetUpStencilMark();
253

    
254
      return 1;
255
      }
256
    return 0;
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
// return the total number of render calls issued
261

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

    
266
    if( input.setAsInput() )
267
      {
268
      mState.apply();
269
      GLES31.glDisable(GLES31.GL_BLEND);
270
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
271
      GLES31.glEnable(GLES31.GL_BLEND);
272
      return 1;
273
      }
274

    
275
    return 0;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
// Use the Order Independent Transparency method to draw a non-postprocessed child.
280

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

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

    
292
    return 0;
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
// return the total number of render calls issued
297

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

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

    
309
    return 0;
310
    }
311

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

    
315
  int renderRecursive(long currTime)
316
    {
317
    int numRenders = 0;
318

    
319
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
320
      {
321
      mData.currTime = currTime;
322

    
323
      for (int i=0; i<mNumChildren[0]; i++)
324
        {
325
        numRenders += mChildren.get(i).renderRecursive(currTime);
326
        }
327

    
328
      if( mData.mFBO==null )
329
        {
330
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
331
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
332
        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
333
        }
334

    
335
      mData.mFBO.setAsOutput(currTime);
336

    
337
      if( mSurface.setAsInput() )
338
        {
339
        numRenders++;
340
        DistortedEffects.blitPriv(mData.mFBO);
341
        }
342

    
343
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren,0, mRenderWayOIT);
344
      }
345

    
346
    return numRenders;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  void setSurfaceParent(DistortedOutputSurface dep)
352
    {
353
    mSurfaceParent = dep;
354
    mParent = null;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  void sort()
360
    {
361
    if( mParent!=null )
362
      {
363
      mParent.mChildren.remove(this);
364
      DistortedMaster.addSortingByBuckets(mParent.mChildren,this);
365
      }
366
    else if( mSurfaceParent!=null )
367
      {
368
      ArrayList<DistortedNode> children = mSurfaceParent.getChildren();
369
      children.remove(this);
370
      DistortedMaster.addSortingByBuckets(children,this);
371
      }
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
/**
376
 * Not part of the Public API.
377
 *
378
 * @y.exclude
379
 */
380
  public EffectQueuePostprocess getPostprocessQueue()
381
    {
382
    return mEffects.getPostprocess();
383
    }
384

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

    
408
    mFboW            = 0;  // i.e. take this from
409
    mFboH            = 0;  // mSurface's dimensions
410
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
411

    
412
    ArrayList<Long> list = new ArrayList<>();
413
    list.add(mSurface.getID());
414
    list.add(-mEffects.getID());
415

    
416
    mData = mMapNodeID.get(list);
417
   
418
    if( mData!=null )
419
      {
420
      mData.numPointingNodes++;
421
      }
422
    else
423
      {
424
      mData = new NodeData(++mNextNodeID,list);
425
      mMapNodeID.put(list, mData);
426
      }
427

    
428
    mEffects.newNode(this);
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////  
432
/**
433
 * Copy-constructs new Node from another Node.
434
 *     
435
 * @param node The DistortedNode to copy data from.
436
 * @param flags bit field composed of a subset of the following:
437
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
438
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
439
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
440
 */
441
  public DistortedNode(DistortedNode node, int flags)
442
    {
443
    mEffects      = new DistortedEffects(node.mEffects,flags);
444
    mMesh         = node.mMesh;
445
    mState        = new DistortedRenderState();
446
    mParent       = null;
447
    mSurfaceParent= null;
448
    mRenderWayOIT = false;
449

    
450
    mFboW            = node.mFboW;
451
    mFboH            = node.mFboH;
452
    mFboDepthStencil = node.mFboDepthStencil;
453

    
454
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
455
      {
456
      mSurface = node.mSurface;
457
      }
458
    else
459
      {
460
      int w = node.mSurface.getWidth();
461
      int h = node.mSurface.getHeight();
462

    
463
      if( node.mSurface instanceof DistortedTexture )
464
        {
465
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
466
        }
467
      else if( node.mSurface instanceof DistortedFramebuffer )
468
        {
469
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
470

    
471
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
472
          {
473
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
474
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
475
          }
476

    
477
        mSurface = new DistortedFramebuffer(1,depthStencil,DistortedSurface.TYPE_TREE,w,h);
478
        }
479
      }
480
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
481
      {
482
      if( node.mChildren==null )     // do NOT copy over the NULL!
483
        {
484
        node.mChildren = new ArrayList<>(2);
485
        }
486

    
487
      mChildren = node.mChildren;
488
      mNumChildren = node.mNumChildren;
489
      }
490
    else
491
      {
492
      mChildren = null;
493
      mNumChildren = new int[1];
494
      mNumChildren[0] = 0;
495
      }
496
   
497
    ArrayList<Long> list = generateIDList();
498
   
499
    mData = mMapNodeID.get(list);
500
   
501
    if( mData!=null )
502
      {
503
      mData.numPointingNodes++;
504
      }
505
    else
506
      {
507
      mData = new NodeData(++mNextNodeID,list);
508
      mMapNodeID.put(list, mData);
509
      }
510

    
511
    mEffects.newNode(this);
512
    }
513

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
  /**
516
   * When rendering this Node, should we use the Order Independent Transparency render more?
517
   * <p>
518
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
519
   * fragments in different ways depending on which fragments get rendered first, or the slower
520
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
521
   *
522
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
523
   */
524
  public void setOrderIndependentTransparency(boolean oit)
525
    {
526
    mRenderWayOIT = oit;
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
  /**
531
   * When rendering this Node, should we use the Order Independent Transparency render more?
532
   * <p>
533
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
534
   * fragments in different ways depending on which fragments get rendered first, or the slower
535
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
536
   *
537
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
538
   * @param initialSize Initial number of transparent fragments we expect, in screenfulls.
539
   *                    I.e '1.0' means 'the scene we are going to render contains about 1 screen
540
   *                    worth of transparent fragments'. Valid values: 0.0 &lt; initialSize &lt; 10.0
541
   *                    Even if you get this wrong, the library will detect that there are more
542
   *                    transparent fragments than it has space for and readjust its internal buffers,
543
   *                    but only after a few frames during which one will probably see missing objects.
544
   */
545
  public void setOrderIndependentTransparency(boolean oit, float initialSize)
546
    {
547
    mRenderWayOIT = oit;
548

    
549
    if( initialSize>0.0f && initialSize<10.0f )
550
      DistortedEffects.setSSBOSize(initialSize);
551
    }
552

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

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

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

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

    
621
    for(int i=0; i<mNumChildren[0]; i++)
622
      {
623
      node = mChildren.get(i);
624

    
625
      if( node.getEffects().getID()==id )
626
        {
627
        detached = true;
628
        mJobs.add(new Job(DETACH,node));
629
        DistortedMaster.newSlave(this);
630
        break;
631
        }
632
      }
633

    
634
    if( !detached )
635
      {
636
      // if we failed to detach any, it still might be the case that
637
      // there's an ATTACH job that we need to cancel.
638
      int num = mJobs.size();
639
      Job job;
640

    
641
      for(int i=0; i<num; i++)
642
        {
643
        job = mJobs.get(i);
644

    
645
        if( job.type==ATTACH && job.node.getEffects()==effects )
646
          {
647
          mJobs.remove(i);
648
          break;
649
          }
650
        }
651
      }
652
    }
653

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

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

    
680
    int numChanges=0;
681

    
682
    for(int i=0; i<num; i++)
683
      {
684
      job = mJobs.remove(0);
685

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

    
708
                       for(int j=mNumChildren[0]-1; j>=0; j--)
709
                         {
710
                         tmp = mChildren.remove(j);
711
                         tmp.mParent = null;
712
                         tmp.mSurfaceParent = null;
713
                         }
714

    
715
                       mNumChildren[0] = 0;
716
                       }
717
                     break;
718
        case SORT  : mChildren.remove(job.node);
719
                     DistortedMaster.addSortingByBuckets(mChildren,job.node);
720
                     break;
721
        }
722
      }
723

    
724
    if( numChanges>0 ) adjustIsomorphism();
725
    }
726

    
727
///////////////////////////////////////////////////////////////////////////////////////////////////
728
/**
729
 * Returns the DistortedEffects object that's in the Node.
730
 * 
731
 * @return The DistortedEffects contained in the Node.
732
 */
733
  public DistortedEffects getEffects()
734
    {
735
    return mEffects;
736
    }
737

    
738
///////////////////////////////////////////////////////////////////////////////////////////////////
739
/**
740
 * Returns the DistortedInputSurface object that's in the Node.
741
 *
742
 * @return The DistortedInputSurface contained in the Node.
743
 */
744
  public DistortedSurface getSurface()
745
    {
746
    return mSurface;
747
    }
748

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750
/**
751
 * Returns the Mesh object that's in the Node.
752
 *
753
 * @return Mesh contained in the Node.
754
 */
755
  public MeshObject getMesh()
756
    {
757
    return mMesh;
758
    }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761
/**
762
 * Resizes the DistortedFramebuffer object that we render this Node to.
763
 */
764
  public void resize(int width, int height)
765
    {
766
    mFboW = width;
767
    mFboH = height;
768

    
769
    if ( mData.mFBO !=null )
770
      {
771
      // TODO: potentially allocate a new NodeData if we have to
772
      mData.mFBO.resize(width,height);
773
      }
774
    }
775

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777
/**
778
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
779
 */
780
  public void enableDepthStencil(int depthStencil)
781
    {
782
    mFboDepthStencil = depthStencil;
783

    
784
    if ( mData.mFBO !=null )
785
      {
786
      // TODO: potentially allocate a new NodeData if we have to
787
      mData.mFBO.enableDepthStencil(depthStencil);
788
      }
789
    }
790

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

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810
 * When rendering this Node, switch on writing to Depth buffer?
811
 *
812
 * @param mask Write to the Depth buffer when rendering this Node?
813
 */
814
  @SuppressWarnings("unused")
815
  public void glDepthMask(boolean mask)
816
    {
817
    mState.glDepthMask(mask);
818
    }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821
/**
822
 * When rendering this Node, which bits of the Stencil buffer to write to?
823
 *
824
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
825
 */
826
  @SuppressWarnings("unused")
827
  public void glStencilMask(int mask)
828
    {
829
    mState.glStencilMask(mask);
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 glEnable(int test)
840
    {
841
    mState.glEnable(test);
842
    }
843

    
844
///////////////////////////////////////////////////////////////////////////////////////////////////
845
/**
846
 * When rendering this Node, which Tests to enable?
847
 *
848
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
849
 */
850
  @SuppressWarnings("unused")
851
  public void glDisable(int test)
852
    {
853
    mState.glDisable(test);
854
    }
855

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

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

    
886
///////////////////////////////////////////////////////////////////////////////////////////////////
887
/**
888
 * When rendering this Node, use the following DepthFunc.
889
 *
890
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
891
 */
892
  @SuppressWarnings("unused")
893
  public void glDepthFunc(int func)
894
    {
895
    mState.glDepthFunc(func);
896
    }
897

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

    
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916
/**
917
 * Before rendering this Node, clear the following buffers.
918
 * <p>
919
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
920
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
921
 * Default: 0
922
 *
923
 * @param mask bitwise OR of BUFFER_BITs to clear.
924
 */
925
  @SuppressWarnings("unused")
926
  public void glClear(int mask)
927
    {
928
    mState.glClear(mask);
929
    }
930
  }
(5-5/20)