Project

General

Profile

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

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

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 8a57da61 Leszek Koltunski
  private int mLastValue =-1;
118 12f45260 Leszek Koltunski
  // end section
119
  ////////////////////////////////////////////////////////////////////////////////
120
121 86e99907 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123
  abstract void prepareDebug(long time);
124
  abstract void renderDebug(long time);
125 12f45260 Leszek Koltunski
  abstract void createSurface();
126
  abstract void deleteSurface();
127
  abstract void recreateSurface();
128 86e99907 leszek
129 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 9ed80185 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
132 af4cc5db Leszek Koltunski
    {
133 9ed80185 Leszek Koltunski
    super(width,height,createColor,numcolors,type);
134 af4cc5db Leszek Koltunski
135
    mProjectionMatrix = new float[16];
136
137
    mFOV = 60.0f;
138 54fe333a leszek
    mNear=  0.5f;
139 af4cc5db Leszek Koltunski
140 23eecbd9 Leszek Koltunski
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
141 89de975c leszek
    mDepthStencil = depthStencil;
142
143
    mFBOH[0]         = fbo;
144
    mDepthStencilH[0]= 0;
145 a436ccc5 leszek
146 95c441a2 leszek
    mTime = 0;
147 86e99907 leszek
    mDebugLevel = 0;
148 95c441a2 leszek
149 a9f41fa3 leszek
    mClearR = 0.0f;
150
    mClearG = 0.0f;
151
    mClearB = 0.0f;
152
    mClearA = 0.0f;
153
154
    mClearDepth = 1.0f;
155 23eecbd9 Leszek Koltunski
    mClearStencil = 0;
156 e6519ac8 Leszek Koltunski
    mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
157 a9f41fa3 leszek
158 70b6a155 Leszek Koltunski
    mBuffer = new DistortedOutputSurface[1+EffectQuality.LENGTH];
159 638b5b5c leszek
160 8426bd6a Leszek Koltunski
    mMipmap = 1.0f;
161 78db8663 Leszek Koltunski
162 af4cc5db Leszek Koltunski
    createProjection();
163
    }
164
165 12f45260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 66ace7f5 Leszek Koltunski
      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 2ab60f72 Leszek Koltunski
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
180 12f45260 Leszek Koltunski
      }
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 27cd6b98 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213 8a57da61 Leszek Koltunski
  int getNewCounter()
214 27cd6b98 Leszek Koltunski
    {
215 8a57da61 Leszek Koltunski
    int value = mIntBuffer.get(BUFFERING*mSurfaceID+mLastIndex);
216 12f45260 Leszek Koltunski
217 8a57da61 Leszek Koltunski
    if( value!=mLastValue )
218
      {
219
      mLastValue = value;
220
      android.util.Log.d("surface", "surface id: "+mSurfaceID+" value now: "+mLastValue);
221
      }
222 12f45260 Leszek Koltunski
223
    mLastIndex++;
224
    if( mLastIndex>=BUFFERING ) mLastIndex-=BUFFERING;
225
226 8a57da61 Leszek Koltunski
    mIntBuffer.put(BUFFERING*mSurfaceID+mLastIndex,0);
227
    return BUFFERING*mSurfaceID + mLastIndex;
228 12f45260 Leszek Koltunski
    }
229
230 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232 be60d4ff leszek
  private void createProjection()
233 af4cc5db Leszek Koltunski
    {
234
    if( mWidth>0 && mHeight>1 )
235
      {
236
      if( mFOV>0.0f )  // perspective projection
237
        {
238 54fe333a leszek
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
239 8426bd6a Leszek Koltunski
        float q = mWidth*mNear;
240
        float c = mHeight*mNear;
241 54fe333a leszek
242 c2c08950 leszek
        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 54fe333a leszek
248 8426bd6a Leszek Koltunski
        mDistance    = mHeight/a;
249 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
250
251
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
252
        }
253
      else             // parallel projection
254
        {
255 8426bd6a Leszek Koltunski
        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 af4cc5db Leszek Koltunski
263
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
264
        }
265
      }
266
    }
267
268 1d6d261e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270 6b962e80 Leszek Koltunski
  private void createBuffers()
