Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ 0f011027

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;
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 DistortedSlave
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
    DistortedEffectsPostprocess dep;
52

    
53
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
54
      {
55
      type = t;
56
      node = n;
57
      dep  = d;
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 DistortedNode mParent;
67
  private DistortedOutputSurface mSurfaceParent;
68
  private MeshObject mMesh;
69
  private DistortedEffects mEffects;
70
  private DistortedEffectsPostprocess mPostprocess;
71
  private DistortedInputSurface mSurface;
72
  private DistortedRenderState mState;
73
  private NodeData mData;
74
  private int mFboW, mFboH, mFboDepthStencil;
75

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

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

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

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

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

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

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

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

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

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

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

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

    
161
    return ret;
162
    }
163

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
255
    mData = newData;
256

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

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

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

    
266
    if( input.setAsInput() )
267
      {
268
      int quality = effects.getQuality();
269
      DistortedFramebuffer buffer = surface.mBuffer[quality];
270
      float w = mSurface.getWidth() /2.0f;
271
      float h = mSurface.getHeight()/2.0f;
272
      buffer.setAsOutput();
273

    
274
      // Draw the color buffer of the object.
275
      mState.apply();
276
      mEffects.drawPriv(w, h, mMesh, buffer, currTime, 0);
277

    
278
      // Draw stencil + depth buffers of the object enlarged by HALO pixels around.
279
      DistortedRenderState.setUpStencilMark();
280
      mEffects.drawPriv(w, h, mMesh, buffer, currTime, effects.getHalo()*buffer.mMipmap);
281
      DistortedRenderState.unsetUpStencilMark();
282

    
283
      return 1;
284
      }
285
    return 0;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
// return the total number of render calls issued
290

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

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

    
303
    return 0;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307
// return the total number of render calls issued
308

    
309
  int renderRecursive(long currTime)
310
    {
311
    int numRenders = 0;
312

    
313
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
314
      {
315
      mData.currTime = currTime;
316

    
317
      for (int i=0; i<mNumChildren[0]; i++)
318
        {
319
        numRenders += mChildren.get(i).renderRecursive(currTime);
320
        }
321

    
322
      if( mData.mFBO==null )
323
        {
324
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
325
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
326
        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
327
        }
328

    
329
      mData.mFBO.setAsOutput(currTime);
330

    
331
      if( mSurface.setAsInput() )
332
        {
333
        numRenders++;
334
        DistortedEffects.blitPriv(mData.mFBO);
335
        }
336

    
337
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren);
338
      }
339

    
340
    return numRenders;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  private void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
