Project

General

Profile

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

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

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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
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 86d322b5 Leszek Koltunski
public class DistortedNode implements DistortedMaster.Slave
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
52 ffbe7ecf Leszek Koltunski
    Job(int t, DistortedNode n)
53 efe3d8fe leszek
      {
54
      type = t;
55
      node = n;
56
      }
57
    }
58
59
  private ArrayList<Job> mJobs = new ArrayList<>();
60
61 bd3da5b2 Leszek Koltunski
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
62
  private static long mNextNodeID =0;
63
64 f28fffc2 Leszek Koltunski
  private DistortedNode mParent;
65 be60d4ff leszek
  private DistortedOutputSurface mSurfaceParent;
66 05403bba Leszek Koltunski
  private MeshObject mMesh;
67 07d8ef09 Leszek Koltunski
  private DistortedEffects mEffects;
68 c5369f1b leszek
  private DistortedInputSurface mSurface;
69 c834348d leszek
  private DistortedRenderState mState;
70 6a06a912 Leszek Koltunski
  private NodeData mData;
71 23eecbd9 Leszek Koltunski
  private int mFboW, mFboH, mFboDepthStencil;
72 bd3da5b2 Leszek Koltunski
73 6a06a912 Leszek Koltunski
  private class NodeData
74
    {
75 bd3da5b2 Leszek Koltunski
    long ID;
76 6a06a912 Leszek Koltunski
    int numPointingNodes;
77 50642a86 Leszek Koltunski
    long currTime;
78 af27df87 leszek
    ArrayList<Long> key;
79 8c327653 Leszek Koltunski
    DistortedFramebuffer mFBO;
80 6a06a912 Leszek Koltunski
81 af27df87 leszek
    NodeData(long id, ArrayList<Long> k)
82 6a06a912 Leszek Koltunski
      {
83 bd3da5b2 Leszek Koltunski
      ID              = id;
84 af27df87 leszek
      key             = k;
85 bd3da5b2 Leszek Koltunski
      numPointingNodes= 1;
86 50642a86 Leszek Koltunski
      currTime        =-1;
87 8c327653 Leszek Koltunski
      mFBO            = null;
88 6a06a912 Leszek Koltunski
      }
89 bd3da5b2 Leszek Koltunski
    }
90 6a06a912 Leszek Koltunski
 
91 0c303a2c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 436899f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 7b8086eb Leszek Koltunski
  static synchronized void onDestroy()
112 436899f2 Leszek Koltunski
    {
113
    mNextNodeID = 0;
114
    mMapNodeID.clear();
115
    }
116
117 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119
  private ArrayList<Long> generateIDList()
