Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 8a57da61

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
  private int mLastValue =-1;
118
  // end section
119
  ////////////////////////////////////////////////////////////////////////////////
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
135
    mProjectionMatrix = new float[16];
136

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

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

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

    
146
    mTime = 0;
147
    mDebugLevel = 0;
148

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

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

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

    
160
    mMipmap = 1.0f;
161

    
162
    createProjection();
163
    }
164

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

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

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

    
182
    mSurfaceID = mSurfaceCounter.returnNext();
183

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

    
190
    createSurface();
191
    }
192

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

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

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  int getNewCounter()
214
    {
215
    int value = mIntBuffer.get(BUFFERING*mSurfaceID+mLastIndex);
216

    
217
    if( value!=mLastValue )
218
      {
219
      mLastValue = value;
220
      android.util.Log.d("surface", "surface id: "+mSurfaceID+" value now: "+mLastValue);
221
      }
222

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

    
226
    mIntBuffer.put(BUFFERING*mSurfaceID+mLastIndex,0);
227
    return BUFFERING*mSurfaceID + mLastIndex;
228
    }
229

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

    
232
  private void createProjection()
233
    {
234
    if( mWidth>0 && mHeight>1 )
235
      {
236
      if( mFOV>0.0f )  // perspective projection
237
        {
238
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
239
        float q = mWidth*mNear;
240
        float c = mHeight*mNear;
241

    
242
        float left   = -q/2;
243
        float right  = +q/2;
244
        float bottom = -c/2;
245
        float top    = +c/2;
246
        float near   =  c/a;
247

    
248
        mDistance    = mHeight/a;
249
        float far    = 2*mDistance-near;
250

    
251
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
252
        }
253
      else             // parallel projection
254
        {
255
        float left   = -mWidth/2.0f;
256
        float right  = +mWidth/2.0f;
257
        float bottom = -mHeight/2.0f;
258
        float top    = +mHeight/2.0f;
259
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
260
        mDistance    = mWidth+mHeight;
261
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
262

    
263
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
264
        }
265
      }
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private void createBuffers()
271
    {
272
    float mipmap=1.0f;
273

    
274
    for(int j=0; j<EffectQuality.LENGTH; j++)
275
      {
276
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
277
      mBuffer[j].mMipmap = mipmap;
278
      mBuffer[j].mNear   = mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
279
      mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
280
      mipmap *= EffectQuality.MULTIPLIER;
281
      }
282

    
283
    mBuffer[EffectQuality.LENGTH] = this;
284

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

    
287
    GLES31.glStencilMask(0xff);
288
    GLES31.glDepthMask(true);
289
    GLES31.glColorMask(true,true,true,true);
290
    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
291
    GLES31.glClearDepthf(1.0f);
292
    GLES31.glClearStencil(0);
293

    
294
    for(int j=0; j<EffectQuality.LENGTH; j++)
295
      {
296
      mBuffer[j].setAsOutput();
297
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
298
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
299
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
300
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
301
      }
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
307
    {
308
    GLES31.glViewport(0, 0, mWidth, mHeight);
309
    setAsOutput(currTime);
310
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
311
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[0]);
312
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
313
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
314

    
315
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
316
    GLES31.glStencilMask(0x00);
317

    
318
    DistortedEffects.blitDepthPriv(this);
319
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
320
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
321
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
322
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
323

    
324
    // clear buffers
325
    GLES31.glStencilMask(0xff);
326
    GLES31.glDepthMask(true);
327
    GLES31.glColorMask(true,true,true,true);
328
    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
329
    GLES31.glClearDepthf(1.0f);
330
    GLES31.glClearStencil(0);
331

    
332
    buffer.setAsOutput();
333
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[1], 0);
334
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
335
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[0], 0);
336
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
337

    
338
    return 1;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
