Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 9a3607b3

1 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c5369f1b leszek
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
23 af4cc5db Leszek Koltunski
import android.opengl.Matrix;
24 86e99907 leszek
25 3521c6fe leszek
import org.distorted.library.effect.EffectQuality;
26 715e7726 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
27 3521c6fe leszek
28 a09ada4c Leszek Koltunski
import java.util.ArrayList;
29 af4cc5db Leszek Koltunski
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31 70b6a155 Leszek Koltunski
/**
32
 * This is not really part of the public API.
33
 *
34
 * @y.exclude
35
 */
36
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
37 af4cc5db Leszek Koltunski
{
38 89de975c leszek
/**
39
 * Do not create DEPTH or STENCIL attachment
40
 */
41 23eecbd9 Leszek Koltunski
  public static final int NO_DEPTH_NO_STENCIL = 0;
42 89de975c leszek
/**
43
 * Create DEPTH, but not STENCIL
44
 */
45 23eecbd9 Leszek Koltunski
  public static final int DEPTH_NO_STENCIL    = 1;
46 89de975c leszek
/**
47
 * Create both DEPTH and STENCIL
48
 */
49 23eecbd9 Leszek Koltunski
  public static final int BOTH_DEPTH_STENCIL  = 2;
50 89de975c leszek
51 efe3d8fe leszek
  private static final int ATTACH = 0;
52
  private static final int DETACH = 1;
53
  private static final int DETALL = 2;
54
  private static final int SORT   = 3;
55
56 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
57
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
58 406e2f6b Leszek Koltunski
  private boolean mRenderWayOIT;
59 a09ada4c Leszek Koltunski
60 efe3d8fe leszek
  private class Job
61
    {
62
    int type;
63
    DistortedNode node;
64
65 ffbe7ecf Leszek Koltunski
    Job(int t, DistortedNode n)
66 efe3d8fe leszek
      {
67
      type = t;
68
      node = n;
69
      }
70
    }
71
72
  private ArrayList<Job> mJobs = new ArrayList<>();
73
74 baa3989b Leszek Koltunski
  // Global buffers used for postprocessing.
75 9e771d06 Leszek Koltunski
  private static DistortedFramebuffer[] mBuffer=null;
76 60c1c622 leszek
77 b1058021 Leszek Koltunski
  float mFOV;
78 c2c08950 leszek
  float mDistance, mNear;
79 af4cc5db Leszek Koltunski
  float[] mProjectionMatrix;
80
81 89de975c leszek
  int mDepthStencilCreated;
82
  int mDepthStencil;
83 8ebbc730 Leszek Koltunski
  int[] mDepthStencilH;
84
  int[] mFBOH;
85
  private long[] mTime;
86 a436ccc5 leszek
87 a9f41fa3 leszek
  private float mClearR, mClearG, mClearB, mClearA;
88
  private float mClearDepth;
89 23eecbd9 Leszek Koltunski
  private int mClearStencil;
90 ad16ed3b Leszek Koltunski
  private int mClear;
91 78db8663 Leszek Koltunski
  float mMipmap;
92
93 61441ce2 Leszek Koltunski
  int mRealWidth;   // the Surface can be backed up with a texture that is
94
  int mRealHeight;  // larger than the viewport we have to it.
95
                    // mWidth,mHeight are the sizes of the Viewport, those -
96
                    // sizes of the backing up texture.
97 a4b182d4 Leszek Koltunski
98 5f7e4f2c Leszek Koltunski
  int mCurrFBO;     // internal current FBO (see Distorted.FBO_QUEUE_SIZE)
99
100 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102 8ebbc730 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int numfbos, int numcolors, int depthStencil, int fbo, int type)
103 af4cc5db Leszek Koltunski
    {
104 8ebbc730 Leszek Koltunski
    super(width,height,createColor,numfbos,numcolors,type);
105
106 406e2f6b Leszek Koltunski
    mRenderWayOIT = false;
107 5f7e4f2c Leszek Koltunski
    mCurrFBO      = 0;
108 406e2f6b Leszek Koltunski
109 8ebbc730 Leszek Koltunski
    mDepthStencilH = new int[numfbos];
110
    mFBOH          = new int[numfbos];
111
112
    mTime = new long[numfbos];
113
    for(int i=0; i<mNumFBOs;i++) mTime[i]=0;
114 af4cc5db Leszek Koltunski
115 a4b182d4 Leszek Koltunski
    mRealWidth = width;
116
    mRealHeight= height;
117
118 af4cc5db Leszek Koltunski
    mProjectionMatrix = new float[16];
119
120
    mFOV = 60.0f;
121 54fe333a leszek
    mNear=  0.5f;
122 af4cc5db Leszek Koltunski
123 23eecbd9 Leszek Koltunski
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
124 89de975c leszek
    mDepthStencil = depthStencil;
125
126
    mFBOH[0]         = fbo;
127
    mDepthStencilH[0]= 0;
128 a436ccc5 leszek
129 a9f41fa3 leszek
    mClearR = 0.0f;
130
    mClearG = 0.0f;
131
    mClearB = 0.0f;
132
    mClearA = 0.0f;
133
134
    mClearDepth = 1.0f;
135 23eecbd9 Leszek Koltunski
    mClearStencil = 0;
136 e6519ac8 Leszek Koltunski
    mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
137 a9f41fa3 leszek
138 8426bd6a Leszek Koltunski
    mMipmap = 1.0f;
139 78db8663 Leszek Koltunski
140 af4cc5db Leszek Koltunski
    createProjection();
141
    }
