Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 27f42cd6

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