Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ a1003b17

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 a436ccc5 leszek
import android.opengl.GLES30;
23 af4cc5db Leszek Koltunski
import android.opengl.Matrix;
24 86e99907 leszek
25 3521c6fe leszek
import org.distorted.library.effect.EffectQuality;
26
27 a09ada4c Leszek Koltunski
import java.util.ArrayList;
28 af4cc5db Leszek Koltunski
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30 70b6a155 Leszek Koltunski
/**
31
 * This is not really part of the public API.
32
 *
33
 * @y.exclude
34
 */
35
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
36 af4cc5db Leszek Koltunski
{
37 86e99907 leszek
//////////// DEBUG FLAGS /////////////////////////////////////////////
38
/**
39
 * When rendering a Screen, show FPS in the upper-left corner?
40
 */
41
public static final int DEBUG_FPS = 1;
42
//////////// END DEBUG FLAGS /////////////////////////////////////////
43
44 89de975c leszek
/**
45
 * Do not create DEPTH or STENCIL attachment
46
 */
47 23eecbd9 Leszek Koltunski
  public static final int NO_DEPTH_NO_STENCIL = 0;
48 89de975c leszek
/**
49
 * Create DEPTH, but not STENCIL
50
 */
51 23eecbd9 Leszek Koltunski
  public static final int DEPTH_NO_STENCIL    = 1;
52 89de975c leszek
/**
53
 * Create both DEPTH and STENCIL
54
 */
55 23eecbd9 Leszek Koltunski
  public static final int BOTH_DEPTH_STENCIL  = 2;
56 89de975c leszek
57 efe3d8fe leszek
  private static final int ATTACH = 0;
58
  private static final int DETACH = 1;
59
  private static final int DETALL = 2;
60
  private static final int SORT   = 3;
61
62 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
63
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
64
65 efe3d8fe leszek
  private class Job
66
    {
67
    int type;
68
    DistortedNode node;
69
70 ffbe7ecf Leszek Koltunski
    Job(int t, DistortedNode n)
71 efe3d8fe leszek
      {
72
      type = t;
73
      node = n;
74
      }
75
    }
76
77
  private ArrayList<Job> mJobs = new ArrayList<>();
78
79 70b6a155 Leszek Koltunski
  DistortedOutputSurface[] mBuffer;
80 60c1c622 leszek
81 95c441a2 leszek
  private long mTime;
82 c2c08950 leszek
  private float mFOV;
83
  float mDistance, mNear;
84 af4cc5db Leszek Koltunski
  float[] mProjectionMatrix;
85
86 89de975c leszek
  int mDepthStencilCreated;
87
  int mDepthStencil;
88
  int[] mDepthStencilH = new int[1];
89
  int[] mFBOH          = new int[1];
90 a436ccc5 leszek
91 a9f41fa3 leszek
  private float mClearR, mClearG, mClearB, mClearA;
92
  private float mClearDepth;
93 23eecbd9 Leszek Koltunski
  private int mClearStencil;
94 ad16ed3b Leszek Koltunski
  private int mClear;
95 78db8663 Leszek Koltunski
  float mMipmap;
96
97 86e99907 leszek
  private int mDebugLevel;
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
  abstract void prepareDebug(long time);
102
  abstract void renderDebug(long time);
103
104 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106 9ed80185 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
107 af4cc5db Leszek Koltunski
    {
108 9ed80185 Leszek Koltunski
    super(width,height,createColor,numcolors,type);
109 af4cc5db Leszek Koltunski
110
    mProjectionMatrix = new float[16];
111
112
    mFOV = 60.0f;
113 54fe333a leszek
    mNear=  0.5f;
114 af4cc5db Leszek Koltunski
115 23eecbd9 Leszek Koltunski
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
116 89de975c leszek
    mDepthStencil = depthStencil;
117
118
    mFBOH[0]         = fbo;
119
    mDepthStencilH[0]= 0;
120 a436ccc5 leszek
121 95c441a2 leszek
    mTime = 0;
122 86e99907 leszek
    mDebugLevel = 0;
123 95c441a2 leszek
124 a9f41fa3 leszek
    mClearR = 0.0f;
125
    mClearG = 0.0f;
126
    mClearB = 0.0f;
127
    mClearA = 0.0f;
128
129
    mClearDepth = 1.0f;
130 23eecbd9 Leszek Koltunski
    mClearStencil = 0;
131 ad16ed3b Leszek Koltunski
    mClear = GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT;
132 a9f41fa3 leszek
133 70b6a155 Leszek Koltunski
    mBuffer = new DistortedOutputSurface[1+EffectQuality.LENGTH];
134 638b5b5c leszek
135 8426bd6a Leszek Koltunski
    mMipmap = 1.0f;
136 78db8663 Leszek Koltunski
137 af4cc5db Leszek Koltunski
    createProjection();
138
    }
139
140 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 be60d4ff leszek
  private void createProjection()
143 af4cc5db Leszek Koltunski
    {
144
    if( mWidth>0 && mHeight>1 )
145
      {
146
      if( mFOV>0.0f )  // perspective projection
147
        {
148 54fe333a leszek
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
149 8426bd6a Leszek Koltunski
        float q = mWidth*mNear;
150
        float c = mHeight*mNear;
151 54fe333a leszek
152 c2c08950 leszek
        float left   = -q/2;
153
        float right  = +q/2;
154
        float bottom = -c/2;
155
        float top    = +c/2;
156
        float near   =  c/a;
157 54fe333a leszek
158 8426bd6a Leszek Koltunski
        mDistance    = mHeight/a;
159 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
160
161
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
162
        }
163
      else             // parallel projection
164
        {
165 8426bd6a Leszek Koltunski
        float left   = -mWidth/2.0f;
166
        float right  = +mWidth/2.0f;
167
        float bottom = -mHeight/2.0f;
168
        float top    = +mHeight/2.0f;
169
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
170
        mDistance    = mWidth+mHeight;
171
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
172 af4cc5db Leszek Koltunski
173
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
174
        }
175
      }
176
    }
177
178 1d6d261e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180 6b962e80 Leszek Koltunski
  private void createBuffers()
181 1d6d261e Leszek Koltunski
    {
182
    float mipmap=1.0f;
183
184
    for(int j=0; j<EffectQuality.LENGTH; j++)
185
      {
186 0f011027 Leszek Koltunski
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
187
      mBuffer[j].mMipmap = mipmap;
188 73208cab leszek
      mBuffer[j].mNear   = mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
189 7ad20cce Leszek Koltunski
      mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
190 1d6d261e Leszek Koltunski
      mipmap *= EffectQuality.MULTIPLIER;
191
      }
192
193 70b6a155 Leszek Koltunski
    mBuffer[EffectQuality.LENGTH] = this;
194
195 1d6d261e Leszek Koltunski
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
196
197
    GLES30.glStencilMask(0xff);
198
    GLES30.glDepthMask(true);
199
    GLES30.glColorMask(true,true,true,true);
200
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
201
    GLES30.glClearDepthf(1.0f);
202
    GLES30.glClearStencil(0);
203
204
    for(int j=0; j<EffectQuality.LENGTH; j++)
205
      {
206 0f011027 Leszek Koltunski
      mBuffer[j].setAsOutput();
207
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
208 1d6d261e Leszek Koltunski
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
209 0f011027 Leszek Koltunski
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
210
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
211 1d6d261e Leszek Koltunski
      }
212
    }
213
214 6b962e80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216 70b6a155 Leszek Koltunski
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
217 6b962e80 Leszek Koltunski
    {
218
    GLES30.glViewport(0, 0, mWidth, mHeight);
219
    setAsOutput(currTime);
220
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
221
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mColorH[0]);
222
    GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
223
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
224
225
    GLES30.glDisable(GLES30.GL_STENCIL_TEST);
226
    GLES30.glStencilMask(0x00);
227
228
    DistortedEffects.blitDepthPriv(this);
229
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
230
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
231
    GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
232
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
233
234
    // clear buffers
235
    GLES30.glStencilMask(0xff);
236
    GLES30.glDepthMask(true);
237
    GLES30.glColorMask(true,true,true,true);
238
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
239
    GLES30.glClearDepthf(1.0f);
240
    GLES30.glClearStencil(0);
241
242
    buffer.setAsOutput();
243
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[1], 0);
244
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
245
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[0], 0);
246
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
247
248
    return 1;
