Project

General

Profile

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

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

1 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 c5369f1b leszek
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
23 af4cc5db Leszek Koltunski
import android.opengl.Matrix;
24 86e99907 leszek
25 3521c6fe leszek
import org.distorted.library.effect.EffectQuality;
26
27 12f45260 Leszek Koltunski
import java.nio.ByteBuffer;
28
import java.nio.ByteOrder;
29 66ace7f5 Leszek Koltunski
import java.nio.IntBuffer;
30 a09ada4c Leszek Koltunski
import java.util.ArrayList;
31 af4cc5db Leszek Koltunski
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33 70b6a155 Leszek Koltunski
/**
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 af4cc5db Leszek Koltunski
{
40 86e99907 leszek
//////////// 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 89de975c leszek
/**
48
 * Do not create DEPTH or STENCIL attachment
49
 */
50 23eecbd9 Leszek Koltunski
  public static final int NO_DEPTH_NO_STENCIL = 0;
51 89de975c leszek
/**
52
 * Create DEPTH, but not STENCIL
53
 */
54 23eecbd9 Leszek Koltunski
  public static final int DEPTH_NO_STENCIL    = 1;
55 89de975c leszek
/**
56
 * Create both DEPTH and STENCIL
57
 */
58 23eecbd9 Leszek Koltunski
  public static final int BOTH_DEPTH_STENCIL  = 2;
59 89de975c leszek
60 efe3d8fe leszek
  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 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
66
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
67
68 efe3d8fe leszek
  private class Job
69
    {
70
    int type;
71
    DistortedNode node;
72
73 ffbe7ecf Leszek Koltunski
    Job(int t, DistortedNode n)
74 efe3d8fe leszek
      {
75
      type = t;
76
      node = n;
77
      }
78
    }
79
80
  private ArrayList<Job> mJobs = new ArrayList<>();
81
82 70b6a155 Leszek Koltunski
  DistortedOutputSurface[] mBuffer;
83 60c1c622 leszek
84 95c441a2 leszek
  private long mTime;
85 c2c08950 leszek
  private float mFOV;
86
  float mDistance, mNear;
87 af4cc5db Leszek Koltunski
  float[] mProjectionMatrix;
88
89 89de975c leszek
  int mDepthStencilCreated;
90
  int mDepthStencil;
91
  int[] mDepthStencilH = new int[1];
92
  int[] mFBOH          = new int[1];
93 a436ccc5 leszek
94 a9f41fa3 leszek
  private float mClearR, mClearG, mClearB, mClearA;
95
  private float mClearDepth;
96 23eecbd9 Leszek Koltunski
  private int mClearStencil;
97 ad16ed3b Leszek Koltunski
  private int mClear;
98 78db8663 Leszek Koltunski
  float mMipmap;
99
100 86e99907 leszek
  private int mDebugLevel;
101
102 12f45260 Leszek Koltunski
  ////////////////////////////////////////////////////////////////////////////////
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 66ace7f5 Leszek Koltunski
  private static IntBuffer mIntBuffer;
109 12f45260 Leszek Koltunski
110
  static
111
    {
112 66ace7f5 Leszek Koltunski
    mSSBO[0]= -1;
113 12f45260 Leszek Koltunski
    }
114
115
  private int mSurfaceID;
116
  private int mLastIndex;
117
  // end section
118
  ////////////////////////////////////////////////////////////////////////////////
119
120 86e99907 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
  abstract void prepareDebug(long time);
123
  abstract void renderDebug(long time);
124 12f45260 Leszek Koltunski
  abstract void createSurface();
125
  abstract void deleteSurface();
126
  abstract void recreateSurface();
