Project

General

Profile

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

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

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