Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalOutputSurface.java @ 7602a827

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of DistortedLibrary.                                                               //
5
//                                                                                               //
6
// DistortedLibrary 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
// DistortedLibrary 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 DistortedLibrary.  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
import org.distorted.library.effectqueue.EffectQueuePostprocess;
27
import org.distorted.library.mesh.MeshBase;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * This is not really part of the public API.
32
 *
33
 * @y.exclude
34
 */
35
public abstract class InternalOutputSurface extends InternalSurface implements InternalChildrenList.Parent
36
{
37
  public static final int NO_DEPTH_NO_STENCIL = 0;
38
  public static final int DEPTH_NO_STENCIL    = 1;
39
  public static final int BOTH_DEPTH_STENCIL  = 2;
40

    
41
  float mFOV, mDistance, mNear;
42
  float[] mProjectionMatrix;
43

    
44
  int mDepthStencilCreated;
45
  int mDepthStencil;
46
  int[] mDepthStencilH;
47
  int[] mFBOH;
48

    
49
  float mMipmap;
50

    
51
  int mRealWidth;   // the Surface can be backed up with a texture that is
52
  int mRealHeight;  // larger than the viewport we have to it.
53
                    // mWidth,mHeight are the sizes of the Viewport, those -
54
                    // sizes of the backing up texture.
55

    
56
  int mCurrFBO;     // internal current FBO (see DistortedLibrary.FBO_QUEUE_SIZE)
57

    
58
  private static DistortedFramebuffer[] mBuffer=null; // Global buffers used for postprocessing.
59
  private long[] mTime;
60
  private float mClearR, mClearG, mClearB, mClearA, mClearDepth;
61
  private int mClear, mClearStencil;
62
  private boolean mRenderWayOIT;
63
  private InternalChildrenList mChildren;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  InternalOutputSurface(int width, int height, int createColor, int numfbos, int numcolors, int depthStencil, int fbo, int type)
68
    {
69
    super(width,height,createColor,numfbos,numcolors,type);
70

    
71
    mRenderWayOIT = false;
72
    mCurrFBO      = 0;
73

    
74
    mDepthStencilH = new int[numfbos];
75
    mFBOH          = new int[numfbos];
76

    
77
    mTime = new long[numfbos];
78
    for(int i=0; i<mNumFBOs;i++) mTime[i]=0;
79

    
80
    mRealWidth = width;
81
    mRealHeight= height;
82

    
83
    mProjectionMatrix = new float[16];
84

    
85
    mFOV = 60.0f;
86
    mNear=  0.5f;
87

    
88
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
89
    mDepthStencil = depthStencil;
90

    
91
    mFBOH[0]         = fbo;
92
    mDepthStencilH[0]= 0;
93

    
94
    mClearR = 0.0f;
95
    mClearG = 0.0f;
96
    mClearB = 0.0f;
97
    mClearA = 0.0f;
98

    
99
    mClearDepth = 1.0f;
100
    mClearStencil = 0;
101
    mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
102

    
103
    mMipmap = 1.0f;
104

    
105
    mChildren = new InternalChildrenList(this);
106

    
107
    createProjection();
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  private void createProjection()
113
    {
114
    if( mWidth>0 && mHeight>1 )
115
      {
116
      if( mFOV>0.0f )  // perspective projection
117
        {
118
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
119
        float q = mWidth*mNear;
120
        float c = mHeight*mNear;
121

    
122
        float left   = -q/2;
123
        float right  = +q/2;
124
        float bottom = -c/2;
125
        float top    = +c/2;
126
        float near   =  c/a;
127

    
128
        mDistance    = mHeight/a;
129
        float far    = 2*mDistance-near;
130

    
131
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
132
        }
133
      else             // parallel projection
134
        {
135
        float left   = -mWidth/2.0f;
136
        float right  = +mWidth/2.0f;
137
        float bottom = -mHeight/2.0f;
138
        float top    = +mHeight/2.0f;
139
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
140
        mDistance    = mWidth+mHeight;
141
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
142

    
143
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
144
        }
145
      }
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private static void createPostprocessingBuffers(int width, int height, float near)
151
    {
152
    final float CLEAR_R = 1.0f;
153
    final float CLEAR_G = 1.0f;
154
    final float CLEAR_B = 1.0f;
155
    final float CLEAR_A = 0.0f;
156
    final float CLEAR_D = 1.0f;
157
    final int   CLEAR_S = 0;
158

    
159
    mBuffer = new DistortedFramebuffer[EffectQuality.LENGTH];
160
    float mipmap=1.0f;
161

    
162
    for (int j=0; j<EffectQuality.LENGTH; j++)
163
      {
164
      mBuffer[j] = new DistortedFramebuffer(DistortedLibrary.FBO_QUEUE_SIZE,2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
165
      mBuffer[j].mMipmap = mipmap;
166
      mBuffer[j].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
167
      mBuffer[j].glClearColor(CLEAR_R, CLEAR_G, CLEAR_B, CLEAR_A);
168

    
169
      mipmap *= EffectQuality.MULTIPLIER;
170
      }
171

    
172
    InternalObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
173

    
174
    InternalRenderState.colorDepthStencilOn();
175
    GLES31.glClearColor(CLEAR_R, CLEAR_G, CLEAR_B, CLEAR_A);
176
    GLES31.glClearDepthf(CLEAR_D);
177
    GLES31.glClearStencil(CLEAR_S);
178

    
179
    for (int j=0; j<EffectQuality.LENGTH; j++)
180
      {
181
      for(int k = 0; k< DistortedLibrary.FBO_QUEUE_SIZE; k++)
182
        {
183
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBuffer[j].mFBOH[k]);
184
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k+1], 0);
185
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
186
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k  ], 0);
187
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
188
        }
189
      }