127 86e99907 leszek
128 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 9ed80185 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
131 af4cc5db Leszek Koltunski
    {
132 9ed80185 Leszek Koltunski
    super(width,height,createColor,numcolors,type);
133 af4cc5db Leszek Koltunski
134
    mProjectionMatrix = new float[16];
135
136
    mFOV = 60.0f;
137 54fe333a leszek
    mNear=  0.5f;
138 af4cc5db Leszek Koltunski
139 23eecbd9 Leszek Koltunski
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
140 89de975c leszek
    mDepthStencil = depthStencil;
141
142
    mFBOH[0]         = fbo;
143
    mDepthStencilH[0]= 0;
144 a436ccc5 leszek
145 95c441a2 leszek
    mTime = 0;
146 86e99907 leszek
    mDebugLevel = 0;
147 95c441a2 leszek
148 a9f41fa3 leszek
    mClearR = 0.0f;
149
    mClearG = 0.0f;
150
    mClearB = 0.0f;
151
    mClearA = 0.0f;
152
153
    mClearDepth = 1.0f;
154 23eecbd9 Leszek Koltunski
    mClearStencil = 0;
155 e6519ac8 Leszek Koltunski
    mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
156 a9f41fa3 leszek
157 70b6a155 Leszek Koltunski
    mBuffer = new DistortedOutputSurface[1+EffectQuality.LENGTH];
158 638b5b5c leszek
159 8426bd6a Leszek Koltunski
    mMipmap = 1.0f;
160 78db8663 Leszek Koltunski
161 af4cc5db Leszek Koltunski
    createProjection();
162
    }
163
164 12f45260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 66ace7f5 Leszek Koltunski
      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 2ab60f72 Leszek Koltunski
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
179 12f45260 Leszek Koltunski
      }
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 66ace7f5 Leszek Koltunski
    //mIntBuffer.put(BUFFERING*mSurfaceID+mLastIndex,0);
215 12f45260 Leszek Koltunski
    return BUFFERING*mSurfaceID + mLastIndex;
216
    }
217
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220
  int returnOldCounter()
221
    {
222 66ace7f5 Leszek Koltunski
    int ret = mIntBuffer.get(BUFFERING*mSurfaceID+mLastIndex);
223 12f45260 Leszek Koltunski
224
    mLastIndex++;
225
    if( mLastIndex>=BUFFERING ) mLastIndex-=BUFFERING;
226
227
    return ret;
228
    }
229
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232
  int returnCounter()
233
    {
234 66ace7f5 Leszek Koltunski
    return mIntBuffer.get(0);
235 12f45260 Leszek Koltunski
    }
236
237 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 be60d4ff leszek
  private void createProjection()
240 af4cc5db Leszek Koltunski
    {
241
    if( mWidth>0 && mHeight>1 )
242
      {
243
      if( mFOV>0.0f )  // perspective projection
244
        {
245 54fe333a leszek
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
246 8426bd6a Leszek Koltunski
        float q = mWidth*mNear;
247
        float c = mHeight*mNear;
248 54fe333a leszek
249 c2c08950 leszek
        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 54fe333a leszek
255 8426bd6a Leszek Koltunski
        mDistance    = mHeight/a;
256 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
257
258
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
259
        }
260
      else             // parallel projection
261
        {
262 8426bd6a Leszek Koltunski
        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 af4cc5db Leszek Koltunski
270
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
271
        }
272
      }
273
    }
274
275 1d6d261e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277 6b962e80 Leszek Koltunski
  private void createBuffers()
278 1d6d261e Leszek Koltunski
    {
279
    float mipmap=1.0f;
280
281
    for(int j=0; j<EffectQuality.LENGTH; j++)
282
      {
283 0f011027 Leszek Koltunski
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
284
      mBuffer[j].mMipmap = mipmap;
285 73208cab leszek
      mBuffer[j].mNear   = mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
286 7ad20cce Leszek Koltunski
      mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
287 1d6d261e Leszek Koltunski
      mipmap *= EffectQuality.MULTIPLIER;
288
      }
289
290 70b6a155 Leszek Koltunski
    mBuffer[EffectQuality.LENGTH] = this;
291
292 1d6d261e Leszek Koltunski
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
293
294 e6519ac8 Leszek Koltunski
    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 1d6d261e Leszek Koltunski
301
    for(int j=0; j<EffectQuality.LENGTH; j++)
302
      {
303 0f011027 Leszek Koltunski
      mBuffer[j].setAsOutput();
304 e6519ac8 Leszek Koltunski
      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 1d6d261e Leszek Koltunski
      }
309
    }
