Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ 163b8d7d

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