Project

General

Profile

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

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

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
  DistortedFramebuffer mBuffer1, mBuffer2;
34

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

    
41
  int mDepthCreated;
42
  int[] mDepthH = new int[1];
43
  int[] mFBOH   = new int[1];
44

    
45
  private float mClearR, mClearG, mClearB, mClearA;
46
  private float mClearDepth;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
54
    mProjectionMatrix = new float[16];
55

    
56
    mWidth = width;
57
    mHeight= height;
58

    
59
    mFOV = 60.0f;
60
    mNear=  0.5f;
61

    
62
    mDepthCreated= createDepth;
63
    mFBOH[0]     = fbo;
64
    mDepthH[0]   = 0;
65

    
66
    mTime = 0;
67

    
68
    mClearR = 0.0f;
69
    mClearG = 0.0f;
70
    mClearB = 0.0f;
71
    mClearA = 0.0f;
72

    
73
    mClearDepth = 1.0f;
74

    
75
    createProjection();
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

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

    
90
        float left   = -q/2;
91
        float right  = +q/2;
92
        float bottom = -c/2;
93
        float top    = +c/2;
94
        float near   =  c/a;
95

    
96
        mDistance    = mHeight/a;
97
        float far    = 2*mDistance-near;
98
        mDepth       = (int)((far-near)/2);
99

    
100
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
101
        }
102
      else             // parallel projection