120
    {
121 9361b337 Leszek Koltunski
    ArrayList<Long> ret = new ArrayList<>();
122 7691a39f leszek
123
    if( mNumChildren[0]==0 )
124
      {
125 c9f953c2 Leszek Koltunski
      // 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 7691a39f leszek
      ret.add(-mEffects.getID());
128
      }
129 c9f953c2 Leszek Koltunski
    else
130 6a06a912 Leszek Koltunski
      {
131 c9f953c2 Leszek Koltunski
      DistortedNode node;
132 6a06a912 Leszek Koltunski
   
133 c9f953c2 Leszek Koltunski
      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 6a06a912 Leszek Koltunski
    return ret;
159
    }
160
161 fee0865c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// Debug - print all the Node IDs
163 c204c69d leszek
164 af27df87 leszek
  @SuppressWarnings("unused")
165 fee0865c Leszek Koltunski
  void debug(int depth)
166
    {
167
    String tmp="";
168
    int i;
169
170
    for(i=0; i<depth; i++) tmp +="   ";
171 07037b8a leszek
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
172 f28fffc2 Leszek Koltunski
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
173
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
174 fee0865c Leszek Koltunski
175 1942537e Leszek Koltunski
    android.util.Log.e("NODE", tmp);
176 fee0865c Leszek Koltunski
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 af27df87 leszek
  @SuppressWarnings("unused")
185 fee0865c Leszek Koltunski
  static void debugMap()
186
    {
187
    NodeData tmp;
188
189
    for(ArrayList<Long> key: mMapNodeID.keySet())
190
      {
191
      tmp = mMapNodeID.get(key);
192 c204c69d leszek
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
193
      }
194
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197 f28fffc2 Leszek Koltunski
// tree isomorphism algorithm
198 c204c69d leszek
199 f28fffc2 Leszek Koltunski
  private void adjustIsomorphism()
200 c204c69d leszek
    {
201
    ArrayList<Long> newList = generateIDList();
202
    NodeData newData = mMapNodeID.get(newList);
203
204 f28fffc2 Leszek Koltunski
    if( newData!=null )
205
      {
206
      newData.numPointingNodes++;
207
      }
208
    else
209 c204c69d leszek
      {
210 af27df87 leszek
      newData = new NodeData(++mNextNodeID,newList);
211 c204c69d leszek
      mMapNodeID.put(newList,newData);
212
      }
213 07037b8a leszek
214 f28fffc2 Leszek Koltunski
    boolean deleteOldFBO = false;
215
    boolean createNewFBO = false;
216 c204c69d leszek
217 f28fffc2 Leszek Koltunski
    if( --mData.numPointingNodes==0 )
218 c204c69d leszek
      {
219 f28fffc2 Leszek Koltunski
      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 c204c69d leszek
233 f28fffc2 Leszek Koltunski
    if( deleteOldFBO && createNewFBO )
234
      {
235
      newData.mFBO = mData.mFBO;  // just copy over
236 eadf0859 leszek
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
237 f28fffc2 Leszek Koltunski
      }
238
    else if( deleteOldFBO )
239
      {
240
      mData.mFBO.markForDeletion();
241 eadf0859 leszek
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
242 f28fffc2 Leszek Koltunski
      mData.mFBO = null;
243
      }
244
    else if( createNewFBO )
245
      {
246 23eecbd9 Leszek Koltunski
      int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
247
      int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
248 9ed80185 Leszek Koltunski
      newData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
249 eadf0859 leszek
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
250 f28fffc2 Leszek Koltunski
      }
251 af27df87 leszek
252 f28fffc2 Leszek Koltunski
    mData = newData;
253 c204c69d leszek
254 f28fffc2 Leszek Koltunski
    if( mParent!=null ) mParent.adjustIsomorphism();
255 fee0865c Leszek Koltunski
    }
256 c204c69d leszek
257 270c27bc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259 70b6a155 Leszek Koltunski
  int markStencilAndDepth(long currTime, DistortedOutputSurface surface, EffectQueuePostprocess queue)
260 270c27bc Leszek Koltunski
    {
261
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
262
263
    if( input.setAsInput() )
264
      {
265 c9a24bfb Leszek Koltunski
      surface.setAsOutput();
266 984dc935 Leszek Koltunski
      DistortedRenderState.setUpStencilMark();
267 70b6a155 Leszek Koltunski
      mEffects.drawPriv(mSurface.getWidth() /2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, queue.getHalo()*surface.mMipmap);
268 984dc935 Leszek Koltunski
      DistortedRenderState.unsetUpStencilMark();
269
270 270c27bc Leszek Koltunski
      return 1;
271
      }
272
    return 0;
273
    }
274
275 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 b3120b1b leszek
      surface.setAsOutput(currTime);
285 39086ebb leszek
      mState.apply();
286 270c27bc Leszek Koltunski
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
287 39086ebb leszek
      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 0c303a2c Leszek Koltunski
      if( mData.mFBO==null )
310
        {
311 23eecbd9 Leszek Koltunski
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
312
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
313 9ed80185 Leszek Koltunski
        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
314 0c303a2c Leszek Koltunski
        }
315
316 95c441a2 leszek
      mData.mFBO.setAsOutput(currTime);
317 39086ebb leszek
318
      if( mSurface.setAsInput() )
319
        {
320
        numRenders++;
321
        DistortedEffects.blitPriv(mData.mFBO);
322
        }
323
324 b2939df4 Leszek Koltunski
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren);
325 39086ebb leszek
      }
326
327
    return numRenders;
328
    }
329
330 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332
  void setSurfaceParent(DistortedOutputSurface dep)
333
    {
334
    mSurfaceParent = dep;
335
    mParent = null;
336
    }
337
338 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 70b6a155 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
367
// PUBLIC API
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369
/**
370 a09ada4c Leszek Koltunski
 * Constructs new Node.
371 6a06a912 Leszek Koltunski
 *     
372 c5369f1b leszek
 * @param surface InputSurface to put into the new Node.
373 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to put into the new Node.
374 05403bba Leszek Koltunski
 * @param mesh MeshObject to put into the new Node.
375 6a06a912 Leszek Koltunski
 */
376 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
377 6a06a912 Leszek Koltunski
    {
378 c5369f1b leszek
    mSurface       = surface;
379 8ca9f899 Leszek Koltunski
    mEffects       = effects;
380
    mMesh          = mesh;
381 c834348d leszek
    mState         = new DistortedRenderState();
382 8ca9f899 Leszek Koltunski
    mChildren      = null;
383
    mNumChildren   = new int[1];
384
    mNumChildren[0]= 0;
385 f28fffc2 Leszek Koltunski
    mParent        = null;
386 be60d4ff leszek
    mSurfaceParent = null;
387 f28fffc2 Leszek Koltunski
388 23eecbd9 Leszek Koltunski
    mFboW            = 0;  // i.e. take this from
389
    mFboH            = 0;  // mSurface's dimensions
390
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
391
392 9361b337 Leszek Koltunski
    ArrayList<Long> list = new ArrayList<>();
393 c5369f1b leszek
    list.add(mSurface.getID());
394 7691a39f leszek
    list.add(-mEffects.getID());
395 1942537e Leszek Koltunski
396 6a06a912 Leszek Koltunski
    mData = mMapNodeID.get(list);
397
   
398
    if( mData!=null )
399
      {
400
      mData.numPointingNodes++;
401
      }
402
    else
403
      {
404 af27df87 leszek
      mData = new NodeData(++mNextNodeID,list);
405 1942537e Leszek Koltunski
      mMapNodeID.put(list, mData);
406 6a06a912 Leszek Koltunski
      }
407 26a4e5f6 leszek
408
    mEffects.newNode(this);
409 6a06a912 Leszek Koltunski
    }
410
411
///////////////////////////////////////////////////////////////////////////////////////////////////  
412
/**
413 a09ada4c Leszek Koltunski
 * Copy-constructs new Node from another Node.
414 6a06a912 Leszek Koltunski
 *     
415 a09ada4c Leszek Koltunski
 * @param node The DistortedNode to copy data from.
416 6a06a912 Leszek Koltunski
 * @param flags bit field composed of a subset of the following:
417 29a06526 Leszek Koltunski
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
418 6a06a912 Leszek Koltunski
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
419 29a06526 Leszek Koltunski
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
420 6a06a912 Leszek Koltunski
 */
421 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedNode node, int flags)
422 6a06a912 Leszek Koltunski
    {
423 be60d4ff leszek
    mEffects      = new DistortedEffects(node.mEffects,flags);
424
    mMesh         = node.mMesh;
425
    mState        = new DistortedRenderState();
426
    mParent       = null;
427
    mSurfaceParent= null;
428 9361b337 Leszek Koltunski
429 23eecbd9 Leszek Koltunski
    mFboW            = node.mFboW;
430
    mFboH            = node.mFboH;
431
    mFboDepthStencil = node.mFboDepthStencil;
432
433 29a06526 Leszek Koltunski
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
434 e7a20702 Leszek Koltunski
      {
435 c5369f1b leszek
      mSurface = node.mSurface;
436 e7a20702 Leszek Koltunski
      }
437
    else
438
      {
439 c5369f1b leszek
      int w = node.mSurface.getWidth();
440
      int h = node.mSurface.getHeight();
441 8ca9f899 Leszek Koltunski
442 c5369f1b leszek
      if( node.mSurface instanceof DistortedTexture )
443 8ca9f899 Leszek Koltunski
        {
444 09ab7524 Leszek Koltunski
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
445 8ca9f899 Leszek Koltunski
        }
446 c5369f1b leszek
      else if( node.mSurface instanceof DistortedFramebuffer )
447 8ca9f899 Leszek Koltunski
        {
448 23eecbd9 Leszek Koltunski
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
449 89de975c leszek
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 9ed80185 Leszek Koltunski
        mSurface = new DistortedFramebuffer(1,depthStencil,DistortedSurface.TYPE_TREE,w,h);
457 8ca9f899 Leszek Koltunski
        }
458 e7a20702 Leszek Koltunski
      }
459 9361b337 Leszek Koltunski
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
460 6a06a912 Leszek Koltunski
      {
461 c204c69d leszek
      if( node.mChildren==null )     // do NOT copy over the NULL!
462
        {
463
        node.mChildren = new ArrayList<>(2);
464
        }
465
466 6a06a912 Leszek Koltunski
      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 af27df87 leszek
      mData = new NodeData(++mNextNodeID,list);
487 6a06a912 Leszek Koltunski
      mMapNodeID.put(list, mData);
488
      }
489 26a4e5f6 leszek
490
    mEffects.newNode(this);
491 6a06a912 Leszek Koltunski
    }
