Project

General

Profile

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

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

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

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

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

    
47
  private class Job
48
    {
49
    int type;
50
    DistortedNode node;
51

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

    
59
  private ArrayList<Job> mJobs = new ArrayList<>();
60

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

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

    
73
  private class NodeData
74
    {
75
    long ID;
76
    int numPointingNodes;
77
    long currTime;
78
    ArrayList<Long> key;
79
    DistortedFramebuffer mFBO;
80

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

    
93
  static synchronized void onPause()
94
    {
95
    NodeData data;
96

    
97
    for (HashMap.Entry<ArrayList<Long>,NodeData> entry : mMapNodeID.entrySet())
98
      {
99
      data = entry.getValue();
100

    
101
      if( data.mFBO != null )
102
        {
103
        data.mFBO.markForDeletion();
104
        data.mFBO = null;
105
        }
106
      }
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  static synchronized void onDestroy()
112
    {
113
    mNextNodeID = 0;
114
    mMapNodeID.clear();
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private ArrayList<Long> generateIDList()
120
    {
121
    ArrayList<Long> ret = new ArrayList<>();
122

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

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

    
156
    ret.add( 0, mSurface.getID() );
157

    
158
    return ret;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// Debug - print all the Node IDs
163

    
164
  @SuppressWarnings("unused")
165
  void debug(int depth)
166
    {
167
    String tmp="";
168
    int i;
169

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

    
175
    android.util.Log.e("NODE", tmp);
176

    
177
    for(i=0; i<mNumChildren[0]; i++)
178
      mChildren.get(i).debug(depth+1);
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// Debug - print contents of the HashMap
183

    
184
  @SuppressWarnings("unused")
185
  static void debugMap()
186
    {
187
    NodeData tmp;
188

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// tree isomorphism algorithm
198

    
199
  private void adjustIsomorphism()
200
    {
201
    ArrayList<Long> newList = generateIDList();
202
    NodeData newData = mMapNodeID.get(newList);
203

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

    
214
    boolean deleteOldFBO = false;
215
    boolean createNewFBO = false;
216

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

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

    
252
    mData = newData;
253

    
254
    if( mParent!=null ) mParent.adjustIsomorphism();
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

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

    
270
      return 1;
271
      }
272
    return 0;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
// return the total number of render calls issued
277

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

    
282
    if( input.setAsInput() )
283
      {
284
      surface.setAsOutput(currTime);
285
      mState.apply();
286
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
287
      return 1;
288
      }
289

    
290
    return 0;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
// return the total number of render calls issued
295

    
296
  int renderRecursive(long currTime)
297
    {
298
    int numRenders = 0;
299

    
300
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
301
      {
302
      mData.currTime = currTime;
303

    
304
      for (int i=0; i<mNumChildren[0]; i++)
305
        {
306
        numRenders += mChildren.get(i).renderRecursive(currTime);
307
        }
308

    
309
      if( mData.mFBO==null )
310
        {
311
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
312
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
313
        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
314
        }
315

    
316
      mData.mFBO.setAsOutput(currTime);
317

    
318
      if( mSurface.setAsInput() )
319
        {
320
        numRenders++;
321
        DistortedEffects.blitPriv(mData.mFBO);
322
        }
323

    
324
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren);
325
      }
326

    
327
    return numRenders;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  void setSurfaceParent(DistortedOutputSurface dep)
333
    {
334
    mSurfaceParent = dep;
335
    mParent = null;
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  void sort()
341
    {
342
    if( mParent!=null )
343
      {
344
      mParent.mChildren.remove(this);
345
      DistortedMaster.addSorted(mParent.mChildren,this);
346
      }
347
    else if( mSurfaceParent!=null )
348
      {
349
      ArrayList<DistortedNode> children = mSurfaceParent.getChildren();
350
      children.remove(this);
351
      DistortedMaster.addSorted(children,this);
352
      }
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
/**
357
 * Not part of the Public API.
358
 *
359
 * @y.exclude
360
 */
361
  public EffectQueuePostprocess getPostprocessQueue()
362
    {
363
    return mEffects.getPostprocess();
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367
// PUBLIC API
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369
/**
370
 * Constructs new Node.
371
 *     
372
 * @param surface InputSurface to put into the new Node.
373
 * @param effects DistortedEffects to put into the new Node.
374
 * @param mesh MeshObject to put into the new Node.
375
 */
376
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
377
    {
378
    mSurface       = surface;
379
    mEffects       = effects;
380
    mMesh          = mesh;
381
    mState         = new DistortedRenderState();
382
    mChildren      = null;
383
    mNumChildren   = new int[1];
384
    mNumChildren[0]= 0;
385
    mParent        = null;
386
    mSurfaceParent = null;
387

    
388
    mFboW            = 0;  // i.e. take this from
389
    mFboH            = 0;  // mSurface's dimensions
390
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
391

    
392
    ArrayList<Long> list = new ArrayList<>();
393
    list.add(mSurface.getID());
394
    list.add(-mEffects.getID());
395

    
396
    mData = mMapNodeID.get(list);
397
   
398
    if( mData!=null )
399
      {
400
      mData.numPointingNodes++;
401
      }
402
    else
403
      {
404
      mData = new NodeData(++mNextNodeID,list);
405
      mMapNodeID.put(list, mData);
406
      }
407

    
408
    mEffects.newNode(this);
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////  
412
/**
413
 * Copy-constructs new Node from another Node.
414
 *     
415
 * @param node The DistortedNode to copy data from.
416
 * @param flags bit field composed of a subset of the following:
417
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
418
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
419
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
420
 */
421
  public DistortedNode(DistortedNode node, int flags)
422
    {
423
    mEffects      = new DistortedEffects(node.mEffects,flags);
424
    mMesh         = node.mMesh;
425
    mState        = new DistortedRenderState();
426
    mParent       = null;
427
    mSurfaceParent= null;
428

    
429
    mFboW            = node.mFboW;
430
    mFboH            = node.mFboH;
431
    mFboDepthStencil = node.mFboDepthStencil;
432

    
433
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
434
      {
435
      mSurface = node.mSurface;
436
      }
437
    else
438
      {
439
      int w = node.mSurface.getWidth();
440
      int h = node.mSurface.getHeight();
441

    
442
      if( node.mSurface instanceof DistortedTexture )
443
        {
444
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
445
        }
446
      else if( node.mSurface instanceof DistortedFramebuffer )
447
        {
448
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
449

    
450
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
451
          {
452
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
453
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
454
          }
455

    
456
        mSurface = new DistortedFramebuffer(1,depthStencil,DistortedSurface.TYPE_TREE,w,h);
457
        }
458
      }
459
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
460
      {
461
      if( node.mChildren==null )     // do NOT copy over the NULL!
462
        {
463
        node.mChildren = new ArrayList<>(2);
464
        }
465

    
466
      mChildren = node.mChildren;
467
      mNumChildren = node.mNumChildren;
468
      }
469
    else
470
      {
471
      mChildren = null;
472
      mNumChildren = new int[1];
473
      mNumChildren[0] = 0;
474
      }
475
   
476
    ArrayList<Long> list = generateIDList();
477
   
478
    mData = mMapNodeID.get(list);
479
   
480
    if( mData!=null )
481
      {
482
      mData.numPointingNodes++;
483
      }
484
    else
485
      {
486
      mData = new NodeData(++mNextNodeID,list);
487
      mMapNodeID.put(list, mData);
488
      }
489

    
490
    mEffects.newNode(this);
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494
/**
495
 * Adds a new child to the last position in the list of our Node's children.
496
 * <p>
497
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
498
 * DistortedMaster (by calling doWork())
499
 *
500
 * @param node The new Node to add.
501
 */
502
  public void attach(DistortedNode node)
503
    {
504
    mJobs.add(new Job(ATTACH,node));
505
    DistortedMaster.newSlave(this);
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
/**
510
 * Adds a new child to the last position in the list of our Node's children.
511
 * <p>
512
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
513
 * DistortedMaster (by calling doWork())
514
 *
515
 * @param surface InputSurface to initialize our child Node with.
516
 * @param effects DistortedEffects to initialize our child Node with.
517
 * @param mesh MeshObject to initialize our child Node with.
518
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
519
 */
520
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
521
    {
522
    DistortedNode node = new DistortedNode(surface,effects,mesh);
523
    mJobs.add(new Job(ATTACH,node));
524
    DistortedMaster.newSlave(this);
525
    return node;
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Removes the first occurrence of a specified child from the list of children of our Node.
531
 * <p>
532
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
533
 * DistortedMaster (by calling doWork())
534
 *
535
 * @param node The Node to remove.
536
 */
537
  public void detach(DistortedNode node)
538
    {
539
    mJobs.add(new Job(DETACH,node));
540
    DistortedMaster.newSlave(this);
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
/**
545
 * Removes the first occurrence of a specified child from the list of children of our Node.
546
 * <p>
547
 * A bit questionable method as there can be many different Nodes attached as children, some
548
 * of them having the same Effects but - for instance - different Mesh. Use with care.
549
 * <p>
550
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
551
 * DistortedMaster (by calling doWork())
552
 *
553
 * @param effects DistortedEffects to remove.
554
 */
555
  public void detach(DistortedEffects effects)
556
    {
557
    long id = effects.getID();
558
    DistortedNode node;
559
    boolean detached = false;
560

    
561
    for(int i=0; i<mNumChildren[0]; i++)
562
      {
563
      node = mChildren.get(i);
564

    
565
      if( node.getEffects().getID()==id )
566
        {
567
        detached = true;
568
        mJobs.add(new Job(DETACH,node));
569
        DistortedMaster.newSlave(this);
570
        break;
571
        }
572
      }
573

    
574
    if( !detached )
575
      {
576
      // if we failed to detach any, it still might be the case that
577
      // there's an ATTACH job that we need to cancel.
578
      int num = mJobs.size();
579
      Job job;
580

    
581
      for(int i=0; i<num; i++)
582
        {
583
        job = mJobs.get(i);
584

    
585
        if( job.type==ATTACH && job.node.getEffects()==effects )
586
          {
587
          mJobs.remove(i);
588
          break;
589
          }
590
        }
591
      }
592
    }
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595
/**
596
 * Removes all children Nodes.
597
 * <p>
598
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
599
 * DistortedMaster (by calling doWork())
600
 */
601
  public void detachAll()
602
    {
603
    mJobs.add(new Job(DETALL,null));
604
    DistortedMaster.newSlave(this);
605
    }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608
/**
609
 * This is not really part of the public API. Has to be public only because it is a part of the
610
 * DistortedSlave interface, which should really be a class that we extend here instead but
611
 * Java has no multiple inheritance.
612
 *
613
 * @y.exclude
614
 */
615
  public void doWork()
616
    {
617
    int num = mJobs.size();
618
    Job job;
619

    
620
    int numChanges=0;
621

    
622
    for(int i=0; i<num; i++)
623
      {
624
      job = mJobs.remove(0);
625

    
626
      switch(job.type)
627
        {
628
        case ATTACH: numChanges++;
629
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
630
                     job.node.mParent = this;
631
                     job.node.mSurfaceParent = null;
632
                     DistortedMaster.addSorted(mChildren,job.node);
633
                     mNumChildren[0]++;
634
                     break;
635
        case DETACH: numChanges++;
636
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
637
                       {
638
                       job.node.mParent = null;
639
                       job.node.mSurfaceParent = null;
640
                       mNumChildren[0]--;
641
                       }
642
                     break;
643
        case DETALL: numChanges++;
644
                     if( mNumChildren[0]>0 )
645
                       {
646
                       DistortedNode tmp;
647

    
648
                       for(int j=mNumChildren[0]-1; j>=0; j--)
649
                         {
650
                         tmp = mChildren.remove(j);
651
                         tmp.mParent = null;
652
                         tmp.mSurfaceParent = null;
653
                         }
654

    
655
                       mNumChildren[0] = 0;
656
                       }
657
                     break;
658
        case SORT  : mChildren.remove(job.node);
659
                     DistortedMaster.addSorted(mChildren,job.node);
660
                     break;
661
        }
662
      }
663

    
664
    if( numChanges>0 ) adjustIsomorphism();
665
    }
666

    
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668
/**
669
 * Returns the DistortedEffects object that's in the Node.
670
 * 
671
 * @return The DistortedEffects contained in the Node.
672
 */
673
  public DistortedEffects getEffects()
674
    {
675
    return mEffects;
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679
/**
680
 * Returns the DistortedInputSurface object that's in the Node.
681
 *
682
 * @return The DistortedInputSurface contained in the Node.
683
 */
684
  public DistortedInputSurface getSurface()
685
    {
686
    return mSurface;
687
    }
688

    
689
///////////////////////////////////////////////////////////////////////////////////////////////////
690
/**
691
 * Returns the Mesh object that's in the Node.
692
 *
693
 * @return Mesh contained in the Node.
694
 */
695
  public MeshObject getMesh()
696
    {
697
    return mMesh;
698
    }
699

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701
/**
702
 * Resizes the DistortedFramebuffer object that we render this Node to.
703
 */
704
  public void resize(int width, int height)
705
    {
706
    mFboW = width;
707
    mFboH = height;
708

    
709
    if ( mData.mFBO !=null )
710
      {
711
      // TODO: potentially allocate a new NodeData if we have to
712
      mData.mFBO.resize(width,height);
713
      }
714
    }
715

    
716
///////////////////////////////////////////////////////////////////////////////////////////////////
717
/**
718
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
719
 */
720
  public void enableDepthStencil(int depthStencil)
721
    {
722
    mFboDepthStencil = depthStencil;
723

    
724
    if ( mData.mFBO !=null )
725
      {
726
      // TODO: potentially allocate a new NodeData if we have to
727
      mData.mFBO.enableDepthStencil(depthStencil);
728
      }
729
    }
730

    
731
///////////////////////////////////////////////////////////////////////////////////////////////////
732
// APIs that control how to set the OpenGL state just before rendering this Node.
733
///////////////////////////////////////////////////////////////////////////////////////////////////
734
/**
735
 * When rendering this Node, use ColorMask (r,g,b,a).
736
 *
737
 * @param r Write to the RED color channel when rendering this Node?
738
 * @param g Write to the GREEN color channel when rendering this Node?
739
 * @param b Write to the BLUE color channel when rendering this Node?
740
 * @param a Write to the ALPHA channel when rendering this Node?
741
 */
742
  @SuppressWarnings("unused")
743
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
744
    {
745
    mState.glColorMask(r,g,b,a);
746
    }
747

    
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749
/**
750
 * When rendering this Node, switch on writing to Depth buffer?
751
 *
752
 * @param mask Write to the Depth buffer when rendering this Node?
753
 */
754
  @SuppressWarnings("unused")
755
  public void glDepthMask(boolean mask)
756
    {
757
    mState.glDepthMask(mask);
758
    }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761
/**
762
 * When rendering this Node, which bits of the Stencil buffer to write to?
763
 *
764
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
765
 */
766
  @SuppressWarnings("unused")
767
  public void glStencilMask(int mask)
768
    {
769
    mState.glStencilMask(mask);
770
    }
771

    
772
///////////////////////////////////////////////////////////////////////////////////////////////////
773
/**
774
 * When rendering this Node, which Tests to enable?
775
 *
776
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
777
 */
778
  @SuppressWarnings("unused")
779
  public void glEnable(int test)
780
    {
781
    mState.glEnable(test);
782
    }
783

    
784
///////////////////////////////////////////////////////////////////////////////////////////////////
785
/**
786
 * When rendering this Node, which Tests to enable?
787
 *
788
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
789
 */
790
  @SuppressWarnings("unused")
791
  public void glDisable(int test)
792
    {
793
    mState.glDisable(test);
794
    }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797
/**
798
 * When rendering this Node, use the following StencilFunc.
799
 *
800
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
801
 * @param ref  Reference valut to compare our stencil with.
802
 * @param mask Mask used when comparing.
803
 */
804
  @SuppressWarnings("unused")
805
  public void glStencilFunc(int func, int ref, int mask)
806
    {
807
    mState.glStencilFunc(func,ref,mask);
808
    }
809

    
810
///////////////////////////////////////////////////////////////////////////////////////////////////
811
/**
812
 * When rendering this Node, use the following StencilOp.
813
 * <p>
814
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
815
 *
816
 * @param sfail  What to do when Stencil Test fails.
817
 * @param dpfail What to do when Depth Test fails.
818
 * @param dppass What to do when Depth Test passes.
819
 */
820
  @SuppressWarnings("unused")
821
  public void glStencilOp(int sfail, int dpfail, int dppass)
822
    {
823
    mState.glStencilOp(sfail,dpfail,dppass);
824
    }
825

    
826
///////////////////////////////////////////////////////////////////////////////////////////////////
827
/**
828
 * When rendering this Node, use the following DepthFunc.
829
 *
830
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
831
 */
832
  @SuppressWarnings("unused")
833
  public void glDepthFunc(int func)
834
    {
835
    mState.glDepthFunc(func);
836
    }
837

    
838
///////////////////////////////////////////////////////////////////////////////////////////////////
839
/**
840
 * When rendering this Node, use the following Blending mode.
841
 * <p>
842
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
843
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
844
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
845
 *
846
 * @param src Source Blend function
847
 * @param dst Destination Blend function
848
 */
849
  @SuppressWarnings("unused")
850
  public void glBlendFunc(int src, int dst)
851
    {
852
    mState.glBlendFunc(src,dst);
853
    }
854

    
855
///////////////////////////////////////////////////////////////////////////////////////////////////
856
/**
857
 * Before rendering this Node, clear the following buffers.
858
 * <p>
859
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
860
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
861
 * Default: 0
862
 *
863
 * @param mask bitwise OR of BUFFER_BITs to clear.
864
 */
865
  @SuppressWarnings("unused")
866
  public void glClear(int mask)
867
    {
868
    mState.glClear(mask);
869
    }
870
  }
(6-6/22)