103
        {
104
        float left   = -mWidth /2.0f;
105
        float right  = +mWidth /2.0f;
106
        float bottom = -mHeight/2.0f;
107
        float top    = +mHeight/2.0f;
108
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
109
        mDistance    = mWidth+mHeight;
110
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
111
        mDepth       = (int)((far-near)/2);
112

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
121
    {
122
    int numRenders = 0;
123
    DistortedNode child;
124
    EffectQueuePostprocess lastP=null, currP;
125
    long lastB=0, currB;
126

    
127
    // Render all children, one by one.
128
    // If there are no postprocessing effects, just render to THIS.
129
    // Otherwise, render to a buffer and on each change of Postprocessing Bucket,
130
    // apply the postprocessing to a whole buffer and merge it.
131

    
132
    for(int i=0; i<num; i++)
133
      {
134
      child = children.get(i);
135
      currP = child.getPostprocess();
136
      currB = currP.getBucket();
137

    
138
      if( i>0 && currB!=lastB ) numRenders += lastP.postprocess(time,this);
139

    
140
      if( currB==0 ) numRenders += child.draw(time,this);
141
      else
142
        {
143
        if( mBuffer1==null )
144
          {
145
          mBuffer1 = new DistortedFramebuffer(mDepthCreated!=DONT_CREATE, DistortedSurface.TYPE_TREE, mWidth, mHeight);
146
          mBuffer2 = new DistortedFramebuffer(false                     , DistortedSurface.TYPE_TREE, mWidth, mHeight);
147
          }
148

    
149
        numRenders += child.draw(time,mBuffer1);
150
        if( i==num-1 ) numRenders += currP.postprocess(time,this);
151
        }
152

    
153
      lastP = currP;
154
      lastB = currB;
155
      }
156

    
157
    return numRenders;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// PUBLIC API
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
/**
164
 * Draws all the attached children to this OutputSurface.
165
 * <p>
166
 * Must be called from a thread holding OpenGL Context.
167
 *
168
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
169
 * @return Number of objects rendered.
170
 */
171
  public int render(long time)
172
    {
173
    // change tree topology (attach and detach children)
174
/*
175
    boolean changed =
176
*/
177
    DistortedAttachDaemon.toDo();
178
/*
179
    // debugging only
180
    if( changed )
181
      {
182
      for(int i=0; i<mNumChildren; i++)
183
        {
184
        mChildren.get(i).debug(0);
185
        }
186

    
187
      DistortedNode.debugMap();
188
      }
189
*/
190
    // create and delete all underlying OpenGL resources
191
    // Watch out: FIRST change topology, only then deal
192
    // with OpenGL resources. That's because changing Tree
193
    // can result in additional Framebuffers that would need
194
    // to be created immediately, before the calls to drawRecursive()
195
    toDo();
196
/*
197
    // debugging only
198
    if( changed )
199
      {
200
      DistortedSurface.debugLists();
201
      }
202
*/
203
    // mark OpenGL state as unknown
204
    DistortedRenderState.reset();
205

    
206
    int numRenders=0;
207

    
208
    for(int i=0; i<mNumChildren; i++)
209
      {
210
      numRenders += mChildren.get(i).renderRecursive(time);
211
      }
212

    
213
    setAsOutput(time);
214
    numRenders += renderChildren(time,mNumChildren,mChildren);
215

    
216
    return numRenders;
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
/**
221
 * Bind this Surface as a Framebuffer we can render to.
222
 *
223
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
224
 *             out if this is the first time during present frame that this FBO is being set as output.
225
 *             If so, the library, in addition to binding the Surface for output, also clears the
226
 *             Surface's color and depth attachments.
227
 */
228
  public void setAsOutput(long time)
229
    {
230
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
231

    
232
    if( mTime!=time )
233
      {
234
      mTime = time;
235
      DistortedRenderState.colorDepthOn();
236
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
237
      GLES30.glClearDepthf(mClearDepth);
238
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
239
      }
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
/**
244
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
245
 * this Surface before each render.
246
 *
247
 * @param r the Red component. Default: 0.0f
248
 * @param g the Green component. Default: 0.0f
249
 * @param b the Blue component. Default: 0.0f
250
 * @param a the Alpha component. Default: 0.0f
251
 */
252
  public void glClearColor(float r, float g, float b, float a)
253
    {
254
    mClearR = r;
255
    mClearG = g;
256
    mClearB = b;
257
    mClearA = a;
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
/**
262
 * Set the Depth value of GLES30.glClearDepthf() to set up depth with which to clear
263
 * the Depth buffer of Surface before each render.
264
 *
265
 * @param d the Depth. Default: 1.0f
266
 */
267
  public void glClearDepthf(float d)
268
    {
269
    mClearDepth = d;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
/**
274
 * Create new Projection matrix.
275
 *
276
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
277
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
278
 * @param near Distance between the screen plane and the near plane.
279
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
280
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
281
 */
282
  public void setProjection(float fov, float near)
283
    {
284
    if( fov < 180.0f && fov >=0.0f )
285
      {
286
      mFOV = fov;
287
      }
288

    
289
    if( near<   1.0f && near> 0.0f )
290
      {
291
      mNear= near;
292
      }
293
    else if( near<=0.0f )
294
      {
295
      mNear = 0.01f;
296
      }
297
    else if( near>=1.0f )
298
      {
299
      mNear=0.99f;
300
      }
301

    
302
    createProjection();
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
/**
307
 * Resize the underlying Framebuffer.
308
 * <p>
309
 * This method can be safely called mid-render as it doesn't interfere with rendering.
310
 *
311
 * @param width The new width.
312
 * @param height The new height.
313
 */
314
  public void resize(int width, int height)
315
    {
316
    if( mWidth!=width || mHeight!=height )
317
      {
318
      mWidth = width;
319
      mHeight= height;
320
      mSizeX = width;
321
      mSizeY = height;
322

    
323
      createProjection();
324

    
325
      if( mColorCreated==CREATED )
326
        {
327
        markForCreation();
328
        recreate();
329
        }
330
      }
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
/**
335
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
336
 *
337
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
338
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
339
 */
340
  public void enableDepth(boolean enable)
341
    {
342
    if( enable && mDepthCreated==DONT_CREATE )
343
      {
344
      mDepthCreated = NOT_CREATED_YET;
345
      markForCreation();
346
      }
347
    if( !enable && mDepthCreated!=DONT_CREATE )
348
      {
349
      mDepthCreated = DONT_CREATE;
350
      markForCreation();
351
      }
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
/**
356
 * Return true if the Surface contains a DEPTH attachment.
357
 *
358
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
359
 */
360
  public boolean hasDepth()
361
    {
362
    return mDepthCreated==CREATED;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366
/**
367
 * Adds a new child to the last position in the list of our Surface's children.
368
 * <p>
369
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
370
 * DistortedAttachDeamon (by calling attachNow())
371
 *
372
 * @param node The new Node to add.
373
 */
374
  public void attach(DistortedNode node)
375
    {
376
    DistortedAttachDaemon.attach(this,node);
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
/**
381
 * Adds a new child to the last position in the list of our Surface's children.
382
 * <p>
383
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
384
 * DistortedAttachDeamon (by calling attachNow())
385
 *
386
 * @param surface InputSurface to initialize our child Node with.
387
 * @param effects DistortedEffects to initialize our child Node with.
388
 * @param mesh MeshObject to initialize our child Node with.
389
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
390
 */
391
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
392
    {
393
    DistortedNode node = new DistortedNode(surface,effects,mesh);
394
    DistortedAttachDaemon.attach(this,node);
395
    return node;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399
/**
400
 * This is not really part of the public API. Has to be public only because it is a part of the
401
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
402
 * Java has no multiple inheritance.
403
 *
404
 * @y.exclude
405
 * @param node new Node to add.
406
 */
407
  public void attachNow(DistortedNode node)
408
    {
409
    if( mChildren==null ) mChildren = new ArrayList<>(2);
410
    mChildren.add(node);
411
    mNumChildren++;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415
/**
416
 * Removes the first occurrence of a specified child from the list of children of our Surface.
417
 * <p>
418
 * A bit questionable method as there can be many different Nodes attached as children, some
419
 * of them having the same Effects but - for instance - different Mesh. Use with care.
420
 * <p>
421
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
422
 * DistortedAttachDeamon (by calling detachNow())
423
 *
424
 * @param effects DistortedEffects to remove.
425
 */
426
  public void detach(DistortedEffects effects)
427
    {
428
    long id = effects.getID();
429
    DistortedNode node;
430
    boolean detached = false;
431

    
432
    for(int i=0; i<mNumChildren; i++)
433
      {
434
      node = mChildren.get(i);
435

    
436
      if( node.getEffects().getID()==id )
437
        {
438
        detached = true;
439
        DistortedAttachDaemon.detach(this,node);
440
        break;
441
        }
442
      }
443

    
444
    if( !detached )
445
      {
446
      // if we failed to detach any, it still might be the case that
447
      // there's a job in Daemon's queue that we need to cancel.
448
      DistortedAttachDaemon.cancelAttachJobs(this,effects);
449
      }
450
    }
451

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

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

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
/**
485
 * Removes all children Nodes.
486
 * <p>
487
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
488
 * DistortedAttachDeamon (by calling detachAllNow())
489
 */
490
  public void detachAll()
491
    {
492
    DistortedAttachDaemon.detachAll(this);
493
    }
494

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