Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 33f59f22

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.GLES31;
23
import android.opengl.Matrix;
24

    
25
import org.distorted.library.effect.EffectQuality;
26

    
27
import java.util.ArrayList;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * This is not really part of the public API.
32
 *
33
 * @y.exclude
34
 */
35
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
36
{
37
/**
38
 * Do not create DEPTH or STENCIL attachment
39
 */
40
  public static final int NO_DEPTH_NO_STENCIL = 0;
41
/**
42
 * Create DEPTH, but not STENCIL
43
 */
44
  public static final int DEPTH_NO_STENCIL    = 1;
45
/**
46
 * Create both DEPTH and STENCIL
47
 */
48
  public static final int BOTH_DEPTH_STENCIL  = 2;
49

    
50
  private static final int ATTACH = 0;
51
  private static final int DETACH = 1;
52
  private static final int DETALL = 2;
53
  private static final int SORT   = 3;
54

    
55
  private ArrayList<DistortedNode> mChildren;
56
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
57

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

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

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

    
72
  // Global buffers used for postprocessing.
73
  private static DistortedOutputSurface[] mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH];
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
  int mRealWidth;   // the Surface can be backed up with a texture that is
92
  int mRealHeight;  // larger than the viewport we have to it.
93
                    // mWidth,mHeight are the sizes of the Viewport, those -