142
143 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145 be60d4ff leszek
  private void createProjection()
146 af4cc5db Leszek Koltunski
    {
147
    if( mWidth>0 && mHeight>1 )
148
      {
149
      if( mFOV>0.0f )  // perspective projection
150
        {
151 54fe333a leszek
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
152 8426bd6a Leszek Koltunski
        float q = mWidth*mNear;
153
        float c = mHeight*mNear;
154 54fe333a leszek
155 c2c08950 leszek
        float left   = -q/2;
156
        float right  = +q/2;
157
        float bottom = -c/2;
158
        float top    = +c/2;
159
        float near   =  c/a;
160 54fe333a leszek
161 8426bd6a Leszek Koltunski
        mDistance    = mHeight/a;
162 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
163
164
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
165
        }
166
      else             // parallel projection
167
        {
168 8426bd6a Leszek Koltunski
        float left   = -mWidth/2.0f;
169
        float right  = +mWidth/2.0f;
170
        float bottom = -mHeight/2.0f;
171
        float top    = +mHeight/2.0f;
172
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
173
        mDistance    = mWidth+mHeight;
174
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
175 af4cc5db Leszek Koltunski
176
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
177
        }
178
      }
179
    }
180
181 1d6d261e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183 56c6ca24 Leszek Koltunski
  private static void createPostprocessingBuffers(int width, int height, float near)
184 1d6d261e Leszek Koltunski
    {
185 9e771d06 Leszek Koltunski
    mBuffer = new DistortedFramebuffer[EffectQuality.LENGTH];
186 1d6d261e Leszek Koltunski
    float mipmap=1.0f;
187
188 baa3989b Leszek Koltunski
    for (int j=0; j<EffectQuality.LENGTH; j++)
189 1d6d261e Leszek Koltunski
      {
190 586b5fa1 Leszek Koltunski
      mBuffer[j] = new DistortedFramebuffer(Distorted.FBO_QUEUE_SIZE,2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
191 baa3989b Leszek Koltunski
      mBuffer[j].mMipmap = mipmap;
192
      mBuffer[j].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
193
      mBuffer[j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
194 a4b182d4 Leszek Koltunski
195 1d6d261e Leszek Koltunski
      mipmap *= EffectQuality.MULTIPLIER;
196
      }
197
198
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
199
200 e6519ac8 Leszek Koltunski
    GLES31.glStencilMask(0xff);
201
    GLES31.glDepthMask(true);
202 56c6ca24 Leszek Koltunski
    GLES31.glColorMask(true, true, true, true);
203
    GLES31.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
204 e6519ac8 Leszek Koltunski
    GLES31.glClearDepthf(1.0f);
205
    GLES31.glClearStencil(0);
206 1d6d261e Leszek Koltunski
207 baa3989b Leszek Koltunski
    for (int j=0; j<EffectQuality.LENGTH; j++)
208 1d6d261e Leszek Koltunski
      {
209 586b5fa1 Leszek Koltunski
      for(int k=0; k<Distorted.FBO_QUEUE_SIZE; k++)
210
        {
211
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBuffer[j].mFBOH[k]);
212
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k+1], 0);
213
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
214
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k  ], 0);
215
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
216
        }
217 1d6d261e Leszek Koltunski
      }
218 586b5fa1 Leszek Koltunski
219
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
220 1d6d261e Leszek Koltunski
    }
221
222 a4b182d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224 ce154014 Leszek Koltunski
  static synchronized void onDestroy()
225 a4b182d4 Leszek Koltunski
    {
226 df5f8dd9 Leszek Koltunski
    if( mBuffer!=null )
227 ce154014 Leszek Koltunski
      {
228 df5f8dd9 Leszek Koltunski
      for (int j = 0; j < EffectQuality.LENGTH; j++)
229
        {
230
        mBuffer[j] = null;
231
        }
232
233
      mBuffer = null;
234 ce154014 Leszek Koltunski
      }
235
    }
236 a4b182d4 Leszek Koltunski
237 ce154014 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
238 ae2802b1 Leszek Koltunski
// The postprocessing buffers mBuffer[] are generally speaking too large (there's just one static
239 2aef1f4d Leszek Koltunski
// set of them) so before we use them for output, we need to adjust the Viewport as if they were
240 ae2802b1 Leszek Koltunski
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
241
// adjust the texture coords - see the get{Width|Height}Correction functions.
242 b1058021 Leszek Koltunski
//
243
// Also, adjust the Buffers so their Projection is the same like the surface we are supposed to be
244
// rendering to.
245 a4b182d4 Leszek Koltunski
246 b1058021 Leszek Koltunski
  private static void clonePostprocessingViewportAndProjection(DistortedOutputSurface from)
247 ce154014 Leszek Koltunski
    {
248 b1058021 Leszek Koltunski
    if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight ||
249
        mBuffer[0].mFOV   != from.mFOV   || mBuffer[0].mNear   != from.mNear    )