271 1d6d261e Leszek Koltunski
    {
272
    float mipmap=1.0f;
273
274
    for(int j=0; j<EffectQuality.LENGTH; j++)
275
      {
276 0f011027 Leszek Koltunski
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
277
      mBuffer[j].mMipmap = mipmap;
278 73208cab leszek
      mBuffer[j].mNear   = mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
279 7ad20cce Leszek Koltunski
      mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
280 1d6d261e Leszek Koltunski
      mipmap *= EffectQuality.MULTIPLIER;
281
      }
282
283 70b6a155 Leszek Koltunski
    mBuffer[EffectQuality.LENGTH] = this;
284
285 1d6d261e Leszek Koltunski
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
286
287 e6519ac8 Leszek Koltunski
    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 1d6d261e Leszek Koltunski
294
    for(int j=0; j<EffectQuality.LENGTH; j++)
295
      {
296 0f011027 Leszek Koltunski
      mBuffer[j].setAsOutput();
297 e6519ac8 Leszek Koltunski
      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 1d6d261e Leszek Koltunski
      }
302
    }
303
304 6b962e80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306 70b6a155 Leszek Koltunski
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
307 6b962e80 Leszek Koltunski
    {
308 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, mWidth, mHeight);
309 6b962e80 Leszek Koltunski
    setAsOutput(currTime);
310 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
315 e6519ac8 Leszek Koltunski
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
316
    GLES31.glStencilMask(0x00);
317 6b962e80 Leszek Koltunski
318
    DistortedEffects.blitDepthPriv(this);
319 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
324
    // clear buffers
325 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
332
    buffer.setAsOutput();
333 e6519ac8 Leszek Koltunski
    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 6b962e80 Leszek Koltunski
338
    return 1;
339
    }
340
341 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
342 e02264ff leszek
// 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 39086ebb leszek
346 8e28b6ff leszek
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
347 b2939df4 Leszek Koltunski
    {
348 7266d8ef Leszek Koltunski
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
349 3f44e745 Leszek Koltunski
    DistortedNode child1, child2;
350 70b6a155 Leszek Koltunski
    EffectQueuePostprocess lastQueue=null, currQueue;
351 915b7b2b leszek
    long lastBucket=0, currBucket;
352 0afc143a leszek
353 8e28b6ff leszek
    for(int i=0; i<numChildren; i++)
354 39086ebb leszek
      {
355 3f44e745 Leszek Koltunski
      child1 = children.get(i);
356 70b6a155 Leszek Koltunski
      currQueue = child1.getPostprocessQueue();
357
      currBucket= currQueue.getID();
358 b9798977 leszek
359 915b7b2b leszek
      if( currBucket==0 ) numRenders += child1.draw(time,this);
360 60c1c622 leszek
      else
361
        {
362 0f011027 Leszek Koltunski
        if( mBuffer[0]==null ) createBuffers();
363 c9a24bfb Leszek Koltunski
364 915b7b2b leszek
        if( lastBucket!=currBucket )
365 c9a24bfb Leszek Koltunski
          {
366 915b7b2b leszek
          if( lastBucket!=0 )
367 c9a24bfb Leszek Koltunski
            {
368
            for(int j=bucketChange; j<i; j++)
369
              {
370 3f44e745 Leszek Koltunski
              child2 = children.get(j);
371 70b6a155 Leszek Koltunski
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
372 c9a24bfb Leszek Koltunski
              }
373
374 70b6a155 Leszek Koltunski
            numRenders += lastQueue.postprocess(this);
375 bed13bea leszek
            numRenders += blitWithDepth(time, mBuffer[quality]);
376 c9a24bfb Leszek Koltunski
            }
377 660cd468 leszek
378 70b6a155 Leszek Koltunski
          internalQuality = currQueue.getInternalQuality();
379
          quality         = currQueue.getQuality();
380
          bucketChange    = i;
381 c9a24bfb Leszek Koltunski
          }
382
383 70b6a155 Leszek Koltunski
        child1.draw(time,mBuffer[quality]);
384
        //numRenders += currQueue.draw(child1,time,mBuffer);
385 c9a24bfb Leszek Koltunski
386 8e28b6ff leszek
        if( i==numChildren-1 )
387 cf7394cc leszek
          {
388 8e28b6ff leszek
          for(int j=bucketChange; j<numChildren; j++)
389 c9a24bfb Leszek Koltunski
            {
390 3f44e745 Leszek Koltunski
            child2 = children.get(j);
391 70b6a155 Leszek Koltunski
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
392 c9a24bfb Leszek Koltunski
            }
393
394 70b6a155 Leszek Koltunski
          numRenders += currQueue.postprocess(this);
395 7266d8ef Leszek Koltunski
          numRenders += blitWithDepth(time, mBuffer[quality]);
396 cf7394cc leszek
          }
397 60c1c622 leszek
        }
398 b9798977 leszek
399 70b6a155 Leszek Koltunski
      lastQueue = currQueue;
400
      lastBucket= currBucket;
401 95c441a2 leszek
      }
402 270c27bc Leszek Koltunski
403 39086ebb leszek
    return numRenders;
404
    }
