Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 66ace7f5

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.nio.ByteBuffer;
28
import java.nio.ByteOrder;
29
import java.nio.IntBuffer;
30
import java.util.ArrayList;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * This is not really part of the public API.
35
 *
36
 * @y.exclude
37
 */
38
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
39
{
40
//////////// DEBUG FLAGS /////////////////////////////////////////////
41
/**
42
 * When rendering a Screen, show FPS in the upper-left corner?
43
 */
44
public static final int DEBUG_FPS = 1;
45
//////////// END DEBUG FLAGS /////////////////////////////////////////
46

    
47
/**
48
 * Do not create DEPTH or STENCIL attachment
49
 */
50
  public static final int NO_DEPTH_NO_STENCIL = 0;
51
/**
52
 * Create DEPTH, but not STENCIL
53
 */
54
  public static final int DEPTH_NO_STENCIL    = 1;
55
/**
56
 * Create both DEPTH and STENCIL
57
 */
58
  public static final int BOTH_DEPTH_STENCIL  = 2;
59

    
60
  private static final int ATTACH = 0;
61
  private static final int DETACH = 1;
62
  private static final int DETALL = 2;
63
  private static final int SORT   = 3;
64

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

    
68
  private class Job
69
    {
70
    int type;
71
    DistortedNode node;
72

    
73
    Job(int t, DistortedNode n)
74
      {
75
      type = t;
76
      node = n;
77
      }
78
    }
79

    
80
  private ArrayList<Job> mJobs = new ArrayList<>();
81

    
82
  DistortedOutputSurface[] mBuffer;
83

    
84
  private long mTime;
85
  private float mFOV;
86
  float mDistance, mNear;
87
  float[] mProjectionMatrix;
88

    
89
  int mDepthStencilCreated;
90
  int mDepthStencil;
91
  int[] mDepthStencilH = new int[1];
92
  int[] mFBOH          = new int[1];
93

    
94
  private float mClearR, mClearG, mClearB, mClearA;
95
  private float mClearDepth;
96
  private int mClearStencil;
97
  private int mClear;
98
  float mMipmap;
99

    
100
  private int mDebugLevel;
101

    
102
  ////////////////////////////////////////////////////////////////////////////////
103
  // section dealing with Shader Storage Buffer Object (for counting transparency)
104
  private static final int BUFFERING = 3;
105
  private static int mBufferSize     =10;
106
  private static DistortedObjectCounter mSurfaceCounter = new DistortedObjectCounter();
107
  private static int[] mSSBO = new int[1];
108
  private static IntBuffer mIntBuffer;
109

    
110
  static
111
    {
112
    mSSBO[0]= -1;
113
    }
114

    
115
  private int mSurfaceID;
116
  private int mLastIndex;
117
  // end section
118
  ////////////////////////////////////////////////////////////////////////////////
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  abstract void prepareDebug(long time);
123
  abstract void renderDebug(long time);
124
  abstract void createSurface();
125
  abstract void deleteSurface();
126
  abstract void recreateSurface();
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
131
    {
132
    super(width,height,createColor,numcolors,type);
133

    
134
    mProjectionMatrix = new float[16];
135

    
136
    mFOV = 60.0f;
137
    mNear=  0.5f;
138

    
139
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
140
    mDepthStencil = depthStencil;
141

    
142
    mFBOH[0]         = fbo;
143
    mDepthStencilH[0]= 0;
144

    
145
    mTime = 0;
146
    mDebugLevel = 0;
147

    
148
    mClearR = 0.0f;
149
    mClearG = 0.0f;
150
    mClearB = 0.0f;
151
    mClearA = 0.0f;
152

    
153
    mClearDepth = 1.0f;
154
    mClearStencil = 0;
155
    mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
156

    
157
    mBuffer = new DistortedOutputSurface[1+EffectQuality.LENGTH];
158

    
159
    mMipmap = 1.0f;
160

    
161
    createProjection();
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// Must be called from a thread holding OpenGL Context
166

    
167
  void create()
168
    {
169
    if( mSSBO[0]<0 )
170
      {
171
      GLES31.glGenBuffers(1,mSSBO,0);
172

    
173
      // here bind the new SSBO and map it
174
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
175
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, BUFFERING*mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
176
      ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, BUFFERING*mBufferSize*4, GLES31.GL_MAP_READ_BIT );
177
      mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
178
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
179
      }
180

    
181
    mSurfaceID = mSurfaceCounter.returnNext();
182

    
183
    if( mSurfaceID>=mBufferSize )
184
      {
185
      // increase the size of mSSBOBuffer, copy over old values, remap.
186
      // TODO (don't need this if there are never more than mBufferSize=10 Surfaces)
187
      }
188

    
189
    createSurface();
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// Must be called from a thread holding OpenGL Context
194

    
195
  void delete()
196
    {
197
    mSurfaceCounter.release(mSurfaceID);
198
    deleteSurface();
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  void recreate()
204
    {
205
    mSSBO[0] = -1;
206
    mSurfaceCounter.releaseAll();
207
    recreateSurface();
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  int resetNewCounter()
213
    {
214
    //mIntBuffer.put(BUFFERING*mSurfaceID+mLastIndex,0);
215
    return BUFFERING*mSurfaceID + mLastIndex;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  int returnOldCounter()
221
    {
222
    int ret = mIntBuffer.get(BUFFERING*mSurfaceID+mLastIndex);
223

    
224
    mLastIndex++;
225
    if( mLastIndex>=BUFFERING ) mLastIndex-=BUFFERING;
226

    
227
    return ret;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  int returnCounter()
233
    {
234
    return mIntBuffer.get(0);
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  private void createProjection()
240
    {
241
    if( mWidth>0 && mHeight>1 )
242
      {
243
      if( mFOV>0.0f )  // perspective projection
244
        {
245
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
246
        float q = mWidth*mNear;
247
        float c = mHeight*mNear;
248

    
249
        float left   = -q/2;
250
        float right  = +q/2;
251
        float bottom = -c/2;
252
        float top    = +c/2;
253
        float near   =  c/a;
254

    
255
        mDistance    = mHeight/a;
256
        float far    = 2*mDistance-near;
257

    
258
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
259
        }
260
      else             // parallel projection
261
        {
262
        float left   = -mWidth/2.0f;
263
        float right  = +mWidth/2.0f;
264
        float bottom = -mHeight/2.0f;
265
        float top    = +mHeight/2.0f;
266
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
267
        mDistance    = mWidth+mHeight;
268
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
269

    
270
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
271
        }
272
      }
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void createBuffers()
278
    {
279
    float mipmap=1.0f;
280

    
281
    for(int j=0; j<EffectQuality.LENGTH; j++)
282
      {
283
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
284
      mBuffer[j].mMipmap = mipmap;
285
      mBuffer[j].mNear   = mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
286
      mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
287
      mipmap *= EffectQuality.MULTIPLIER;
288
      }
289

    
290
    mBuffer[EffectQuality.LENGTH] = this;
291

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

    
294
    GLES31.glStencilMask(0xff);
295
    GLES31.glDepthMask(true);
296
    GLES31.glColorMask(true,true,true,true);
297
    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
298
    GLES31.glClearDepthf(1.0f);
299
    GLES31.glClearStencil(0);
300

    
301
    for(int j=0; j<EffectQuality.LENGTH; j++)
302
      {
303
      mBuffer[j].setAsOutput();
304
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
305
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
306
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
307
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
308
      }
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
314
    {
315
    GLES31.glViewport(0, 0, mWidth, mHeight);
316
    setAsOutput(currTime);
317
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
318
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[0]);
319
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
320
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
321

    
322
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
323
    GLES31.glStencilMask(0x00);
324

    
325
    DistortedEffects.blitDepthPriv(this);
326
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
327
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
328
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
329
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
330

    
331
    // clear buffers
332
    GLES31.glStencilMask(0xff);
333
    GLES31.glDepthMask(true);
334
    GLES31.glColorMask(true,true,true,true);
335
    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
336
    GLES31.glClearDepthf(1.0f);
337
    GLES31.glClearStencil(0);
338

    
339
    buffer.setAsOutput();
340
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[1], 0);
341
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
342
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[0], 0);
343
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
344

    
345
    return 1;
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
350
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
351
// to a whole buffer and merge it.
352

    
353
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
354
    {
355
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
356
    DistortedNode child1, child2;
357
    EffectQueuePostprocess lastQueue=null, currQueue;
358
    long lastBucket=0, currBucket;
359

    
360
    for(int i=0; i<numChildren; i++)
361
      {
362
      child1 = children.get(i);
363
      currQueue = child1.getPostprocessQueue();
364
      currBucket= currQueue.getID();
365

    
366
      if( currBucket==0 ) numRenders += child1.draw(time,this);
367
      else
368
        {
369
        if( mBuffer[0]==null ) createBuffers();
370

    
371
        if( lastBucket!=currBucket )
372
          {
373
          if( lastBucket!=0 )
374
            {
375
            for(int j=bucketChange; j<i; j++)
376
              {
377
              child2 = children.get(j);
378
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
379
              }
380

    
381
            numRenders += lastQueue.postprocess(this);
382
            numRenders += blitWithDepth(time, mBuffer[quality]);
383
            }
384

    
385
          internalQuality = currQueue.getInternalQuality();
386
          quality         = currQueue.getQuality();
387
          bucketChange    = i;
388
          }
389

    
390
        child1.draw(time,mBuffer[quality]);
391
        //numRenders += currQueue.draw(child1,time,mBuffer);
392

    
393
        if( i==numChildren-1 )
394
          {
395
          for(int j=bucketChange; j<numChildren; j++)
396
            {
397
            child2 = children.get(j);
398
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
399
            }
400

    
401
          numRenders += currQueue.postprocess(this);
402
          numRenders += blitWithDepth(time, mBuffer[quality]);
403
          }
404
        }
405

    
406
      lastQueue = currQueue;
407
      lastBucket= currBucket;
408
      }
409

    
410
    return numRenders;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  ArrayList<DistortedNode> getChildren()
416
    {
417
    return mChildren;
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
// PUBLIC API
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
/**
424
 * Make the library show various debugging information.
425
 * <p>
426
 * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
427
 *
428
 * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
429
 */
430
  public void setDebug(int bitmask)
431
    {
432
    if( this instanceof DistortedScreen )
433
      mDebugLevel = bitmask;
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

    
438
/**
439
 * Draws all the attached children to this OutputSurface.
440
 * <p>
441
 * Must be called from a thread holding OpenGL Context.
442
 *
443
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
444
 * @return Number of objects rendered.
445
 */
446
  public int render(long time)
447
    {
448
    if( mDebugLevel!=0 ) prepareDebug(time);
449

    
450
    // change tree topology (attach and detach children)
451
/*
452
    boolean changed1 =
453
*/
454
    DistortedMaster.toDo();
455
/*
456
    if( changed1 )
457
      {
458
      for(int i=0; i<mNumChildren; i++)
459
        {
460
        mChildren.get(i).debug(0);
461
        }
462

    
463
      DistortedNode.debugMap();
464
      }
465
*/
466
    // create and delete all underlying OpenGL resources
467
    // Watch out: FIRST change topology, only then deal
468
    // with OpenGL resources. That's because changing Tree
469
    // can result in additional Framebuffers that would need
470
    // to be created immediately, before the calls to drawRecursive()
471
/*
472
    boolean changed2 =
473
*/
474
    toDo();
475
/*
476
    if( changed2 )
477
      {
478
      DistortedObject.debugLists();
479
      }
480
*/
481
    // mark OpenGL state as unknown
482
    DistortedRenderState.reset();
483

    
484
    int numRenders=0;
485

    
486
    for(int i=0; i<mNumChildren; i++)
487
      {
488
      numRenders += mChildren.get(i).renderRecursive(time);
489
      }
490

    
491
    setAsOutput(time);
492
    numRenders += renderChildren(time,mNumChildren,mChildren);
493

    
494
    if( mDebugLevel != 0 ) renderDebug(time);
495

    
496
    return numRenders;
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500
/**
501
 * Bind this Surface as a Framebuffer we can render to.
502
 *
503
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
504
 *             out if this is the first time during present frame that this FBO is being set as output.
505
 *             If so, the library, in addition to binding the Surface for output, also clears the
506
 *             Surface's color and depth attachments.
507
 */
508
  public void setAsOutput(long time)
509
    {
510
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
511

    
512
    if( mTime!=time )
513
      {
514
      mTime = time;
515
      DistortedRenderState.colorDepthStencilOn();
516
      GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
517
      GLES31.glClearDepthf(mClearDepth);
518
      GLES31.glClearStencil(mClearStencil);
519
      GLES31.glClear(mClear);
520
      DistortedRenderState.colorDepthStencilRestore();
521
      }
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
/**
526
 * Bind this Surface as a Framebuffer we can render to.
527
 * <p>
528
 * This version does not attempt to clear anything.
529
 */
530

    
531
  public void setAsOutput()
532
    {
533
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537
/**
538
 * Return the Near plane of the Projection included in the Surface.
539
 *
540
 * @return the Near plane.
541
 */
542
  public float getNear()
543
    {
544
    return mNear;
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548
/**
549
 * Set mipmap level.
550
 * <p>
551
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
552
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
553
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
554
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
555
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
556
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
557
 * <p>
558
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
559
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
560
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
561
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
562
 *
563
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
564
 *               does not make any sense (that would result in loss of speed and no gain in quality)
565
 */
566
  public void setMipmap(float mipmap)
567
    {
568
    mMipmap = mipmap;
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
/**
573
 * Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
574
 * this Surface at the beginning of each frame.
575
 *
576
 * @param r the Red component. Default: 0.0f
577
 * @param g the Green component. Default: 0.0f
578
 * @param b the Blue component. Default: 0.0f
579
 * @param a the Alpha component. Default: 0.0f
580
 */
581
  public void glClearColor(float r, float g, float b, float a)
582
    {
583
    mClearR = r;
584
    mClearG = g;
585
    mClearB = b;
586
    mClearA = a;
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590
/**
591
 * Uses glClearDepthf() to set up a value with which to clear
592
 * the Depth buffer of our Surface at the beginning of each frame.
593
 *
594
 * @param d the Depth. Default: 1.0f
595
 */
596
  public void glClearDepthf(float d)
597
    {
598
    mClearDepth = d;
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602
/**
603
 * Uses glClearStencil() to set up a value with which to clear the
604
 * Stencil buffer of our Surface at the beginning of each frame.
605
 *
606
 * @param s the Stencil. Default: 0
607
 */
608
  public void glClearStencil(int s)
609
    {
610
    mClearStencil = s;
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
/**
615
 * Which buffers to Clear at the beginning of each frame?
616
 * <p>
617
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
618
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
619
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
620
 *
621
 * @param mask bitwise OR of BUFFER_BITs to clear.
622
 */
623
  public void glClear(int mask)
624
    {
625
    mClear = mask;
626
    }
627

    
628
///////////////////////////////////////////////////////////////////////////////////////////////////
629
/**
630
 * Create new Projection matrix.
631
 *
632
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
633
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
634
 * @param near Distance between the screen plane and the near plane.
635
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
636
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
637
 */
638
  public void setProjection(float fov, float near)
639
    {
640
    if( fov < 180.0f && fov >=0.0f )
641
      {
642
      mFOV = fov;
643
      }
644

    
645
    if( near<   1.0f && near> 0.0f )
646
      {
647
      mNear= near;
648
      }
649
    else if( near<=0.0f )
650
      {
651
      mNear = 0.01f;
652
      }
653
    else if( near>=1.0f )
654
      {
655
      mNear=0.99f;
656
      }
657

    
658
    if( mBuffer[0]!=null )
659
      {
660
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
661
      }
662

    
663
    createProjection();
664
    }
665

    
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667
/**
668
 * Resize the underlying Framebuffer.
669
 * <p>
670
 * This method can be safely called mid-render as it doesn't interfere with rendering.
671
 *
672
 * @param width The new width.
673
 * @param height The new height.
674
 */
675
  public void resize(int width, int height)
676
    {
677
    if( mWidth!=width || mHeight!=height )
678
      {
679
      mWidth = width;
680
      mHeight= height;
681

    
682
      createProjection();
683

    
684
      if( mColorCreated==CREATED )
685
        {
686
        markForCreation();
687
        recreate();
688
        }
689
      }
690
    }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
/**
694
 * Return true if the Surface contains a DEPTH attachment.
695
 *
696
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
697
 */
698
  public boolean hasDepth()
699
    {
700
    return mDepthStencilCreated==CREATED;
701
    }
702

    
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704
/**
705
 * Return true if the Surface contains a STENCIL attachment.
706
 *
707
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
708
 */
709
  public boolean hasStencil()
710
    {
711
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
712
    }
713

    
714
///////////////////////////////////////////////////////////////////////////////////////////////////
715
/**
716
 * Adds a new child to the last position in the list of our Surface's children.
717
 * <p>
718
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
719
 * DistortedMaster (by calling doWork())
720
 *
721
 * @param node The new Node to add.
722
 */
723
  public void attach(DistortedNode node)
724
    {
725
    mJobs.add(new Job(ATTACH,node));
726
    DistortedMaster.newSlave(this);
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 surface InputSurface to initialize our child Node with.
737
 * @param effects DistortedEffects to initialize our child Node with.
738
 * @param mesh MeshObject to initialize our child Node with.
739
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
740
 */
741
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
742
    {
743
    DistortedNode node = new DistortedNode(surface,effects,mesh);
744
    mJobs.add(new Job(ATTACH,node));
745
    DistortedMaster.newSlave(this);
746
    return node;
747
    }
748

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750
/**
751
 * Removes the first occurrence of a specified child from the list of children of our Surface.
752
 * <p>
753
 * A bit questionable method as there can be many different Nodes attached as children, some
754
 * of them having the same Effects but - for instance - different Mesh. Use with care.
755
 * <p>
756
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
757
 * DistortedMaster (by calling doWork())
758
 *
759
 * @param effects DistortedEffects to remove.
760
 */
761
  public void detach(DistortedEffects effects)
762
    {
763
    long id = effects.getID();
764
    DistortedNode node;
765
    boolean detached = false;
766

    
767
    for(int i=0; i<mNumChildren; i++)
768
      {
769
      node = mChildren.get(i);
770

    
771
      if( node.getEffects().getID()==id )
772
        {
773
        detached = true;
774
        mJobs.add(new Job(DETACH,node));
775
        DistortedMaster.newSlave(this);
776
        break;
777
        }
778
      }
779

    
780
    if( !detached )
781
      {
782
      // if we failed to detach any, it still might be the case that
783
      // there's an ATTACH job that we need to cancel.
784
      int num = mJobs.size();
785
      Job job;
786

    
787
      for(int i=0; i<num; i++)
788
        {
789
        job = mJobs.get(i);
790

    
791
        if( job.type==ATTACH && job.node.getEffects()==effects )
792
          {
793
          mJobs.remove(i);
794
          break;
795
          }
796
        }
797
      }
798
    }
799

    
800
///////////////////////////////////////////////////////////////////////////////////////////////////
801
/**
802
 * Removes the first occurrence of a specified child from the list of children of our Surface.
803
 * <p>
804
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
805
 * DistortedMaster (by calling doWork())
806
 *
807
 * @param node The Node to remove.
808
 */
809
  public void detach(DistortedNode node)
810
    {
811
    mJobs.add(new Job(DETACH,node));
812
    DistortedMaster.newSlave(this);
813
    }
814

    
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816
/**
817
 * Removes all children Nodes.
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
  public void detachAll()
823
    {
824
    mJobs.add(new Job(DETALL,null));
825
    DistortedMaster.newSlave(this);
826
    }
827

    
828
///////////////////////////////////////////////////////////////////////////////////////////////////
829
/**
830
 * This is not really part of the public API. Has to be public only because it is a part of the
831
 * DistortedSlave interface, which should really be a class that we extend here instead but
832
 * Java has no multiple inheritance.
833
 *
834
 * @y.exclude
835
 */
836
  public void doWork()
837
    {
838
    int num = mJobs.size();
839
    Job job;
840

    
841
    for(int i=0; i<num; i++)
842
      {
843
      job = mJobs.remove(0);
844

    
845
      switch(job.type)
846
        {
847
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
848
                     job.node.setSurfaceParent(this);
849
                     DistortedMaster.addSorted(mChildren,job.node);
850
                     mNumChildren++;
851
                     break;
852
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
853
                       {
854
                       job.node.setSurfaceParent(null);
855
                       mNumChildren--;
856
                       }
857
                     break;
858
        case DETALL: if( mNumChildren>0 )
859
                       {
860
                       DistortedNode tmp;
861

    
862
                       for(int j=mNumChildren-1; j>=0; j--)
863
                         {
864
                         tmp = mChildren.remove(j);
865
                         tmp.setSurfaceParent(null);
866
                         }
867

    
868
                       mNumChildren = 0;
869
                       }
870
                     break;
871
        case SORT  : mChildren.remove(job.node);
872
                     DistortedMaster.addSorted(mChildren,job.node);
873
                     break;
874
        }
875
      }
876
    }
877
}
(9-9/22)