Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ 23eecbd9

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22
import java.util.ArrayList;
23 c9f953c2 Leszek Koltunski
import java.util.Collections;
24 6a06a912 Leszek Koltunski
import java.util.HashMap;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28 a09ada4c Leszek Koltunski
 * Class which represents a Node in a Tree of (InputSurface,Mesh,Effects) triplets.
29 c204c69d leszek
 * <p>
30 a09ada4c Leszek Koltunski
 * Having organized such sets into a Tree, we can then render any Node to any OutputSurface.
31 7b8086eb Leszek Koltunski
 * That recursively renders the set held in the Node and all its children.
32 c204c69d leszek
 * <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 3a70bd6d leszek
 * will point to the same NodeData; only the first of this is rendered (mData.numRender!).
36 6a06a912 Leszek Koltunski
 */
37 efe3d8fe leszek
public class DistortedNode implements DistortedSlave
38 6a06a912 Leszek Koltunski
  {
39 efe3d8fe leszek
  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 bd3da5b2 Leszek Koltunski
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
64
  private static long mNextNodeID =0;
65
66 f28fffc2 Leszek Koltunski
  private DistortedNode mParent;
67 be60d4ff leszek
  private DistortedOutputSurface mSurfaceParent;
68 05403bba Leszek Koltunski
  private MeshObject mMesh;
69 07d8ef09 Leszek Koltunski
  private DistortedEffects mEffects;
70 13687207 leszek
  private DistortedEffectsPostprocess mPostprocess;
71 c5369f1b leszek
  private DistortedInputSurface mSurface;
72 c834348d leszek
  private DistortedRenderState mState;
73 6a06a912 Leszek Koltunski
  private NodeData mData;
74 23eecbd9 Leszek Koltunski
  private int mFboW, mFboH, mFboDepthStencil;
75 bd3da5b2 Leszek Koltunski
76 6a06a912 Leszek Koltunski
  private class NodeData
77
    {
78 bd3da5b2 Leszek Koltunski
    long ID;
79 6a06a912 Leszek Koltunski
    int numPointingNodes;
80 50642a86 Leszek Koltunski
    long currTime;
81 af27df87 leszek
    ArrayList<Long> key;
82 8c327653 Leszek Koltunski
    DistortedFramebuffer mFBO;
83 6a06a912 Leszek Koltunski
84 af27df87 leszek
    NodeData(long id, ArrayList<Long> k)
85 6a06a912 Leszek Koltunski
      {
86 bd3da5b2 Leszek Koltunski
      ID              = id;
87 af27df87 leszek
      key             = k;
88 bd3da5b2 Leszek Koltunski
      numPointingNodes= 1;
89 50642a86 Leszek Koltunski
      currTime        =-1;
90 8c327653 Leszek Koltunski
      mFBO            = null;
91 6a06a912 Leszek Koltunski
      }
92 bd3da5b2 Leszek Koltunski
    }
93 6a06a912 Leszek Koltunski
 
94 0c303a2c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 436899f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 7b8086eb Leszek Koltunski
  static synchronized void onDestroy()
115 436899f2 Leszek Koltunski
    {
116
    mNextNodeID = 0;
117
    mMapNodeID.clear();
118
    }
119
120 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
  private ArrayList<Long> generateIDList()
123
    {
124 9361b337 Leszek Koltunski
    ArrayList<Long> ret = new ArrayList<>();
125 7691a39f leszek
126
    if( mNumChildren[0]==0 )
127
      {
128 c9f953c2 Leszek Koltunski
      // 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 7691a39f leszek
      ret.add(-mEffects.getID());
131
      }
132 c9f953c2 Leszek Koltunski
    else
133 6a06a912 Leszek Koltunski
      {
134 c9f953c2 Leszek Koltunski
      DistortedNode node;
135 6a06a912 Leszek Koltunski
   
136 c9f953c2 Leszek Koltunski
      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 6a06a912 Leszek Koltunski
    return ret;
162
    }
163
164 fee0865c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// Debug - print all the Node IDs
166 c204c69d leszek
167 af27df87 leszek
  @SuppressWarnings("unused")
168 fee0865c Leszek Koltunski
  void debug(int depth)
169
    {
170
    String tmp="";
171
    int i;
172
173
    for(i=0; i<depth; i++) tmp +="   ";
174 07037b8a leszek
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
175 f28fffc2 Leszek Koltunski
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
176
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
177 fee0865c Leszek Koltunski
178 1942537e Leszek Koltunski
    android.util.Log.e("NODE", tmp);
179 fee0865c Leszek Koltunski
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 af27df87 leszek
  @SuppressWarnings("unused")
188 fee0865c Leszek Koltunski
  static void debugMap()
189
    {
190
    NodeData tmp;
191
192
    for(ArrayList<Long> key: mMapNodeID.keySet())
193
      {
194
      tmp = mMapNodeID.get(key);
195 c204c69d leszek
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
196
      }
197
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200 f28fffc2 Leszek Koltunski
// tree isomorphism algorithm
201 c204c69d leszek
202 f28fffc2 Leszek Koltunski
  private void adjustIsomorphism()
203 c204c69d leszek
    {
204
    ArrayList<Long> newList = generateIDList();
205
    NodeData newData = mMapNodeID.get(newList);
206
207 f28fffc2 Leszek Koltunski
    if( newData!=null )
208
      {
209
      newData.numPointingNodes++;
210
      }
211
    else
212 c204c69d leszek
      {
213 af27df87 leszek
      newData = new NodeData(++mNextNodeID,newList);
214 c204c69d leszek
      mMapNodeID.put(newList,newData);
215
      }
216 07037b8a leszek
217 f28fffc2 Leszek Koltunski
    boolean deleteOldFBO = false;
218
    boolean createNewFBO = false;
219 c204c69d leszek
220 f28fffc2 Leszek Koltunski
    if( --mData.numPointingNodes==0 )
221 c204c69d leszek
      {
222 f28fffc2 Leszek Koltunski
      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 c204c69d leszek
236 f28fffc2 Leszek Koltunski
    if( deleteOldFBO && createNewFBO )
237
      {
238
      newData.mFBO = mData.mFBO;  // just copy over
239 eadf0859 leszek
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
240 f28fffc2 Leszek Koltunski
      }
241
    else if( deleteOldFBO )
242
      {
243
      mData.mFBO.markForDeletion();
244 eadf0859 leszek
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
245 f28fffc2 Leszek Koltunski
      mData.mFBO = null;
246
      }
247
    else if( createNewFBO )
248
      {
249 23eecbd9 Leszek Koltunski
      int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
250
      int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
251
      newData.mFBO = new DistortedFramebuffer(mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
252 eadf0859 leszek
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
253 f28fffc2 Leszek Koltunski
      }
254 af27df87 leszek
255 f28fffc2 Leszek Koltunski
    mData = newData;
256 c204c69d leszek
257 f28fffc2 Leszek Koltunski
    if( mParent!=null ) mParent.adjustIsomorphism();
258 fee0865c Leszek Koltunski
    }
259 c204c69d leszek
260 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
261
// return the total number of render calls issued
262
263
  int draw(long currTime, DistortedOutputSurface surface)
264
    {
265
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
266
267
    if( input.setAsInput() )
268
      {
269
      mState.apply();
270 1aedf874 leszek
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
271 39086ebb leszek
      return 1;
272
      }
273
274
    return 0;
275
    }
276
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// return the total number of render calls issued
279
280
  int renderRecursive(long currTime)
281
    {
282
    int numRenders = 0;
283
284
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
285
      {
286
      mData.currTime = currTime;
287
288
      for (int i=0; i<mNumChildren[0]; i++)
289
        {
290
        numRenders += mChildren.get(i).renderRecursive(currTime);
291
        }
292
293 0c303a2c Leszek Koltunski
      if( mData.mFBO==null )
294
        {
295 23eecbd9 Leszek Koltunski
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
296
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
297
        mData.mFBO = new DistortedFramebuffer(mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
298 0c303a2c Leszek Koltunski
        }
299
300 95c441a2 leszek
      mData.mFBO.setAsOutput(currTime);
301 39086ebb leszek
302
      if( mSurface.setAsInput() )
303
        {
304
        numRenders++;
305
        DistortedEffects.blitPriv(mData.mFBO);
306
        }
307
308 b2939df4 Leszek Koltunski
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren);
309 39086ebb leszek
      }
310
311
    return numRenders;
312
    }
313
314 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316
  private void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
317
    {
318
    mJobs.add(new Job(t,n,d));
319
    }
320
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
323
  void setPost(DistortedEffectsPostprocess dep)
324
    {
325
    mPostprocess = dep;
326
    }
327
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
330
  void setSurfaceParent(DistortedOutputSurface dep)
331
    {
332
    mSurfaceParent = dep;
333
    mParent = null;
334
    }
335
336 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
337
// PUBLIC API
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
/**
340 a09ada4c Leszek Koltunski
 * Constructs new Node.
341 6a06a912 Leszek Koltunski
 *     
342 c5369f1b leszek
 * @param surface InputSurface to put into the new Node.
343 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to put into the new Node.
344 05403bba Leszek Koltunski
 * @param mesh MeshObject to put into the new Node.
345 6a06a912 Leszek Koltunski
 */
346 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
347 6a06a912 Leszek Koltunski
    {
348 c5369f1b leszek
    mSurface       = surface;
349 8ca9f899 Leszek Koltunski
    mEffects       = effects;
350 13687207 leszek
    mPostprocess   = null;
351 8ca9f899 Leszek Koltunski
    mMesh          = mesh;
352 c834348d leszek
    mState         = new DistortedRenderState();
353 8ca9f899 Leszek Koltunski
    mChildren      = null;
354
    mNumChildren   = new int[1];
355
    mNumChildren[0]= 0;
356 f28fffc2 Leszek Koltunski
    mParent        = null;
357 be60d4ff leszek
    mSurfaceParent = null;
358 f28fffc2 Leszek Koltunski
359 23eecbd9 Leszek Koltunski
    mFboW            = 0;  // i.e. take this from
360
    mFboH            = 0;  // mSurface's dimensions
361
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
362
363 9361b337 Leszek Koltunski
    ArrayList<Long> list = new ArrayList<>();
364 c5369f1b leszek
    list.add(mSurface.getID());
365 7691a39f leszek
    list.add(-mEffects.getID());
366 1942537e Leszek Koltunski
367 6a06a912 Leszek Koltunski
    mData = mMapNodeID.get(list);
368
   
369
    if( mData!=null )
370
      {
371
      mData.numPointingNodes++;
372
      }
373
    else
374
      {
375 af27df87 leszek
      mData = new NodeData(++mNextNodeID,list);
376 1942537e Leszek Koltunski
      mMapNodeID.put(list, mData);
377 6a06a912 Leszek Koltunski
      }
378
    }
379
380
///////////////////////////////////////////////////////////////////////////////////////////////////  
381
/**
382 a09ada4c Leszek Koltunski
 * Copy-constructs new Node from another Node.
383 6a06a912 Leszek Koltunski
 *     
384 a09ada4c Leszek Koltunski
 * @param node The DistortedNode to copy data from.
385 6a06a912 Leszek Koltunski
 * @param flags bit field composed of a subset of the following:
386 29a06526 Leszek Koltunski
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
387 6a06a912 Leszek Koltunski
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
388 29a06526 Leszek Koltunski
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
389 6a06a912 Leszek Koltunski
 */
390 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedNode node, int flags)
391 6a06a912 Leszek Koltunski
    {
392 be60d4ff leszek
    mEffects      = new DistortedEffects(node.mEffects,flags);
393
    mPostprocess  = null;
394
    mMesh         = node.mMesh;
395
    mState        = new DistortedRenderState();
396
    mParent       = null;
397
    mSurfaceParent= null;
398 9361b337 Leszek Koltunski
399 23eecbd9 Leszek Koltunski
    mFboW            = node.mFboW;
400
    mFboH            = node.mFboH;
401
    mFboDepthStencil = node.mFboDepthStencil;
402
403 29a06526 Leszek Koltunski
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
404 e7a20702 Leszek Koltunski
      {
405 c5369f1b leszek
      mSurface = node.mSurface;
406 e7a20702 Leszek Koltunski
      }
407
    else
408
      {
409 c5369f1b leszek
      int w = node.mSurface.getWidth();
410
      int h = node.mSurface.getHeight();
411 8ca9f899 Leszek Koltunski
412 c5369f1b leszek
      if( node.mSurface instanceof DistortedTexture )
413 8ca9f899 Leszek Koltunski
        {
414 09ab7524 Leszek Koltunski
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
415 8ca9f899 Leszek Koltunski
        }
416 c5369f1b leszek
      else if( node.mSurface instanceof DistortedFramebuffer )
417 8ca9f899 Leszek Koltunski
        {
418 23eecbd9 Leszek Koltunski
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
419 89de975c leszek
420
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
421
          {
422
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
423
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
424
          }
425
426
        mSurface = new DistortedFramebuffer(depthStencil,DistortedSurface.TYPE_TREE,w,h);
427 8ca9f899 Leszek Koltunski
        }
428 e7a20702 Leszek Koltunski
      }
429 9361b337 Leszek Koltunski
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
430 6a06a912 Leszek Koltunski
      {
431 c204c69d leszek
      if( node.mChildren==null )     // do NOT copy over the NULL!
432
        {
433
        node.mChildren = new ArrayList<>(2);
434
        }
435
436 6a06a912 Leszek Koltunski
      mChildren = node.mChildren;
437
      mNumChildren = node.mNumChildren;
438
      }
439
    else
440
      {
441
      mChildren = null;
442
      mNumChildren = new int[1];
443
      mNumChildren[0] = 0;
444
      }
445
   
446
    ArrayList<Long> list = generateIDList();
447
   
448
    mData = mMapNodeID.get(list);
449
   
450
    if( mData!=null )
451
      {
452
      mData.numPointingNodes++;
453
      }
454
    else
455
      {
456 af27df87 leszek
      mData = new NodeData(++mNextNodeID,list);
457 6a06a912 Leszek Koltunski
      mMapNodeID.put(list, mData);
458
      }
459
    }
460 c204c69d leszek
461 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
462
/**
463
 * Adds a new child to the last position in the list of our Node's children.
464 c204c69d leszek
 * <p>
465
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
466 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
467 c204c69d leszek
 *
468 6a06a912 Leszek Koltunski
 * @param node The new Node to add.
469
 */
470 c204c69d leszek
  public void attach(DistortedNode node)
471 6a06a912 Leszek Koltunski
    {
472 efe3d8fe leszek
    mJobs.add(new Job(ATTACH,node,null));
473
    DistortedMaster.newSlave(this);
474 6a06a912 Leszek Koltunski
    }
475 c204c69d leszek
476 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
477
/**
478
 * Adds a new child to the last position in the list of our Node's children.
479 c204c69d leszek
 * <p>
480
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
481 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
482 c204c69d leszek
 *
483 c5369f1b leszek
 * @param surface InputSurface to initialize our child Node with.
484 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to initialize our child Node with.
485 05403bba Leszek Koltunski
 * @param mesh MeshObject to initialize our child Node with.
486 6a06a912 Leszek Koltunski
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
487
 */
488 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
489 6a06a912 Leszek Koltunski
    {
490 c204c69d leszek
    DistortedNode node = new DistortedNode(surface,effects,mesh);
491 efe3d8fe leszek
    mJobs.add(new Job(ATTACH,node,null));
492
    DistortedMaster.newSlave(this);
493 c204c69d leszek
    return node;
494
    }
495 f8377ef8 leszek
496 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
497
/**
498
 * Removes the first occurrence of a specified child from the list of children of our Node.
499 c204c69d leszek
 * <p>
500
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
501 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
502 c204c69d leszek
 *
503 6a06a912 Leszek Koltunski
 * @param node The Node to remove.
504
 */
505 c204c69d leszek
  public void detach(DistortedNode node)
506 6a06a912 Leszek Koltunski
    {
507 efe3d8fe leszek
    mJobs.add(new Job(DETACH,node,null));
508
    DistortedMaster.newSlave(this);
509 6a06a912 Leszek Koltunski
    }
510 a09ada4c Leszek Koltunski
511 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
512
/**
513
 * Removes the first occurrence of a specified child from the list of children of our Node.
514 a09ada4c Leszek Koltunski
 * <p>
515
 * A bit questionable method as there can be many different Nodes attached as children, some
516
 * of them having the same Effects but - for instance - different Mesh. Use with care.
517 c204c69d leszek
 * <p>
518
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
519 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
520 a09ada4c Leszek Koltunski
 *
521 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to remove.
522 6a06a912 Leszek Koltunski
 */
523 c204c69d leszek
  public void detach(DistortedEffects effects)
524 6a06a912 Leszek Koltunski
    {
525 07d8ef09 Leszek Koltunski
    long id = effects.getID();
526 a09ada4c Leszek Koltunski
    DistortedNode node;
527 efe3d8fe leszek
    boolean detached = false;
528 a09ada4c Leszek Koltunski
529 6a06a912 Leszek Koltunski
    for(int i=0; i<mNumChildren[0]; i++)
530
      {
531
      node = mChildren.get(i);
532 a09ada4c Leszek Koltunski
533 efe3d8fe leszek
      if( node.getEffects().getID()==id )
534 6a06a912 Leszek Koltunski
        {
535 efe3d8fe leszek
        detached = true;
536
        mJobs.add(new Job(DETACH,node,null));
537
        DistortedMaster.newSlave(this);
538 c204c69d leszek
        break;
539 6a06a912 Leszek Koltunski
        }
540
      }
541 8baa1fe6 Leszek Koltunski
542
    if( !detached )
543
      {
544
      // if we failed to detach any, it still might be the case that
545 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
546
      int num = mJobs.size();
547
      Job job;
548 a09ada4c Leszek Koltunski
549 efe3d8fe leszek
      for(int i=0; i<num; i++)
550
        {
551
        job = mJobs.get(i);
552
553
        if( job.type==ATTACH && job.node.getEffects()==effects )
554
          {
555
          mJobs.remove(i);
556
          break;
557
          }
558
        }
559 6a06a912 Leszek Koltunski
      }
560 c204c69d leszek
    }
561
562
///////////////////////////////////////////////////////////////////////////////////////////////////
563
/**
564
 * Removes all children Nodes.
565
 * <p>
566
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
567 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
568 c204c69d leszek
 */
569
  public void detachAll()
570
    {
571 efe3d8fe leszek
    mJobs.add(new Job(DETALL,null,null));
572
    DistortedMaster.newSlave(this);
573 c204c69d leszek
    }
574
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577
 * This is not really part of the public API. Has to be public only because it is a part of the
578 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
579 c204c69d leszek
 * Java has no multiple inheritance.
580 d3725071 Leszek Koltunski
 *
581
 * @y.exclude
582 c204c69d leszek
 */
583 efe3d8fe leszek
  public void doWork()
584 c204c69d leszek
    {
585 efe3d8fe leszek
    int num = mJobs.size();
586
    Job job;
587
588
    int numChanges=0;
589
590
    for(int i=0; i<num; i++)
591 6a06a912 Leszek Koltunski
      {
592 efe3d8fe leszek
      job = mJobs.remove(0);
593 af27df87 leszek
594 efe3d8fe leszek
      switch(job.type)
595 af27df87 leszek
        {
596 efe3d8fe leszek
        case ATTACH: numChanges++;
597
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
598
                     job.node.mParent = this;
599 be60d4ff leszek
                     job.node.mSurfaceParent = null;
600
                     DistortedMaster.addSorted(mChildren,job.node);
601 efe3d8fe leszek
                     mNumChildren[0]++;
602
                     break;
603
        case DETACH: numChanges++;
604
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
605
                       {
606
                       job.node.mParent = null;
607 be60d4ff leszek
                       job.node.mSurfaceParent = null;
608 efe3d8fe leszek
                       mNumChildren[0]--;
609
                       }
610
                     break;
611
        case DETALL: numChanges++;
612
                     if( mNumChildren[0]>0 )
613
                       {
614
                       DistortedNode tmp;
615
616
                       for(int j=mNumChildren[0]-1; j>=0; j--)
617
                         {
618
                         tmp = mChildren.remove(j);
619
                         tmp.mParent = null;
620 be60d4ff leszek
                         tmp.mSurfaceParent = null;
621 efe3d8fe leszek
                         }
622
623
                       mNumChildren[0] = 0;
624
                       }
625
                     break;
626 be60d4ff leszek
        case SORT  : job.node.mPostprocess = job.dep;
627
                     mChildren.remove(job.node);
628
                     DistortedMaster.addSorted(mChildren,job.node);
629 efe3d8fe leszek
                     break;
630 af27df87 leszek
        }
631 efe3d8fe leszek
      }
632 f28fffc2 Leszek Koltunski
633 be60d4ff leszek
    if( numChanges>0 ) adjustIsomorphism();
634 6a06a912 Leszek Koltunski
    }
635 13687207 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
636
/**
637
 * Sets the Postprocessing Effects we will apply to the temporary buffer this Node - and fellow siblings
638
 * with the same Effects - will get rendered to.
639
 * <p>
640
 * For efficiency reasons, it is very important to assign the very same DistortedEffectsPostprocess
641
 * object to all the DistortedNode siblings that are supposed to be postprocessed in the same way,
642
 * because only then will the library assign all such siblings to the same 'Bucket' which gets rendered
643
 * to the same offscreen buffer which then gets postprocessed in one go and subsequently merged to the
644
 * target Surface.
645
 */
646
  public void setPostprocessEffects(DistortedEffectsPostprocess dep)
647
    {
648 be60d4ff leszek
    if( mParent!=null )
649
      {
650
      mParent.newJob(SORT, this, dep);
651
      DistortedMaster.newSlave(mParent);
652
      }
653
    else if( mSurfaceParent!=null )
654
      {
655
      mSurfaceParent.newJob(SORT, this, dep);
656
      DistortedMaster.newSlave(mSurfaceParent);
657
      }
658
    else
659
      {
660
      mPostprocess = dep;
661
      }
662 13687207 leszek
    }
663
664 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
665 27f42cd6 leszek
/**
666
 * Returns the DistortedEffectsPostprocess object that's in the Node.
667
 *
668
 * @return The DistortedEffectsPostprocess contained in the Node.
669
 */
670
  public DistortedEffectsPostprocess getEffectsPostprocess()
671
    {
672
    return mPostprocess;
673
    }
674
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676 6a06a912 Leszek Koltunski
/**
677 421c2728 Leszek Koltunski
 * Returns the DistortedEffects object that's in the Node.
678 6a06a912 Leszek Koltunski
 * 
679 421c2728 Leszek Koltunski
 * @return The DistortedEffects contained in the Node.
680 6a06a912 Leszek Koltunski
 */
681 421c2728 Leszek Koltunski
  public DistortedEffects getEffects()
682 6a06a912 Leszek Koltunski
    {
683 07d8ef09 Leszek Koltunski
    return mEffects;
684 4e2382f3 Leszek Koltunski
    }
685
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687
/**
688 c5369f1b leszek
 * Returns the DistortedInputSurface object that's in the Node.
689 4e2382f3 Leszek Koltunski
 *
690 c5369f1b leszek
 * @return The DistortedInputSurface contained in the Node.
691 4e2382f3 Leszek Koltunski
 */
692 c5369f1b leszek
  public DistortedInputSurface getSurface()
693 4e2382f3 Leszek Koltunski
    {
694 c5369f1b leszek
    return mSurface;
695 6a06a912 Leszek Koltunski
    }
696
697 8c327653 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
698
/**
699 23eecbd9 Leszek Koltunski
 * Resizes the DistortedFramebuffer object that we render this Node to.
700 8c327653 Leszek Koltunski
 */
701 23eecbd9 Leszek Koltunski
  public void resize(int width, int height)
702 8c327653 Leszek Koltunski
    {
703 23eecbd9 Leszek Koltunski
    mFboW = width;
704
    mFboH = height;
705
706
    if ( mData.mFBO !=null )
707
      {
708
      // TODO: potentially allocate a new NodeData if we have to
709
      mData.mFBO.resize(width,height);
710
      }
711
    }
712
713
///////////////////////////////////////////////////////////////////////////////////////////////////
714
/**
715
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
716
 */
717
  public void enableDepthStencil(int depthStencil)
718
    {
719
    mFboDepthStencil = depthStencil;
720
721
    if ( mData.mFBO !=null )
722
      {
723
      // TODO: potentially allocate a new NodeData if we have to
724
      mData.mFBO.enableDepthStencil(depthStencil);
725
      }
726 8c327653 Leszek Koltunski
    }
727 6a06a912 Leszek Koltunski
728 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
729
// APIs that control how to set the OpenGL state just before rendering this Node.
730 c834348d leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
731
/**
732
 * When rendering this Node, use ColorMask (r,g,b,a).
733
 *
734
 * @param r Write to the RED color channel when rendering this Node?
735
 * @param g Write to the GREEN color channel when rendering this Node?
736
 * @param b Write to the BLUE color channel when rendering this Node?
737
 * @param a Write to the ALPHA channel when rendering this Node?
738
 */
739 13687207 leszek
  @SuppressWarnings("unused")
740 c834348d leszek
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
741
    {
742
    mState.glColorMask(r,g,b,a);
743
    }
744
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746
/**
747
 * When rendering this Node, switch on writing to Depth buffer?
748
 *
749
 * @param mask Write to the Depth buffer when rendering this Node?
750
 */
751 13687207 leszek
  @SuppressWarnings("unused")
752 c834348d leszek
  public void glDepthMask(boolean mask)
753
    {
754
    mState.glDepthMask(mask);
755
    }
756
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758
/**
759
 * When rendering this Node, which bits of the Stencil buffer to write to?
760
 *
761
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
762
 */
763 13687207 leszek
  @SuppressWarnings("unused")
764 c834348d leszek
  public void glStencilMask(int mask)
765
    {
766
    mState.glStencilMask(mask);
767
    }
768
769
///////////////////////////////////////////////////////////////////////////////////////////////////
770
/**
771
 * When rendering this Node, which Tests to enable?
772
 *
773
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
774
 */
775 13687207 leszek
  @SuppressWarnings("unused")
776 c834348d leszek
  public void glEnable(int test)
777
    {
778
    mState.glEnable(test);
779
    }
780
781
///////////////////////////////////////////////////////////////////////////////////////////////////
782
/**
783
 * When rendering this Node, which Tests to enable?
784
 *
785
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
786
 */
787 13687207 leszek
  @SuppressWarnings("unused")
788 c834348d leszek
  public void glDisable(int test)
789
    {
790
    mState.glDisable(test);
791
    }
792
793
///////////////////////////////////////////////////////////////////////////////////////////////////
794
/**
795
 * When rendering this Node, use the following StencilFunc.
796
 *
797
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
798
 * @param ref  Reference valut to compare our stencil with.
799
 * @param mask Mask used when comparing.
800
 */
801 13687207 leszek
  @SuppressWarnings("unused")
802 c834348d leszek
  public void glStencilFunc(int func, int ref, int mask)
803
    {
804
    mState.glStencilFunc(func,ref,mask);
805
    }
806
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
/**
809
 * When rendering this Node, use the following StencilOp.
810
 * <p>
811
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
812
 *
813
 * @param sfail  What to do when Stencil Test fails.
814
 * @param dpfail What to do when Depth Test fails.
815
 * @param dppass What to do when Depth Test passes.
816
 */
817 13687207 leszek
  @SuppressWarnings("unused")
818 c834348d leszek
  public void glStencilOp(int sfail, int dpfail, int dppass)
819
    {
820
    mState.glStencilOp(sfail,dpfail,dppass);
821
    }
822
823
///////////////////////////////////////////////////////////////////////////////////////////////////
824
/**
825
 * When rendering this Node, use the following DepthFunc.
826
 *
827
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
828
 */
829 13687207 leszek
  @SuppressWarnings("unused")
830 c834348d leszek
  public void glDepthFunc(int func)
831
    {
832
    mState.glDepthFunc(func);
833
    }
834
835
///////////////////////////////////////////////////////////////////////////////////////////////////
836
/**
837
 * When rendering this Node, use the following Blending mode.
838
 * <p>
839
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
840
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
841
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
842
 *
843
 * @param src Source Blend function
844
 * @param dst Destination Blend function
845
 */
846 13687207 leszek
  @SuppressWarnings("unused")
847 c834348d leszek
  public void glBlendFunc(int src, int dst)
848
    {
849
    mState.glBlendFunc(src,dst);
850
    }
851 ad16ed3b Leszek Koltunski
852
///////////////////////////////////////////////////////////////////////////////////////////////////
853
/**
854
 * Before rendering this Node, clear the following buffers.
855
 * <p>
856
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
857
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
858
 * Default: 0
859
 *
860
 * @param mask bitwise OR of BUFFER_BITs to clear.
861
 */
862
  @SuppressWarnings("unused")
863
  public void glClear(int mask)
864
    {
865
    mState.glClear(mask);
866
    }
867 8c327653 Leszek Koltunski
  }