310
311 6b962e80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313 70b6a155 Leszek Koltunski
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
314 6b962e80 Leszek Koltunski
    {
315 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, mWidth, mHeight);
316 6b962e80 Leszek Koltunski
    setAsOutput(currTime);
317 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
322 e6519ac8 Leszek Koltunski
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
323
    GLES31.glStencilMask(0x00);
324 6b962e80 Leszek Koltunski
325
    DistortedEffects.blitDepthPriv(this);
326 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
331
    // clear buffers
332 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
339
    buffer.setAsOutput();
340 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
345
    return 1;
346
    }
347
348 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
349 e02264ff leszek
// 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 39086ebb leszek
353 8e28b6ff leszek
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
354 b2939df4 Leszek Koltunski
    {
355 7266d8ef Leszek Koltunski
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
356 3f44e745 Leszek Koltunski
    DistortedNode child1, child2;
357 70b6a155 Leszek Koltunski
    EffectQueuePostprocess lastQueue=null, currQueue;
358 915b7b2b leszek
    long lastBucket=0, currBucket;
359 0afc143a leszek
360 8e28b6ff leszek
    for(int i=0; i<numChildren; i++)
361 39086ebb leszek
      {
362 3f44e745 Leszek Koltunski
      child1 = children.get(i);
363 70b6a155 Leszek Koltunski
      currQueue = child1.getPostprocessQueue();
364
      currBucket= currQueue.getID();
365 b9798977 leszek
366 915b7b2b leszek
      if( currBucket==0 ) numRenders += child1.draw(time,this);
367 60c1c622 leszek
      else
368
        {
369 0f011027 Leszek Koltunski
        if( mBuffer[0]==null ) createBuffers();
370 c9a24bfb Leszek Koltunski
371 915b7b2b leszek
        if( lastBucket!=currBucket )
372 c9a24bfb Leszek Koltunski
          {
373 915b7b2b leszek
          if( lastBucket!=0 )
374 c9a24bfb Leszek Koltunski
            {
375
            for(int j=bucketChange; j<i; j++)
376
              {
377 3f44e745 Leszek Koltunski
              child2 = children.get(j);
378 70b6a155 Leszek Koltunski
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
379 c9a24bfb Leszek Koltunski
              }
380
381 70b6a155 Leszek Koltunski
            numRenders += lastQueue.postprocess(this);
382 bed13bea leszek
            numRenders += blitWithDepth(time, mBuffer[quality]);
383 c9a24bfb Leszek Koltunski
            }
384 660cd468 leszek
385 70b6a155 Leszek Koltunski
          internalQuality = currQueue.getInternalQuality();
386
          quality         = currQueue.getQuality();
387
          bucketChange    = i;
388 c9a24bfb Leszek Koltunski
          }
389
390 70b6a155 Leszek Koltunski
        child1.draw(time,mBuffer[quality]);
391
        //numRenders += currQueue.draw(child1,time,mBuffer);
392 c9a24bfb Leszek Koltunski
393 8e28b6ff leszek
        if( i==numChildren-1 )
394 cf7394cc leszek
          {
395 8e28b6ff leszek
          for(int j=bucketChange; j<numChildren; j++)
396 c9a24bfb Leszek Koltunski
            {
397 3f44e745 Leszek Koltunski
            child2 = children.get(j);
398 70b6a155 Leszek Koltunski
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
399 c9a24bfb Leszek Koltunski
            }
400
401 70b6a155 Leszek Koltunski
          numRenders += currQueue.postprocess(this);
402 7266d8ef Leszek Koltunski
          numRenders += blitWithDepth(time, mBuffer[quality]);
403 cf7394cc leszek
          }
404 60c1c622 leszek
        }
405 b9798977 leszek
406 70b6a155 Leszek Koltunski
      lastQueue = currQueue;
407
      lastBucket= currBucket;
408 95c441a2 leszek
      }
409 270c27bc Leszek Koltunski
410 39086ebb leszek
    return numRenders;
411
    }
