Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedNode.java @ 40f0cea6

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 2e569ff6 Leszek Koltunski
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3 d333eb6b Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 2e569ff6 Leszek Koltunski
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10 d333eb6b Leszek Koltunski
//                                                                                               //
11 2e569ff6 Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 d333eb6b Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 2e569ff6 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 d333eb6b Leszek Koltunski
//                                                                                               //
16 2e569ff6 Leszek Koltunski
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 fe82a979 Leszek Koltunski
package org.distorted.library.main;
22 6a06a912 Leszek Koltunski
23 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
24 8dccc3c2 Leszek Koltunski
25 715e7726 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
26 6c00149d Leszek Koltunski
27 6a06a912 Leszek Koltunski
import java.util.ArrayList;
28 c9f953c2 Leszek Koltunski
import java.util.Collections;
29 6a06a912 Leszek Koltunski
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 7602a827 Leszek Koltunski
public class DistortedNode implements InternalChildrenList.Parent
42 6a06a912 Leszek Koltunski
  {
43 209ea1c7 Leszek Koltunski
  private static final int DEFAULT_FBO_SIZE = 100;
44
45 3272cfb8 Leszek Koltunski
  private final DistortedEffects mEffects;
46
  private final InternalRenderState mState;
47
  private final InternalChildrenList mChildren;
48
  private InternalChildrenList.Parent mParent;
49 7602a827 Leszek Koltunski
  private InternalSurface mSurface;
50
  private InternalNodeData mData;
51 96381044 Leszek Koltunski
  private MeshBase mMesh;
52 11845a9e Leszek Koltunski
53 23eecbd9 Leszek Koltunski
  private int mFboW, mFboH, mFboDepthStencil;
54 11845a9e Leszek Koltunski
  private boolean mRenderWayOIT;
55 209ea1c7 Leszek Koltunski
  private float mFOV, mNear;
56 835b197e Leszek Koltunski
  private long mLastTime;
57 bd3da5b2 Leszek Koltunski
58 c43abe6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60
  public void markForDeletion()
61
    {
62 8bfefd68 Leszek Koltunski
    if( mData.removeData() )
63 c43abe6c Leszek Koltunski
      {
64 8bfefd68 Leszek Koltunski
      mData.mFBO.markForDeletion();
65
      mData.mFBO = null;
66 c43abe6c Leszek Koltunski
      }
67 d5b709df Leszek Koltunski
    }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70 46b572b5 Leszek Koltunski
// [3] --> the postprocessing queue. See EffectType.
71 c43abe6c Leszek Koltunski
72 d5b709df Leszek Koltunski
  long getBucket()
73
    {
74
    return mEffects.getQueues()[3].getID();
75 c43abe6c Leszek Koltunski
    }
76
77 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
  private ArrayList<Long> generateIDList()
80
    {
81 9361b337 Leszek Koltunski
    ArrayList<Long> ret = new ArrayList<>();
82 11845a9e Leszek Koltunski
    int numChildren = mChildren.getNumChildren();
83 7691a39f leszek
84 11845a9e Leszek Koltunski
    if( numChildren==0 )
85 7691a39f leszek
      {
86 c9f953c2 Leszek Koltunski
      // add a negative number so this leaf never gets confused with a internal node
87
      // with a single child that happens to have ID identical to some leaf's Effects ID.
88 7691a39f leszek
      ret.add(-mEffects.getID());
89
      }
90 c9f953c2 Leszek Koltunski
    else
91 6a06a912 Leszek Koltunski
      {
92 c9f953c2 Leszek Koltunski
      DistortedNode node;
93 6a06a912 Leszek Koltunski
   
94 11845a9e Leszek Koltunski
      for(int i=0; i<numChildren; i++)
95 c9f953c2 Leszek Koltunski
        {
96 11845a9e Leszek Koltunski
        node = mChildren.getChild(i);
97 c9f953c2 Leszek Koltunski
        ret.add(node.mData.ID);
98
        }
99
100
      // A bit questionable decision here - we are sorting the children IDs, which means
101
      // that order in which we draw the children is going to be undefined (well, this is not
102
      // strictly speaking true - when rendering, if no postprocessing and isomorphism are
103
      // involved, we *DO* render the children in order they were added; if however there
104
      // are two internal nodes with the same list of identical children, just added in a
105
      // different order each time, then we consider them isomorphic, i.e. identical and only
106
      // render the first one. If then two children of such 'pseudo-isomorphic' nodes are at
107
      // exactly the same Z-height this might result in some unexpected sights).
108
      //
109
      // Reason: with the children being sorted by postprocessing buckets, the order is
110
      // undefined anyway (although only when postprocessing is applied).
111
      //
112
      // See the consequences in the 'Olympic' app - remove a few leaves and add them back in
113 d5e053a5 Leszek Koltunski
      // different order. You will see the number of renders go back to the original 15.
114 c9f953c2 Leszek Koltunski
      Collections.sort(ret);
115
      }
116
117
    ret.add( 0, mSurface.getID() );
118
119 6a06a912 Leszek Koltunski
    return ret;
120
    }
121
122 9cae4322 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123 11845a9e Leszek Koltunski
/**
124
 * This is not really part of the public API. Has to be public only because it is a part of the
125 7602a827 Leszek Koltunski
 * InternalChildrenList.Parent interface.
126 11845a9e Leszek Koltunski
 *
127
 * @y.exclude
128
 */
129
  public void adjustIsomorphism()
130 9cae4322 Leszek Koltunski
    {
131 7602a827 Leszek Koltunski
    InternalNodeData newData = InternalNodeData.returnData(generateIDList());
132 8bfefd68 Leszek Koltunski
    boolean deleteOldFBO = mData.removeData();
133 11845a9e Leszek Koltunski
    boolean createNewFBO = (mChildren.getNumChildren()>0 && newData.mFBO==null);
134 c204c69d leszek
135 f28fffc2 Leszek Koltunski
    if( deleteOldFBO && createNewFBO )
136
      {
137 8bfefd68 Leszek Koltunski
      newData.mFBO = mData.mFBO;
138 f28fffc2 Leszek Koltunski
      }
139
    else if( deleteOldFBO )
140
      {
141
      mData.mFBO.markForDeletion();
142
      mData.mFBO = null;
143
      }
144
    else if( createNewFBO )
145
      {
146 11845a9e Leszek Koltunski
      newData.mFBO = allocateNewFBO();
147 f28fffc2 Leszek Koltunski
      }
148 af27df87 leszek
149 f28fffc2 Leszek Koltunski
    mData = newData;
150 c204c69d leszek
151 f28fffc2 Leszek Koltunski
    if( mParent!=null ) mParent.adjustIsomorphism();
152 fee0865c Leszek Koltunski
    }
153 c204c69d leszek
154 8dccc3c2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// return the total number of render calls issued
156
157 7602a827 Leszek Koltunski
  int drawNoBlend(long currTime, InternalOutputSurface surface)
158 8dccc3c2 Leszek Koltunski
    {
159 eddf0cb7 Leszek Koltunski
    InternalSurface input = getSurface();
160 8dccc3c2 Leszek Koltunski
161
    if( input.setAsInput() )
162
      {
163
      mState.apply();
164 b7074bc6 Leszek Koltunski
      GLES30.glDisable(GLES30.GL_BLEND);
165 835b197e Leszek Koltunski
      if( mLastTime==0 ) mLastTime=currTime;
166
      DistortedLibrary.drawPriv(mEffects, mMesh, surface, currTime, (currTime-mLastTime));
167
      mLastTime = currTime;
168 b7074bc6 Leszek Koltunski
      GLES30.glEnable(GLES30.GL_BLEND);
169 8dccc3c2 Leszek Koltunski
      return 1;
170
      }
171 c1a38ba3 Leszek Koltunski
172
    return 0;
173
    }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// Use the Order Independent Transparency method to draw a non-postprocessed child.
177
178 7602a827 Leszek Koltunski
  int drawOIT(long currTime, InternalOutputSurface surface)
179 c1a38ba3 Leszek Koltunski
    {
180 eddf0cb7 Leszek Koltunski
    InternalSurface input = getSurface();
181 c1a38ba3 Leszek Koltunski
182
    if( input.setAsInput() )
183
      {
184
      mState.apply();
185 835b197e Leszek Koltunski
      if( mLastTime==0 ) mLastTime=currTime;
186
      DistortedLibrary.drawPrivOIT(mEffects, mMesh, surface, currTime, (currTime-mLastTime));
187
      mLastTime = currTime;
188 c1a38ba3 Leszek Koltunski
      return 1;
189
      }
190 8dccc3c2 Leszek Koltunski
191
    return 0;
192
    }
193
194 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
195
// return the total number of render calls issued
196
197 7602a827 Leszek Koltunski
  int draw(long currTime, InternalOutputSurface surface)
198 39086ebb leszek
    {
199 eddf0cb7 Leszek Koltunski
    InternalSurface input = getSurface();
200 39086ebb leszek
201
    if( input.setAsInput() )
202
      {
203
      mState.apply();
204 835b197e Leszek Koltunski
      if( mLastTime==0 ) mLastTime=currTime;
205
      DistortedLibrary.drawPriv(mEffects, mMesh, surface, currTime, (currTime-mLastTime));
206
      mLastTime = currTime;
207 39086ebb leszek
      return 1;
208
      }
209
210
    return 0;
211
    }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// return the total number of render calls issued
215
216
  int renderRecursive(long currTime)
217
    {
218
    int numRenders = 0;
219 11845a9e Leszek Koltunski
    int numChildren = mChildren.getNumChildren();
220 39086ebb leszek
221 11845a9e Leszek Koltunski
    if( numChildren>0 && mData.notRenderedYetAtThisTime(currTime) )
222 39086ebb leszek
      {
223 d5b709df Leszek Koltunski
      DistortedNode node;
224
      long oldBucket=0, newBucket;
225
226 11845a9e Leszek Koltunski
      for (int i=0; i<numChildren; i++)
227 0c303a2c Leszek Koltunski
        {
228 d5b709df Leszek Koltunski
        node = mChildren.getChild(i);
229
        newBucket = node.getBucket();
230
        numRenders += node.renderRecursive(currTime);
231
        if( newBucket<oldBucket ) mChildren.rearrangeByBuckets(i,newBucket);
232
        else oldBucket=newBucket;
233 0c303a2c Leszek Koltunski
        }
234
235 11845a9e Leszek Koltunski
      if( mData.mFBO==null ) mData.mFBO = allocateNewFBO();
236 95c441a2 leszek
      mData.mFBO.setAsOutput(currTime);
237 39086ebb leszek
238
      if( mSurface.setAsInput() )
239
        {
240
        numRenders++;
241 7602a827 Leszek Koltunski
        DistortedLibrary.blitPriv(mData.mFBO);
242 39086ebb leszek
        }
243
244 11845a9e Leszek Koltunski
      numRenders += mData.mFBO.renderChildren(currTime,numChildren,mChildren,0, mRenderWayOIT);
245 39086ebb leszek
      }
246
247
    return numRenders;
248
    }
249
250 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
251
252 11845a9e Leszek Koltunski
  private DistortedFramebuffer allocateNewFBO()
253 be60d4ff leszek
    {
254 9ecac8cd Leszek Koltunski
    int width, height;
255
256
    if( mFboW>0 && mFboH>0 )
257
      {
258
      width = mFboW;
259
      height= mFboH;
260
      }
261
    else
262
      {
263
      if( mSurface instanceof DistortedFramebuffer )
264
        {
265
        DistortedFramebuffer fbo = (DistortedFramebuffer)mSurface;
266
        width = fbo.mWidth;
267
        height= fbo.mHeight;
268
        }
269
      else
270
        {
271 209ea1c7 Leszek Koltunski
        width = DEFAULT_FBO_SIZE;
272
        height= DEFAULT_FBO_SIZE;
273 9ecac8cd Leszek Koltunski
        }
274
      }
275
276 209ea1c7 Leszek Koltunski
    DistortedFramebuffer fbo = new DistortedFramebuffer(1,mFboDepthStencil, InternalSurface.TYPE_TREE, width, height);
277
278
    if( mFOV!=InternalOutputSurface.DEFAULT_FOV || mNear!=InternalOutputSurface.DEFAULT_NEAR )
279
      {
280
      fbo.setProjection(mFOV,mNear);
281
      }
282
283
    return fbo;
284 11845a9e Leszek Koltunski
    }
285
286 3272cfb8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
287
288
  void resetLastTime()
289
    {
290
    mLastTime = 0;
291
    }
292
293 11845a9e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
294
295 7602a827 Leszek Koltunski
  void setParent(InternalChildrenList.Parent parent)
296 11845a9e Leszek Koltunski
    {
297
    mParent = parent;
298 be60d4ff leszek
    }
299
300 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
301
// PUBLIC API
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
/**
304 a09ada4c Leszek Koltunski
 * Constructs new Node.
305 6a06a912 Leszek Koltunski
 *     
306 c5369f1b leszek
 * @param surface InputSurface to put into the new Node.
307 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to put into the new Node.
308 715e7726 Leszek Koltunski
 * @param mesh MeshBase to put into the new Node.
309 6a06a912 Leszek Koltunski
 */
310 7602a827 Leszek Koltunski
  public DistortedNode(InternalSurface surface, DistortedEffects effects, MeshBase mesh)
311 6a06a912 Leszek Koltunski
    {
312 835b197e Leszek Koltunski
    mLastTime      = 0;
313 c5369f1b leszek
    mSurface       = surface;
314 8ca9f899 Leszek Koltunski
    mEffects       = effects;
315
    mMesh          = mesh;
316 7602a827 Leszek Koltunski
    mState         = new InternalRenderState();
317
    mChildren      = new InternalChildrenList(this);
318 f28fffc2 Leszek Koltunski
    mParent        = null;
319 406e2f6b Leszek Koltunski
    mRenderWayOIT  = false;
320 f28fffc2 Leszek Koltunski
321 209ea1c7 Leszek Koltunski
    mFOV = InternalOutputSurface.DEFAULT_FOV;
322
    mNear= InternalOutputSurface.DEFAULT_NEAR;
323
324 23eecbd9 Leszek Koltunski
    mFboW            = 0;  // i.e. take this from
325 c90aca24 Leszek Koltunski
    mFboH            = 0;  // mEffects's stretch{X,Y}
326 23eecbd9 Leszek Koltunski
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
327
328 7602a827 Leszek Koltunski
    mData = InternalNodeData.returnData(generateIDList());
329 6a06a912 Leszek Koltunski
    }
330
331
///////////////////////////////////////////////////////////////////////////////////////////////////  
332
/**
333 a09ada4c Leszek Koltunski
 * Copy-constructs new Node from another Node.
334 6a06a912 Leszek Koltunski
 *     
335 a09ada4c Leszek Koltunski
 * @param node The DistortedNode to copy data from.
336 6a06a912 Leszek Koltunski
 * @param flags bit field composed of a subset of the following:
337 7602a827 Leszek Koltunski
 *        {@link DistortedLibrary#CLONE_SURFACE},  {@link DistortedLibrary#CLONE_MATRIX}, {@link DistortedLibrary#CLONE_VERTEX},
338
 *        {@link DistortedLibrary#CLONE_FRAGMENT} and {@link DistortedLibrary#CLONE_CHILDREN}.
339 29a06526 Leszek Koltunski
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
340 6a06a912 Leszek Koltunski
 */
341 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedNode node, int flags)
342 6a06a912 Leszek Koltunski
    {
343 835b197e Leszek Koltunski
    mLastTime     = 0;
344 be60d4ff leszek
    mEffects      = new DistortedEffects(node.mEffects,flags);
345
    mMesh         = node.mMesh;
346 7602a827 Leszek Koltunski
    mState        = new InternalRenderState();
347 be60d4ff leszek
    mParent       = null;
348 406e2f6b Leszek Koltunski
    mRenderWayOIT = false;
349 9361b337 Leszek Koltunski
350 209ea1c7 Leszek Koltunski
    mFOV = InternalOutputSurface.DEFAULT_FOV;
351
    mNear= InternalOutputSurface.DEFAULT_NEAR;
352
353 23eecbd9 Leszek Koltunski
    mFboW            = node.mFboW;
354
    mFboH            = node.mFboH;
355
    mFboDepthStencil = node.mFboDepthStencil;
356
357 7602a827 Leszek Koltunski
    if( (flags & DistortedLibrary.CLONE_SURFACE) != 0 )
358 e7a20702 Leszek Koltunski
      {
359 c5369f1b leszek
      mSurface = node.mSurface;
360 e7a20702 Leszek Koltunski
      }
361
    else
362
      {
363 c5369f1b leszek
      if( node.mSurface instanceof DistortedTexture )
364 8ca9f899 Leszek Koltunski
        {
365 c90aca24 Leszek Koltunski
        mSurface = new DistortedTexture(InternalSurface.TYPE_TREE);
366 8ca9f899 Leszek Koltunski
        }
367 c5369f1b leszek
      else if( node.mSurface instanceof DistortedFramebuffer )
368 8ca9f899 Leszek Koltunski
        {
369 d58b50e7 Leszek Koltunski
        DistortedFramebuffer fbo = (DistortedFramebuffer)node.mSurface;
370
371
        int w = fbo.getWidth();
372
        int h = fbo.getHeight();
373 23eecbd9 Leszek Koltunski
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
374 89de975c leszek
375 d58b50e7 Leszek Koltunski
        if( fbo.hasDepth() )
376 89de975c leszek
          {
377 d58b50e7 Leszek Koltunski
          boolean hasStencil = fbo.hasStencil();
378 89de975c leszek
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
379
          }
380
381 7602a827 Leszek Koltunski
        mSurface = new DistortedFramebuffer(1,depthStencil, InternalSurface.TYPE_TREE,w,h);
382 8ca9f899 Leszek Koltunski
        }
383 e7a20702 Leszek Koltunski
      }
384 11845a9e Leszek Koltunski
385 7602a827 Leszek Koltunski
    if( (flags & DistortedLibrary.CLONE_CHILDREN) != 0 )
386 6a06a912 Leszek Koltunski
      {
387
      mChildren = node.mChildren;
388
      }
389
    else
390
      {
391 7602a827 Leszek Koltunski
      mChildren = new InternalChildrenList(this);
392 6a06a912 Leszek Koltunski
      }
393 26a4e5f6 leszek
394 7602a827 Leszek Koltunski
    mData = InternalNodeData.returnData(generateIDList());
395 6a06a912 Leszek Koltunski
    }
