Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ 85bfeb7a

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