412
413 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415 26a4e5f6 leszek
  ArrayList<DistortedNode> getChildren()
416 be60d4ff leszek
    {
417 26a4e5f6 leszek
    return mChildren;
418 be60d4ff leszek
    }
419
420 b2939df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
421
// PUBLIC API
422 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
423 86e99907 leszek
/**
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 26a4e5f6 leszek
    if( this instanceof DistortedScreen )
433
      mDebugLevel = bitmask;
434 86e99907 leszek
    }
435
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
438 c5369f1b leszek
/**
439 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
440 af4cc5db Leszek Koltunski
 * <p>
441
 * Must be called from a thread holding OpenGL Context.
442
 *
443 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
444 7691a39f leszek
 * @return Number of objects rendered.
445 c5369f1b leszek
 */
446 b2939df4 Leszek Koltunski
  public int render(long time)
447 af4cc5db Leszek Koltunski
    {
448 26a4e5f6 leszek
    if( mDebugLevel!=0 ) prepareDebug(time);
449 86e99907 leszek
450 c204c69d leszek
    // change tree topology (attach and detach children)
451 889cce10 Leszek Koltunski
/*
452 42571056 Leszek Koltunski
    boolean changed1 =
453 889cce10 Leszek Koltunski
*/
454 efe3d8fe leszek
    DistortedMaster.toDo();
455 eadf0859 leszek
/*
456 42571056 Leszek Koltunski
    if( changed1 )
457 c204c69d leszek
      {
458
      for(int i=0; i<mNumChildren; i++)
459
        {
460 af27df87 leszek
        mChildren.get(i).debug(0);
461 c204c69d leszek
        }
462 af27df87 leszek
463 7691a39f leszek
      DistortedNode.debugMap();
464 c204c69d leszek
      }
465 eadf0859 leszek
*/
466 09ab7524 Leszek Koltunski
    // 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 42571056 Leszek Koltunski
/*
472
    boolean changed2 =
473
*/
474 f8377ef8 leszek
    toDo();
475 eadf0859 leszek
/*
476 42571056 Leszek Koltunski
    if( changed2 )
477 af27df87 leszek
      {
478 226144d0 leszek
      DistortedObject.debugLists();
479 42571056 Leszek Koltunski
      }
480 eadf0859 leszek
*/
481 c834348d leszek
    // mark OpenGL state as unknown
482
    DistortedRenderState.reset();
483 2ed1c692 leszek
484 b2939df4 Leszek Koltunski
    int numRenders=0;
485 7691a39f leszek
486 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
487 af4cc5db Leszek Koltunski
      {
488 b2939df4 Leszek Koltunski
      numRenders += mChildren.get(i).renderRecursive(time);
489 af4cc5db Leszek Koltunski
      }
490 7691a39f leszek
491 070695a5 Leszek Koltunski
    setAsOutput(time);
492 b2939df4 Leszek Koltunski
    numRenders += renderChildren(time,mNumChildren,mChildren);
493
494 26a4e5f6 leszek
    if( mDebugLevel != 0 ) renderDebug(time);
495 b28c6c21 leszek
496 7691a39f leszek
    return numRenders;
497 af4cc5db Leszek Koltunski
    }
498
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500 c5369f1b leszek
/**
501
 * Bind this Surface as a Framebuffer we can render to.
502 02ab6f9d leszek
 *
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 c5369f1b leszek
 */
508 95c441a2 leszek
  public void setAsOutput(long time)