346
    {
347
    mJobs.add(new Job(t,n,d));
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
  void setPost(DistortedEffectsPostprocess dep)
353
    {
354
    mPostprocess = dep;
355
    }
356

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

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

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366
// PUBLIC API
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
/**
369
 * Constructs new Node.
370
 *     
371
 * @param surface InputSurface to put into the new Node.
372
 * @param effects DistortedEffects to put into the new Node.
373
 * @param mesh MeshObject to put into the new Node.
374
 */
375
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
376
    {
377
    mSurface       = surface;
378
    mEffects       = effects;
379
    mPostprocess   = null;
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

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

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

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

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

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

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

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

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

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

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

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

    
558
    for(int i=0; i<mNumChildren[0]; i++)
559
      {
560
      node = mChildren.get(i);
561

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

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

    
578
      for(int i=0; i<num; i++)
579
        {
580
        job = mJobs.get(i);
581

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

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

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

    
617
    int numChanges=0;
618

    
619
    for(int i=0; i<num; i++)
620
      {
621
      job = mJobs.remove(0);
622

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

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

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

    
662
    if( numChanges>0 ) adjustIsomorphism();
663
    }
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
/**
666
 * Sets the Postprocessing Effects we will apply to the temporary buffer this Node - and fellow siblings
667
 * with the same Effects - will get rendered to.
668
 * <p>
669
 * For efficiency reasons, it is very important to assign the very same DistortedEffectsPostprocess
670
 * object to all the DistortedNode siblings that are supposed to be postprocessed in the same way,
671
 * because only then will the library assign all such siblings to the same 'Bucket' which gets rendered
672
 * to the same offscreen buffer which then gets postprocessed in one go and subsequently merged to the
673
 * target Surface.
674
 */
675
  public void setPostprocessEffects(DistortedEffectsPostprocess dep)
676
    {
677
    if( mParent!=null )
678
      {
679
      mParent.newJob(SORT, this, dep);
680
      DistortedMaster.newSlave(mParent);
681
      }
682
    else if( mSurfaceParent!=null )
683
      {
684
      mSurfaceParent.newJob(SORT, this, dep);
685
      DistortedMaster.newSlave(mSurfaceParent);
686
      }
687
    else
688
      {
689
      mPostprocess = dep;
690
      }
691
    }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694
/**
695
 * Returns the DistortedEffectsPostprocess object that's in the Node.
696
 *
697
 * @return The DistortedEffectsPostprocess contained in the Node.
698
 */
699
  public DistortedEffectsPostprocess getEffectsPostprocess()
700
    {
701
    return mPostprocess;
702
    }
703

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

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

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727
/**
728
 * Resizes the DistortedFramebuffer object that we render this Node to.
729
 */
730
  public void resize(int width, int height)
731
    {
732
    mFboW = width;
733
    mFboH = height;
734

    
735
    if ( mData.mFBO !=null )
736
      {
737
      // TODO: potentially allocate a new NodeData if we have to
738
      mData.mFBO.resize(width,height);
739
      }
740
    }
741

    
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743
/**
744
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
745
 */
746
  public void enableDepthStencil(int depthStencil)
747
    {
748
    mFboDepthStencil = depthStencil;
749

    
750
    if ( mData.mFBO !=null )
751
      {
752
      // TODO: potentially allocate a new NodeData if we have to
753
      mData.mFBO.enableDepthStencil(depthStencil);
754
      }
755
    }
756

    
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758
// APIs that control how to set the OpenGL state just before rendering this Node.
759
///////////////////////////////////////////////////////////////////////////////////////////////////
760
/**
761
 * When rendering this Node, use ColorMask (r,g,b,a).
762
 *
763
 * @param r Write to the RED color channel when rendering this Node?
764
 * @param g Write to the GREEN color channel when rendering this Node?
765
 * @param b Write to the BLUE color channel when rendering this Node?
766
 * @param a Write to the ALPHA channel when rendering this Node?
767
 */
768
  @SuppressWarnings("unused")
769
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
770
    {
771
    mState.glColorMask(r,g,b,a);
772
    }
773

    
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775
/**
776
 * When rendering this Node, switch on writing to Depth buffer?
777
 *
778
 * @param mask Write to the Depth buffer when rendering this Node?
779
 */
780
  @SuppressWarnings("unused")
781
  public void glDepthMask(boolean mask)
782
    {
783
    mState.glDepthMask(mask);
784
    }
785

    
786
///////////////////////////////////////////////////////////////////////////////////////////////////
787
/**
788
 * When rendering this Node, which bits of the Stencil buffer to write to?
789
 *
790
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
791
 */
792
  @SuppressWarnings("unused")
793
  public void glStencilMask(int mask)
794
    {
795
    mState.glStencilMask(mask);
796
    }
797

    
798
///////////////////////////////////////////////////////////////////////////////////////////////////
799
/**
800
 * When rendering this Node, which Tests to enable?
801
 *
802
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
803
 */
804
  @SuppressWarnings("unused")
805
  public void glEnable(int test)
806
    {
807
    mState.glEnable(test);
808
    }
809

    
810
///////////////////////////////////////////////////////////////////////////////////////////////////
811
/**
812
 * When rendering this Node, which Tests to enable?
813
 *
814
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
815
 */
816
  @SuppressWarnings("unused")
817
  public void glDisable(int test)
818
    {
819
    mState.glDisable(test);
820
    }
821

    
822
///////////////////////////////////////////////////////////////////////////////////////////////////
823
/**
824
 * When rendering this Node, use the following StencilFunc.
825
 *
826
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
827
 * @param ref  Reference valut to compare our stencil with.
828
 * @param mask Mask used when comparing.
829
 */
830
  @SuppressWarnings("unused")
831
  public void glStencilFunc(int func, int ref, int mask)
832
    {
833
    mState.glStencilFunc(func,ref,mask);
834
    }
835

    
836
///////////////////////////////////////////////////////////////////////////////////////////////////
837
/**
838
 * When rendering this Node, use the following StencilOp.
839
 * <p>
840
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
841
 *
842
 * @param sfail  What to do when Stencil Test fails.
843
 * @param dpfail What to do when Depth Test fails.
844
 * @param dppass What to do when Depth Test passes.
845
 */
846
  @SuppressWarnings("unused")
847
  public void glStencilOp(int sfail, int dpfail, int dppass)
848
    {
849
    mState.glStencilOp(sfail,dpfail,dppass);
850
    }
851

    
852
///////////////////////////////////////////////////////////////////////////////////////////////////
853
/**
854
 * When rendering this Node, use the following DepthFunc.
855
 *
856
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
857
 */
858
  @SuppressWarnings("unused")
859
  public void glDepthFunc(int func)
860
    {
861
    mState.glDepthFunc(func);
862
    }
863

    
864
///////////////////////////////////////////////////////////////////////////////////////////////////
865
/**
866
 * When rendering this Node, use the following Blending mode.
867
 * <p>
868
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
869
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
870
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
871
 *
872
 * @param src Source Blend function
873
 * @param dst Destination Blend function
874
 */
875
  @SuppressWarnings("unused")
876
  public void glBlendFunc(int src, int dst)
877
    {
878
    mState.glBlendFunc(src,dst);
879
    }
880

    
881
///////////////////////////////////////////////////////////////////////////////////////////////////
882
/**
883
 * Before rendering this Node, clear the following buffers.
884
 * <p>
885
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
886
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
887
 * Default: 0
888
 *
889
 * @param mask bitwise OR of BUFFER_BITs to clear.
890
 */
891
  @SuppressWarnings("unused")
892
  public void glClear(int mask)
893
    {
894
    mState.glClear(mask);
895
    }
896
  }
(7-7/26)