343
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
344
// to a whole buffer and merge it.
345

    
346
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
347
    {
348
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
349
    DistortedNode child1, child2;
350
    EffectQueuePostprocess lastQueue=null, currQueue;
351
    long lastBucket=0, currBucket;
352

    
353
    for(int i=0; i<numChildren; i++)
354
      {
355
      child1 = children.get(i);
356
      currQueue = child1.getPostprocessQueue();
357
      currBucket= currQueue.getID();
358

    
359
      if( currBucket==0 ) numRenders += child1.draw(time,this);
360
      else
361
        {
362
        if( mBuffer[0]==null ) createBuffers();
363

    
364
        if( lastBucket!=currBucket )
365
          {
366
          if( lastBucket!=0 )
367
            {
368
            for(int j=bucketChange; j<i; j++)
369
              {
370
              child2 = children.get(j);
371
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
372
              }
373

    
374
            numRenders += lastQueue.postprocess(this);
375
            numRenders += blitWithDepth(time, mBuffer[quality]);
376
            }
377

    
378
          internalQuality = currQueue.getInternalQuality();
379
          quality         = currQueue.getQuality();
380
          bucketChange    = i;
381
          }
382

    
383
        child1.draw(time,mBuffer[quality]);
384
        //numRenders += currQueue.draw(child1,time,mBuffer);
385

    
386
        if( i==numChildren-1 )
387
          {
388
          for(int j=bucketChange; j<numChildren; j++)
389
            {
390
            child2 = children.get(j);
391
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
392
            }
393

    
394
          numRenders += currQueue.postprocess(this);
395
          numRenders += blitWithDepth(time, mBuffer[quality]);
396
          }
397
        }
398

    
399
      lastQueue = currQueue;
400
      lastBucket= currBucket;
401
      }
402

    
403
    return numRenders;
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  ArrayList<DistortedNode> getChildren()
409
    {
410
    return mChildren;
411
    }
412

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

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

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

    
443
    // change tree topology (attach and detach children)
444
/*
445
    boolean changed1 =
446
*/
447
    DistortedMaster.toDo();
448
/*
449
    if( changed1 )
450
      {
451
      for(int i=0; i<mNumChildren; i++)
452
        {
453
        mChildren.get(i).debug(0);
454
        }
455

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

    
477
    int numRenders=0;
478

    
479
    for(int i=0; i<mNumChildren; i++)
480
      {
481
      numRenders += mChildren.get(i).renderRecursive(time);
482
      }
483

    
484
    setAsOutput(time);
485
    numRenders += renderChildren(time,mNumChildren,mChildren);
486

    
487
    if( mDebugLevel != 0 ) renderDebug(time);
488

    
489
    return numRenders;
490
    }
491

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

    
505
    if( mTime!=time )
506
      {
507
      mTime = time;
508
      DistortedRenderState.colorDepthStencilOn();
509
      GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
510
      GLES31.glClearDepthf(mClearDepth);
511
      GLES31.glClearStencil(mClearStencil);
512
      GLES31.glClear(mClear);
513
      DistortedRenderState.colorDepthStencilRestore();
514
      }
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
/**
519
 * Bind this Surface as a Framebuffer we can render to.
520
 * <p>
521
 * This version does not attempt to clear anything.
522
 */
523

    
524
  public void setAsOutput()
525
    {
526
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
/**
531
 * Return the Near plane of the Projection included in the Surface.
532
 *
533
 * @return the Near plane.
534
 */
535
  public float getNear()
536
    {
537
    return mNear;
538
    }
539

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

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

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
/**
584
 * Uses glClearDepthf() to set up a value with which to clear
585
 * the Depth buffer of our Surface at the beginning of each frame.
586
 *
587
 * @param d the Depth. Default: 1.0f
588
 */
589
  public void glClearDepthf(float d)
590
    {
591
    mClearDepth = d;
592
    }
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595
/**
596
 * Uses glClearStencil() to set up a value with which to clear the
597
 * Stencil buffer of our Surface at the beginning of each frame.
598
 *
599
 * @param s the Stencil. Default: 0
600
 */
601
  public void glClearStencil(int s)
602
    {
603
    mClearStencil = s;
604
    }
605

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

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

    
638
    if( near<   1.0f && near> 0.0f )
639
      {
640
      mNear= near;
641
      }
642
    else if( near<=0.0f )
643
      {
644
      mNear = 0.01f;
645
      }
646
    else if( near>=1.0f )
647
      {
648
      mNear=0.99f;
649
      }
650

    
651
    if( mBuffer[0]!=null )
652
      {
653
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
654
      }
655

    
656
    createProjection();
657
    }
658

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

    
675
      createProjection();
676

    
677
      if( mColorCreated==CREATED )
678
        {
679
        markForCreation();
680
        recreate();
681
        }
682
      }
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686
/**
687
 * Return true if the Surface contains a DEPTH attachment.
688
 *
689
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
690
 */