509 a436ccc5 leszek
    {
510 e6519ac8 Leszek Koltunski
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
511 95c441a2 leszek
512
    if( mTime!=time )
513
      {
514
      mTime = time;
515 23eecbd9 Leszek Koltunski
      DistortedRenderState.colorDepthStencilOn();
516 e6519ac8 Leszek Koltunski
      GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
517
      GLES31.glClearDepthf(mClearDepth);
518
      GLES31.glClearStencil(mClearStencil);
519
      GLES31.glClear(mClear);
520 144d252c Leszek Koltunski
      DistortedRenderState.colorDepthStencilRestore();
521 95c441a2 leszek
      }
522 a436ccc5 leszek
    }
523 af4cc5db Leszek Koltunski
524 1dfc9074 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e6519ac8 Leszek Koltunski
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
534 1dfc9074 leszek
    }
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 638b5b5c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8426bd6a Leszek Koltunski
 * <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 638b5b5c leszek
 *
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 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
572
/**
573 e6519ac8 Leszek Koltunski
 * Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
574 ad16ed3b Leszek Koltunski
 * this Surface at the beginning of each frame.
575 a9f41fa3 leszek
 *
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 23eecbd9 Leszek Koltunski
 * 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 a9f41fa3 leszek
 *
594
 * @param d the Depth. Default: 1.0f
595
 */
596
  public void glClearDepthf(float d)
597
    {
598
    mClearDepth = d;
599
    }
600
601 23eecbd9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
629
/**
630
 * Create new Projection matrix.
631
 *
632
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
633 54fe333a leszek
 *            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 af4cc5db Leszek Koltunski
 */
638 54fe333a leszek
  public void setProjection(float fov, float near)
639 af4cc5db Leszek Koltunski
    {
640 8069e806 Leszek Koltunski
    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 af4cc5db Leszek Koltunski
658 1dfc9074 leszek
    if( mBuffer[0]!=null )
659
      {
660
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
661
      }
662
663 af4cc5db Leszek Koltunski
    createProjection();
664
    }
665
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667 c5369f1b leszek
/**
668 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
669 c7da4e65 leszek
 * <p>
670
 * This method can be safely called mid-render as it doesn't interfere with rendering.
671 af4cc5db Leszek Koltunski
 *
672
 * @param width The new width.
673
 * @param height The new height.
674 c5369f1b leszek
 */
675 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
676
    {
677
    if( mWidth!=width || mHeight!=height )
678
      {
679
      mWidth = width;
680
      mHeight= height;
681 f8377ef8 leszek
682
      createProjection();
683
684 c7da4e65 leszek
      if( mColorCreated==CREATED )
685 f8377ef8 leszek
        {
686 f28fffc2 Leszek Koltunski
        markForCreation();
687 f8377ef8 leszek
        recreate();
688
        }
689 af4cc5db Leszek Koltunski
      }
690
    }
691 a09ada4c Leszek Koltunski
692 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
693
/**
694 89de975c leszek
 * Return true if the Surface contains a DEPTH attachment.
695 a436ccc5 leszek
 *
696 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
697 a436ccc5 leszek
 */
698 89de975c leszek
  public boolean hasDepth()
699 a436ccc5 leszek
    {
700 89de975c leszek
    return mDepthStencilCreated==CREATED;
701 a436ccc5 leszek
    }
702
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704
/**
705 89de975c leszek
 * Return true if the Surface contains a STENCIL attachment.
706 a436ccc5 leszek
 *
707 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
708 a436ccc5 leszek
 */
709 89de975c leszek
  public boolean hasStencil()
710 a436ccc5 leszek
    {
711 89de975c leszek
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
712 a436ccc5 leszek
    }
713
714 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
715
/**
716
 * Adds a new child to the last position in the list of our Surface's children.
717 c204c69d leszek
 * <p>
718
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
719 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
720 a09ada4c Leszek Koltunski
 *
721
 * @param node The new Node to add.
722
 */
723 c204c69d leszek
  public void attach(DistortedNode node)
