Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 8069e806

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24
import java.util.ArrayList;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedAttacheable
29
{
30
  private ArrayList<DistortedNode> mChildren;
31
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
32

    
33
  private long mTime;
34
  private float mFOV;
35
  int mWidth,mHeight,mDepth;
36
  float mDistance, mNear;
37
  float[] mProjectionMatrix;
38

    
39
  int mDepthCreated;
40
  int[] mDepthH = new int[1];
41
  int[] mFBOH   = new int[1];
42

    
43
  private float mClearR, mClearG, mClearB, mClearA;
44
  private float mClearDepth;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  abstract DistortedFramebuffer getBuffer();
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
53
    {
54
    super(width,height,createColor,type);
55

    
56
    mProjectionMatrix = new float[16];
57

    
58
    mWidth = width;
59
    mHeight= height;
60

    
61
    mFOV = 60.0f;
62
    mNear=  0.5f;
63

    
64
    mDepthCreated= createDepth;
65
    mFBOH[0]     = fbo;
66
    mDepthH[0]   = 0;
67

    
68
    mTime = 0;
69

    
70
    mClearR = 0.0f;
71
    mClearG = 0.0f;
72
    mClearB = 0.0f;
73
    mClearA = 0.0f;
74

    
75
    mClearDepth = 1.0f;
76

    
77
    createProjection();
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  void createProjection()
83
    {
84
    if( mWidth>0 && mHeight>1 )
85
      {
86
      if( mFOV>0.0f )  // perspective projection
87
        {
88
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
89
        float q = mWidth*mNear;
90
        float c = mHeight*mNear;
91

    
92
        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

    
98
        mDistance    = mHeight/a;
99
        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
        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

    
115
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
116
        }
117
      }
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
123
    {
124
    int numRenders = 0;
125
    DistortedNode child;
126
    DistortedFramebuffer fbo;
127
    DistortedOutputSurface surface;
128

    
129
    // 1. Render all children that have postprocessing effects to their own buffer FBOs
130

    
131
    for(int i=0; i<num; i++)
132
      {
133
      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
    surface = this;//numRenders>0 ? getBuffer() : this;
147

    
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
        numRenders += child.draw(time,surface);
158
        }
159
      }
160

    
161
    // 4. For all postprocessing fbo,
162
    //       postprocess fbo
163
    //       merge to buffer
164

    
165
    for(int i=0; i<num; i++)
166
      {
167
      numRenders += children.get(i).postprocess(time,surface);
168
      }
169

    
170
    // 5. finally blit to this if we have to
171

    
172
    if( surface!=this && ((DistortedFramebuffer)surface).setAsInput() )
173
      {
174
      numRenders++;
175
      DistortedEffects.blitPriv(this);
176
      }
177

    
178
    return numRenders;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// PUBLIC API
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
/**
185
 * Draws all the attached children to this OutputSurface.
186
 * <p>
187
 * Must be called from a thread holding OpenGL Context.
188
 *
189
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
190
 * @return Number of objects rendered.
191
 */
192
  public int render(long time)
193
    {
194
    // change tree topology (attach and detach children)
195
/*
196
    boolean changed =
197
*/
198
    DistortedAttachDaemon.toDo();
199
/*
200
    // debugging only
201
    if( changed )
202
      {
203
      for(int i=0; i<mNumChildren; i++)
204
        {
205
        mChildren.get(i).debug(0);
206
        }
207

    
208
      DistortedNode.debugMap();
209
      }
210
*/
211
    // 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
    toDo();
217
/*
218
    // debugging only
219
    if( changed )
220
      {
221
      DistortedSurface.debugLists();
222
      }
223
*/
224
    // mark OpenGL state as unknown
225
    DistortedRenderState.reset();
226

    
227
    int numRenders=0;
228

    
229
    for(int i=0; i<mNumChildren; i++)
230
      {
231
      numRenders += mChildren.get(i).renderRecursive(time);
232
      }
233

    
234
    setAsOutput(time);
235
    numRenders += renderChildren(time,mNumChildren,mChildren);
236

    
237
    return numRenders;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
/**
242
 * Bind this Surface as a Framebuffer we can render to.
243
 */
244
  public void setAsOutput(long time)
245
    {
246
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
247

    
248
    if( mTime!=time )
249
      {
250
      mTime = time;
251
      DistortedRenderState.colorDepthOn();
252
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
253
      GLES30.glClearDepthf(mClearDepth);
254
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
255
      }
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
261
 * this Surface before each render.
262
 *
263
 * @param r the Red component. Default: 0.0f
264
 * @param g the Green component. Default: 0.0f
265
 * @param b the Blue component. Default: 0.0f
266
 * @param a the Alpha component. Default: 0.0f
267
 */
268
  public void glClearColor(float r, float g, float b, float a)
269
    {
270
    mClearR = r;
271
    mClearG = g;
272
    mClearB = b;
273
    mClearA = a;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
/**
278
 * Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
279
 * the Depth buffer of Surface before each render.
280
 *
281
 * @param d the Depth. Default: 1.0f
282
 */
283
  public void glClearDepthf(float d)
284
    {
285
    mClearDepth = d;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
/**
290
 * Create new Projection matrix.
291
 *
292
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
293
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
294
 * @param near Distance between the screen plane and the near plane.
295
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
296
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
297
 */
298
  public void setProjection(float fov, float near)
299
    {
300
    if( fov < 180.0f && fov >=0.0f )
301
      {
302
      mFOV = fov;
303
      }
304

    
305
    if( near<   1.0f && near> 0.0f )
306
      {
307
      mNear= near;
308
      }
309
    else if( near<=0.0f )
310
      {
311
      mNear = 0.01f;
312
      }
313
    else if( near>=1.0f )
314
      {
315
      mNear=0.99f;
316
      }
317

    
318
    createProjection();
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
/**
323
 * Resize the underlying Framebuffer.
324
 * <p>
325
 * This method can be safely called mid-render as it doesn't interfere with rendering.
326
 *
327
 * @param width The new width.
328
 * @param height The new height.
329
 */
330
  public void resize(int width, int height)
331
    {
332
    if( mWidth!=width || mHeight!=height )
333
      {
334
      mWidth = width;
335
      mHeight= height;
336
      mSizeX = width;
337
      mSizeY = height;
338

    
339
      createProjection();
340

    
341
      if( mColorCreated==CREATED )
342
        {
343
        markForCreation();
344
        recreate();
345
        }
346
      }
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
/**
351
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
352
 *
353
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
354
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
355
 */
356
  public void enableDepth(boolean enable)
357
    {
358
    if( enable && mDepthCreated==DONT_CREATE )
359
      {
360
      mDepthCreated = NOT_CREATED_YET;
361
      markForCreation();
362
      }
363
    if( !enable && mDepthCreated!=DONT_CREATE )
364
      {
365
      mDepthCreated = DONT_CREATE;
366
      markForCreation();
367
      }
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
/**
372
 * Return true if the Surface contains a DEPTH attachment.
373
 *
374
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
375
 */
376
  public boolean hasDepth()
377
    {
378
    return mDepthCreated==CREATED;
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
/**
383
 * Adds a new child to the last position in the list of our Surface's children.
384
 * <p>
385
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
386
 * DistortedAttachDeamon (by calling attachNow())
387
 *
388
 * @param node The new Node to add.
389
 */
390
  public void attach(DistortedNode node)
391
    {
392
    DistortedAttachDaemon.attach(this,node);
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
/**
397
 * Adds a new child to the last position in the list of our Surface's children.
398
 * <p>
399
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
400
 * DistortedAttachDeamon (by calling attachNow())
401
 *
402
 * @param surface InputSurface to initialize our child Node with.
403
 * @param effects DistortedEffects to initialize our child Node with.
404
 * @param mesh MeshObject to initialize our child Node with.
405
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
406
 */
407
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
408
    {
409
    DistortedNode node = new DistortedNode(surface,effects,mesh);
410
    DistortedAttachDaemon.attach(this,node);
411
    return node;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415
/**
416
 * This is not really part of the public API. Has to be public only because it is a part of the
417
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
418
 * Java has no multiple inheritance.
419
 *
420
 * @y.exclude
421
 * @param node new Node to add.
422
 */
423
  public void attachNow(DistortedNode node)
424
    {
425
    if( mChildren==null ) mChildren = new ArrayList<>(2);
426
    mChildren.add(node);
427
    mNumChildren++;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431
/**
432
 * Removes the first occurrence of a specified child from the list of children of our Surface.
433
 * <p>
434
 * A bit questionable method as there can be many different Nodes attached as children, some
435
 * of them having the same Effects but - for instance - different Mesh. Use with care.
436
 * <p>
437
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
438
 * DistortedAttachDeamon (by calling detachNow())
439
 *
440
 * @param effects DistortedEffects to remove.
441
 */
442
  public void detach(DistortedEffects effects)
443
    {
444
    long id = effects.getID();
445
    DistortedNode node;
446

    
447
    for(int i=0; i<mNumChildren; i++)
448
      {
449
      node = mChildren.get(i);
450

    
451
      if( node.getEffects().getID()==id )
452
        {
453
        DistortedAttachDaemon.detach(this,node);
454
        break;
455
        }
456
      }
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460
/**
461
 * Removes the first occurrence of a specified child from the list of children of our Surface.
462
 * <p>
463
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
464
 * DistortedAttachDeamon (by calling detachNow())
465
 *
466
 * @param node The Node to remove.
467
 */
468
  public void detach(DistortedNode node)
469
    {
470
    DistortedAttachDaemon.detach(this,node);
471
    }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474
/**
475
 * This is not really part of the public API. Has to be public only because it is a part of the
476
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
477
 * Java has no multiple inheritance.
478
 *
479
 * @y.exclude
480
 * @param node The Node to remove.
481
 */
482
  public void detachNow(DistortedNode node)
483
    {
484
    if( mNumChildren>0 && mChildren.remove(node) )
485
      {
486
      mNumChildren--;
487
      }
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491
/**
492
 * Removes all children Nodes.
493
 * <p>
494
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
495
 * DistortedAttachDeamon (by calling detachAllNow())
496
 */
497
  public void detachAll()
498
    {
499
    DistortedAttachDaemon.detachAll(this);
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
/**
504
 * This is not really part of the public API. Has to be public only because it is a part of the
505
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
506
 * Java has no multiple inheritance.
507
 *
508
 * @y.exclude
509
 */
510
  public void detachAllNow()
511
    {
512
    if( mNumChildren>0 )
513
      {
514
      mNumChildren = 0;
515
      mChildren.clear();
516
      }
517
    }
518
}
(8-8/23)