249
    }
250
251 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252 e02264ff leszek
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
253
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
254
// to a whole buffer and merge it.
255 39086ebb leszek
256 8e28b6ff leszek
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
257 b2939df4 Leszek Koltunski
    {
258 7266d8ef Leszek Koltunski
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
259 3f44e745 Leszek Koltunski
    DistortedNode child1, child2;
260 70b6a155 Leszek Koltunski
    EffectQueuePostprocess lastQueue=null, currQueue;
261 915b7b2b leszek
    long lastBucket=0, currBucket;
262 0afc143a leszek
263 8e28b6ff leszek
    for(int i=0; i<numChildren; i++)
264 39086ebb leszek
      {
265 3f44e745 Leszek Koltunski
      child1 = children.get(i);
266 70b6a155 Leszek Koltunski
      currQueue = child1.getPostprocessQueue();
267
      currBucket= currQueue.getID();
268 b9798977 leszek
269 915b7b2b leszek
      if( currBucket==0 ) numRenders += child1.draw(time,this);
270 60c1c622 leszek
      else
271
        {
272 0f011027 Leszek Koltunski
        if( mBuffer[0]==null ) createBuffers();
273 c9a24bfb Leszek Koltunski
274 915b7b2b leszek
        if( lastBucket!=currBucket )
275 c9a24bfb Leszek Koltunski
          {
276 915b7b2b leszek
          if( lastBucket!=0 )
277 c9a24bfb Leszek Koltunski
            {
278
            for(int j=bucketChange; j<i; j++)
279
              {
280 3f44e745 Leszek Koltunski
              child2 = children.get(j);
281 70b6a155 Leszek Koltunski
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
282 c9a24bfb Leszek Koltunski
              }
283
284 70b6a155 Leszek Koltunski
            numRenders += lastQueue.postprocess(this);
285 bed13bea leszek
            numRenders += blitWithDepth(time, mBuffer[quality]);
286 c9a24bfb Leszek Koltunski
            }
287 660cd468 leszek
288 70b6a155 Leszek Koltunski
          internalQuality = currQueue.getInternalQuality();
289
          quality         = currQueue.getQuality();
290
          bucketChange    = i;
291 c9a24bfb Leszek Koltunski
          }
292
293 70b6a155 Leszek Koltunski
        child1.draw(time,mBuffer[quality]);
294
        //numRenders += currQueue.draw(child1,time,mBuffer);
295 c9a24bfb Leszek Koltunski
296 8e28b6ff leszek
        if( i==numChildren-1 )
297 cf7394cc leszek
          {
298 8e28b6ff leszek
          for(int j=bucketChange; j<numChildren; j++)
299 c9a24bfb Leszek Koltunski
            {
300 3f44e745 Leszek Koltunski
            child2 = children.get(j);
301 70b6a155 Leszek Koltunski
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
302 c9a24bfb Leszek Koltunski
            }
303
304 70b6a155 Leszek Koltunski
          numRenders += currQueue.postprocess(this);
305 7266d8ef Leszek Koltunski
          numRenders += blitWithDepth(time, mBuffer[quality]);
306 cf7394cc leszek
          }
307 60c1c622 leszek
        }
308 b9798977 leszek
309 70b6a155 Leszek Koltunski
      lastQueue = currQueue;
310
      lastBucket= currBucket;
311 95c441a2 leszek
      }
312 270c27bc Leszek Koltunski
313 39086ebb leszek
    return numRenders;
314
    }