396 c204c69d leszek
397 406e2f6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
398 b4999b55 Leszek Koltunski
  /**
399
   * Change the input surface while keeping everything else about the Node the same.
400
   *
401
   * @param surface The new input surface.
402
   */
403
  public void changeInputSurface(InternalSurface surface)
404
    {
405
    mSurface = surface;
406
    }
407
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409
410 406e2f6b Leszek Koltunski
  /**
411
   * When rendering this Node, should we use the Order Independent Transparency render more?
412
   * <p>
413
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
414
   * fragments in different ways depending on which fragments get rendered first, or the slower
415
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
416
   *
417
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
418
   */
419
  public void setOrderIndependentTransparency(boolean oit)
420
    {
421
    mRenderWayOIT = oit;
422
    }
423
424 12f9e4bb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
425
  /**
426
   * When rendering this Node, should we use the Order Independent Transparency render more?
427
   * <p>
428
   * There are two modes of rendering: the fast 'normal' way, which however renders transparent
429
   * fragments in different ways depending on which fragments get rendered first, or the slower
430
   * 'oit' way, which renders transparent fragments correctly regardless of their order.
431
   *
432
   * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
433
   * @param initialSize Initial number of transparent fragments we expect, in screenfulls.
434 f953bee0 Leszek Koltunski
   *                    I.e '1.0' means 'the scene we are going to render contains dialog_about 1 screen
435 12f9e4bb Leszek Koltunski
   *                    worth of transparent fragments'. Valid values: 0.0 &lt; initialSize &lt; 10.0
436
   *                    Even if you get this wrong, the library will detect that there are more
437
   *                    transparent fragments than it has space for and readjust its internal buffers,
438
   *                    but only after a few frames during which one will probably see missing objects.
439
   */