250 a4b182d4 Leszek Koltunski
      {
251 ce154014 Leszek Koltunski
      DistortedOutputSurface surface;
252
253
      for(int i=0; i<EffectQuality.LENGTH; i++)
254
        {
255 baa3989b Leszek Koltunski
        surface = mBuffer[i];
256 ce154014 Leszek Koltunski
257 baa3989b Leszek Koltunski
        surface.mWidth  = (int)(from.mWidth *surface.mMipmap);
258
        surface.mHeight = (int)(from.mHeight*surface.mMipmap);
259 b1058021 Leszek Koltunski
        surface.mFOV    = from.mFOV;
260 baa3989b Leszek Koltunski
        surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
261 ce154014 Leszek Koltunski
262 baa3989b Leszek Koltunski
        surface.createProjection();
263 ce154014 Leszek Koltunski
264 baa3989b Leszek Koltunski
        int maxw = surface.mWidth  > surface.mRealWidth  ? surface.mWidth  : surface.mRealWidth;
265
        int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
266 a4b182d4 Leszek Koltunski
267 baa3989b Leszek Koltunski
        if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
268
          {
269
          surface.mRealWidth = maxw;
270
          surface.mRealHeight = maxh;
271 ce154014 Leszek Koltunski
272 baa3989b Leszek Koltunski
          surface.recreate();
273
          surface.create();
274 ce154014 Leszek Koltunski
          }
275
        }
276 a4b182d4 Leszek Koltunski
      }
277
    }
278
279 97020807 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
280
281
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer,int fbo)
282
    {
283
    GLES31.glViewport(0, 0, mWidth, mHeight);
284 5f7e4f2c Leszek Koltunski
    setAsOutput(currTime);
285 97020807 Leszek Koltunski
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
286
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
287
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
288
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
289
290
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
291
    GLES31.glStencilMask(0x00);
292
293
    DistortedEffects.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
294
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
295
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
296
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
297
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
298
299
    // clear buffers
300
    GLES31.glStencilMask(0xff);
301
    GLES31.glDepthMask(true);
302
    GLES31.glColorMask(true,true,true,true);
303
    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
304
    GLES31.glClearDepthf(1.0f);
305
    GLES31.glClearStencil(0);
306
307 5f7e4f2c Leszek Koltunski
    buffer.setAsOutput();
308 97020807 Leszek Koltunski
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo+1], 0);
309
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
310
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo  ], 0);
311
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
312
313
    return 1;
314
    }
315
316 66a0fa17 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318
  private static void oitClear(DistortedOutputSurface buffer)
319
    {
320 12f9e4bb Leszek Koltunski
    int counter = DistortedEffects.zeroOutAtomic();
321
    DistortedEffects.oitClear(buffer,counter);
322 66a0fa17 Leszek Koltunski
    GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
323
    }
324
325 6b962e80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
326
327 8ebbc730 Leszek Koltunski
  private int oitBuild(long time, DistortedOutputSurface buffer, int fbo)
328 6b962e80 Leszek Koltunski
    {
329 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, mWidth, mHeight);
330 5f7e4f2c Leszek Koltunski
    setAsOutput(time);
331 e6519ac8 Leszek Koltunski
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
332 586b5fa1 Leszek Koltunski
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
333 e6519ac8 Leszek Koltunski
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
334 586b5fa1 Leszek Koltunski
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
335 6b962e80 Leszek Koltunski
336 56c6ca24 Leszek Koltunski
    DistortedRenderState.colorDepthStencilOn();
337
    DistortedRenderState.enableDepthTest();
338 6b962e80 Leszek Koltunski
339 56c6ca24 Leszek Koltunski
    DistortedEffects.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
340 e6519ac8 Leszek Koltunski
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
341
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
342
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
343
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
344 6b962e80 Leszek Koltunski
345 56c6ca24 Leszek Koltunski
    DistortedRenderState.colorDepthStencilRestore();
346
    DistortedRenderState.restoreDepthTest();
347
348 8777ce17 Leszek Koltunski
    return 1;
349
    }
350
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352 baa3989b Leszek Koltunski
// two phases: 1. collapse the SSBO 2. blend the ssbo's color
353 8777ce17 Leszek Koltunski
354 1f19fba8 Leszek Koltunski
  private int oitRender(long currTime, int fbo)
355 8777ce17 Leszek Koltunski
    {
356 c207da02 Leszek Koltunski
    float corrW = getWidthCorrection();
357
    float corrH = getHeightCorrection();
358 c8d70463 Leszek Koltunski
359 5fdae11d Leszek Koltunski
    // Do the Collapse Pass only if we do have a Depth attachment.
360
    // Otherwise there's no point (in fact we then would create a feedback loop!)
361 88c7b603 Leszek Koltunski
362 1f19fba8 Leszek Koltunski
    if( mDepthStencilH[fbo] != 0 )
363 5fdae11d Leszek Koltunski
      {
364
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
365
      GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
366 1f19fba8 Leszek Koltunski
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mDepthStencilH[fbo]);
367 5fdae11d Leszek Koltunski
      DistortedRenderState.switchOffColorDepthStencil();
368
      DistortedEffects.oitCollapse(this, corrW, corrH);
369
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
370
      }
371 8777ce17 Leszek Koltunski
372 5f7e4f2c Leszek Koltunski
    setAsOutput(currTime);