315
316 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318 26a4e5f6 leszek
  ArrayList<DistortedNode> getChildren()
319 be60d4ff leszek
    {
320 26a4e5f6 leszek
    return mChildren;
321 be60d4ff leszek
    }
322
323 b2939df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
324
// PUBLIC API
325 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
326 86e99907 leszek
/**
327
 * Make the library show various debugging information.
328
 * <p>
329
 * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
330
 *
331
 * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
332
 */
333
  public void setDebug(int bitmask)
334
    {
335 26a4e5f6 leszek
    if( this instanceof DistortedScreen )
336
      mDebugLevel = bitmask;
337 86e99907 leszek
    }
338
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
341 c5369f1b leszek
/**
342 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
343 af4cc5db Leszek Koltunski
 * <p>
344
 * Must be called from a thread holding OpenGL Context.
345
 *
346 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
347 7691a39f leszek
 * @return Number of objects rendered.
348 c5369f1b leszek
 */
349 b2939df4 Leszek Koltunski
  public int render(long time)
350 af4cc5db Leszek Koltunski
    {
351 26a4e5f6 leszek
    if( mDebugLevel!=0 ) prepareDebug(time);
352 86e99907 leszek
353 c204c69d leszek
    // change tree topology (attach and detach children)
354 889cce10 Leszek Koltunski
/*
355 42571056 Leszek Koltunski
    boolean changed1 =
356 889cce10 Leszek Koltunski
*/
357 efe3d8fe leszek
    DistortedMaster.toDo();
358 eadf0859 leszek
/*
359 42571056 Leszek Koltunski
    if( changed1 )
360 c204c69d leszek
      {
361
      for(int i=0; i<mNumChildren; i++)
362
        {
363 af27df87 leszek
        mChildren.get(i).debug(0);
364 c204c69d leszek
        }
365 af27df87 leszek
366 7691a39f leszek
      DistortedNode.debugMap();
367 c204c69d leszek
      }
368 eadf0859 leszek
*/
369 09ab7524 Leszek Koltunski
    // create and delete all underlying OpenGL resources
370
    // Watch out: FIRST change topology, only then deal
371
    // with OpenGL resources. That's because changing Tree
372
    // can result in additional Framebuffers that would need
373
    // to be created immediately, before the calls to drawRecursive()
374 42571056 Leszek Koltunski
/*
375
    boolean changed2 =
376
*/
377 f8377ef8 leszek
    toDo();
378 eadf0859 leszek
/*
379 42571056 Leszek Koltunski
    if( changed2 )
380 af27df87 leszek
      {
381 226144d0 leszek
      DistortedObject.debugLists();
382 42571056 Leszek Koltunski
      }
383 eadf0859 leszek
*/
384 c834348d leszek
    // mark OpenGL state as unknown
385
    DistortedRenderState.reset();
386 2ed1c692 leszek
387 b2939df4 Leszek Koltunski
    int numRenders=0;
388 7691a39f leszek
389 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
390 af4cc5db Leszek Koltunski
      {
391 b2939df4 Leszek Koltunski
      numRenders += mChildren.get(i).renderRecursive(time);
392 af4cc5db Leszek Koltunski
      }
393 7691a39f leszek
394 070695a5 Leszek Koltunski
    setAsOutput(time);
395 b2939df4 Leszek Koltunski
    numRenders += renderChildren(time,mNumChildren,mChildren);
396
397 26a4e5f6 leszek
    if( mDebugLevel != 0 ) renderDebug(time);
398 b28c6c21 leszek
399 7691a39f leszek
    return numRenders;
400 af4cc5db Leszek Koltunski
    }