440
  public void setOrderIndependentTransparency(boolean oit, float initialSize)
441
    {
442
    mRenderWayOIT = oit;
443
444
    if( initialSize>0.0f && initialSize<10.0f )
445 7602a827 Leszek Koltunski
      DistortedLibrary.setSSBOSize(initialSize);
446 12f9e4bb Leszek Koltunski
    }
447
448 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
449
/**
450
 * Adds a new child to the last position in the list of our Node's children.
451 c204c69d leszek
 * <p>
452
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
453 7602a827 Leszek Koltunski
 * InternalMaster (by calling doWork())
454 c204c69d leszek
 *
455 6a06a912 Leszek Koltunski
 * @param node The new Node to add.
456
 */
457 c204c69d leszek
  public void attach(DistortedNode node)
458 6a06a912 Leszek Koltunski
    {
459 11845a9e Leszek Koltunski
    mChildren.attach(node);
460 6a06a912 Leszek Koltunski
    }
461 c204c69d leszek
462 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
463
/**
464
 * Adds a new child to the last position in the list of our Node's children.
465 c204c69d leszek
 * <p>
466
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
467 7602a827 Leszek Koltunski
 * InternalMaster (by calling doWork())
468 c204c69d leszek
 *
469 c5369f1b leszek
 * @param surface InputSurface to initialize our child Node with.
470 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to initialize our child Node with.
471 715e7726 Leszek Koltunski
 * @param mesh MeshBase to initialize our child Node with.
472 6a06a912 Leszek Koltunski
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
473
 */