190

    
191
    InternalRenderState.colorDepthStencilRestore();
192

    
193
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  static synchronized void onDestroy()
199
    {
200
    if( mBuffer!=null )
201
      {
202
      for (int j = 0; j < EffectQuality.LENGTH; j++)
203
        {
204
        mBuffer[j] = null;
205
        }
206

    
207
      mBuffer = null;
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
// The postprocessing buffers mBuffer[] are generally speaking too large (there's just one static
213
// set of them) so before we use them for output, we need to adjust the Viewport as if they were
214
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
215
// adjust the texture coords - see the get{Width|Height}Correction functions.
216
//
217
// Also, adjust the Buffers so their Projection is the same like the surface we are supposed to be
218
// rendering to.
219

    
220
  private static void clonePostprocessingViewportAndProjection(InternalOutputSurface from)
221
    {
222
    if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight ||
223
        mBuffer[0].mFOV   != from.mFOV   || mBuffer[0].mNear   != from.mNear    )
224
      {
225
      InternalOutputSurface 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
        surface.mFOV    = from.mFOV;
234
        surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
235

    
236
        surface.createProjection();
237

    
238
        int maxw = surface.mWidth  > surface.mRealWidth  ? surface.mWidth  : surface.mRealWidth;
239
        int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
240

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

    
246
          surface.recreate();
247
          surface.create();
248
          }
249
        }
250
      }
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  private int blitWithDepth(long currTime, InternalOutputSurface buffer, int fbo)
256
    {
257
    GLES31.glViewport(0, 0, mWidth, mHeight);
258
    setAsOutput(currTime);
259
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
260
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
261
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
262
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
263

    
264
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
265
    GLES31.glStencilMask(0x00);
266

    
267
    DistortedLibrary.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
268
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
269
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
270
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
271
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
272

    
273
    // clear buffers
274
    GLES31.glStencilMask(0xff);
275
    GLES31.glDepthMask(true);
276
    GLES31.glColorMask(true,true,true,true);
277
    GLES31.glClearColor(buffer.mClearR,buffer.mClearG,buffer.mClearB,buffer.mClearA);
278
    GLES31.glClearDepthf(buffer.mClearDepth);
279
    GLES31.glClearStencil(buffer.mClearStencil);
280

    
281
    buffer.setAsOutput();
282
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo+1], 0);
283
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
284
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo  ], 0);
285
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
286

    
287
    return 1;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  private static void oitClear(InternalOutputSurface buffer)