492 c204c69d leszek
493 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
494
/**
495
 * Adds a new child to the last position in the list of our Node's children.
496 c204c69d leszek
 * <p>
497
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
498 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
499 c204c69d leszek
 *
500 6a06a912 Leszek Koltunski
 * @param node The new Node to add.
501
 */
502 c204c69d leszek
  public void attach(DistortedNode node)
503 6a06a912 Leszek Koltunski
    {
504 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
505 efe3d8fe leszek
    DistortedMaster.newSlave(this);
506 6a06a912 Leszek Koltunski
    }
507 c204c69d leszek
508 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
509
/**
510
 * Adds a new child to the last position in the list of our Node's children.
511 c204c69d leszek
 * <p>
512
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
513 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
514 c204c69d leszek
 *
515 c5369f1b leszek
 * @param surface InputSurface to initialize our child Node with.
516 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to initialize our child Node with.
517 05403bba Leszek Koltunski
 * @param mesh MeshObject to initialize our child Node with.
518 6a06a912 Leszek Koltunski
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
519
 */
520 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
521 6a06a912 Leszek Koltunski
    {
522 c204c69d leszek
    DistortedNode node = new DistortedNode(surface,effects,mesh);
523 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
524 efe3d8fe leszek
    DistortedMaster.newSlave(this);
525 c204c69d leszek
    return node;
526
    }