373 33f59f22 Leszek Koltunski
    DistortedRenderState.switchColorDepthOnStencilOff();
374
    DistortedEffects.oitRender(this, corrW, corrH);
375
    DistortedRenderState.restoreColorDepthStencil();
376
377 8777ce17 Leszek Koltunski
    return 1;
378
    }
379
380 88c7b603 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
381
382
  void clear()
383
    {
384
    DistortedRenderState.colorDepthStencilOn();
385
    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
386
    GLES31.glClearDepthf(mClearDepth);
387
    GLES31.glClearStencil(mClearStencil);
388
    GLES31.glClear(mClear);
389
    DistortedRenderState.colorDepthStencilRestore();
390
    }
391
392 5f7e4f2c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
393
394
  void setCurrFBO(int fbo)
395
    {
396
    mCurrFBO = fbo;
397
    }
398
399 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
400 e02264ff leszek
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
401
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
402 9a3607b3 Leszek Koltunski
// to a whole buffer (lastQueue.postprocess) and merge it (this.oitBuild or blitWithDepth - depending
403
// on the type of rendering)
404 39086ebb leszek
405 406e2f6b Leszek Koltunski
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children, int fbo, boolean oit)
406 b2939df4 Leszek Koltunski
    {
407 2386a081 Leszek Koltunski
    int quality=0, numRenders=0, bucketChange=0;
408
    DistortedNode child;
409 70b6a155 Leszek Koltunski
    EffectQueuePostprocess lastQueue=null, currQueue;
410 33f59f22 Leszek Koltunski
    long lastBucket=0, currBucket;
411 9e771d06 Leszek Koltunski
    boolean renderDirectly=false;
412 56c6ca24 Leszek Koltunski
413 5f7e4f2c Leszek Koltunski
    setCurrFBO(fbo);
414
415
    if( mBuffer!=null )
416
      {
417 9e771d06 Leszek Koltunski
      for (int i=0; i<EffectQuality.LENGTH; i++) mBuffer[i].setCurrFBO(fbo);
418 5f7e4f2c Leszek Koltunski
      }
419
420 c1a38ba3 Leszek Koltunski
    if( oit && numChildren>0 )
421
      {
422
      oitClear(this);
423
      }
424
425 8e28b6ff leszek
    for(int i=0; i<numChildren; i++)
426 39086ebb leszek
      {
427 2386a081 Leszek Koltunski
      child = children.get(i);
428
      currQueue = child.getPostprocessQueue();
429 70b6a155 Leszek Koltunski
      currBucket= currQueue.getID();
430 b9798977 leszek
431 56c6ca24 Leszek Koltunski
      if( currBucket==0 )
432
        {
433 5f7e4f2c Leszek Koltunski
        setAsOutput(time);
434 c1a38ba3 Leszek Koltunski
435
        if( oit )
436
          {
437 2386a081 Leszek Koltunski
          numRenders += child.drawOIT(time, this);
438 c1a38ba3 Leszek Koltunski
          GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
439
          }
440
        else
441
          {
442 2386a081 Leszek Koltunski
          numRenders += child.draw(time, this);
443 c1a38ba3 Leszek Koltunski
          }
444 56c6ca24 Leszek Koltunski
        }
445 60c1c622 leszek
      else
446
        {
447 5f7e4f2c Leszek Koltunski
        if( mBuffer==null )
448
          {
449
          createPostprocessingBuffers(mWidth,mHeight,mNear);
450 9e771d06 Leszek Koltunski
          for (int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].setCurrFBO(fbo);
451 5f7e4f2c Leszek Koltunski
          }
452 c9a24bfb Leszek Koltunski
453 915b7b2b leszek
        if( lastBucket!=currBucket )
454 c9a24bfb Leszek Koltunski
          {
455 375b3950 Leszek Koltunski
          if( lastBucket==0 )
456
            {
457 b1058021 Leszek Koltunski
            clonePostprocessingViewportAndProjection(this);
458 375b3950 Leszek Koltunski
            }
459
          else
460 c9a24bfb Leszek Koltunski
            {
461 9e771d06 Leszek Koltunski
            for(int j=bucketChange; j<i; j++) numRenders += lastQueue.preprocess( mBuffer[quality],children.get(j) );
462
            numRenders += lastQueue.postprocess(mBuffer[quality]);
463 406e2f6b Leszek Koltunski
464
            if( oit )
465
              {
466
              numRenders += oitBuild(time, mBuffer[quality], fbo);
467
              GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
468
              }
469
            else
470
              {
471
              numRenders += blitWithDepth(time, mBuffer[quality],fbo);
472
              }
473 61ce8e90 Leszek Koltunski
            mBuffer[quality].clearBuffer(fbo);
474 c9a24bfb Leszek Koltunski
            }
475 660cd468 leszek
476 9e771d06 Leszek Koltunski
          quality= currQueue.getQuality();
477 2386a081 Leszek Koltunski
          bucketChange= i;
478 9e771d06 Leszek Koltunski
          renderDirectly = currQueue.getRender();
479 c9a24bfb Leszek Koltunski
          }
480
481 9e771d06 Leszek Koltunski
        if( renderDirectly )
482
          {
483
          setAsOutput(time);
484
485
          if( oit )
486
            {
487
            numRenders += child.drawOIT(time, this);
488
            GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
489
            }
490
          else
491
            {
492
            numRenders += child.draw(time, this);
493
            }
494
          }
495
        else
496
          {
497
          mBuffer[quality].setAsOutput(time);
498
          child.drawNoBlend(time, mBuffer[quality]);
499
          }
500 c9a24bfb Leszek Koltunski
501 8e28b6ff leszek
        if( i==numChildren-1 )
502 cf7394cc leszek
          {
503 9e771d06 Leszek Koltunski
          for(int j=bucketChange; j<numChildren; j++) numRenders += currQueue.preprocess( mBuffer[quality],children.get(j) );
504
          numRenders += currQueue.postprocess(mBuffer[quality]);
505 406e2f6b Leszek Koltunski
506
          if( oit )
507
            {
508
            numRenders += oitBuild(time, mBuffer[quality], fbo);
509
            GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
510
            mBuffer[quality].clearBuffer(fbo);
511
            }
512
          else
513
            {
514
            numRenders += blitWithDepth(time, mBuffer[quality],fbo);
515
            }
516 cf7394cc leszek
          }
517 56c6ca24 Leszek Koltunski
        } // end else (postprocessed child)
518 b9798977 leszek
519 70b6a155 Leszek Koltunski
      lastQueue = currQueue;
520
      lastBucket= currBucket;
521 56c6ca24 Leszek Koltunski
      } // end main for loop
522
523 c1a38ba3 Leszek Koltunski
    if( oit && numChildren>0 )
524
      {
525
      numRenders += oitRender(time, fbo);  // merge the OIT linked list
526
      }
527
528 39086ebb leszek
    return numRenders;
529
    }
