Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ 9cae4322

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