405
406 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408 26a4e5f6 leszek
  ArrayList<DistortedNode> getChildren()
409 be60d4ff leszek
    {
410 26a4e5f6 leszek
    return mChildren;
411 be60d4ff leszek
    }
412
413 b2939df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
414
// PUBLIC API
415 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
416 86e99907 leszek
/**
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 26a4e5f6 leszek
    if( this instanceof DistortedScreen )
426
      mDebugLevel = bitmask;
427 86e99907 leszek
    }
428
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430
431 c5369f1b leszek
/**
432 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
433 af4cc5db Leszek Koltunski
 * <p>
434
 * Must be called from a thread holding OpenGL Context.
435
 *
436 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
437 7691a39f leszek
 * @return Number of objects rendered.
438 c5369f1b leszek
 */
439 b2939df4 Leszek Koltunski
  public int render(long time)
440 af4cc5db Leszek Koltunski
    {
441 26a4e5f6 leszek
    if( mDebugLevel!=0 ) prepareDebug(time);
442 86e99907 leszek
443 c204c69d leszek
    // change tree topology (attach and detach children)
444 889cce10 Leszek Koltunski
/*
445 42571056 Leszek Koltunski
    boolean changed1 =
446 889cce10 Leszek Koltunski
*/
447 efe3d8fe leszek
    DistortedMaster.toDo();
448 eadf0859 leszek
/*
449 42571056 Leszek Koltunski
    if( changed1 )
450 c204c69d leszek
      {
451
      for(int i=0; i<mNumChildren; i++)
452
        {
453 af27df87 leszek
        mChildren.get(i).debug(0);
454 c204c69d leszek
        }
455 af27df87 leszek
456 7691a39f leszek
      DistortedNode.debugMap();
457 c204c69d leszek
      }
458 eadf0859 leszek
*/
459 09ab7524 Leszek Koltunski
    // 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 42571056 Leszek Koltunski
/*
465
    boolean changed2 =
466
*/
467 f8377ef8 leszek
    toDo();
468 eadf0859 leszek
/*
469 42571056 Leszek Koltunski
    if( changed2 )
470 af27df87 leszek
      {
471 226144d0 leszek
      DistortedObject.debugLists();
472 42571056 Leszek Koltunski
      }
473 eadf0859 leszek
*/
474 c834348d leszek
    // mark OpenGL state as unknown
475
    DistortedRenderState.reset();
476 2ed1c692 leszek
477 b2939df4 Leszek Koltunski
    int numRenders=0;
478 7691a39f leszek
479 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
480 af4cc5db Leszek Koltunski
      {
481 b2939df4 Leszek Koltunski
      numRenders += mChildren.get(i).renderRecursive(time);
482 af4cc5db Leszek Koltunski
      }
483 7691a39f leszek
484 070695a5 Leszek Koltunski
    setAsOutput(time);
485 b2939df4 Leszek Koltunski
    numRenders += renderChildren(time,mNumChildren,mChildren);
486
487 26a4e5f6 leszek
    if( mDebugLevel != 0 ) renderDebug(time);
488 b28c6c21 leszek
489 7691a39f leszek
    return numRenders;
490 af4cc5db Leszek Koltunski
    }