691
  public boolean hasDepth()
692
    {
693
    return mDepthStencilCreated==CREATED;
694
    }
695

    
696
///////////////////////////////////////////////////////////////////////////////////////////////////
697
/**
698
 * Return true if the Surface contains a STENCIL attachment.
699
 *
700
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
701
 */
702
  public boolean hasStencil()
703
    {
704
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708
/**
709
 * Adds a new child to the last position in the list of our Surface's children.
710
 * <p>
711
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
712
 * DistortedMaster (by calling doWork())
713
 *
714
 * @param node The new Node to add.
715
 */
716
  public void attach(DistortedNode node)
717
    {
718
    mJobs.add(new Job(ATTACH,node));
719
    DistortedMaster.newSlave(this);
720
    }
721

    
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723
/**
724
 * Adds a new child to the last position in the list of our Surface's children.
725
 * <p>
726
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
727
 * DistortedMaster (by calling doWork())
728
 *
729
 * @param surface InputSurface to initialize our child Node with.
730
 * @param effects DistortedEffects to initialize our child Node with.
731
 * @param mesh MeshObject to initialize our child Node with.
732
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
733
 */
734
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
735
    {
736
    DistortedNode node = new DistortedNode(surface,effects,mesh);
737
    mJobs.add(new Job(ATTACH,node));
738
    DistortedMaster.newSlave(this);
739
    return node;
740
    }
741

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

    
760
    for(int i=0; i<mNumChildren; i++)
761
      {
762
      node = mChildren.get(i);
763

    
764
      if( node.getEffects().getID()==id )
765
        {
766
        detached = true;
767
        mJobs.add(new Job(DETACH,node));
768
        DistortedMaster.newSlave(this);
769
        break;
770
        }
771
      }
772

    
773
    if( !detached )
774
      {
775
      // if we failed to detach any, it still might be the case that
776
      // there's an ATTACH job that we need to cancel.
777
      int num = mJobs.size();
778
      Job job;
779

    
780
      for(int i=0; i<num; i++)
781
        {
782
        job = mJobs.get(i);
783

    
784
        if( job.type==ATTACH && job.node.getEffects()==effects )
785
          {
786
          mJobs.remove(i);
787
          break;
788
          }
789
        }
790
      }
791
    }
792

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

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810
 * Removes all children Nodes.
811
 * <p>
812
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
813
 * DistortedMaster (by calling doWork())
814
 */
815
  public void detachAll()
816
    {
817
    mJobs.add(new Job(DETALL,null));
818
    DistortedMaster.newSlave(this);
819
    }
820

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

    
834
    for(int i=0; i<num; i++)
835
      {
836
      job = mJobs.remove(0);
837

    
838
      switch(job.type)
839
        {
840
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
841
                     job.node.setSurfaceParent(this);
842
                     DistortedMaster.addSorted(mChildren,job.node);
843
                     mNumChildren++;
844
                     break;
845
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
846
                       {
847
                       job.node.setSurfaceParent(null);
848
                       mNumChildren--;
849
                       }
850
                     break;
851
        case DETALL: if( mNumChildren>0 )
852
                       {
853
                       DistortedNode tmp;
854

    
855
                       for(int j=mNumChildren-1; j>=0; j--)
856
                         {
857
                         tmp = mChildren.remove(j);
858
                         tmp.setSurfaceParent(null);
859
                         }
860

    
861
                       mNumChildren = 0;
862
                       }
863
                     break;
864
        case SORT  : mChildren.remove(job.node);
865
                     DistortedMaster.addSorted(mChildren,job.node);
866
                     break;
867
        }
868
      }
869
    }
870
}
(9-9/22)