401
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403 c5369f1b leszek
/**
404
 * Bind this Surface as a Framebuffer we can render to.
405 02ab6f9d leszek
 *
406
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
407
 *             out if this is the first time during present frame that this FBO is being set as output.
408
 *             If so, the library, in addition to binding the Surface for output, also clears the
409
 *             Surface's color and depth attachments.
410 c5369f1b leszek
 */
411 95c441a2 leszek
  public void setAsOutput(long time)
412 a436ccc5 leszek
    {
413
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
414 95c441a2 leszek
415
    if( mTime!=time )
416
      {
417
      mTime = time;
418 23eecbd9 Leszek Koltunski
      DistortedRenderState.colorDepthStencilOn();
419 a9f41fa3 leszek
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
420
      GLES30.glClearDepthf(mClearDepth);
421 23eecbd9 Leszek Koltunski
      GLES30.glClearStencil(mClearStencil);
422 ad16ed3b Leszek Koltunski
      GLES30.glClear(mClear);
423 144d252c Leszek Koltunski
      DistortedRenderState.colorDepthStencilRestore();
424 95c441a2 leszek
      }
425 a436ccc5 leszek
    }
426 af4cc5db Leszek Koltunski
427 1dfc9074 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
428
/**
429
 * Bind this Surface as a Framebuffer we can render to.
430
 * <p>
431
 * This version does not attempt to clear anything.
432
 */
