Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 02ab6f9d

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 c204c69d leszek
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedAttacheable
29 af4cc5db Leszek Koltunski
{
30 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
31
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
32
33 95c441a2 leszek
  private long mTime;
34 c2c08950 leszek
  private float mFOV;
35 af4cc5db Leszek Koltunski
  int mWidth,mHeight,mDepth;
36 c2c08950 leszek
  float mDistance, mNear;
37 af4cc5db Leszek Koltunski
  float[] mProjectionMatrix;
38
39 c7da4e65 leszek
  int mDepthCreated;
40 a436ccc5 leszek
  int[] mDepthH = new int[1];
41
  int[] mFBOH   = new int[1];
42
43 a9f41fa3 leszek
  private float mClearR, mClearG, mClearB, mClearA;
44
  private float mClearDepth;
45
46 b9798977 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
  abstract DistortedFramebuffer getBuffer();
49
50 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 09ab7524 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
53 af4cc5db Leszek Koltunski
    {
54 09ab7524 Leszek Koltunski
    super(width,height,createColor,type);
55 af4cc5db Leszek Koltunski
56
    mProjectionMatrix = new float[16];
57
58
    mWidth = width;
59
    mHeight= height;
60
61
    mFOV = 60.0f;
62 54fe333a leszek
    mNear=  0.5f;
63 af4cc5db Leszek Koltunski
64 c7da4e65 leszek
    mDepthCreated= createDepth;
65 a436ccc5 leszek
    mFBOH[0]     = fbo;
66 c7da4e65 leszek
    mDepthH[0]   = 0;
67 a436ccc5 leszek
68 95c441a2 leszek
    mTime = 0;
69
70 a9f41fa3 leszek
    mClearR = 0.0f;
71
    mClearG = 0.0f;
72
    mClearB = 0.0f;
73
    mClearA = 0.0f;
74
75
    mClearDepth = 1.0f;
76
77 af4cc5db Leszek Koltunski
    createProjection();
78
    }
79
80 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82 af4cc5db Leszek Koltunski
  void createProjection()
83
    {
84
    if( mWidth>0 && mHeight>1 )
85
      {
86
      if( mFOV>0.0f )  // perspective projection
87
        {
88 54fe333a leszek
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
89 c2c08950 leszek
        float q = mWidth*mNear;
90 54fe333a leszek
        float c = mHeight*mNear;
91
92 c2c08950 leszek
        float left   = -q/2;
93
        float right  = +q/2;
94
        float bottom = -c/2;
95
        float top    = +c/2;
96
        float near   =  c/a;
97 54fe333a leszek
98
        mDistance    = mHeight/a;
99 af4cc5db Leszek Koltunski
        float far    = 2*mDistance-near;
100
        mDepth       = (int)((far-near)/2);
101
102
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
103
        }
104
      else             // parallel projection
105
        {
106 54fe333a leszek
        float left   = -mWidth /2.0f;
107
        float right  = +mWidth /2.0f;
108
        float bottom = -mHeight/2.0f;
109
        float top    = +mHeight/2.0f;
110
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
111
        mDistance    = mWidth+mHeight;
112
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
113
        mDepth       = (int)((far-near)/2);
114 af4cc5db Leszek Koltunski
115
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
116
        }
117
      }
118
    }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121 39086ebb leszek
122 b2939df4 Leszek Koltunski
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
123
    {
124
    int numRenders = 0;
125 b9798977 leszek
    DistortedNode child;
126
    DistortedFramebuffer fbo;
127 95c441a2 leszek
    DistortedOutputSurface surface;
128 b9798977 leszek
129
    // 1. Render all children that have postprocessing effects to their own buffer FBOs
130 39086ebb leszek
131 b2939df4 Leszek Koltunski
    for(int i=0; i<num; i++)
132 39086ebb leszek
      {
133 b9798977 leszek
      child = children.get(i);
134
      fbo   = child.getPostprocessingBuffer();
135
136
      if( fbo!=null )
137
        {
138
        fbo.resizeFast(mWidth,mHeight);
139
        numRenders += child.draw(time,fbo);
140
        }
141
      }
142
143
    // 2. If we have rendered anything so far, and we are a Screen, then render to an
144
    //    intermediate FBO instead.
145
146 95c441a2 leszek
    surface = this;//numRenders>0 ? getBuffer() : this;
147 b9798977 leszek
148
    // 3. Render all children without postprocessing effects to buffer
149
150
    for(int i=0; i<num; i++)
151
      {
152
      child = children.get(i);
153
      fbo   = child.getPostprocessingBuffer();
154
155
      if( fbo==null )
156
        {
157 95c441a2 leszek
        numRenders += child.draw(time,surface);
158 b9798977 leszek
        }
159
      }
160
161
    // 4. For all postprocessing fbo,
162
    //       postprocess fbo
163
    //       merge to buffer
164
165 95c441a2 leszek
    for(int i=0; i<num; i++)
166
      {
167
      numRenders += children.get(i).postprocess(time,surface);
168
      }
169 b9798977 leszek
170
    // 5. finally blit to this if we have to
171
172 95c441a2 leszek
    if( surface!=this && ((DistortedFramebuffer)surface).setAsInput() )
173 b9798977 leszek
      {
174
      numRenders++;
175
      DistortedEffects.blitPriv(this);
176 39086ebb leszek
      }
177
178
    return numRenders;
179
    }
180
181 b2939df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// PUBLIC API
183 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
184 c5369f1b leszek
/**
185 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
186 af4cc5db Leszek Koltunski
 * <p>
187
 * Must be called from a thread holding OpenGL Context.
188
 *
189 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
190 7691a39f leszek
 * @return Number of objects rendered.
191 c5369f1b leszek
 */
192 b2939df4 Leszek Koltunski
  public int render(long time)
193 af4cc5db Leszek Koltunski
    {
194 c204c69d leszek
    // change tree topology (attach and detach children)
195 889cce10 Leszek Koltunski
/*
196
    boolean changed =
197
*/
198
    DistortedAttachDaemon.toDo();
199 eadf0859 leszek
/*
200 f28fffc2 Leszek Koltunski
    // debugging only
201 af27df87 leszek
    if( changed )
202 c204c69d leszek
      {
203
      for(int i=0; i<mNumChildren; i++)
204
        {
205 af27df87 leszek
        mChildren.get(i).debug(0);
206 c204c69d leszek
        }
207 af27df87 leszek
208 7691a39f leszek
      DistortedNode.debugMap();
209 c204c69d leszek
      }
210 eadf0859 leszek
*/
211 09ab7524 Leszek Koltunski
    // create and delete all underlying OpenGL resources
212
    // Watch out: FIRST change topology, only then deal
213
    // with OpenGL resources. That's because changing Tree
214
    // can result in additional Framebuffers that would need
215
    // to be created immediately, before the calls to drawRecursive()
216 f8377ef8 leszek
    toDo();
217 eadf0859 leszek
/*
218 f28fffc2 Leszek Koltunski
    // debugging only
219 af27df87 leszek
    if( changed )
220
      {
221
      DistortedSurface.debugLists();
222
      }
223 eadf0859 leszek
*/
224 c834348d leszek
    // mark OpenGL state as unknown
225
    DistortedRenderState.reset();
226 2ed1c692 leszek
227 b2939df4 Leszek Koltunski
    int numRenders=0;
228 7691a39f leszek
229 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
230 af4cc5db Leszek Koltunski
      {
231 b2939df4 Leszek Koltunski
      numRenders += mChildren.get(i).renderRecursive(time);
232 af4cc5db Leszek Koltunski
      }
233 7691a39f leszek
234 95c441a2 leszek
    setAsOutput(time);
235 b2939df4 Leszek Koltunski
    numRenders += renderChildren(time,mNumChildren,mChildren);
236
237 7691a39f leszek
    return numRenders;
238 af4cc5db Leszek Koltunski
    }
239
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241 c5369f1b leszek
/**
242
 * Bind this Surface as a Framebuffer we can render to.
243 02ab6f9d leszek
 *
244
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
245
 *             out if this is the first time during present frame that this FBO is being set as output.
246
 *             If so, the library, in addition to binding the Surface for output, also clears the
247
 *             Surface's color and depth attachments.
248 c5369f1b leszek
 */
249 95c441a2 leszek
  public void setAsOutput(long time)
250 a436ccc5 leszek
    {
251
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
252 95c441a2 leszek
253
    if( mTime!=time )
254
      {
255
      mTime = time;
256
      DistortedRenderState.colorDepthOn();
257 a9f41fa3 leszek
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
258
      GLES30.glClearDepthf(mClearDepth);
259 95c441a2 leszek
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
260
      }
261 a436ccc5 leszek
    }
262 af4cc5db Leszek Koltunski
263 a9f41fa3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
264
/**
265
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
266
 * this Surface before each render.
267
 *
268
 * @param r the Red component. Default: 0.0f
269
 * @param g the Green component. Default: 0.0f
270
 * @param b the Blue component. Default: 0.0f
271
 * @param a the Alpha component. Default: 0.0f
272
 */
273
  public void glClearColor(float r, float g, float b, float a)
274
    {
275
    mClearR = r;
276
    mClearG = g;
277
    mClearB = b;
278
    mClearA = a;
279
    }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
/**
283
 * Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
284
 * the Depth buffer of Surface before each render.
285
 *
286
 * @param d the Depth. Default: 1.0f
287
 */
288
  public void glClearDepthf(float d)
289
    {
290
    mClearDepth = d;
291
    }
292
293 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
294
/**
295
 * Create new Projection matrix.
296
 *
297
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
298 54fe333a leszek
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
299
 * @param near Distance between the screen plane and the near plane.
300
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
301
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
302 af4cc5db Leszek Koltunski
 */
303 54fe333a leszek
  public void setProjection(float fov, float near)
304 af4cc5db Leszek Koltunski
    {
305 8069e806 Leszek Koltunski
    if( fov < 180.0f && fov >=0.0f )
306
      {
307
      mFOV = fov;
308
      }
309
310
    if( near<   1.0f && near> 0.0f )
311
      {
312
      mNear= near;
313
      }
314
    else if( near<=0.0f )
315
      {
316
      mNear = 0.01f;
317
      }
318
    else if( near>=1.0f )
319
      {
320
      mNear=0.99f;
321
      }
322 af4cc5db Leszek Koltunski
323
    createProjection();
324
    }
325
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327 c5369f1b leszek
/**
328 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
329 c7da4e65 leszek
 * <p>
330
 * This method can be safely called mid-render as it doesn't interfere with rendering.
331 af4cc5db Leszek Koltunski
 *
332
 * @param width The new width.
333
 * @param height The new height.
334 c5369f1b leszek
 */
335 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
336
    {
337
    if( mWidth!=width || mHeight!=height )
338
      {
339
      mWidth = width;
340
      mHeight= height;
341
      mSizeX = width;
342
      mSizeY = height;
343 f8377ef8 leszek
344
      createProjection();
345
346 c7da4e65 leszek
      if( mColorCreated==CREATED )
347 f8377ef8 leszek
        {
348 f28fffc2 Leszek Koltunski
        markForCreation();
349 f8377ef8 leszek
        recreate();
350
        }
351 af4cc5db Leszek Koltunski
      }
352
    }
353 a09ada4c Leszek Koltunski
354 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
355
/**
356
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
357
 *
358
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
359
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
360
 */
361
  public void enableDepth(boolean enable)
362
    {
363 c7da4e65 leszek
    if( enable && mDepthCreated==DONT_CREATE )
364
      {
365
      mDepthCreated = NOT_CREATED_YET;
366 f28fffc2 Leszek Koltunski
      markForCreation();
367 c7da4e65 leszek
      }
368
    if( !enable && mDepthCreated!=DONT_CREATE )
369 a436ccc5 leszek
      {
370 c7da4e65 leszek
      mDepthCreated = DONT_CREATE;
371 f28fffc2 Leszek Koltunski
      markForCreation();
372 a436ccc5 leszek
      }
373
    }
374
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
/**
377
 * Return true if the Surface contains a DEPTH attachment.
378
 *
379
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
380
 */
381
  public boolean hasDepth()
382
    {
383 c7da4e65 leszek
    return mDepthCreated==CREATED;
384 a436ccc5 leszek
    }
385
386 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
387
/**
388
 * Adds a new child to the last position in the list of our Surface's children.
389 c204c69d leszek
 * <p>
390
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
391
 * DistortedAttachDeamon (by calling attachNow())
392 a09ada4c Leszek Koltunski
 *
393
 * @param node The new Node to add.
394
 */
395 c204c69d leszek
  public void attach(DistortedNode node)
396 a09ada4c Leszek Koltunski
    {
397 c204c69d leszek
    DistortedAttachDaemon.attach(this,node);
398 a09ada4c Leszek Koltunski
    }
399
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
/**
402
 * Adds a new child to the last position in the list of our Surface's children.
403 c204c69d leszek
 * <p>
404
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
405
 * DistortedAttachDeamon (by calling attachNow())
406 a09ada4c Leszek Koltunski
 *
407
 * @param surface InputSurface to initialize our child Node with.
408
 * @param effects DistortedEffects to initialize our child Node with.
409
 * @param mesh MeshObject to initialize our child Node with.
410
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
411
 */
412 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
413 a09ada4c Leszek Koltunski
    {
414
    DistortedNode node = new DistortedNode(surface,effects,mesh);
415 c204c69d leszek
    DistortedAttachDaemon.attach(this,node);
416
    return node;
417
    }
418
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420
/**
421
 * This is not really part of the public API. Has to be public only because it is a part of the
422
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
423
 * Java has no multiple inheritance.
424
 *
425 43fbf0dd leszek
 * @y.exclude
426 c204c69d leszek
 * @param node new Node to add.
427
 */
428
  public void attachNow(DistortedNode node)
429
    {
430
    if( mChildren==null ) mChildren = new ArrayList<>(2);
431 a09ada4c Leszek Koltunski
    mChildren.add(node);
432
    mNumChildren++;
433
    }
434
435 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
436
/**
437
 * Removes the first occurrence of a specified child from the list of children of our Surface.
438
 * <p>
439
 * A bit questionable method as there can be many different Nodes attached as children, some
440
 * of them having the same Effects but - for instance - different Mesh. Use with care.
441
 * <p>
442
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
443
 * DistortedAttachDeamon (by calling detachNow())
444
 *
445
 * @param effects DistortedEffects to remove.
446
 */
447
  public void detach(DistortedEffects effects)
448
    {
449
    long id = effects.getID();
450
    DistortedNode node;
451
452
    for(int i=0; i<mNumChildren; i++)
453
      {
454
      node = mChildren.get(i);
455
456
      if( node.getEffects().getID()==id )
457
        {
458
        DistortedAttachDaemon.detach(this,node);
459
        break;
460
        }
461
      }
462
    }
463
464 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
465
/**
466
 * Removes the first occurrence of a specified child from the list of children of our Surface.
467 c204c69d leszek
 * <p>
468
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
469
 * DistortedAttachDeamon (by calling detachNow())
470
 *
471
 * @param node The Node to remove.
472
 */
473
  public void detach(DistortedNode node)
474
    {
475
    DistortedAttachDaemon.detach(this,node);
476
    }
477
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479
/**
480
 * This is not really part of the public API. Has to be public only because it is a part of the
481
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
482
 * Java has no multiple inheritance.
483 a09ada4c Leszek Koltunski
 *
484 43fbf0dd leszek
 * @y.exclude
485 a09ada4c Leszek Koltunski
 * @param node The Node to remove.
486
 */
487 c204c69d leszek
  public void detachNow(DistortedNode node)
488 a09ada4c Leszek Koltunski
    {
489 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 && mChildren.remove(node) )
490 a09ada4c Leszek Koltunski
      {
491
      mNumChildren--;
492
      }
493
    }
494
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496
/**
497
 * Removes all children Nodes.
498 c204c69d leszek
 * <p>
499
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
500
 * DistortedAttachDeamon (by calling detachAllNow())
501
 */
502
  public void detachAll()
503
    {
504
    DistortedAttachDaemon.detachAll(this);
505
    }
506
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
/**
509
 * This is not really part of the public API. Has to be public only because it is a part of the
510
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
511
 * Java has no multiple inheritance.
512 43fbf0dd leszek
 *
513
 * @y.exclude
514 a09ada4c Leszek Koltunski
 */
515 c204c69d leszek
  public void detachAllNow()
516 a09ada4c Leszek Koltunski
    {
517 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 )
518
      {
519
      mNumChildren = 0;
520
      mChildren.clear();
521
      }
522 a09ada4c Leszek Koltunski
    }
523 af4cc5db Leszek Koltunski
}