474 7602a827 Leszek Koltunski
  public DistortedNode attach(InternalSurface surface, DistortedEffects effects, MeshBase mesh)
475 6a06a912 Leszek Koltunski
    {
476 11845a9e Leszek Koltunski
    return mChildren.attach(surface,effects,mesh);
477 c204c69d leszek
    }
478 f8377ef8 leszek
479 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
480
/**
481
 * Removes the first occurrence of a specified child from the list of children of our Node.
482 c204c69d leszek
 * <p>
483
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
484 7602a827 Leszek Koltunski
 * InternalMaster (by calling doWork())
485 c204c69d leszek
 *
486 6a06a912 Leszek Koltunski
 * @param node The Node to remove.
487
 */
488 c204c69d leszek
  public void detach(DistortedNode node)
489 6a06a912 Leszek Koltunski
    {
490 11845a9e Leszek Koltunski
    mChildren.detach(node);
491 6a06a912 Leszek Koltunski
    }
492 a09ada4c Leszek Koltunski
493 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
494
/**
495
 * Removes the first occurrence of a specified child from the list of children of our Node.
496 a09ada4c Leszek Koltunski
 * <p>
497
 * A bit questionable method as there can be many different Nodes attached as children, some
498
 * of them having the same Effects but - for instance - different Mesh. Use with care.
499 c204c69d leszek
 * <p>
500
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
501 7602a827 Leszek Koltunski
 * InternalMaster (by calling doWork())
502 a09ada4c Leszek Koltunski
 *
503 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to remove.
504 6a06a912 Leszek Koltunski
 */
