Project

General

Profile

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

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

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
    long ID;
81
    int numPointingNodes;
82
    long currTime;
83
    ArrayList<Long> key;
84
    DistortedFramebuffer mFBO;
85

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

    
119
    mEffects.removeNode(this);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

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

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

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

    
163
    return ret;
164
    }
165

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

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

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

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

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

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

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

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// tree isomorphism algorithm
203

    
204
  private void adjustIsomorphism()
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
    boolean deleteOldFBO = false;
220
    boolean createNewFBO = false;
221

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

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

    
257
    mData = newData;
258

    
259
    if( mParent!=null ) mParent.adjustIsomorphism();
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
// return the total number of render calls issued
264

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

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

    
278
    return 0;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
// Use the Order Independent Transparency method to draw a non-postprocessed child.
283

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

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

    
295
    return 0;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
// return the total number of render calls issued
300

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

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

    
312
    return 0;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
// return the total number of render calls issued
317

    
318
  int renderRecursive(long currTime)
319
    {
320
    int numRenders = 0;
321

    
322
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
323
      {
324
      mData.currTime = currTime;
325

    
326
      for (int i=0; i<mNumChildren[0]; i++)
327
        {
328
        numRenders += mChildren.get(i).renderRecursive(currTime);
329
        }
330

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

    
338
      mData.mFBO.setAsOutput(currTime);
339

    
340
      if( mSurface.setAsInput() )
341
        {
342
        numRenders++;
343
        DistortedEffects.blitPriv(mData.mFBO);
344
        }
345

    
346
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren,0, mRenderWayOIT);
347
      }
348

    
349
    return numRenders;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  void setSurfaceParent(DistortedOutputSurface dep)
355
    {
356
    mSurfaceParent = dep;
357
    mParent = null;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  EffectQueuePostprocess getPostprocessQueue()
380
    {
381
    return mEffects.getPostprocess();
382
    }
383

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

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

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

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

    
427
    mEffects.newNode(this);
428
    }
429

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

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

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

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

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

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

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

    
510
    mEffects.newNode(this);
511
    }
512

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
678
    if( num>0 )
679
      {
680
      Job job;
681
      int numChanges=0;
682

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

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

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

    
716
                         mNumChildren[0] = 0;
717
                         }
718
                       break;
719
          case SORT  : mChildren.remove(job.node);
720
                       DistortedMaster.addSortingByBuckets(mChildren,job.node);
721
                       break;
722
          }
723
        }
724
      if( numChanges>0 ) adjustIsomorphism();
725
      }
726
    }
727

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

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

    
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751
  /**
752
   * Returns the DistortedSurface object that's in the Node.
753
   *
754
   * @return The DistortedSurface contained in the Node (if a leaf), or the FBO (if an internal Node)
755
   */
756
  public DistortedSurface getInternalSurface()
757
    {
758
    return mNumChildren[0]==0 ? mSurface : mData.mFBO;
759
    }
760

    
761
///////////////////////////////////////////////////////////////////////////////////////////////////
762
/**
763
 * Returns the Mesh object that's in the Node.
764
 *
765
 * @return Mesh contained in the Node.
766
 */
767
  public MeshBase getMesh()
768
    {
769
    return mMesh;
770
    }
771

    
772
///////////////////////////////////////////////////////////////////////////////////////////////////
773
/**
774
 * Resizes the DistortedFramebuffer object that we render this Node to.
775
 */
776
  public void resize(int width, int height)
777
    {
778
    mFboW = width;
779
    mFboH = height;
780

    
781
    if ( mData.mFBO !=null )
782
      {
783
      // TODO: potentially allocate a new NodeData if we have to
784
      mData.mFBO.resize(width,height);
785
      }
786
    }
787

    
788
///////////////////////////////////////////////////////////////////////////////////////////////////
789
/**
790
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
791
 */
792
  public void enableDepthStencil(int depthStencil)
793
    {
794
    mFboDepthStencil = depthStencil;
795

    
796
    if ( mData.mFBO !=null )
797
      {
798
      // TODO: potentially allocate a new NodeData if we have to
799
      mData.mFBO.enableDepthStencil(depthStencil);
800
      }
801
    }