530
531 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
532
533 26a4e5f6 leszek
  ArrayList<DistortedNode> getChildren()
534 be60d4ff leszek
    {
535 26a4e5f6 leszek
    return mChildren;
536 be60d4ff leszek
    }
537
538 ae2802b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
539
/**
540
 * Not part of the Public API.
541
 *
542
 * @y.exclude
543
 */
544
  public float getWidthCorrection()
545
    {
546
    return (float)mWidth/mRealWidth;
547
    }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Not part of the Public API.
552
 *
553
 * @y.exclude
554
 */
555
  public float getHeightCorrection()
556
    {
557
    return (float)mHeight/mRealHeight;
558
    }
559
560 61ce8e90 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
561
562 9e771d06 Leszek Koltunski
  void clearBuffer(int fbo)
563 61ce8e90 Leszek Koltunski
    {
564 a07e0204 Leszek Koltunski
    DistortedRenderState.colorDepthStencilOn();
565
566
    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
567
    GLES31.glClearDepthf(mClearDepth);
568
    GLES31.glClearStencil(mClearStencil);
569 61ce8e90 Leszek Koltunski
570
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[fbo]);
571
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*fbo+1], 0);
572
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
573
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*fbo  ], 0);
574
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
575 a07e0204 Leszek Koltunski
576
    DistortedRenderState.colorDepthStencilRestore();
577 61ce8e90 Leszek Koltunski
    }
578
579 b2939df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
580
// PUBLIC API
581 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
582 c5369f1b leszek
/**
583 8ebbc730 Leszek Koltunski
 * Draws all the attached children to this OutputSurface's 0th FBO.
584 af4cc5db Leszek Koltunski
 * <p>
585
 * Must be called from a thread holding OpenGL Context.
586
 *
587 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
588 7691a39f leszek
 * @return Number of objects rendered.
589 c5369f1b leszek
 */
590 b2939df4 Leszek Koltunski
  public int render(long time)
591 8ebbc730 Leszek Koltunski
    {
592
    return render(time,0);
593
    }
594
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596
/**
597
 * Draws all the attached children to this OutputSurface.
598
 * <p>
599
 * Must be called from a thread holding OpenGL Context.
600
 *
601
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
602
 * @param fbo The surface can have many FBOs backing it up - render this to FBO number 'fbo'.
603
 * @return Number of objects rendered.
604
 */
605
  public int render(long time, int fbo)
606 af4cc5db Leszek Koltunski
    {
607 c204c69d leszek
    // change tree topology (attach and detach children)
608 889cce10 Leszek Koltunski
/*
609 42571056 Leszek Koltunski
    boolean changed1 =
610 889cce10 Leszek Koltunski
*/
611 efe3d8fe leszek
    DistortedMaster.toDo();
612 eadf0859 leszek
/*
613 42571056 Leszek Koltunski
    if( changed1 )
614 c204c69d leszek
      {
615
      for(int i=0; i<mNumChildren; i++)
616
        {
617 af27df87 leszek
        mChildren.get(i).debug(0);
618 c204c69d leszek
        }
619 af27df87 leszek
620 7691a39f leszek
      DistortedNode.debugMap();
621 c204c69d leszek
      }
622 eadf0859 leszek
*/
623 09ab7524 Leszek Koltunski
    // create and delete all underlying OpenGL resources
624
    // Watch out: FIRST change topology, only then deal
625
    // with OpenGL resources. That's because changing Tree
626
    // can result in additional Framebuffers that would need
627
    // to be created immediately, before the calls to drawRecursive()
628 42571056 Leszek Koltunski
/*
629
    boolean changed2 =
630
*/
631 f8377ef8 leszek
    toDo();
632 eadf0859 leszek
/*
633 42571056 Leszek Koltunski
    if( changed2 )
634 af27df87 leszek
      {
635 226144d0 leszek
      DistortedObject.debugLists();
636 42571056 Leszek Koltunski
      }
637 eadf0859 leszek
*/
638 c834348d leszek
    // mark OpenGL state as unknown
639
    DistortedRenderState.reset();
640 2ed1c692 leszek
641 b2939df4 Leszek Koltunski
    int numRenders=0;
642 7691a39f leszek
643 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
644 af4cc5db Leszek Koltunski
      {
645 b2939df4 Leszek Koltunski
      numRenders += mChildren.get(i).renderRecursive(time);
646 af4cc5db Leszek Koltunski
      }
647 7691a39f leszek
648 406e2f6b Leszek Koltunski
    numRenders += renderChildren(time,mNumChildren,mChildren,fbo, mRenderWayOIT);
649 b2939df4 Leszek Koltunski
650 7691a39f leszek
    return numRenders;
651 af4cc5db Leszek Koltunski
    }