505 c204c69d leszek
  public void detach(DistortedEffects effects)
506 6a06a912 Leszek Koltunski
    {
507 11845a9e Leszek Koltunski
    mChildren.detach(effects);
508 c204c69d leszek
    }
509
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511
/**
512
 * Removes all children Nodes.
513
 * <p>
514
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
515 7602a827 Leszek Koltunski
 * InternalMaster (by calling doWork())
516 c204c69d leszek
 */
517
  public void detachAll()
518
    {
519 11845a9e Leszek Koltunski
    mChildren.detachAll();
520 6a06a912 Leszek Koltunski
    }
521 13687207 leszek
522 27f42cd6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
523 6a06a912 Leszek Koltunski
/**
524 421c2728 Leszek Koltunski
 * Returns the DistortedEffects object that's in the Node.
525 6a06a912 Leszek Koltunski
 * 
526 421c2728 Leszek Koltunski
 * @return The DistortedEffects contained in the Node.
527 6a06a912 Leszek Koltunski
 */
528 421c2728 Leszek Koltunski
  public DistortedEffects getEffects()
529 6a06a912 Leszek Koltunski
    {
530 07d8ef09 Leszek Koltunski
    return mEffects;
531 4e2382f3 Leszek Koltunski
    }
532
533 a13dde77 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
534
  /**
535 eddf0cb7 Leszek Koltunski
   * Returns the surface this object gets rendered to.
536 a13dde77 Leszek Koltunski
   *
537 7602a827 Leszek Koltunski
   * @return The InternalSurface contained in the Node (if a leaf), or the FBO (if an internal Node)
538 a13dde77 Leszek Koltunski
   */
