Project

General

Profile

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

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

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 float mFOV;
34
  int mWidth,mHeight,mDepth;
35
  float mDistance, mNear;
36
  float[] mProjectionMatrix;
37

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  abstract DistortedFramebuffer getBuffer();
45

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

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

    
52
    mProjectionMatrix = new float[16];
53

    
54
    mWidth = width;
55
    mHeight= height;
56

    
57
    mFOV = 60.0f;
58
    mNear=  0.5f;
59

    
60
    mDepthCreated= createDepth;
61
    mFBOH[0]     = fbo;
62
    mDepthH[0]   = 0;
63

    
64
    createProjection();
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  void createProjection()
70
    {
71
    if( mWidth>0 && mHeight>1 )
72
      {
73
      if( mFOV>0.0f )  // perspective projection
74
        {
75
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
76
        float q = mWidth*mNear;
77
        float c = mHeight*mNear;
78

    
79
        float left   = -q/2;
80
        float right  = +q/2;
81
        float bottom = -c/2;
82
        float top    = +c/2;
83
        float near   =  c/a;
84

    
85
        mDistance    = mHeight/a;
86
        float far    = 2*mDistance-near;
87
        mDepth       = (int)((far-near)/2);
88

    
89
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
90
        }
91
      else             // parallel projection
92
        {
93
        float left   = -mWidth /2.0f;
94
        float right  = +mWidth /2.0f;
95
        float bottom = -mHeight/2.0f;
96
        float top    = +mHeight/2.0f;
97
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
98
        mDistance    = mWidth+mHeight;
99
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
100
        mDepth       = (int)((far-near)/2);
101

    
102
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
103
        }
104
      }
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
110
    {
111
    int numRenders = 0;
112
    DistortedNode child;
113
    DistortedFramebuffer fbo;
114
    DistortedOutputSurface buffer;
115

    
116
    // 1. Render all children that have postprocessing effects to their own buffer FBOs
117

    
118
    for(int i=0; i<num; i++)
119
      {
120
      child = children.get(i);
121
      fbo   = child.getPostprocessingBuffer();
122

    
123
      if( fbo!=null )
124
        {
125
        fbo.resizeFast(mWidth,mHeight);
126
        numRenders += child.draw(time,fbo);
127
        }
128
      }
129

    
130
    // 2. If we have rendered anything so far, and we are a Screen, then render to an
131
    //    intermediate FBO instead.
132

    
133
    buffer = numRenders>0 ? getBuffer() : this;
134

    
135
    // 3. Render all children without postprocessing effects to buffer
136

    
137
    for(int i=0; i<num; i++)
138
      {
139
      child = children.get(i);
140
      fbo   = child.getPostprocessingBuffer();
141

    
142
      if( fbo==null )
143
        {
144
        numRenders += child.draw(time,buffer);
145
        }
146
      }
147

    
148
    // 4. For all postprocessing fbo,
149
    //       postprocess fbo
150
    //       merge to buffer
151

    
152
    // TODO
153

    
154
    // 5. finally blit to this if we have to
155

    
156
    if( buffer!=this && ((DistortedFramebuffer)buffer).setAsInput() )
157
      {
158
      numRenders++;
159
      DistortedEffects.blitPriv(this);
160
      }
161

    
162
    return numRenders;
163
    }
164

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

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

    
211
    int numRenders=0;
212

    
213
    for(int i=0; i<mNumChildren; i++)
214
      {
215
      numRenders += mChildren.get(i).renderRecursive(time);
216
      }
217

    
218
    setAsOutput();
219
    numRenders += renderChildren(time,mNumChildren,mChildren);
220

    
221
    return numRenders;
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
/**
226
 * Bind this Surface as a Framebuffer we can render to.
227
 */
228
  public void setAsOutput()
229
    {
230
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
/**
235
 * Create new Projection matrix.
236
 *
237
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
238
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
239
 * @param near Distance between the screen plane and the near plane.
240
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
241
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
242
 */
243
  public void setProjection(float fov, float near)
244
    {
245
    if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
246
    if( near<   1.0f && near> 0.0f ) mNear= near;
247

    
248
    createProjection();
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
/**
253
 * Resize the underlying Framebuffer.
254
 * <p>
255
 * This method can be safely called mid-render as it doesn't interfere with rendering.
256
 *
257
 * @param width The new width.
258
 * @param height The new height.
259
 */
260
  public void resize(int width, int height)
261
    {
262
    if( mWidth!=width || mHeight!=height )
263
      {
264
      mWidth = width;
265
      mHeight= height;
266
      mSizeX = width;
267
      mSizeY = height;
268

    
269
      createProjection();
270

    
271
      if( mColorCreated==CREATED )
272
        {
273
        markForCreation();
274
        recreate();
275
        }
276
      }
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280
/**
281
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
282
 *
283
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
284
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
285
 */
286
  public void enableDepth(boolean enable)
287
    {
288
    if( enable && mDepthCreated==DONT_CREATE )
289
      {
290
      mDepthCreated = NOT_CREATED_YET;
291
      markForCreation();
292
      }
293
    if( !enable && mDepthCreated!=DONT_CREATE )
294
      {
295
      mDepthCreated = DONT_CREATE;
296
      markForCreation();
297
      }
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
/**
302
 * Return true if the Surface contains a DEPTH attachment.
303
 *
304
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
305
 */
306
  public boolean hasDepth()
307
    {
308
    return mDepthCreated==CREATED;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
/**
313
 * Adds a new child to the last position in the list of our Surface's children.
314
 * <p>
315
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
316
 * DistortedAttachDeamon (by calling attachNow())
317
 *
318
 * @param node The new Node to add.
319
 */
320
  public void attach(DistortedNode node)
321
    {
322
    DistortedAttachDaemon.attach(this,node);
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
/**
327
 * Adds a new child to the last position in the list of our Surface's children.
328
 * <p>
329
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
330
 * DistortedAttachDeamon (by calling attachNow())
331
 *
332
 * @param surface InputSurface to initialize our child Node with.
333
 * @param effects DistortedEffects to initialize our child Node with.
334
 * @param mesh MeshObject to initialize our child Node with.
335
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
336
 */
337
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
338
    {
339
    DistortedNode node = new DistortedNode(surface,effects,mesh);
340
    DistortedAttachDaemon.attach(this,node);
341
    return node;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
/**
346
 * This is not really part of the public API. Has to be public only because it is a part of the
347
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
348
 * Java has no multiple inheritance.
349
 *
350
 * @y.exclude
351
 * @param node new Node to add.
352
 */
353
  public void attachNow(DistortedNode node)
354
    {
355
    if( mChildren==null ) mChildren = new ArrayList<>(2);
356
    mChildren.add(node);
357
    mNumChildren++;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
/**
362
 * Removes the first occurrence of a specified child from the list of children of our Surface.
363
 * <p>
364
 * A bit questionable method as there can be many different Nodes attached as children, some
365
 * of them having the same Effects but - for instance - different Mesh. Use with care.
366
 * <p>
367
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
368
 * DistortedAttachDeamon (by calling detachNow())
369
 *
370
 * @param effects DistortedEffects to remove.
371
 */
372
  public void detach(DistortedEffects effects)
373
    {
374
    long id = effects.getID();
375
    DistortedNode node;
376

    
377
    for(int i=0; i<mNumChildren; i++)
378
      {
379
      node = mChildren.get(i);
380

    
381
      if( node.getEffects().getID()==id )
382
        {
383
        DistortedAttachDaemon.detach(this,node);
384
        break;
385
        }
386
      }
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
/**
391
 * Removes the first occurrence of a specified child from the list of children of our Surface.
392
 * <p>
393
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
394
 * DistortedAttachDeamon (by calling detachNow())
395
 *
396
 * @param node The Node to remove.
397
 */
398
  public void detach(DistortedNode node)
399
    {
400
    DistortedAttachDaemon.detach(this,node);
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404
/**
405
 * This is not really part of the public API. Has to be public only because it is a part of the
406
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
407
 * Java has no multiple inheritance.
408
 *
409
 * @y.exclude
410
 * @param node The Node to remove.
411
 */
412
  public void detachNow(DistortedNode node)
413
    {
414
    if( mNumChildren>0 && mChildren.remove(node) )
415
      {
416
      mNumChildren--;
417
      }
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
/**
422
 * Removes all children Nodes.
423
 * <p>
424
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
425
 * DistortedAttachDeamon (by calling detachAllNow())
426
 */
427
  public void detachAll()
428
    {
429
    DistortedAttachDaemon.detachAll(this);
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
/**
434
 * This is not really part of the public API. Has to be public only because it is a part of the
435
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
436
 * Java has no multiple inheritance.
437
 *
438
 * @y.exclude
439
 */
440
  public void detachAllNow()
441
    {
442
    if( mNumChildren>0 )
443
      {
444
      mNumChildren = 0;
445
      mChildren.clear();
446
      }
447
    }
448
}
(8-8/23)