94
                    // sizes of the backing up texture.
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
99
    {
100
    super(width,height,createColor,numcolors,type);
101

    
102
    mRealWidth = width;
103
    mRealHeight= height;
104

    
105
    mProjectionMatrix = new float[16];
106

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

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

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

    
116
    mTime = 0;
117

    
118
    mClearR = 0.0f;
119
    mClearG = 0.0f;
120
    mClearB = 0.0f;
121
    mClearA = 0.0f;
122

    
123
    mClearDepth = 1.0f;
124
    mClearStencil = 0;
125
    mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
126

    
127
    mMipmap = 1.0f;
128

    
129
    createProjection();
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

    
144
        float left   = -q/2;
145
        float right  = +q/2;
146
        float bottom = -c/2;
147
        float top    = +c/2;
148
        float near   =  c/a;
149

    
150
        mDistance    = mHeight/a;
151
        float far    = 2*mDistance-near;
152

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

    
165
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
166
        }
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private static void createPostprocessingBuffers(int width, int height, float near)
173
    {
174
    float mipmap=1.0f;
175

    
176
    for (int j=0; j<EffectQuality.LENGTH; j++)
177
      {
178
      mBuffer[j] = new DistortedFramebuffer(2, BOTH_DEPTH_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
179
      mBuffer[j].mMipmap = mipmap;
180
      mBuffer[j].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
181
      mBuffer[j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
182

    
183
      mipmap *= EffectQuality.MULTIPLIER;
184
      }
185

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

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  static synchronized void onDestroy()
208
    {
209
    for(int j=0; j<EffectQuality.LENGTH; j++)
210
      {
211
      mBuffer[j] = null;
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
// The postprocessing buffers mBuffer[] are generally speaking too large (there's just one static
217
// set of them) so before we use them for output, we need to adjust the Vieport as if they were
218
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
219
// adjust the texture coords - see the get{Width|Height}Correction functions.
220

    
221
  private static void clonePostprocessingViewport(DistortedOutputSurface from)
222
    {
223
    if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight )
224
      {
225
      DistortedOutputSurface surface;
226

    
227
      for(int i=0; i<EffectQuality.LENGTH; i++)
228
        {
229
        surface = mBuffer[i];
230

    
231
        surface.mWidth  = (int)(from.mWidth *surface.mMipmap);
232
        surface.mHeight = (int)(from.mHeight*surface.mMipmap);
233

    
234
        surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
235

    
236
        //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
237

    
238
        surface.createProjection();
239

    
240
        int maxw = surface.mWidth  > surface.mRealWidth  ? surface.mWidth  : surface.mRealWidth;
241
        int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
242

    
243
        if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
244
          {
245
          surface.mRealWidth = maxw;
246
          surface.mRealHeight = maxh;
247

    
248
          surface.recreate();
249
          surface.create();
250
          }
251
        }
252
      }
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  private static void oitClear(DistortedOutputSurface buffer)
258
    {
259
    DistortedEffects.zeroOutAtomic();
260
    DistortedEffects.oitClear(buffer);
261
    GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  private int oitBuild(DistortedOutputSurface buffer)
267
    {
268
    GLES31.glViewport(0, 0, mWidth, mHeight);
269
    setAsOutput();
270
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
271
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[0]);
272
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
273
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
274

    
275
    //GLES31.glDisable(GLES31.GL_STENCIL_TEST);
276
    //GLES31.glStencilMask(0x00);
277

    
278
    DistortedRenderState.colorDepthStencilOn();
279
    DistortedRenderState.enableDepthTest();
280

    
281
    DistortedEffects.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
282
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
283
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
284
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
285
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
286

    
287
    DistortedRenderState.colorDepthStencilRestore();
288
    DistortedRenderState.restoreDepthTest();
289

    
290
    return 1;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
// two phases: 1. collapse the SSBO 2. blend the ssbo's color
295

    
296
  private int oitRender(long currTime, DistortedOutputSurface buffer)
297
    {
298
    float corrW = buffer.getWidthCorrection();
299
    float corrH = buffer.getHeightCorrection();
300

    
301
    GLES31.glViewport(0, 0, mWidth, mHeight);
302
    setAsOutput(currTime);
303
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
304
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
305

    
306
    DistortedRenderState.switchOffColorDepthStencil();
307
    DistortedEffects.oitCollapse(this, corrW, corrH );
308

    
309
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
310
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
311

    
312
    DistortedRenderState.switchColorDepthOnStencilOff();
313
    DistortedEffects.oitRender(this, corrW, corrH);
314
    DistortedRenderState.restoreColorDepthStencil();
315

    
316
    return 1;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  private static void clearBuffer(DistortedOutputSurface buffer)
322
    {
323
    GLES31.glStencilMask(0xff);
324
    GLES31.glDepthMask(true);
325
    GLES31.glColorMask(true,true,true,true);
326
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
327
    GLES31.glClearDepthf(1.0f);
328
    GLES31.glClearStencil(0);
329

    
330
    buffer.setAsOutput();
331
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[buffer.mNumColors-1], 0);
332
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
333

    
334
    for(int i=buffer.mNumColors-2; i>=0; i--)
335
      {
336
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[i], 0);
337
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
338
      }
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  void clear()
344
    {
345
    DistortedRenderState.colorDepthStencilOn();
346
    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
347
    GLES31.glClearDepthf(mClearDepth);
348
    GLES31.glClearStencil(mClearStencil);
349
    GLES31.glClear(mClear);
350
    DistortedRenderState.colorDepthStencilRestore();
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
355
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
356
// to a whole buffer (lastQueue.postprocess) and merge it (this.oitBuild).
357

    
358
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
359
    {
360
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
361
    DistortedNode child1, child2;
362
    EffectQueuePostprocess lastQueue=null, currQueue;
363
    long lastBucket=0, currBucket;
364

    
365
    oitClear(this);
366

    
367
    for(int i=0; i<numChildren; i++)
368
      {
369
      child1 = children.get(i);
370
      currQueue = child1.getPostprocessQueue();
371
      currBucket= currQueue.getID();
372

    
373
      if( currBucket==0 )
374
        {
375
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
376
        numRenders += child1.draw(time, this);
377

    
378
        //setAsOutput(time);
379
        //numRenders += child1.draw(time,this);
380
        }
381
      else
382
        {
383
        if( mBuffer[0]==null ) createPostprocessingBuffers(mWidth,mHeight,mNear);
384

    
385
        if( lastBucket!=currBucket )
386
          {
387
          if( lastBucket==0 )
388
            {
389
            clonePostprocessingViewport(this);
390
            }
391
          else
392
            {
393
            for(int j=bucketChange; j<i; j++)
394
              {
395
              child2 = children.get(j);
396
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
397
              }
398

    
399
            numRenders += lastQueue.postprocess(mBuffer);
400
            numRenders += oitBuild(mBuffer[quality]);
401
            clearBuffer(mBuffer[quality]);
402
            }
403

    
404
          internalQuality = currQueue.getInternalQuality();
405
          quality         = currQueue.getQuality();
406
          bucketChange    = i;
407
          }
408

    
409
        mBuffer[quality].setAsOutput(time);
410
        child1.drawNoBlend(time,mBuffer[quality]);
411

    
412
        if( i==numChildren-1 )
413
          {
414
          for(int j=bucketChange; j<numChildren; j++)
415
            {
416
            child2 = children.get(j);
417
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
418
            }
419

    
420
          numRenders += currQueue.postprocess(mBuffer);
421
          numRenders += oitBuild(mBuffer[quality]);
422
          GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT);
423
          numRenders += oitRender(time,this);  // merge the OIT linked list
424
          clearBuffer(mBuffer[quality]);
425
          }
426
        } // end else (postprocessed child)
427

    
428
      lastQueue = currQueue;
429
      lastBucket= currBucket;
430
      } // end main for loop
431

    
432
    return numRenders;
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  ArrayList<DistortedNode> getChildren()
438
    {
439
    return mChildren;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
/**
444
 * Not part of the Public API.
445
 *
446
 * @y.exclude
447
 */
448
  public float getWidthCorrection()
449
    {
450
    return (float)mWidth/mRealWidth;
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454
/**
455
 * Not part of the Public API.
456
 *
457
 * @y.exclude
458
 */
459
  public float getHeightCorrection()
460
    {
461
    return (float)mHeight/mRealHeight;
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
// PUBLIC API
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467
/**
468
 * Draws all the attached children to this OutputSurface.
469
 * <p>
470
 * Must be called from a thread holding OpenGL Context.
471
 *
472
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
473
 * @return Number of objects rendered.
474
 */
475
  public int render(long time)
476
    {
477
    // change tree topology (attach and detach children)
478
/*
479
    boolean changed1 =
480
*/
481
    DistortedMaster.toDo();
482
/*
483
    if( changed1 )
484
      {
485
      for(int i=0; i<mNumChildren; i++)
486
        {
487
        mChildren.get(i).debug(0);
488
        }
489

    
490
      DistortedNode.debugMap();
491
      }
492
*/
493
    // create and delete all underlying OpenGL resources
494
    // Watch out: FIRST change topology, only then deal
495
    // with OpenGL resources. That's because changing Tree
496
    // can result in additional Framebuffers that would need
497
    // to be created immediately, before the calls to drawRecursive()
498
/*
499
    boolean changed2 =
500
*/
501
    toDo();
502
/*
503
    if( changed2 )
504
      {
505
      DistortedObject.debugLists();
506
      }
507
*/
508
    // mark OpenGL state as unknown
509
    DistortedRenderState.reset();
510

    
511
    int numRenders=0;
512

    
513
    for(int i=0; i<mNumChildren; i++)
514
      {
515
      numRenders += mChildren.get(i).renderRecursive(time);
516
      }
517

    
518
    setAsOutput(time);
519
    numRenders += renderChildren(time,mNumChildren,mChildren);
520

    
521
    return numRenders;
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
/**
526
 * Bind this Surface as a Framebuffer we can render to.
527
 *
528
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
529
 *             out if this is the first time during present frame that this FBO is being set as output.
530
 *             If so, the library, in addition to binding the Surface for output, also clears the
531
 *             Surface's color and depth attachments.
532
 */
533
  public void setAsOutput(long time)
534
    {
535
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
536

    
537
    if( mTime!=time )
538
      {
539
      mTime = time;
540
      clear();
541
      }
542
    }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545
/**
546
 * Bind this Surface as a Framebuffer we can render to.
547
 * <p>
548
 * This version does not attempt to clear anything.
549
 */
550

    
551
  public void setAsOutput()
552
    {
553
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
/**
558
 * Return the Near plane of the Projection included in the Surface.
559
 *
560
 * @return the Near plane.
561
 */
562
  public float getNear()
563
    {
564
    return mNear;
565
    }
566

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568
/**
569
 * Set mipmap level.
570
 * <p>
571
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
572
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
573
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
574
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
575
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
576
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
577
 * <p>
578
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
579
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
580
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
581
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
582
 *
583
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
584
 *               does not make any sense (that would result in loss of speed and no gain in quality)
585
 */
586
  public void setMipmap(float mipmap)
587
    {
588
    mMipmap = mipmap;
589
    }
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592
/**
593
 * Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
594
 * this Surface at the beginning of each frame.
595
 *
596
 * @param r the Red component. Default: 0.0f
597
 * @param g the Green component. Default: 0.0f
598
 * @param b the Blue component. Default: 0.0f
599
 * @param a the Alpha component. Default: 0.0f
600
 */
601
  public void glClearColor(float r, float g, float b, float a)
602
    {
603
    mClearR = r;
604
    mClearG = g;
605
    mClearB = b;
606
    mClearA = a;
607
    }
608

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610
/**
611
 * Uses glClearDepthf() to set up a value with which to clear
612
 * the Depth buffer of our Surface at the beginning of each frame.
613
 *
614
 * @param d the Depth. Default: 1.0f
615
 */
616
  public void glClearDepthf(float d)
617
    {
618
    mClearDepth = d;
619
    }
620

    
621
///////////////////////////////////////////////////////////////////////////////////////////////////
622
/**
623
 * Uses glClearStencil() to set up a value with which to clear the
624
 * Stencil buffer of our Surface at the beginning of each frame.
625
 *
626
 * @param s the Stencil. Default: 0
627
 */
628
  public void glClearStencil(int s)
629
    {
630
    mClearStencil = s;
631
    }
632

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634
/**
635
 * Which buffers to Clear at the beginning of each frame?
636
 * <p>
637
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
638
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
639
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
640
 *
641
 * @param mask bitwise OR of BUFFER_BITs to clear.
642
 */
643
  public void glClear(int mask)
644
    {
645
    mClear = mask;
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
/**
650
 * Create new Projection matrix.
651
 *
652
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
653
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
654
 * @param near Distance between the screen plane and the near plane.
655
 *             Valid vaules: 0<near<1. When near==0 (illegal!), the Near Plane is exactly at the tip of
656
 *             the pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
657
 */
658
  public void setProjection(float fov, float near)
659
    {
660
    if( fov < 180.0f && fov >=0.0f )
661
      {
662
      mFOV = fov;
663
      }
664

    
665
    if( near<   1.0f && near> 0.0f )
666
      {
667
      mNear= near;
668
      }
669
    else if( near<=0.0f )
670
      {
671
      mNear = 0.01f;
672
      }
673
    else if( near>=1.0f )
674
      {
675
      mNear=0.99f;
676
      }
677

    
678
    createProjection();
679
    }
680

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682
/**
683
 * Resize the underlying Framebuffer.
684
 * <p>
685
 * This method can be safely called mid-render as it doesn't interfere with rendering.
686
 *
687
 * @param width The new width.
688
 * @param height The new height.
689
 */
690
  public void resize(int width, int height)
691
    {
692
    if( mWidth!=width || mHeight!=height )
693
      {
694
      mWidth = mRealWidth = width;
695
      mHeight= mRealHeight= height;
696

    
697
      createProjection();
698

    
699
      if( mColorCreated==CREATED )
700
        {
701
        markForCreation();
702
        recreate();
703
        }
704
      }
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708
/**
709
 * Return true if the Surface contains a DEPTH attachment.
710
 *
711
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
712
 */
713
  public boolean hasDepth()
714
    {
715
    return mDepthStencilCreated==CREATED;
716
    }
717

    
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719
/**
720
 * Return true if the Surface contains a STENCIL attachment.
721
 *
722
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
723
 */
724
  public boolean hasStencil()
725
    {
726
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
727
    }
728

    
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730
/**
731
 * Adds a new child to the last position in the list of our Surface's children.
732
 * <p>
733
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
734
 * DistortedMaster (by calling doWork())
735
 *
736
 * @param node The new Node to add.
737
 */
738
  public void attach(DistortedNode node)
739
    {
740
    mJobs.add(new Job(ATTACH,node));
741
    DistortedMaster.newSlave(this);
742
    }
743

    
744
///////////////////////////////////////////////////////////////////////////////////////////////////
745
/**
746
 * Adds a new child to the last position in the list of our Surface's children.
747
 * <p>
748
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
749
 * DistortedMaster (by calling doWork())
750
 *
751
 * @param surface InputSurface to initialize our child Node with.
752
 * @param effects DistortedEffects to initialize our child Node with.
753
 * @param mesh MeshObject to initialize our child Node with.
754
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
755
 */
756
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
757
    {
758
    DistortedNode node = new DistortedNode(surface,effects,mesh);
759
    mJobs.add(new Job(ATTACH,node));
760
    DistortedMaster.newSlave(this);
761
    return node;
762
    }
763

    
764
///////////////////////////////////////////////////////////////////////////////////////////////////
765
/**
766
 * Removes the first occurrence of a specified child from the list of children of our Surface.
767
 * <p>
768
 * A bit questionable method as there can be many different Nodes attached as children, some
769
 * of them having the same Effects but - for instance - different Mesh. Use with care.
770
 * <p>
771
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
772
 * DistortedMaster (by calling doWork())
773
 *
774
 * @param effects DistortedEffects to remove.
775
 */
776
  public void detach(DistortedEffects effects)
777
    {
778
    long id = effects.getID();
779
    DistortedNode node;
780
    boolean detached = false;
781

    
782
    for(int i=0; i<mNumChildren; i++)
783
      {
784
      node = mChildren.get(i);
785

    
786
      if( node.getEffects().getID()==id )
787
        {
788
        detached = true;
789
        mJobs.add(new Job(DETACH,node));
790
        DistortedMaster.newSlave(this);
791
        break;
792
        }
793
      }
794

    
795
    if( !detached )
796
      {
797
      // if we failed to detach any, it still might be the case that
798
      // there's an ATTACH job that we need to cancel.
799
      int num = mJobs.size();
800
      Job job;
801

    
802
      for(int i=0; i<num; i++)
803
        {
804
        job = mJobs.get(i);
805

    
806
        if( job.type==ATTACH && job.node.getEffects()==effects )
807
          {
808
          mJobs.remove(i);
809
          break;
810
          }
811
        }
812
      }
813
    }
814

    
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816
/**
817
 * Removes the first occurrence of a specified child from the list of children of our Surface.
818
 * <p>
819
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
820
 * DistortedMaster (by calling doWork())
821
 *
822
 * @param node The Node to remove.
823
 */
824
  public void detach(DistortedNode node)
825
    {
826
    mJobs.add(new Job(DETACH,node));
827
    DistortedMaster.newSlave(this);
828
    }
829

    
830
///////////////////////////////////////////////////////////////////////////////////////////////////
831
/**
832
 * Removes all children Nodes.
833
 * <p>
834
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
835
 * DistortedMaster (by calling doWork())
836
 */
837
  public void detachAll()
838
    {
839
    mJobs.add(new Job(DETALL,null));
840
    DistortedMaster.newSlave(this);
841
    }
842

    
843
///////////////////////////////////////////////////////////////////////////////////////////////////
844
/**
845
 * This is not really part of the public API. Has to be public only because it is a part of the
846
 * DistortedSlave interface, which should really be a class that we extend here instead but
847
 * Java has no multiple inheritance.
848
 *
849
 * @y.exclude
850
 */
851
  public void doWork()
852
    {
853
    int num = mJobs.size();
854
    Job job;
855

    
856
    for(int i=0; i<num; i++)
857
      {
858
      job = mJobs.remove(0);
859

    
860
      switch(job.type)
861
        {
862
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
863
                     job.node.setSurfaceParent(this);
864
                     DistortedMaster.addSorted(mChildren,job.node);
865
                     mNumChildren++;
866
                     break;
867
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
868
                       {
869
                       job.node.setSurfaceParent(null);
870
                       mNumChildren--;
871
                       }
872
                     break;
873
        case DETALL: if( mNumChildren>0 )
874
                       {
875
                       DistortedNode tmp;
876

    
877
                       for(int j=mNumChildren-1; j>=0; j--)
878
                         {
879
                         tmp = mChildren.remove(j);
880
                         tmp.setSurfaceParent(null);
881
                         }
882

    
883
                       mNumChildren = 0;
884
                       }
885
                     break;
886
        case SORT  : mChildren.remove(job.node);
887
                     DistortedMaster.addSorted(mChildren,job.node);
888
                     break;
889
        }
890
      }
891
    }
892
}
(8-8/21)