539 eddf0cb7 Leszek Koltunski
  public InternalSurface getSurface()
540 a13dde77 Leszek Koltunski
    {
541 11845a9e Leszek Koltunski
    return mChildren.getNumChildren()==0 ? mSurface : mData.mFBO;
542 a13dde77 Leszek Koltunski
    }
543
544 460a2b7d Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
545
  /**
546
   * Returns the FBO contained in this object, even if it is a leaf.
547
   *
548
   * @return The DistortedFramebuffer.
549
   */
550
  public DistortedFramebuffer getFramebuffer()
551
    {
552
    return mData.mFBO;
553
    }
554
555 f1a82766 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
556
/**
557
 * Returns the Mesh object that's in the Node.
558
 *
559
 * @return Mesh contained in the Node.
560
 */
561 715e7726 Leszek Koltunski
  public MeshBase getMesh()
562 f1a82766 Leszek Koltunski
    {
563
    return mMesh;
564
    }
565
566 96381044 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
567
/**
568
 * Set a new Mesh.
569
 */
570
  public void setMesh(MeshBase mesh)
571
    {
572
    mMesh = mesh;
573
    }
574
575 8c327653 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577 23eecbd9 Leszek Koltunski
 * Resizes the DistortedFramebuffer object that we render this Node to.
578 8c327653 Leszek Koltunski
 */
