Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 26a4e5f6

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library.main;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24

    
25
import java.util.ArrayList;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
30
{
31
//////////// DEBUG FLAGS /////////////////////////////////////////////
32
/**
33
 * When rendering a Screen, show FPS in the upper-left corner?
34
 */
35
public static final int DEBUG_FPS = 1;
36
//////////// END DEBUG FLAGS /////////////////////////////////////////
37

    
38
/**
39
 * Do not create DEPTH or STENCIL attachment
40
 */
41
  public static final int NO_DEPTH_NO_STENCIL = 0;
42
/**
43
 * Create DEPTH, but not STENCIL
44
 */
45
  public static final int DEPTH_NO_STENCIL    = 1;
46
/**
47
 * Create both DEPTH and STENCIL
48
 */
49
  public static final int BOTH_DEPTH_STENCIL  = 2;
50

    
51
  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
  private ArrayList<DistortedNode> mChildren;
57
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
58

    
59
  private class Job
60
    {
61
    int type;
62
    DistortedNode node;
63

    
64
    Job(int t, DistortedNode n)
65
      {
66
      type = t;
67
      node = n;
68
      }
69
    }
70

    
71
  private ArrayList<Job> mJobs = new ArrayList<>();
72

    
73
  DistortedFramebuffer[] mBuffer;
74

    
75
  private long mTime;
76
  private float mFOV;
77
  float mDistance, mNear;
78
  float[] mProjectionMatrix;
79

    
80
  int mDepthStencilCreated;
81
  int mDepthStencil;
82
  int[] mDepthStencilH = new int[1];
83
  int[] mFBOH          = new int[1];
84

    
85
  private float mClearR, mClearG, mClearB, mClearA;
86
  private float mClearDepth;
87
  private int mClearStencil;
88
  private int mClear;
89
  float mMipmap;
90

    
91
  private int mDebugLevel;
92

    
93
private String sLast="", sCurr="";
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  abstract void prepareDebug(long time);
98
  abstract void renderDebug(long time);
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
103
    {
104
    super(width,height,createColor,numcolors,type);
105

    
106
    mProjectionMatrix = new float[16];
107

    
108
    mFOV = 60.0f;
109
    mNear=  0.5f;
110

    
111
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
112
    mDepthStencil = depthStencil;
113

    
114
    mFBOH[0]         = fbo;
115
    mDepthStencilH[0]= 0;
116

    
117
    mTime = 0;
118
    mDebugLevel = 0;
119

    
120
    mClearR = 0.0f;
121
    mClearG = 0.0f;
122
    mClearB = 0.0f;
123
    mClearA = 0.0f;
124

    
125
    mClearDepth = 1.0f;
126
    mClearStencil = 0;
127
    mClear = GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT;
128

    
129
    mBuffer = new DistortedFramebuffer[EffectQuality.LENGTH];
130

    
131
    mMipmap = 1.0f;
132

    
133
    createProjection();
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private void createProjection()
139
    {
140
    if( mWidth>0 && mHeight>1 )
141
      {
142
      if( mFOV>0.0f )  // perspective projection
143
        {
144
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
145
        float q = mWidth*mNear;
146
        float c = mHeight*mNear;
147

    
148
        float left   = -q/2;
149
        float right  = +q/2;
150
        float bottom = -c/2;
151
        float top    = +c/2;
152
        float near   =  c/a;
153

    
154
        mDistance    = mHeight/a;
155
        float far    = 2*mDistance-near;
156

    
157
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
158
        }
159
      else             // parallel projection
160
        {
161
        float left   = -mWidth/2.0f;
162
        float right  = +mWidth/2.0f;
163
        float bottom = -mHeight/2.0f;
164
        float top    = +mHeight/2.0f;
165
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
166
        mDistance    = mWidth+mHeight;
167
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
168

    
169
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
170
        }
171
      }
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void createBuffers()
177
    {
178
    float mipmap=1.0f;
179

    
180
    for(int j=0; j<EffectQuality.LENGTH; j++)
181
      {
182
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
183
      mBuffer[j].mMipmap = mipmap;
184
      mipmap *= EffectQuality.MULTIPLIER;
185
      }
186

    
187
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
188

    
189
    GLES30.glStencilMask(0xff);
190
    GLES30.glDepthMask(true);
191
    GLES30.glColorMask(true,true,true,true);
192
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
193
    GLES30.glClearDepthf(1.0f);
194
    GLES30.glClearStencil(0);
195

    
196
    for(int j=0; j<EffectQuality.LENGTH; j++)
197
      {
198
      mBuffer[j].setAsOutput();
199
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
200
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
201
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
202
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  private int blitWithDepth(int quality, long currTime)
209
    {
210
    DistortedFramebuffer buffer = mBuffer[quality];
211

    
212
    GLES30.glViewport(0, 0, mWidth, mHeight);
213
    setAsOutput(currTime);
214
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
215
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mColorH[0]);
216
    GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
217
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
218

    
219
    GLES30.glDisable(GLES30.GL_STENCIL_TEST);
220
    GLES30.glStencilMask(0x00);
221

    
222
    DistortedEffects.blitDepthPriv(this);
223
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
224
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
225
    GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
226
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
227

    
228
    // clear buffers
229
    GLES30.glStencilMask(0xff);
230
    GLES30.glDepthMask(true);
231
    GLES30.glColorMask(true,true,true,true);
232
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
233
    GLES30.glClearDepthf(1.0f);
234
    GLES30.glClearStencil(0);
235

    
236
    buffer.setAsOutput();
237
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[1], 0);
238
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
239
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[0], 0);
240
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
241

    
242
    return 1;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
247
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
248
// to a whole buffer and merge it.
249

    
250
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
251
    {
252
    int numRenders = 0;
253
    DistortedNode child1, child2;
254
    DistortedEffects lastP=null, currP;
255
    long lastB=0, currB;
256
    int bucketChange=0;
257
    int lastQ=0, currQ;
258

    
259
sCurr = "";
260

    
261
    for(int i=0; i<num; i++)
262
      {
263
      child1 = children.get(i);
264
      currP = child1.getEffects();
265
      currB = currP==null ? 0 : currP.getBucket();
266
      currQ = currP==null ? 0 : currP.getQuality();
267

    
268
sCurr += (" "+currB);
269

    
270
      if( currB==0 ) numRenders += child1.draw(time,this);
271
      else
272
        {
273
        if( mBuffer[0]==null ) createBuffers();
274

    
275
        if( lastB!=currB )
276
          {
277
          if( lastB!=0 )
278
            {
279
            for(int j=bucketChange; j<i; j++)
280
              {
281
              child2 = children.get(j);
282
              numRenders += child2.markStencilAndDepth(time,mBuffer[lastQ],lastP);
283
              }
284

    
285
            numRenders += lastP.postprocess(time, this);
286
            numRenders += blitWithDepth(lastQ,time);
287
            }
288

    
289
          bucketChange = i;
290
          }
291

    
292
        numRenders += child1.draw(time,mBuffer[currQ]);
293

    
294
        if( i==num-1 )
295
          {
296
          for(int j=bucketChange; j<num; j++)
297
            {
298
            child2 = children.get(j);
299
            numRenders += child2.markStencilAndDepth(time,mBuffer[currQ],currP);
300
            }
301

    
302
          numRenders += currP.postprocess(time,this);
303
          numRenders += blitWithDepth(currQ,time);
304
          }
305
        }
306

    
307
      lastQ = currQ;
308
      lastP = currP;
309
      lastB = currB;
310
      }
311

    
312
if( !sLast.equals(sCurr) ) android.util.Log.e("surface", "rendering: "+sCurr);
313
sLast = sCurr;
314

    
315
    return numRenders;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  ArrayList<DistortedNode> getChildren()
321
    {
322
    return mChildren;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  void setAsOutput()
328
    {
329
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
// PUBLIC API
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
/**
336
 * Make the library show various debugging information.
337
 * <p>
338
 * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
339
 *
340
 * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
341
 */
342
  public void setDebug(int bitmask)
343
    {
344
    if( this instanceof DistortedScreen )
345
      mDebugLevel = bitmask;
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
/**
351
 * Draws all the attached children to this OutputSurface.
352
 * <p>
353
 * Must be called from a thread holding OpenGL Context.
354
 *
355
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
356
 * @return Number of objects rendered.
357
 */
358
  public int render(long time)
359
    {
360
    if( mDebugLevel!=0 ) prepareDebug(time);
361

    
362
    // change tree topology (attach and detach children)
363
/*
364
    boolean changed1 =
365
*/
366
    DistortedMaster.toDo();
367
/*
368
    if( changed1 )
369
      {
370
      for(int i=0; i<mNumChildren; i++)
371
        {
372
        mChildren.get(i).debug(0);
373
        }
374

    
375
      DistortedNode.debugMap();
376
      }
377
*/
378
    // create and delete all underlying OpenGL resources
379
    // Watch out: FIRST change topology, only then deal
380
    // with OpenGL resources. That's because changing Tree
381
    // can result in additional Framebuffers that would need
382
    // to be created immediately, before the calls to drawRecursive()
383
/*
384
    boolean changed2 =
385
*/
386
    toDo();
387
/*
388
    if( changed2 )
389
      {
390
      DistortedObject.debugLists();
391
      }
392
*/
393
    // mark OpenGL state as unknown
394
    DistortedRenderState.reset();
395

    
396
    int numRenders=0;
397

    
398
    for(int i=0; i<mNumChildren; i++)
399
      {
400
      numRenders += mChildren.get(i).renderRecursive(time);
401
      }
402

    
403
    setAsOutput(time);
404
    numRenders += renderChildren(time,mNumChildren,mChildren);
405

    
406
    if( mDebugLevel != 0 ) renderDebug(time);
407

    
408
    return numRenders;
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
/**
413
 * Bind this Surface as a Framebuffer we can render to.
414
 *
415
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
416
 *             out if this is the first time during present frame that this FBO is being set as output.
417
 *             If so, the library, in addition to binding the Surface for output, also clears the
418
 *             Surface's color and depth attachments.
419
 */
420
  public void setAsOutput(long time)
421
    {
422
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
423

    
424
    if( mTime!=time )
425
      {
426
      mTime = time;
427
      DistortedRenderState.colorDepthStencilOn();
428
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
429
      GLES30.glClearDepthf(mClearDepth);
430
      GLES30.glClearStencil(mClearStencil);
431
      GLES30.glClear(mClear);
432
      }
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436
/**
437
 * Set mipmap level.
438
 * <p>
439
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
440
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
441
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
442
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
443
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
444
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
445
 * <p>
446
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
447
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
448
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
449
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
450
 *
451
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
452
 *               does not make any sense (that would result in loss of speed and no gain in quality)
453
 */
454
  public void setMipmap(float mipmap)
455
    {
456
    mMipmap = mipmap;
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460
/**
461
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
462
 * this Surface at the beginning of each frame.
463
 *
464
 * @param r the Red component. Default: 0.0f
465
 * @param g the Green component. Default: 0.0f
466
 * @param b the Blue component. Default: 0.0f
467
 * @param a the Alpha component. Default: 0.0f
468
 */
469
  public void glClearColor(float r, float g, float b, float a)
470
    {
471
    mClearR = r;
472
    mClearG = g;
473
    mClearB = b;
474
    mClearA = a;
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478
/**
479
 * Uses glClearDepthf() to set up a value with which to clear
480
 * the Depth buffer of our Surface at the beginning of each frame.
481
 *
482
 * @param d the Depth. Default: 1.0f
483
 */
484
  public void glClearDepthf(float d)
485
    {
486
    mClearDepth = d;
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490
/**
491
 * Uses glClearStencil() to set up a value with which to clear the
492
 * Stencil buffer of our Surface at the beginning of each frame.
493
 *
494
 * @param s the Stencil. Default: 0
495
 */
496
  public void glClearStencil(int s)
497
    {
498
    mClearStencil = s;
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502
/**
503
 * Which buffers to Clear at the beginning of each frame?
504
 * <p>
505
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
506
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
507
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
508
 *
509
 * @param mask bitwise OR of BUFFER_BITs to clear.
510
 */
511
  public void glClear(int mask)
512
    {
513
    mClear = mask;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Create new Projection matrix.
519
 *
520
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
521
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
522
 * @param near Distance between the screen plane and the near plane.
523
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
524
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
525
 */
526
  public void setProjection(float fov, float near)
527
    {
528
    if( fov < 180.0f && fov >=0.0f )
529
      {
530
      mFOV = fov;
531
      }
532

    
533
    if( near<   1.0f && near> 0.0f )
534
      {
535
      mNear= near;
536
      }
537
    else if( near<=0.0f )
538
      {
539
      mNear = 0.01f;
540
      }
541
    else if( near>=1.0f )
542
      {
543
      mNear=0.99f;
544
      }
545

    
546
    createProjection();
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Resize the underlying Framebuffer.
552
 * <p>
553
 * This method can be safely called mid-render as it doesn't interfere with rendering.
554
 *
555
 * @param width The new width.
556
 * @param height The new height.
557
 */
558
  public void resize(int width, int height)
559
    {
560
    if( mWidth!=width || mHeight!=height )
561
      {
562
      mWidth = width;
563
      mHeight= height;
564

    
565
      createProjection();
566

    
567
      if( mColorCreated==CREATED )
568
        {
569
        markForCreation();
570
        recreate();
571
        }
572
      }
573
    }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577
 * Return true if the Surface contains a DEPTH attachment.
578
 *
579
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
580
 */
581
  public boolean hasDepth()
582
    {
583
    return mDepthStencilCreated==CREATED;
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587
/**
588
 * Return true if the Surface contains a STENCIL attachment.
589
 *
590
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
591
 */
592
  public boolean hasStencil()
593
    {
594
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
/**
599
 * Adds a new child to the last position in the list of our Surface's children.
600
 * <p>
601
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
602
 * DistortedMaster (by calling doWork())
603
 *
604
 * @param node The new Node to add.
605
 */
606
  public void attach(DistortedNode node)
607
    {
608
    mJobs.add(new Job(ATTACH,node));
609
    DistortedMaster.newSlave(this);
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
/**
614
 * Adds a new child to the last position in the list of our Surface's children.
615
 * <p>
616
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
617
 * DistortedMaster (by calling doWork())
618
 *
619
 * @param surface InputSurface to initialize our child Node with.
620
 * @param effects DistortedEffects to initialize our child Node with.
621
 * @param mesh MeshObject to initialize our child Node with.
622
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
623
 */
624
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
625
    {
626
    DistortedNode node = new DistortedNode(surface,effects,mesh);
627
    mJobs.add(new Job(ATTACH,node));
628
    DistortedMaster.newSlave(this);
629
    return node;
630
    }
631

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633
/**
634
 * Removes the first occurrence of a specified child from the list of children of our Surface.
635
 * <p>
636
 * A bit questionable method as there can be many different Nodes attached as children, some
637
 * of them having the same Effects but - for instance - different Mesh. Use with care.
638
 * <p>
639
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
640
 * DistortedMaster (by calling doWork())
641
 *
642
 * @param effects DistortedEffects to remove.
643
 */
644
  public void detach(DistortedEffects effects)
645
    {
646
    long id = effects.getID();
647
    DistortedNode node;
648
    boolean detached = false;
649

    
650
    for(int i=0; i<mNumChildren; i++)
651
      {
652
      node = mChildren.get(i);
653

    
654
      if( node.getEffects().getID()==id )
655
        {
656
        detached = true;
657
        mJobs.add(new Job(DETACH,node));
658
        DistortedMaster.newSlave(this);
659
        break;
660
        }
661
      }
662

    
663
    if( !detached )
664
      {
665
      // if we failed to detach any, it still might be the case that
666
      // there's an ATTACH job that we need to cancel.
667
      int num = mJobs.size();
668
      Job job;
669

    
670
      for(int i=0; i<num; i++)
671
        {
672
        job = mJobs.get(i);
673

    
674
        if( job.type==ATTACH && job.node.getEffects()==effects )
675
          {
676
          mJobs.remove(i);
677
          break;
678
          }
679
        }
680
      }
681
    }
682

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684
/**
685
 * Removes the first occurrence of a specified child from the list of children of our Surface.
686
 * <p>
687
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
688
 * DistortedMaster (by calling doWork())
689
 *
690
 * @param node The Node to remove.
691
 */
692
  public void detach(DistortedNode node)
693
    {
694
    mJobs.add(new Job(DETACH,node));
695
    DistortedMaster.newSlave(this);
696
    }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699
/**
700
 * Removes all children Nodes.
701
 * <p>
702
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
703
 * DistortedMaster (by calling doWork())
704
 */
705
  public void detachAll()
706
    {
707
    mJobs.add(new Job(DETALL,null));
708
    DistortedMaster.newSlave(this);
709
    }
710

    
711
///////////////////////////////////////////////////////////////////////////////////////////////////
712
/**
713
 * This is not really part of the public API. Has to be public only because it is a part of the
714
 * DistortedSlave interface, which should really be a class that we extend here instead but
715
 * Java has no multiple inheritance.
716
 *
717
 * @y.exclude
718
 */
719
  public void doWork()
720
    {
721
    int num = mJobs.size();
722
    Job job;
723

    
724
    for(int i=0; i<num; i++)
725
      {
726
      job = mJobs.remove(0);
727

    
728
      switch(job.type)
729
        {
730
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
731
                     job.node.setSurfaceParent(this);
732
                     DistortedMaster.addSorted(mChildren,job.node);
733
                     mNumChildren++;
734
                     break;
735
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
736
                       {
737
                       job.node.setSurfaceParent(null);
738
                       mNumChildren--;
739
                       }
740
                     break;
741
        case DETALL: if( mNumChildren>0 )
742
                       {
743
                       DistortedNode tmp;
744

    
745
                       for(int j=mNumChildren-1; j>=0; j--)
746
                         {
747
                         tmp = mChildren.remove(j);
748
                         tmp.setSurfaceParent(null);
749
                         }
750

    
751
                       mNumChildren = 0;
752
                       }
753
                     break;
754
        case SORT  : mChildren.remove(job.node);
755
                     DistortedMaster.addSorted(mChildren,job.node);
756
                     break;
757
        }
758
      }
759
    }
760
}
(8-8/23)