433
434
  public void setAsOutput()
435
    {
436
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
437
    }
438
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440
/**
441
 * Return the Near plane of the Projection included in the Surface.
442
 *
443
 * @return the Near plane.
444
 */
445
  public float getNear()
446
    {
447
    return mNear;
448
    }
449
450 638b5b5c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
451
/**
452
 * Set mipmap level.
453
 * <p>
454
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
455
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
456
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
457
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
458
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
459
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
460 8426bd6a Leszek Koltunski
 * <p>
461
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
462
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
463
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
464
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
465 638b5b5c leszek
 *
466
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
467
 *               does not make any sense (that would result in loss of speed and no gain in quality)
468
 */
469
  public void setMipmap(float mipmap)
470
    {
471
    mMipmap = mipmap;
472
    }
473
474 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
475
/**
476
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
477 ad16ed3b Leszek Koltunski
 * this Surface at the beginning of each frame.
478 a9f41fa3 leszek
 *
479
 * @param r the Red component. Default: 0.0f
480
 * @param g the Green component. Default: 0.0f
481
 * @param b the Blue component. Default: 0.0f
482
 * @param a the Alpha component. Default: 0.0f
483
 */
484
  public void glClearColor(float r, float g, float b, float a)
485
    {
486
    mClearR = r;
487
    mClearG = g;
488
    mClearB = b;
489
    mClearA = a;
490
    }
491
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493
/**
494 23eecbd9 Leszek Koltunski
 * Uses glClearDepthf() to set up a value with which to clear
495
 * the Depth buffer of our Surface at the beginning of each frame.
496 a9f41fa3 leszek
 *
497
 * @param d the Depth. Default: 1.0f
498
 */
499
  public void glClearDepthf(float d)
500
    {
501
    mClearDepth = d;
502
    }
503
504 23eecbd9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
505
/**
506
 * Uses glClearStencil() to set up a value with which to clear the
507
 * Stencil buffer of our Surface at the beginning of each frame.
508
 *
509
 * @param s the Stencil. Default: 0
510
 */
511
  public void glClearStencil(int s)
512
    {
513
    mClearStencil = s;
514
    }
515
516 ad16ed3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Which buffers to Clear at the beginning of each frame?
519
 * <p>
520
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
521
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
522
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
523
 *
524
 * @param mask bitwise OR of BUFFER_BITs to clear.
525
 */
526
  public void glClear(int mask)
527
    {
528
    mClear = mask;
529
    }
530
531 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
532
/**
533
 * Create new Projection matrix.
534
 *
535
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
536 54fe333a leszek
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
537
 * @param near Distance between the screen plane and the near plane.
538
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
539
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
540 af4cc5db Leszek Koltunski
 */
541 54fe333a leszek
  public void setProjection(float fov, float near)