579 9ecac8cd Leszek Koltunski
  public void resizeFBO(int width, int height)
580 8c327653 Leszek Koltunski
    {
581 23eecbd9 Leszek Koltunski
    mFboW = width;
582
    mFboH = height;
583
584
    if ( mData.mFBO !=null )
585
      {
586
      // TODO: potentially allocate a new NodeData if we have to
587
      mData.mFBO.resize(width,height);
588
      }
589
    }
590
591 209ea1c7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
592
/**
593
 * Set Projection Matrix for the Framebuffer contained in this Node.
594
 * <p>
595
 * If this Node is a Leaf and has no Framebuffer in it, this call does nothing.
596
 *
597
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
598
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
599
 * @param near The Near plane.
600
 */
601
  public void setProjection(float fov, float near)
602
    {
603
    if( fov < 180.0f && fov >=0.0f )
604
      {
605
      mFOV = fov;
606
      }
607
608
    if( near<   1.0f && near> 0.0f )
609
      {
610
      mNear= near;
611
      }
612
    else if( near<=0.0f )
613
      {
614
      mNear = 0.01f;
615
      }
616
    else if( near>=1.0f )
617
      {
618
      mNear=0.99f;
619
      }
620
621
    if( mData.mFBO!=null )
622
      {
623
      mData.mFBO.setProjection(mFOV,mNear);
624
      }
625
    }
626
627 23eecbd9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
628
/**
629
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
630
 */
631
  public void enableDepthStencil(int depthStencil)
632
    {
633
    mFboDepthStencil = depthStencil;
634
635
    if ( mData.mFBO !=null )
636
      {
637
      // TODO: potentially allocate a new NodeData if we have to
638
      mData.mFBO.enableDepthStencil(depthStencil);
639
      }
640 8c327653 Leszek Koltunski
    }
641 6a06a912 Leszek Koltunski
642 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
643
// APIs that control how to set the OpenGL state just before rendering this Node.
644 c834348d leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
645
/**
646
 * When rendering this Node, use ColorMask (r,g,b,a).
647
 *
648
 * @param r Write to the RED color channel when rendering this Node?
649
 * @param g Write to the GREEN color channel when rendering this Node?
650
 * @param b Write to the BLUE color channel when rendering this Node?
651
 * @param a Write to the ALPHA channel when rendering this Node?
652
 */
653 13687207 leszek
  @SuppressWarnings("unused")
654 c834348d leszek
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
655
    {
656
    mState.glColorMask(r,g,b,a);
657
    }