527 f8377ef8 leszek
528 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Removes the first occurrence of a specified child from the list of children of our Node.
531 c204c69d leszek
 * <p>
532
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
533 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
534 c204c69d leszek
 *
535 6a06a912 Leszek Koltunski
 * @param node The Node to remove.
536
 */
537 c204c69d leszek
  public void detach(DistortedNode node)
538 6a06a912 Leszek Koltunski
    {
539 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETACH,node));
540 efe3d8fe leszek
    DistortedMaster.newSlave(this);
541 6a06a912 Leszek Koltunski
    }
542 a09ada4c Leszek Koltunski
543 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
544
/**
545
 * Removes the first occurrence of a specified child from the list of children of our Node.
546 a09ada4c Leszek Koltunski
 * <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 c204c69d leszek
 * <p>
550
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
551 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
552 a09ada4c Leszek Koltunski
 *
553 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to remove.
554 6a06a912 Leszek Koltunski
 */
555 c204c69d leszek
  public void detach(DistortedEffects effects)
556 6a06a912 Leszek Koltunski
    {
557 07d8ef09 Leszek Koltunski
    long id = effects.getID();
558 a09ada4c Leszek Koltunski
    DistortedNode node;
559 efe3d8fe leszek
    boolean detached = false;
560 a09ada4c Leszek Koltunski
561 6a06a912 Leszek Koltunski
    for(int i=0; i<mNumChildren[0]; i++)
562
      {
563
      node = mChildren.get(i);
564 a09ada4c Leszek Koltunski
565 efe3d8fe leszek
      if( node.getEffects().getID()==id )
566 6a06a912 Leszek Koltunski
        {
567 efe3d8fe leszek
        detached = true;
568 ffbe7ecf Leszek Koltunski
        mJobs.add(new Job(DETACH,node));
569 efe3d8fe leszek
        DistortedMaster.newSlave(this);
570 c204c69d leszek
        break;
571 6a06a912 Leszek Koltunski
        }
572
      }
573 8baa1fe6 Leszek Koltunski
574
    if( !detached )
575
      {
576
      // if we failed to detach any, it still might be the case that
577 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
578
      int num = mJobs.size();
579
      Job job;
580 a09ada4c Leszek Koltunski
581 efe3d8fe leszek
      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 6a06a912 Leszek Koltunski
      }
592 c204c69d leszek
    }
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 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
600 c204c69d leszek
 */
601
  public void detachAll()