542 af4cc5db Leszek Koltunski
    {
543 8069e806 Leszek Koltunski
    if( fov < 180.0f && fov >=0.0f )
544
      {
545
      mFOV = fov;
546
      }
547
548
    if( near<   1.0f && near> 0.0f )
549
      {
550
      mNear= near;
551
      }
552
    else if( near<=0.0f )
553
      {
554
      mNear = 0.01f;
555
      }
556
    else if( near>=1.0f )
557
      {
558
      mNear=0.99f;
559
      }
560 af4cc5db Leszek Koltunski
561 1dfc9074 leszek
    if( mBuffer[0]!=null )
562
      {
563
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
564
      }
565
566 af4cc5db Leszek Koltunski
    createProjection();
567
    }
568
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570 c5369f1b leszek
/**
571 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
572 c7da4e65 leszek
 * <p>
573
 * This method can be safely called mid-render as it doesn't interfere with rendering.
574 af4cc5db Leszek Koltunski
 *
575
 * @param width The new width.
576
 * @param height The new height.
577 c5369f1b leszek
 */
578 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
579
    {
580
    if( mWidth!=width || mHeight!=height )
581
      {
582
      mWidth = width;
583
      mHeight= height;
584 f8377ef8 leszek
585
      createProjection();
586
587 c7da4e65 leszek
      if( mColorCreated==CREATED )
588 f8377ef8 leszek
        {
589 f28fffc2 Leszek Koltunski
        markForCreation();
590 f8377ef8 leszek
        recreate();
591
        }
592 af4cc5db Leszek Koltunski
      }
593
    }
594 a09ada4c Leszek Koltunski
595 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
596
/**
597 89de975c leszek
 * Return true if the Surface contains a DEPTH attachment.
598 a436ccc5 leszek
 *
599 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
600 a436ccc5 leszek
 */
601 89de975c leszek
  public boolean hasDepth()
602 a436ccc5 leszek
    {
603 89de975c leszek
    return mDepthStencilCreated==CREATED;
604 a436ccc5 leszek
    }
605
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607
/**
608 89de975c leszek
 * Return true if the Surface contains a STENCIL attachment.
609 a436ccc5 leszek
 *
610 89de975c leszek
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
611 a436ccc5 leszek
 */
612 89de975c leszek
  public boolean hasStencil()
613 a436ccc5 leszek
    {
614 89de975c leszek
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
615 a436ccc5 leszek
    }
616
617 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
618
/**
619
 * Adds a new child to the last position in the list of our Surface's children.
620 c204c69d leszek
 * <p>
621
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
622 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
623 a09ada4c Leszek Koltunski
 *
624
 * @param node The new Node to add.
625
 */
626 c204c69d leszek
  public void attach(DistortedNode node)
627 a09ada4c Leszek Koltunski
    {
628 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
629 efe3d8fe leszek
    DistortedMaster.newSlave(this);
630 a09ada4c Leszek Koltunski
    }
631
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633
/**
634
 * Adds a new child to the last position in the list of our Surface's children.
635 c204c69d leszek
 * <p>
636
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
637 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
638 a09ada4c Leszek Koltunski
 *
639
 * @param surface InputSurface to initialize our child Node with.
640
 * @param effects DistortedEffects to initialize our child Node with.
641
 * @param mesh MeshObject to initialize our child Node with.
642
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
643
 */
644 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
645 a09ada4c Leszek Koltunski
    {
646
    DistortedNode node = new DistortedNode(surface,effects,mesh);
647 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(ATTACH,node));
648 efe3d8fe leszek
    DistortedMaster.newSlave(this);
649 c204c69d leszek
    return node;
650
    }
651
652 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
653
/**
654
 * Removes the first occurrence of a specified child from the list of children of our Surface.
655
 * <p>
656
 * A bit questionable method as there can be many different Nodes attached as children, some
657
 * of them having the same Effects but - for instance - different Mesh. Use with care.
658
 * <p>
659
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
660 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
661 af27df87 leszek
 *
662
 * @param effects DistortedEffects to remove.
663
 */
664
  public void detach(DistortedEffects effects)