491
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493 c5369f1b leszek
/**
494
 * Bind this Surface as a Framebuffer we can render to.
495 02ab6f9d leszek
 *
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 c5369f1b leszek
 */
501 95c441a2 leszek
  public void setAsOutput(long time)
502 a436ccc5 leszek
    {
503 e6519ac8 Leszek Koltunski
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
504 95c441a2 leszek
505
    if( mTime!=time )
506
      {
507
      mTime = time;
508 23eecbd9 Leszek Koltunski
      DistortedRenderState.colorDepthStencilOn();
509 e6519ac8 Leszek Koltunski
      GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
510
      GLES31.glClearDepthf(mClearDepth);
511
      GLES31.glClearStencil(mClearStencil);
512
      GLES31.glClear(mClear);
513 144d252c Leszek Koltunski
      DistortedRenderState.colorDepthStencilRestore();
514 95c441a2 leszek
      }
515 a436ccc5 leszek
    }
516 af4cc5db Leszek Koltunski
517 1dfc9074 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e6519ac8 Leszek Koltunski
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
527 1dfc9074 leszek
    }
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 638b5b5c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8426bd6a Leszek Koltunski
 * <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 638b5b5c leszek
 *
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 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
565
/**
566 e6519ac8 Leszek Koltunski
 * Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
567 ad16ed3b Leszek Koltunski
 * this Surface at the beginning of each frame.
568 a9f41fa3 leszek
 *
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 23eecbd9 Leszek Koltunski
 * 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 a9f41fa3 leszek
 *
587
 * @param d the Depth. Default: 1.0f
588
 */
589
  public void glClearDepthf(float d)
590
    {
591
    mClearDepth = d;
592
    }
593
594 23eecbd9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
622
/**
623
 * Create new Projection matrix.
624
 *
625
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
626 54fe333a leszek
 *            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 af4cc5db Leszek Koltunski
 */
631 54fe333a leszek
  public void setProjection(float fov, float near)
632 af4cc5db Leszek Koltunski
    {
633 8069e806 Leszek Koltunski
    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 af4cc5db Leszek Koltunski
651 1dfc9074 leszek
    if( mBuffer[0]!=null )
652
      {
653
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
654
      }
655
656 af4cc5db Leszek Koltunski
    createProjection();
657
    }
658
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660 c5369f1b leszek
/**
661 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
662 c7da4e65 leszek
 * <p>
663
 * This method can be safely called mid-render as it doesn't interfere with rendering.
664 af4cc5db Leszek Koltunski
 *
665
 * @param width The new width.
666
 * @param height The new height.
667 c5369f1b leszek
 */
668 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
669
    {
670
    if( mWidth!=width || mHeight!=height )
671
      {
672
      mWidth = width;
673
      mHeight= height;
674 f8377ef8 leszek
675
      createProjection();
676
677 c7da4e65 leszek
      if( mColorCreated==CREATED )
678 f8377ef8 leszek
        {
679 f28fffc2 Leszek Koltunski
        markForCreation();
680 f8377ef8 leszek
        recreate();
681
        }
682 af4cc5db Leszek Koltunski
      }
683
    }
684 a09ada4c Leszek Koltunski
685 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
686
/**
687 89de975c leszek
 * Return true if the Surface contains a DEPTH attachment.
688 a436ccc5 leszek
 *
689 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
690 a436ccc5 leszek
 */
691 89de975c leszek
  public boolean hasDepth()
692 a436ccc5 leszek
    {
693 89de975c leszek
    return mDepthStencilCreated==CREATED;
694 a436ccc5 leszek
    }
695
696
///////////////////////////////////////////////////////////////////////////////////////////////////
697
/**
698 89de975c leszek
 * Return true if the Surface contains a STENCIL attachment.
699 a436ccc5 leszek
 *
700 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
701 a436ccc5 leszek
 */
702 89de975c leszek
  public boolean hasStencil()
703 a436ccc5 leszek
    {
704 89de975c leszek
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
705 a436ccc5 leszek
    }
706
707 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
708
/**
709
 * Adds a new child to the last position in the list of our Surface's children.
710 c204c69d leszek
 * <p>
711
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
712 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
713 a09ada4c Leszek Koltunski
 *
714
 * @param node The new Node to add.
715
 */