802

    
803
///////////////////////////////////////////////////////////////////////////////////////////////////
804
// APIs that control how to set the OpenGL state just before rendering this Node.
805
///////////////////////////////////////////////////////////////////////////////////////////////////
806
/**
807
 * When rendering this Node, use ColorMask (r,g,b,a).
808
 *
809
 * @param r Write to the RED color channel when rendering this Node?
810
 * @param g Write to the GREEN color channel when rendering this Node?
811
 * @param b Write to the BLUE color channel when rendering this Node?
812
 * @param a Write to the ALPHA channel when rendering this Node?
813
 */
814
  @SuppressWarnings("unused")
815
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
816
    {
817
    mState.glColorMask(r,g,b,a);
818
    }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821
/**
822
 * When rendering this Node, switch on writing to Depth buffer?
823
 *
824
 * @param mask Write to the Depth buffer when rendering this Node?
825
 */
826
  @SuppressWarnings("unused")
827
  public void glDepthMask(boolean mask)
828
    {
829
    mState.glDepthMask(mask);
830
    }
831

    
832
///////////////////////////////////////////////////////////////////////////////////////////////////
833
/**
834
 * When rendering this Node, which bits of the Stencil buffer to write to?
835
 *
836
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
837
 */
838
  @SuppressWarnings("unused")
839
  public void glStencilMask(int mask)
840
    {
841
    mState.glStencilMask(mask);
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 glEnable(int test)
852
    {
853
    mState.glEnable(test);
854
    }
855

    
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857
/**
858
 * When rendering this Node, which Tests to enable?
859
 *
860
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
861
 */
862
  @SuppressWarnings("unused")
863
  public void glDisable(int test)
864
    {
865
    mState.glDisable(test);
866
    }
867

    
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869
/**
870
 * When rendering this Node, use the following StencilFunc.
871
 *
872
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
873
 * @param ref  Reference valut to compare our stencil with.
874
 * @param mask Mask used when comparing.
875
 */
876
  @SuppressWarnings("unused")
877
  public void glStencilFunc(int func, int ref, int mask)
878
    {
879
    mState.glStencilFunc(func,ref,mask);
880
    }
881

    
882
///////////////////////////////////////////////////////////////////////////////////////////////////
883
/**
884
 * When rendering this Node, use the following StencilOp.
885
 * <p>
886
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
887
 *
888
 * @param sfail  What to do when Stencil Test fails.
889
 * @param dpfail What to do when Depth Test fails.
890
 * @param dppass What to do when Depth Test passes.
891
 */
892
  @SuppressWarnings("unused")
893
  public void glStencilOp(int sfail, int dpfail, int dppass)
894
    {
895
    mState.glStencilOp(sfail,dpfail,dppass);
896
    }
897

    
898
///////////////////////////////////////////////////////////////////////////////////////////////////
899
/**
900
 * When rendering this Node, use the following DepthFunc.
901
 *
902
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
903
 */
904
  @SuppressWarnings("unused")
905
  public void glDepthFunc(int func)
906
    {
907
    mState.glDepthFunc(func);
908
    }
909

    
910
///////////////////////////////////////////////////////////////////////////////////////////////////
911
/**
912
 * When rendering this Node, use the following Blending mode.
913
 * <p>
914
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
915
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
916
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
917
 *
918
 * @param src Source Blend function
919
 * @param dst Destination Blend function
920
 */
921
  @SuppressWarnings("unused")
922
  public void glBlendFunc(int src, int dst)
923
    {
924
    mState.glBlendFunc(src,dst);
925
    }
926

    
927
///////////////////////////////////////////////////////////////////////////////////////////////////
928
/**
929
 * Before rendering this Node, clear the following buffers.
930
 * <p>
931
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
932
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
933
 * Default: 0
934
 *
935
 * @param mask bitwise OR of BUFFER_BITs to clear.
936
 */
937
  @SuppressWarnings("unused")
938
  public void glClear(int mask)
939
    {
940
    mState.glClear(mask);
941
    }
942
  }
(6-6/17)