665
    {
666
    long id = effects.getID();
667
    DistortedNode node;
668 8baa1fe6 Leszek Koltunski
    boolean detached = false;
669 af27df87 leszek
670
    for(int i=0; i<mNumChildren; i++)
671
      {
672
      node = mChildren.get(i);
673
674
      if( node.getEffects().getID()==id )
675
        {
676 8baa1fe6 Leszek Koltunski
        detached = true;
677 ffbe7ecf Leszek Koltunski
        mJobs.add(new Job(DETACH,node));
678 efe3d8fe leszek
        DistortedMaster.newSlave(this);
679 af27df87 leszek
        break;
680
        }
681
      }
682 8baa1fe6 Leszek Koltunski
683
    if( !detached )
684
      {
685
      // if we failed to detach any, it still might be the case that
686 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
687
      int num = mJobs.size();
688
      Job job;
689
690
      for(int i=0; i<num; i++)
691
        {
692
        job = mJobs.get(i);
693
694
        if( job.type==ATTACH && job.node.getEffects()==effects )
695
          {
696
          mJobs.remove(i);
697
          break;
698
          }
699
        }
700 8baa1fe6 Leszek Koltunski
      }
701 af27df87 leszek
    }
702
703 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
704
/**
705
 * Removes the first occurrence of a specified child from the list of children of our Surface.
706 c204c69d leszek
 * <p>
707
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
708 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
709 c204c69d leszek
 *
710
 * @param node The Node to remove.
711
 */
712
  public void detach(DistortedNode node)
713
    {
714 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETACH,node));
715 efe3d8fe leszek
    DistortedMaster.newSlave(this);
716 a09ada4c Leszek Koltunski
    }
717
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719
/**
720
 * Removes all children Nodes.
721 c204c69d leszek
 * <p>
722
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
723 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
724 c204c69d leszek
 */
725
  public void detachAll()
726
    {
727 ffbe7ecf Leszek Koltunski
    mJobs.add(new Job(DETALL,null));
728 efe3d8fe leszek
    DistortedMaster.newSlave(this);
729 c204c69d leszek
    }
730
731
///////////////////////////////////////////////////////////////////////////////////////////////////
732
/**
733
 * This is not really part of the public API. Has to be public only because it is a part of the
734 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
735 c204c69d leszek
 * Java has no multiple inheritance.
736 43fbf0dd leszek
 *
737
 * @y.exclude
738 a09ada4c Leszek Koltunski
 */
739 efe3d8fe leszek
  public void doWork()
740 a09ada4c Leszek Koltunski
    {
741 efe3d8fe leszek
    int num = mJobs.size();
742
    Job job;
743
744
    for(int i=0; i<num; i++)
745
      {
746
      job = mJobs.remove(0);
747
748
      switch(job.type)
749
        {
750 be60d4ff leszek
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
751
                     job.node.setSurfaceParent(this);
752
                     DistortedMaster.addSorted(mChildren,job.node);
753 efe3d8fe leszek
                     mNumChildren++;
754
                     break;
755 be60d4ff leszek
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
756 efe3d8fe leszek
                       {
757 be60d4ff leszek
                       job.node.setSurfaceParent(null);
758 efe3d8fe leszek
                       mNumChildren--;
759
                       }
760
                     break;
761 be60d4ff leszek
        case DETALL: if( mNumChildren>0 )
762 efe3d8fe leszek
                       {
763 be60d4ff leszek
                       DistortedNode tmp;
764
765
                       for(int j=mNumChildren-1; j>=0; j--)
766
                         {
767
                         tmp = mChildren.remove(j);
768
                         tmp.setSurfaceParent(null);
769
                         }
770
771 efe3d8fe leszek
                       mNumChildren = 0;
772
                       }
773
                     break;
774 ffbe7ecf Leszek Koltunski
        case SORT  : mChildren.remove(job.node);
775 be60d4ff leszek
                     DistortedMaster.addSorted(mChildren,job.node);
776 efe3d8fe leszek
                     break;
777
        }
778
      }
779 a09ada4c Leszek Koltunski
    }
780 af4cc5db Leszek Koltunski
}