716 c204c69d leszek
  public void attach(DistortedNode node)
717 a09ada4c Leszek Koltunski
    {
718 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
719 efe3d8fe leszek
    DistortedMaster.newSlave(this);
720 a09ada4c Leszek Koltunski
    }
721
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723
/**
724
 * Adds a new child to the last position in the list of our Surface's children.
725 c204c69d leszek
 * <p>
726
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
727 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
728 a09ada4c Leszek Koltunski
 *
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 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
735 a09ada4c Leszek Koltunski
    {
736
    DistortedNode node = new DistortedNode(surface,effects,mesh);
737 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
738 efe3d8fe leszek
    DistortedMaster.newSlave(this);
739 c204c69d leszek
    return node;
740
    }
741
742 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
751 af27df87 leszek
 *
752
 * @param effects DistortedEffects to remove.
753
 */
754
  public void detach(DistortedEffects effects)
755
    {
756
    long id = effects.getID();
757
    DistortedNode node;
758 8baa1fe6 Leszek Koltunski
    boolean detached = false;
759 af27df87 leszek
760
    for(int i=0; i<mNumChildren; i++)
761
      {
762
      node = mChildren.get(i);
763
764
      if( node.getEffects().getID()==id )
765
        {
766 8baa1fe6 Leszek Koltunski
        detached = true;
767 ffbe7ecf Leszek Koltunski
        mJobs.add(new Job(DETACH,node));
768 efe3d8fe leszek
        DistortedMaster.newSlave(this);
769 af27df87 leszek
        break;
770
        }
771
      }
772 8baa1fe6 Leszek Koltunski
773
    if( !detached )
774
      {
775
      // if we failed to detach any, it still might be the case that
776 efe3d8fe leszek
      // 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 8baa1fe6 Leszek Koltunski
      }
791 af27df87 leszek
    }
792
793 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
794
/**
795
 * Removes the first occurrence of a specified child from the list of children of our Surface.
796 c204c69d leszek
 * <p>
797
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
798 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
799 c204c69d leszek
 *
800
 * @param node The Node to remove.
801
 */
802
  public void detach(DistortedNode node)
803
    {
804 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETACH,node));
805 efe3d8fe leszek
    DistortedMaster.newSlave(this);
806 a09ada4c Leszek Koltunski
    }
807
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810
 * Removes all children Nodes.
811 c204c69d leszek
 * <p>
812
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
813 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
814 c204c69d leszek
 */
815
  public void detachAll()
816
    {
817 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETALL,null));
818 efe3d8fe leszek
    DistortedMaster.newSlave(this);
819 c204c69d leszek
    }
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 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
825 c204c69d leszek
 * Java has no multiple inheritance.
826 43fbf0dd leszek
 *
827
 * @y.exclude
828 a09ada4c Leszek Koltunski
 */
829 efe3d8fe leszek
  public void doWork()
830 a09ada4c Leszek Koltunski
    {
831 efe3d8fe leszek
    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 be60d4ff leszek
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
841
                     job.node.setSurfaceParent(this);
842
                     DistortedMaster.addSorted(mChildren,job.node);
843 efe3d8fe leszek
                     mNumChildren++;
844
                     break;
845 be60d4ff leszek
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
846 efe3d8fe leszek
                       {
847 be60d4ff leszek
                       job.node.setSurfaceParent(null);
848 efe3d8fe leszek
                       mNumChildren--;
849
                       }
850
                     break;
851 be60d4ff leszek
        case DETALL: if( mNumChildren>0 )
852 efe3d8fe leszek
                       {
853 be60d4ff leszek
                       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 efe3d8fe leszek
                       mNumChildren = 0;
862
                       }
863
                     break;
864 ffbe7ecf Leszek Koltunski
        case SORT  : mChildren.remove(job.node);
865 be60d4ff leszek
                     DistortedMaster.addSorted(mChildren,job.node);
866 efe3d8fe leszek
                     break;
867
        }
868
      }
869 a09ada4c Leszek Koltunski
    }
870 af4cc5db Leszek Koltunski
}