652
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654 c5369f1b leszek
/**
655
 * Bind this Surface as a Framebuffer we can render to.
656 02ab6f9d leszek
 *
657
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
658
 *             out if this is the first time during present frame that this FBO is being set as output.
659
 *             If so, the library, in addition to binding the Surface for output, also clears the
660
 *             Surface's color and depth attachments.
661 c5369f1b leszek
 */
662 95c441a2 leszek
  public void setAsOutput(long time)
663 a436ccc5 leszek
    {
664 5f7e4f2c Leszek Koltunski
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
665 95c441a2 leszek
666 5f7e4f2c Leszek Koltunski
    if( mTime[mCurrFBO]!=time )
667 95c441a2 leszek
      {
668 5f7e4f2c Leszek Koltunski
      mTime[mCurrFBO] = time;
669 88c7b603 Leszek Koltunski
      clear();
670 95c441a2 leszek
      }
671 a436ccc5 leszek
    }
672 af4cc5db Leszek Koltunski
673 1dfc9074 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
674
/**
675
 * Bind this Surface as a Framebuffer we can render to.
676
 * <p>
677
 * This version does not attempt to clear anything.
678
 */
679
  public void setAsOutput()
680
    {
681 5f7e4f2c Leszek Koltunski
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
682 1dfc9074 leszek
    }
683
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685
/**
686
 * Return the Near plane of the Projection included in the Surface.
687
 *
688
 * @return the Near plane.
689
 */
690
  public float getNear()
691
    {
692
    return mNear;
693
    }
694
695 638b5b5c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
696
/**
697
 * Set mipmap level.
698
 * <p>
699
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
700
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
701
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
702
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
703
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
704
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
705 8426bd6a Leszek Koltunski
 * <p>
706
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
707
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
708
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
709
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
710 638b5b5c leszek
 *
711
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
712
 *               does not make any sense (that would result in loss of speed and no gain in quality)
713
 */
714
  public void setMipmap(float mipmap)
715
    {
716
    mMipmap = mipmap;
717
    }
718
719 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
720
/**
721 e6519ac8 Leszek Koltunski
 * Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
722 ad16ed3b Leszek Koltunski
 * this Surface at the beginning of each frame.
723 a9f41fa3 leszek
 *
724
 * @param r the Red component. Default: 0.0f
725
 * @param g the Green component. Default: 0.0f
726
 * @param b the Blue component. Default: 0.0f
727
 * @param a the Alpha component. Default: 0.0f
728
 */
729
  public void glClearColor(float r, float g, float b, float a)
730
    {
731
    mClearR = r;
732
    mClearG = g;
733
    mClearB = b;
734
    mClearA = a;
735
    }
736
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738
/**
739 23eecbd9 Leszek Koltunski
 * Uses glClearDepthf() to set up a value with which to clear
740
 * the Depth buffer of our Surface at the beginning of each frame.
741 a9f41fa3 leszek
 *
742
 * @param d the Depth. Default: 1.0f
743
 */
744
  public void glClearDepthf(float d)
745
    {
746
    mClearDepth = d;
747
    }
748
749 23eecbd9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
750
/**
751
 * Uses glClearStencil() to set up a value with which to clear the
752
 * Stencil buffer of our Surface at the beginning of each frame.
753
 *
754
 * @param s the Stencil. Default: 0
755
 */
756
  public void glClearStencil(int s)
757
    {
758
    mClearStencil = s;
759
    }
760
761 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
762
/**
763
 * Which buffers to Clear at the beginning of each frame?
764
 * <p>
765
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
766
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
767
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
768
 *
769
 * @param mask bitwise OR of BUFFER_BITs to clear.
770
 */
771
  public void glClear(int mask)
772
    {
773
    mClear = mask;
774
    }
775
776 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
777
/**
778
 * Create new Projection matrix.
779
 *
780
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
781 54fe333a leszek
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
782
 * @param near Distance between the screen plane and the near plane.
783 61441ce2 Leszek Koltunski
 *             Valid vaules: 0<near<1. When near==0 (illegal!), the Near Plane is exactly at the tip of
784
 *             the pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
785 af4cc5db Leszek Koltunski
 */
786 54fe333a leszek
  public void setProjection(float fov, float near)