658
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660
/**
661
 * When rendering this Node, switch on writing to Depth buffer?
662
 *
663
 * @param mask Write to the Depth buffer when rendering this Node?
664
 */
665 13687207 leszek
  @SuppressWarnings("unused")
666 c834348d leszek
  public void glDepthMask(boolean mask)
667
    {
668
    mState.glDepthMask(mask);
669
    }
670
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672
/**
673
 * When rendering this Node, which bits of the Stencil buffer to write to?
674
 *
675
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
676
 */
677 13687207 leszek
  @SuppressWarnings("unused")
678 c834348d leszek
  public void glStencilMask(int mask)
679
    {
680
    mState.glStencilMask(mask);
681
    }
682
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684
/**
685
 * When rendering this Node, which Tests to enable?
686
 *
687
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
688
 */
689 13687207 leszek
  @SuppressWarnings("unused")
690 c834348d leszek
  public void glEnable(int test)
691
    {
692
    mState.glEnable(test);
693
    }
694
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696
/**
697
 * When rendering this Node, which Tests to enable?
698
 *
699
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
700
 */
701 13687207 leszek
  @SuppressWarnings("unused")
702 c834348d leszek
  public void glDisable(int test)
703
    {
704
    mState.glDisable(test);
705
    }
706
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708
/**
709
 * When rendering this Node, use the following StencilFunc.
710
 *
711
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
712
 * @param ref  Reference valut to compare our stencil with.
713
 * @param mask Mask used when comparing.
714
 */
715 13687207 leszek
  @SuppressWarnings("unused")
716 c834348d leszek
  public void glStencilFunc(int func, int ref, int mask)
717
    {
718
    mState.glStencilFunc(func,ref,mask);
719
    }
720
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722
/**
723
 * When rendering this Node, use the following StencilOp.
724
 * <p>
725
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
726
 *
727
 * @param sfail  What to do when Stencil Test fails.
728
 * @param dpfail What to do when Depth Test fails.
729
 * @param dppass What to do when Depth Test passes.
730
 */
731 13687207 leszek
  @SuppressWarnings("unused")
732 c834348d leszek
  public void glStencilOp(int sfail, int dpfail, int dppass)
733
    {
734
    mState.glStencilOp(sfail,dpfail,dppass);
735
    }
736
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738
/**
739
 * When rendering this Node, use the following DepthFunc.
740
 *
741
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
742
 */
743 13687207 leszek
  @SuppressWarnings("unused")
744 c834348d leszek
  public void glDepthFunc(int func)
745
    {
746
    mState.glDepthFunc(func);
747
    }
748
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750
/**
751
 * When rendering this Node, use the following Blending mode.
752
 * <p>
753
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
754
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
755
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
756
 *
757
 * @param src Source Blend function
758
 * @param dst Destination Blend function
759
 */
760 13687207 leszek
  @SuppressWarnings("unused")
761 c834348d leszek
  public void glBlendFunc(int src, int dst)
762
    {
763
    mState.glBlendFunc(src,dst);
764
    }
765 ad16ed3b Leszek Koltunski
766
///////////////////////////////////////////////////////////////////////////////////////////////////
767
/**
768
 * Before rendering this Node, clear the following buffers.
769
 * <p>
770
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
771
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
772
 * Default: 0
773
 *
774
 * @param mask bitwise OR of BUFFER_BITs to clear.
775
 */
776
  @SuppressWarnings("unused")
777
  public void glClear(int mask)
778
    {
779
    mState.glClear(mask);
780
    }
781 40f0cea6 Leszek Koltunski
782
///////////////////////////////////////////////////////////////////////////////////////////////////
783
/**
784
 * Recursively print all the effect queues attached to the children Nodes and to this Node.
785
 */
786
  public void debug(int depth)
787
    {
788
    String dbg = mEffects.debug(depth);
789
    android.util.Log.e("D", dbg);
790
791
    int numChildren = mChildren.getNumChildren();
792
793
    for(int i=0; i<numChildren; i++)
794
      {
795
      DistortedNode node = mChildren.getChild(i);
796
      node.debug(depth+1);
797
      }
798
    }
799 8c327653 Leszek Koltunski
  }