Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 95c441a2

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
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  abstract DistortedFramebuffer getBuffer();
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
53
    mProjectionMatrix = new float[16];
54

    
55
    mWidth = width;
56
    mHeight= height;
57

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

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

    
65
    mTime = 0;
66

    
67
    createProjection();
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

    
82
        float left   = -q/2;
83
        float right  = +q/2;
84
        float bottom = -c/2;
85
        float top    = +c/2;
86
        float near   =  c/a;
87

    
88
        mDistance    = mHeight/a;
89
        float far    = 2*mDistance-near;
90
        mDepth       = (int)((far-near)/2);
91

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

    
105
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
106
        }
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
113
    {
114
    int numRenders = 0;
115
    DistortedNode child;
116
    DistortedFramebuffer fbo;
117
    DistortedOutputSurface surface;
118

    
119
    // 1. Render all children that have postprocessing effects to their own buffer FBOs
120

    
121
    for(int i=0; i<num; i++)
122
      {
123
      child = children.get(i);
124
      fbo   = child.getPostprocessingBuffer();
125

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

    
133
    // 2. If we have rendered anything so far, and we are a Screen, then render to an
134
    //    intermediate FBO instead.
135

    
136
    surface = this;//numRenders>0 ? getBuffer() : this;
137

    
138
    // 3. Render all children without postprocessing effects to buffer
139

    
140
    for(int i=0; i<num; i++)
141
      {
142
      child = children.get(i);
143
      fbo   = child.getPostprocessingBuffer();
144

    
145
      if( fbo==null )
146
        {
147
        numRenders += child.draw(time,surface);
148
        }
149
      }
150

    
151
    // 4. For all postprocessing fbo,
152
    //       postprocess fbo
153
    //       merge to buffer
154

    
155
    for(int i=0; i<num; i++)
156
      {
157
      numRenders += children.get(i).postprocess(time,surface);
158
      }
159

    
160
    // 5. finally blit to this if we have to
161

    
162
    if( surface!=this && ((DistortedFramebuffer)surface).setAsInput() )
163
      {
164
      numRenders++;
165
      DistortedEffects.blitPriv(this);
166
      }
167

    
168
    return numRenders;
169
    }
170

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

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

    
217
    int numRenders=0;
218

    
219
    for(int i=0; i<mNumChildren; i++)
220
      {
221
      numRenders += mChildren.get(i).renderRecursive(time);
222
      }
223

    
224
    setAsOutput(time);
225
    numRenders += renderChildren(time,mNumChildren,mChildren);
226

    
227
    return numRenders;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
/**
232
 * Bind this Surface as a Framebuffer we can render to.
233
 */
234
  public void setAsOutput(long time)
235
    {
236
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
237

    
238
    if( mTime!=time )
239
      {
240
      mTime = time;
241
      DistortedRenderState.colorDepthOn();
242
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
243
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
244
      }
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
/**
249
 * Create new Projection matrix.
250
 *
251
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
252
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
253
 * @param near Distance between the screen plane and the near plane.
254
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
255
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
256
 */
257
  public void setProjection(float fov, float near)
258
    {
259
    if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
260
    if( near<   1.0f && near> 0.0f ) mNear= near;
261

    
262
    createProjection();
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
/**
267
 * Resize the underlying Framebuffer.
268
 * <p>
269
 * This method can be safely called mid-render as it doesn't interfere with rendering.
270
 *
271
 * @param width The new width.
272
 * @param height The new height.
273
 */
274
  public void resize(int width, int height)
275
    {
276
    if( mWidth!=width || mHeight!=height )
277
      {
278
      mWidth = width;
279
      mHeight= height;
280
      mSizeX = width;
281
      mSizeY = height;
282

    
283
      createProjection();
284

    
285
      if( mColorCreated==CREATED )
286
        {
287
        markForCreation();
288
        recreate();
289
        }
290
      }
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
/**
295
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
296
 *
297
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
298
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
299
 */
300
  public void enableDepth(boolean enable)
301
    {
302
    if( enable && mDepthCreated==DONT_CREATE )
303
      {
304
      mDepthCreated = NOT_CREATED_YET;
305
      markForCreation();
306
      }
307
    if( !enable && mDepthCreated!=DONT_CREATE )
308
      {
309
      mDepthCreated = DONT_CREATE;
310
      markForCreation();
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
/**
316
 * Return true if the Surface contains a DEPTH attachment.
317
 *
318
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
319
 */
320
  public boolean hasDepth()
321
    {
322
    return mDepthCreated==CREATED;
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 node The new Node to add.
333
 */
334
  public void attach(DistortedNode node)
335
    {
336
    DistortedAttachDaemon.attach(this,node);
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
/**
341
 * Adds a new child to the last position in the list of our Surface's children.
342
 * <p>
343
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
344
 * DistortedAttachDeamon (by calling attachNow())
345
 *
346
 * @param surface InputSurface to initialize our child Node with.
347
 * @param effects DistortedEffects to initialize our child Node with.
348
 * @param mesh MeshObject to initialize our child Node with.
349
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
350
 */
351
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
352
    {
353
    DistortedNode node = new DistortedNode(surface,effects,mesh);
354
    DistortedAttachDaemon.attach(this,node);
355
    return node;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359
/**
360
 * This is not really part of the public API. Has to be public only because it is a part of the
361
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
362
 * Java has no multiple inheritance.
363
 *
364
 * @y.exclude
365
 * @param node new Node to add.
366
 */
367
  public void attachNow(DistortedNode node)
368
    {
369
    if( mChildren==null ) mChildren = new ArrayList<>(2);
370
    mChildren.add(node);
371
    mNumChildren++;
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
/**
376
 * Removes the first occurrence of a specified child from the list of children of our Surface.
377
 * <p>
378
 * A bit questionable method as there can be many different Nodes attached as children, some
379
 * of them having the same Effects but - for instance - different Mesh. Use with care.
380
 * <p>
381
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
382
 * DistortedAttachDeamon (by calling detachNow())
383
 *
384
 * @param effects DistortedEffects to remove.
385
 */
386
  public void detach(DistortedEffects effects)
387
    {
388
    long id = effects.getID();
389
    DistortedNode node;
390

    
391
    for(int i=0; i<mNumChildren; i++)
392
      {
393
      node = mChildren.get(i);
394

    
395
      if( node.getEffects().getID()==id )
396
        {
397
        DistortedAttachDaemon.detach(this,node);
398
        break;
399
        }
400
      }
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404
/**
405
 * Removes the first occurrence of a specified child from the list of children of our Surface.
406
 * <p>
407
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
408
 * DistortedAttachDeamon (by calling detachNow())
409
 *
410
 * @param node The Node to remove.
411
 */
412
  public void detach(DistortedNode node)
413
    {
414
    DistortedAttachDaemon.detach(this,node);
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
/**
419
 * This is not really part of the public API. Has to be public only because it is a part of the
420
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
421
 * Java has no multiple inheritance.
422
 *
423
 * @y.exclude
424
 * @param node The Node to remove.
425
 */
426
  public void detachNow(DistortedNode node)
427
    {
428
    if( mNumChildren>0 && mChildren.remove(node) )
429
      {
430
      mNumChildren--;
431
      }
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
/**
436
 * Removes all children Nodes.
437
 * <p>
438
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
439
 * DistortedAttachDeamon (by calling detachAllNow())
440
 */
441
  public void detachAll()
442
    {
443
    DistortedAttachDaemon.detachAll(this);
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
/**
448
 * This is not really part of the public API. Has to be public only because it is a part of the
449
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
450
 * Java has no multiple inheritance.
451
 *
452
 * @y.exclude
453
 */
454
  public void detachAllNow()
455
    {
456
    if( mNumChildren>0 )
457
      {
458
      mNumChildren = 0;
459
      mChildren.clear();
460
      }
461
    }
462
}
(8-8/23)