787 af4cc5db Leszek Koltunski
    {
788 8069e806 Leszek Koltunski
    if( fov < 180.0f && fov >=0.0f )
789
      {
790
      mFOV = fov;
791
      }
792
793
    if( near<   1.0f && near> 0.0f )
794
      {
795
      mNear= near;
796
      }
797
    else if( near<=0.0f )
798
      {
799
      mNear = 0.01f;
800
      }
801
    else if( near>=1.0f )
802
      {
803
      mNear=0.99f;
804
      }
805 af4cc5db Leszek Koltunski
806 61ce8e90 Leszek Koltunski
    if( mBuffer!=null )
807
      {
808
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
809
      }
810
811 af4cc5db Leszek Koltunski
    createProjection();
812
    }
813
814
///////////////////////////////////////////////////////////////////////////////////////////////////
815 c5369f1b leszek
/**
816 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
817 c7da4e65 leszek
 * <p>
818
 * This method can be safely called mid-render as it doesn't interfere with rendering.
819 af4cc5db Leszek Koltunski
 *
820
 * @param width The new width.
821
 * @param height The new height.
822 c5369f1b leszek
 */
823 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
824
    {
825
    if( mWidth!=width || mHeight!=height )
826
      {
827 a4b182d4 Leszek Koltunski
      mWidth = mRealWidth = width;
828
      mHeight= mRealHeight= height;
829 f8377ef8 leszek
830
      createProjection();
831
832 c7da4e65 leszek
      if( mColorCreated==CREATED )
833 f8377ef8 leszek
        {
834 f28fffc2 Leszek Koltunski
        markForCreation();
835 f8377ef8 leszek
        recreate();
836
        }
837 af4cc5db Leszek Koltunski
      }
838
    }
839 a09ada4c Leszek Koltunski
840 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
841
/**
842 89de975c leszek
 * Return true if the Surface contains a DEPTH attachment.
843 a436ccc5 leszek
 *
844 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
845 a436ccc5 leszek
 */
846 89de975c leszek
  public boolean hasDepth()
847 a436ccc5 leszek
    {
848 89de975c leszek
    return mDepthStencilCreated==CREATED;
849 a436ccc5 leszek
    }
850
851
///////////////////////////////////////////////////////////////////////////////////////////////////
852
/**
853 89de975c leszek
 * Return true if the Surface contains a STENCIL attachment.
854 a436ccc5 leszek
 *
855 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
856 a436ccc5 leszek
 */
857 89de975c leszek
  public boolean hasStencil()
858 a436ccc5 leszek
    {
859 89de975c leszek
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
860 a436ccc5 leszek
    }
861
862 406e2f6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
863
/**
864
 * When rendering this Node, should we use the Order Independent Transparency render more?
865
 * <p>
866
 * There are two modes of rendering: the fast 'normal' way, which however renders transparent
867
 * fragments in different ways depending on which fragments get rendered first, or the slower
868
 * 'oit' way, which renders transparent fragments correctly regardless of their order.
869
 *
870
 * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
871
 */
872
  public void setOrderIndependentTransparency(boolean oit)
873
    {
874
    mRenderWayOIT = oit;
875
    }
876
877 12f9e4bb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
878
/**
879
 * When rendering this Node, should we use the Order Independent Transparency render more?
880
 * <p>
881
 * There are two modes of rendering: the fast 'normal' way, which however renders transparent
882
 * fragments in different ways depending on which fragments get rendered first, or the slower
883
 * 'oit' way, which renders transparent fragments correctly regardless of their order.
884
 *
885
 * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
886
 * @param initialSize Initial number of transparent fragments we expect, in screenfuls.
887
 *                    I.e '1.0' means 'the scene we are going to render contains about 1 screen
888
 *                    worth of transparent fragments'. Valid values: 0.0 &lt; initialSize &lt; 10.0
889
 *                    Even if you get this wrong, the library will detect that there are more
890
 *                    transparent fragments than it has space for and readjust its internal buffers,
891
 *                    but only after a few frames during which one will probably see missing objects.
892
 */
893
public void setOrderIndependentTransparency(boolean oit, float initialSize)
894
  {
895
  mRenderWayOIT = oit;
896
897
  if( initialSize>0.0f && initialSize<10.0f )
898
    DistortedEffects.setSSBOSize(initialSize);
899
  }
900
901 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
902
/**
903
 * Adds a new child to the last position in the list of our Surface's children.
904 c204c69d leszek
 * <p>
905
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
906 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
907 a09ada4c Leszek Koltunski
 *
908
 * @param node The new Node to add.
909
 */
910 c204c69d leszek
  public void attach(DistortedNode node)
911 a09ada4c Leszek Koltunski
    {
912 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
913 efe3d8fe leszek
    DistortedMaster.newSlave(this);
914 a09ada4c Leszek Koltunski
    }
915
916
///////////////////////////////////////////////////////////////////////////////////////////////////
917
/**
918
 * Adds a new child to the last position in the list of our Surface's children.
919 c204c69d leszek
 * <p>
920
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
921 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
922 a09ada4c Leszek Koltunski
 *
923
 * @param surface InputSurface to initialize our child Node with.
924
 * @param effects DistortedEffects to initialize our child Node with.
925 715e7726 Leszek Koltunski
 * @param mesh MeshBase to initialize our child Node with.
926 a09ada4c Leszek Koltunski
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
927
 */