602
    {
603 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETALL,null));
604 efe3d8fe leszek
    DistortedMaster.newSlave(this);
605 c204c69d leszek
    }
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 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
611 c204c69d leszek
 * Java has no multiple inheritance.
612 d3725071 Leszek Koltunski
 *
613
 * @y.exclude
614 c204c69d leszek
 */
615 efe3d8fe leszek
  public void doWork()
616 c204c69d leszek
    {
617 efe3d8fe leszek
    int num = mJobs.size();
618
    Job job;
619
620
    int numChanges=0;
621
622
    for(int i=0; i<num; i++)
623 6a06a912 Leszek Koltunski
      {
624 efe3d8fe leszek
      job = mJobs.remove(0);
625 af27df87 leszek
626 efe3d8fe leszek
      switch(job.type)
627 af27df87 leszek
        {
628 efe3d8fe leszek
        case ATTACH: numChanges++;
629
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
630
                     job.node.mParent = this;
631 be60d4ff leszek
                     job.node.mSurfaceParent = null;
632
                     DistortedMaster.addSorted(mChildren,job.node);
633 efe3d8fe leszek
                     mNumChildren[0]++;
634
                     break;
635
        case DETACH: numChanges++;
636
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
637
                       {
638
                       job.node.mParent = null;
639 be60d4ff leszek
                       job.node.mSurfaceParent = null;
640 efe3d8fe leszek
                       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 be60d4ff leszek
                         tmp.mSurfaceParent = null;
653 efe3d8fe leszek
                         }
654
655
                       mNumChildren[0] = 0;
656
                       }
657
                     break;
658 ffbe7ecf Leszek Koltunski
        case SORT  : mChildren.remove(job.node);
659 be60d4ff leszek
                     DistortedMaster.addSorted(mChildren,job.node);
660 efe3d8fe leszek
                     break;
661 af27df87 leszek
        }
662 efe3d8fe leszek
      }
663 f28fffc2 Leszek Koltunski
664 be60d4ff leszek
    if( numChanges>0 ) adjustIsomorphism();
665 6a06a912 Leszek Koltunski
    }
666 13687207 leszek
667 27f42cd6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
668 6a06a912 Leszek Koltunski
/**
669 421c2728 Leszek Koltunski
 * Returns the DistortedEffects object that's in the Node.
670 6a06a912 Leszek Koltunski
 * 
671 421c2728 Leszek Koltunski
 * @return The DistortedEffects contained in the Node.
672 6a06a912 Leszek Koltunski
 */
673 421c2728 Leszek Koltunski
  public DistortedEffects getEffects()
674 6a06a912 Leszek Koltunski
    {
675 07d8ef09 Leszek Koltunski
    return mEffects;
676 4e2382f3 Leszek Koltunski
    }
677
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679
/**
680 c5369f1b leszek
 * Returns the DistortedInputSurface object that's in the Node.
681 4e2382f3 Leszek Koltunski
 *
682 c5369f1b leszek
 * @return The DistortedInputSurface contained in the Node.
683 4e2382f3 Leszek Koltunski
 */
684 c5369f1b leszek
  public DistortedInputSurface getSurface()
685 4e2382f3 Leszek Koltunski
    {
686 c5369f1b leszek
    return mSurface;
687 6a06a912 Leszek Koltunski
    }
688
689 f1a82766 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8c327653 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
701
/**
702 23eecbd9 Leszek Koltunski
 * Resizes the DistortedFramebuffer object that we render this Node to.
703 8c327653 Leszek Koltunski
 */
704 23eecbd9 Leszek Koltunski
  public void resize(int width, int height)
705 8c327653 Leszek Koltunski
    {
706 23eecbd9 Leszek Koltunski
    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 8c327653 Leszek Koltunski
    }
730 6a06a912 Leszek Koltunski
731 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
732
// APIs that control how to set the OpenGL state just before rendering this Node.
733 c834348d leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 13687207 leszek
  @SuppressWarnings("unused")
743 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
755 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
767 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
779 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
791 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
805 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
821 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
833 c834348d leszek
  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 13687207 leszek
  @SuppressWarnings("unused")
850 c834348d leszek
  public void glBlendFunc(int src, int dst)
851
    {
852
    mState.glBlendFunc(src,dst);
853
    }
854 ad16ed3b Leszek Koltunski
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 8c327653 Leszek Koltunski
  }