293
    {
294
    int counter = DistortedLibrary.zeroOutAtomic();
295
    DistortedLibrary.oitClear(buffer,counter);
296
    GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  private int oitBuild(long time, InternalOutputSurface buffer, int fbo)
302
    {
303
    GLES31.glViewport(0, 0, mWidth, mHeight);
304
    setAsOutput(time);
305
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
306
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
307
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
308
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
309

    
310
    InternalRenderState.colorDepthStencilOn();
311
    InternalRenderState.enableDepthTest();
312

    
313
    DistortedLibrary.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
314
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
315
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
316
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
317
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
318

    
319
    InternalRenderState.colorDepthStencilRestore();
320
    InternalRenderState.restoreDepthTest();
321

    
322
    return 1;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// two phases: 1. collapse the SSBO 2. blend the ssbo's color
327

    
328
  private int oitRender(long currTime, int fbo)
329
    {
330
    float corrW = getWidthCorrection();
331
    float corrH = getHeightCorrection();
332

    
333
    // Do the Collapse Pass only if we do have a Depth attachment.
334
    // Otherwise there's no point (in fact we then would create a feedback loop!)
335

    
336
    if( mDepthStencilH[fbo] != 0 )
337
      {
338
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
339
      GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
340
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mDepthStencilH[fbo]);
341
      InternalRenderState.switchOffColorDepthStencil();
342
      DistortedLibrary.oitCollapse(this, corrW, corrH);
343
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
344
      }
345

    
346
    setAsOutput(currTime);
347
    InternalRenderState.switchColorDepthOnStencilOff();
348
    DistortedLibrary.oitRender(this, corrW, corrH);
349
    InternalRenderState.restoreColorDepthStencil();
350

    
351
    return 1;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  private void clear()
357
    {
358
    InternalRenderState.colorDepthStencilOn();
359
    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
360
    GLES31.glClearDepthf(mClearDepth);
361
    GLES31.glClearStencil(mClearStencil);
362
    GLES31.glClear(mClear);
363
    InternalRenderState.colorDepthStencilRestore();
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  void setCurrFBO(int fbo)
369
    {
370
    mCurrFBO = fbo;
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
375
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
376
// to a whole buffer (lastQueue.postprocess) and merge it (this.oitBuild or blitWithDepth - depending
377
// on the type of rendering)
378

    
379
  int renderChildren(long time, int numChildren, InternalChildrenList children, int fbo, boolean oit)
380
    {
381
    int numRenders=0, bucketChange=0;
382
    DistortedNode child;
383
    DistortedFramebuffer buffer=null;
384
    EffectQueuePostprocess lastQueue=null, currQueue;
385
    long lastBucket=0, currBucket;
386
    boolean renderDirectly=false;
387

    
388
    setCurrFBO(fbo);
389

    
390
    if( mBuffer!=null )
391
      {
392
      for (int i=0; i<EffectQuality.LENGTH; i++) mBuffer[i].setCurrFBO(fbo);
393
      }
394

    
395
    if( oit && numChildren>0 )
396
      {
397
      oitClear(this);
398
      }
399

    
400
    for(int i=0; i<numChildren; i++)
401
      {
402
      child = children.getChild(i);
403
      currQueue = (EffectQueuePostprocess)child.getEffects().getQueues()[3];
404
      currBucket= currQueue.getID();
405

    
406
      if( currBucket==0 )
407
        {
408
        setAsOutput(time);
409

    
410
        if( oit )
411
          {
412
          numRenders += child.drawOIT(time, this);
413
          GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
414
          }
415
        else
416
          {
417
          numRenders += child.draw(time, this);
418
          }
419
        }
420
      else
421
        {
422
        if( mBuffer==null )
423
          {
424
          createPostprocessingBuffers(mWidth,mHeight,mNear);
425
          for (int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].setCurrFBO(fbo);
426
          }
427

    
428
        if( lastBucket!=currBucket )
429
          {
430
          if( lastBucket==0 )
431
            {
432
            clonePostprocessingViewportAndProjection(this);
433
            }
434
          else
435
            {
436
            for(int j=bucketChange; j<i; j++) numRenders += lastQueue.preprocess( buffer,children.getChild(j), buffer.mDistance, buffer.mMipmap, buffer.mProjectionMatrix );
437
            numRenders += lastQueue.postprocess(buffer);
438

    
439
            if( oit )
440
              {
441
              numRenders += oitBuild(time, buffer, fbo);
442
              GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
443
              }
444
            else
445
              {
446
              numRenders += blitWithDepth(time, buffer, fbo);
447
              }
448
            buffer.clearBuffer(fbo);
449
            }
450

    
451
          buffer= mBuffer[currQueue.getQuality()];
452
          bucketChange= i;
453
          renderDirectly = currQueue.getRender();
454
          }
455

    
456
        if( renderDirectly )
457
          {
458
          setAsOutput(time);
459

    
460
          if( oit )
461
            {
462
            numRenders += child.drawOIT(time, this);
463
            GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
464
            }
465
          else
466
            {
467
            numRenders += child.draw(time, this);
468
            }
469
          }
470
        else
471
          {
472
          buffer.setAsOutput(time);
473
          child.drawNoBlend(time, buffer);
474
          }
475

    
476
        if( i==numChildren-1 )
477
          {
478
          for(int j=bucketChange; j<numChildren; j++) numRenders += currQueue.preprocess( buffer,children.getChild(j), buffer.mDistance, buffer.mMipmap, buffer.mProjectionMatrix );
479
          numRenders += currQueue.postprocess(buffer);
480

    
481
          if( oit )
482
            {
483
            numRenders += oitBuild(time, buffer, fbo);
484
            GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
485
            buffer.clearBuffer(fbo);
486
            }
487
          else
488
            {
489
            numRenders += blitWithDepth(time, buffer,fbo);
490
            }
491
          }
492
        } // end else (postprocessed child)
493

    
494
      lastQueue = currQueue;
495
      lastBucket= currBucket;
496
      } // end main for loop
497

    
498
    if( oit && numChildren>0 )
499
      {
500
      numRenders += oitRender(time, fbo);  // merge the OIT linked list
501
      }
502

    
503
    return numRenders;
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
/**
508
 * Not part of the public API.
509
 *
510
 * @y.exclude
511
 */
512
  public void adjustIsomorphism() { }
513

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
/**
516
 * Not part of the Public API.
517
 *
518
 * @y.exclude
519
 */
520
  public float getWidthCorrection()
521
    {
522
    return (float)mWidth/mRealWidth;
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
/**
527
 * Not part of the Public API.
528
 *
529
 * @y.exclude
530
 */
531
  public float getHeightCorrection()
532
    {
533
    return (float)mHeight/mRealHeight;
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  void clearBuffer(int fbo)
539
    {
540
    InternalRenderState.colorDepthStencilOn();
541

    
542
    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
543
    GLES31.glClearDepthf(mClearDepth);
544
    GLES31.glClearStencil(mClearStencil);
545

    
546
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[fbo]);
547
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*fbo+1], 0);
548
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
549
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*fbo  ], 0);
550
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
551

    
552
    InternalRenderState.colorDepthStencilRestore();
553
    }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

    
557
  void setAsOutput(long time)
558
    {
559
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
560

    
561
    if( mTime[mCurrFBO]!=time )
562
      {
563
      mTime[mCurrFBO] = time;
564
      clear();
565
      }
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569
// PUBLIC API
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571
/**
572
 * Draws all the attached children to this OutputSurface's 0th FBO.
573
 * <p>
574
 * Must be called from a thread holding OpenGL Context.
575
 *
576
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
577
 * @return Number of objects rendered.
578
 */
579
  public int render(long time)
580
    {
581
    return render(time,0);
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
/**
586
 * Draws all the attached children to this OutputSurface.
587
 * <p>
588
 * Must be called from a thread holding OpenGL Context.
589
 *
590
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
591
 * @param fbo The surface can have many FBOs backing it up - render this to FBO number 'fbo'.
592
 * @return Number of objects rendered.
593
 */
594
  public int render(long time, int fbo)
595
    {
596
    InternalMaster.toDo();
597
    toDo();
598
    InternalRenderState.reset();
599

    
600
    int numRenders=0, numChildren = mChildren.getNumChildren();
601
    DistortedNode node;
602
    long oldBucket=0, newBucket;
603

    
604
    for(int i=0; i<numChildren; i++)
605
      {
606
      node = mChildren.getChild(i);
607
      newBucket = node.getBucket();
608
      numRenders += node.renderRecursive(time);
609
      if( newBucket<oldBucket ) mChildren.rearrangeByBuckets(i,newBucket);
610
      else oldBucket=newBucket;
611
      }
612

    
613
    numRenders += renderChildren(time,numChildren,mChildren,fbo, mRenderWayOIT);
614

    
615
    return numRenders;
616
    }
617

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619
/**
620
 * Bind this Surface as a Framebuffer we can render to.
621
 * <p>
622
 * This version does not attempt to clear anything.
623
 */
624
  public void setAsOutput()
625
    {
626
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630
/**
631
 * Return the Near plane of the Projection included in the Surface.
632
 *
633
 * @return the Near plane.
634
 */
635
  public float getNear()
636
    {
637
    return mNear;
638
    }
639

    
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641
/**
642
 * Set mipmap level.
643
 * <p>
644
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
645
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
646
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
647
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
648
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
649
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
650
 * <p>
651
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
652
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
653
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
654
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
655
 *
656
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
657
 *               does not make any sense (that would result in loss of speed and no gain in quality)
658
 */
659
  public void setMipmap(float mipmap)
660
    {
661
    mMipmap = mipmap;
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
/**
666
 * Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
667
 * this Surface at the beginning of each frame.
668
 *
669
 * @param r the Red component. Default: 0.0f
670
 * @param g the Green component. Default: 0.0f
671
 * @param b the Blue component. Default: 0.0f
672
 * @param a the Alpha component. Default: 0.0f
673
 */
674
  public void glClearColor(float r, float g, float b, float a)
675
    {
676
    mClearR = r;
677
    mClearG = g;
678
    mClearB = b;
679
    mClearA = a;
680
    }
681

    
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683
/**
684
 * Uses glClearDepthf() to set up a value with which to clear
685
 * the Depth buffer of our Surface at the beginning of each frame.
686
 *
687
 * @param d the Depth. Default: 1.0f
688
 */
689
  public void glClearDepthf(float d)
690
    {
691
    mClearDepth = d;
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695
/**
696
 * Uses glClearStencil() to set up a value with which to clear the
697
 * Stencil buffer of our Surface at the beginning of each frame.
698
 *
699
 * @param s the Stencil. Default: 0
700
 */
701
  public void glClearStencil(int s)
702
    {
703
    mClearStencil = s;
704
    }
705

    
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707
/**
708
 * Which buffers to Clear at the beginning of each frame?
709
 * <p>
710
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
711
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
712
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
713
 *
714
 * @param mask bitwise OR of BUFFER_BITs to clear.
715
 */
716
  public void glClear(int mask)
717
    {
718
    mClear = mask;
719
    }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722
/**
723
 * Create new Projection matrix.
724
 *
725
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
726
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
727
 * @param near Distance between the screen plane and the near plane.
728
 *             Valid vaules: 0<near<1. When near==0 (illegal!), the Near Plane is exactly at the tip of
729
 *             the pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
730
 */
731
  public void setProjection(float fov, float near)
732
    {
733
    if( fov < 180.0f && fov >=0.0f )
734
      {
735
      mFOV = fov;
736
      }
737

    
738
    if( near<   1.0f && near> 0.0f )
739
      {
740
      mNear= near;
741
      }
742
    else if( near<=0.0f )
743
      {
744
      mNear = 0.01f;
745
      }
746
    else if( near>=1.0f )
747
      {
748
      mNear=0.99f;
749
      }
750

    
751
    if( mBuffer!=null )
752
      {
753
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
754
      }
755

    
756
    createProjection();
757
    }
758

    
759
///////////////////////////////////////////////////////////////////////////////////////////////////
760
/**
761
 * Return the vertical field of view angle.
762
 *
763
 * @return Vertival Field of View Angle, in degrees.
764
 */
765
  public float getFOV()
766
    {
767
    return mFOV;
768
    }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771
/**
772
 * Resize the underlying Framebuffer.
773
 * <p>
774
 * This method can be safely called mid-render as it doesn't interfere with rendering.
775
 *
776
 * @param width The new width.
777
 * @param height The new height.
778
 */
779
  public void resize(int width, int height)
780
    {
781
    if( mWidth!=width || mHeight!=height )
782
      {
783
      mWidth = mRealWidth = width;
784
      mHeight= mRealHeight= height;
785

    
786
      createProjection();
787

    
788
      if( mColorCreated==CREATED )
789
        {
790
        markForCreation();
791
        recreate();
792
        }
793
      }
794
    }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797
/**
798
 * Return true if the Surface contains a DEPTH attachment.
799
 *
800
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
801
 */
802
  public boolean hasDepth()
803
    {
804
    return mDepthStencilCreated==CREATED;
805
    }
806

    
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
/**
809
 * Return true if the Surface contains a STENCIL attachment.
810
 *
811
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
812
 */
813
  public boolean hasStencil()
814
    {
815
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
816
    }
817

    
818
///////////////////////////////////////////////////////////////////////////////////////////////////
819
/**
820
 * When rendering this Node, should we use the Order Independent Transparency render more?
821
 * <p>
822
 * There are two modes of rendering: the fast 'normal' way, which however renders transparent
823
 * fragments in different ways depending on which fragments get rendered first, or the slower
824
 * 'oit' way, which renders transparent fragments correctly regardless of their order.
825
 *
826
 * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
827
 */
828
  public void setOrderIndependentTransparency(boolean oit)
829
    {
830
    mRenderWayOIT = oit;
831
    }
832

    
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834
/**
835
 * When rendering this Node, should we use the Order Independent Transparency render more?
836
 * <p>
837
 * There are two modes of rendering: the fast 'normal' way, which however renders transparent
838
 * fragments in different ways depending on which fragments get rendered first, or the slower
839
 * 'oit' way, which renders transparent fragments correctly regardless of their order.
840
 *
841
 * @param oit True if we want to render more slowly, but in a way which accounts for transparency.
842
 * @param initialSize Initial number of transparent fragments we expect, in screenfuls.
843
 *                    I.e '1.0' means 'the scene we are going to render contains about 1 screen
844
 *                    worth of transparent fragments'. Valid values: 0.0 &lt; initialSize &lt; 10.0
845
 *                    Even if you get this wrong, the library will detect that there are more
846
 *                    transparent fragments than it has space for and readjust its internal buffers,
847
 *                    but only after a few frames during which one will probably see missing objects.
848
 */
849
public void setOrderIndependentTransparency(boolean oit, float initialSize)
850
  {
851
  mRenderWayOIT = oit;
852

    
853
  if( initialSize>0.0f && initialSize<10.0f )
854
    DistortedLibrary.setSSBOSize(initialSize);
855
  }
856

    
857
///////////////////////////////////////////////////////////////////////////////////////////////////
858
/**
859
 * Adds a new child to the last position in the list of our Surface's children.
860
 * <p>
861
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
862
 * InternalMaster (by calling doWork())
863
 *
864
 * @param node The new Node to add.
865
 */
866
  public void attach(DistortedNode node)
867
    {
868
    mChildren.attach(node);
869
    }
870

    
871
///////////////////////////////////////////////////////////////////////////////////////////////////
872
/**
873
 * Adds a new child to the last position in the list of our Surface's children.
874
 * <p>
875
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
876
 * InternalMaster (by calling doWork())
877
 *
878
 * @param surface InputSurface to initialize our child Node with.
879
 * @param effects DistortedEffects to initialize our child Node with.
880
 * @param mesh MeshBase to initialize our child Node with.
881
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
882
 */
883
  public DistortedNode attach(InternalSurface surface, DistortedEffects effects, MeshBase mesh)
884
    {
885
    return mChildren.attach(surface,effects,mesh);
886
    }
887

    
888
///////////////////////////////////////////////////////////////////////////////////////////////////
889
/**
890
 * Removes the first occurrence of a specified child from the list of children of our Surface.
891
 * <p>
892
 * A bit questionable method as there can be many different Nodes attached as children, some
893
 * of them having the same Effects but - for instance - different Mesh. Use with care.
894
 * <p>
895
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
896
 * InternalMaster (by calling doWork())
897
 *
898
 * @param effects DistortedEffects to remove.
899
 */
900
  public void detach(DistortedEffects effects)
901
    {
902
    mChildren.detach(effects);
903
    }
904

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906
/**
907
 * Removes the first occurrence of a specified child from the list of children of our Surface.
908
 * <p>
909
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
910
 * InternalMaster (by calling doWork())
911
 *
912
 * @param node The Node to remove.
913
 */
914
  public void detach(DistortedNode node)
915
    {
916
    mChildren.detach(node);
917
    }
918

    
919
///////////////////////////////////////////////////////////////////////////////////////////////////
920
/**
921
 * Removes all children Nodes.
922
 * <p>
923
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
924
 * InternalMaster (by calling doWork())
925
 */
926
  public void detachAll()
927
    {
928
    mChildren.detachAll();
929
    }
930
}
(12-12/14)