928 715e7726 Leszek Koltunski
  public DistortedNode attach(DistortedSurface surface, DistortedEffects effects, MeshBase mesh)
929 a09ada4c Leszek Koltunski
    {
930
    DistortedNode node = new DistortedNode(surface,effects,mesh);
931 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
932 efe3d8fe leszek
    DistortedMaster.newSlave(this);
933 c204c69d leszek
    return node;
934
    }
935
936 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
937
/**
938
 * Removes the first occurrence of a specified child from the list of children of our Surface.
939
 * <p>
940
 * A bit questionable method as there can be many different Nodes attached as children, some
941
 * of them having the same Effects but - for instance - different Mesh. Use with care.
942
 * <p>
943
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
944 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
945 af27df87 leszek
 *
946
 * @param effects DistortedEffects to remove.
947
 */
948
  public void detach(DistortedEffects effects)
949
    {
950
    long id = effects.getID();
951
    DistortedNode node;
952 8baa1fe6 Leszek Koltunski
    boolean detached = false;
953 af27df87 leszek
954
    for(int i=0; i<mNumChildren; i++)
955
      {
956
      node = mChildren.get(i);
957
958
      if( node.getEffects().getID()==id )
959
        {
960 8baa1fe6 Leszek Koltunski
        detached = true;
961 ffbe7ecf Leszek Koltunski
        mJobs.add(new Job(DETACH,node));
962 efe3d8fe leszek
        DistortedMaster.newSlave(this);
963 af27df87 leszek
        break;
964
        }
965
      }
966 8baa1fe6 Leszek Koltunski
967
    if( !detached )
968
      {
969
      // if we failed to detach any, it still might be the case that
970 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
971
      int num = mJobs.size();
972
      Job job;
973
974
      for(int i=0; i<num; i++)
975
        {
976
        job = mJobs.get(i);
977
978
        if( job.type==ATTACH && job.node.getEffects()==effects )
979
          {
980
          mJobs.remove(i);
981
          break;
982
          }
983
        }
984 8baa1fe6 Leszek Koltunski
      }
985 af27df87 leszek
    }
986
987 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
988
/**
989
 * Removes the first occurrence of a specified child from the list of children of our Surface.
990 c204c69d leszek
 * <p>
991
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
992 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
993 c204c69d leszek
 *
994
 * @param node The Node to remove.
995
 */
996
  public void detach(DistortedNode node)
997
    {
998 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETACH,node));
999 efe3d8fe leszek
    DistortedMaster.newSlave(this);
1000 a09ada4c Leszek Koltunski
    }
1001
1002
///////////////////////////////////////////////////////////////////////////////////////////////////
1003
/**
1004
 * Removes all children Nodes.
1005 c204c69d leszek
 * <p>
1006
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
1007 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
1008 c204c69d leszek
 */
1009
  public void detachAll()
1010
    {
1011 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETALL,null));
1012 efe3d8fe leszek
    DistortedMaster.newSlave(this);
1013 c204c69d leszek
    }
1014
1015
///////////////////////////////////////////////////////////////////////////////////////////////////
1016
/**
1017
 * This is not really part of the public API. Has to be public only because it is a part of the
1018 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
1019 c204c69d leszek
 * Java has no multiple inheritance.
1020 43fbf0dd leszek
 *
1021
 * @y.exclude
1022 a09ada4c Leszek Koltunski
 */
1023 efe3d8fe leszek
  public void doWork()
1024 a09ada4c Leszek Koltunski
    {
1025 efe3d8fe leszek
    int num = mJobs.size();
1026
    Job job;
1027
1028
    for(int i=0; i<num; i++)
1029
      {
1030
      job = mJobs.remove(0);
1031
1032
      switch(job.type)
1033
        {
1034 be60d4ff leszek
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
1035
                     job.node.setSurfaceParent(this);
1036 85bfeb7a Leszek Koltunski
                     DistortedMaster.addSortingByBuckets(mChildren,job.node);
1037 efe3d8fe leszek
                     mNumChildren++;
1038
                     break;
1039 be60d4ff leszek
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
1040 efe3d8fe leszek
                       {
1041 be60d4ff leszek
                       job.node.setSurfaceParent(null);
1042 efe3d8fe leszek
                       mNumChildren--;
1043
                       }
1044
                     break;
1045 be60d4ff leszek
        case DETALL: if( mNumChildren>0 )
1046 efe3d8fe leszek
                       {
1047 be60d4ff leszek
                       DistortedNode tmp;
1048
1049
                       for(int j=mNumChildren-1; j>=0; j--)
1050
                         {
1051
                         tmp = mChildren.remove(j);
1052
                         tmp.setSurfaceParent(null);
1053
                         }
1054
1055 efe3d8fe leszek
                       mNumChildren = 0;
1056
                       }
1057
                     break;
1058 ffbe7ecf Leszek Koltunski
        case SORT  : mChildren.remove(job.node);
1059 85bfeb7a Leszek Koltunski
                     DistortedMaster.addSortingByBuckets(mChildren,job.node);
1060 efe3d8fe leszek
                     break;
1061
        }
1062
      }
1063 a09ada4c Leszek Koltunski
    }
1064 af4cc5db Leszek Koltunski
}