724 a09ada4c Leszek Koltunski
    {
725 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
726 efe3d8fe leszek
    DistortedMaster.newSlave(this);
727 a09ada4c Leszek Koltunski
    }
728
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730
/**
731
 * Adds a new child to the last position in the list of our Surface's children.
732 c204c69d leszek
 * <p>
733
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
734 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
735 a09ada4c Leszek Koltunski
 *
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 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
742 a09ada4c Leszek Koltunski
    {
743
    DistortedNode node = new DistortedNode(surface,effects,mesh);
744 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
745 efe3d8fe leszek
    DistortedMaster.newSlave(this);
746 c204c69d leszek
    return node;
747
    }
748
749 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
758 af27df87 leszek
 *
759
 * @param effects DistortedEffects to remove.
760
 */
761
  public void detach(DistortedEffects effects)
762
    {
763
    long id = effects.getID();
764
    DistortedNode node;
765 8baa1fe6 Leszek Koltunski
    boolean detached = false;
766 af27df87 leszek
767
    for(int i=0; i<mNumChildren; i++)
768
      {
769
      node = mChildren.get(i);
770
771
      if( node.getEffects().getID()==id )
772
        {
773 8baa1fe6 Leszek Koltunski
        detached = true;
774 ffbe7ecf Leszek Koltunski
        mJobs.add(new Job(DETACH,node));
775 efe3d8fe leszek
        DistortedMaster.newSlave(this);
776 af27df87 leszek
        break;
777
        }
778
      }
779 8baa1fe6 Leszek Koltunski
780
    if( !detached )
781
      {
782
      // if we failed to detach any, it still might be the case that
783 efe3d8fe leszek
      // 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 8baa1fe6 Leszek Koltunski
      }
798 af27df87 leszek
    }
799
800 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
801
/**
802
 * Removes the first occurrence of a specified child from the list of children of our Surface.
803 c204c69d leszek
 * <p>
804
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
805 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
806 c204c69d leszek
 *
807
 * @param node The Node to remove.
808
 */
809
  public void detach(DistortedNode node)
810
    {
811 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETACH,node));
812 efe3d8fe leszek
    DistortedMaster.newSlave(this);
813 a09ada4c Leszek Koltunski
    }
814
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816
/**
817
 * Removes all children Nodes.
818 c204c69d leszek
 * <p>
819
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
820 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
821 c204c69d leszek
 */
822
  public void detachAll()
823
    {
824 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETALL,null));
825 efe3d8fe leszek
    DistortedMaster.newSlave(this);
826 c204c69d leszek
    }
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 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
832 c204c69d leszek
 * Java has no multiple inheritance.
833 43fbf0dd leszek
 *
834
 * @y.exclude
835 a09ada4c Leszek Koltunski
 */
836 efe3d8fe leszek
  public void doWork()
837 a09ada4c Leszek Koltunski
    {
838 efe3d8fe leszek
    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 be60d4ff leszek
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
848
                     job.node.setSurfaceParent(this);
849
                     DistortedMaster.addSorted(mChildren,job.node);
850 efe3d8fe leszek
                     mNumChildren++;
851
                     break;
852 be60d4ff leszek
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
853 efe3d8fe leszek
                       {
854 be60d4ff leszek
                       job.node.setSurfaceParent(null);
855 efe3d8fe leszek
                       mNumChildren--;
856
                       }
857
                     break;
858 be60d4ff leszek
        case DETALL: if( mNumChildren>0 )
859 efe3d8fe leszek
                       {
860 be60d4ff leszek
                       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 efe3d8fe leszek
                       mNumChildren = 0;
869
                       }
870
                     break;
871 ffbe7ecf Leszek Koltunski
        case SORT  : mChildren.remove(job.node);
872 be60d4ff leszek
                     DistortedMaster.addSorted(mChildren,job.node);
873 efe3d8fe leszek
                     break;
874
        }
875
      }
876 a09ada4c Leszek Koltunski
    }
877 af4cc5db Leszek Koltunski
}