Project

General

Profile

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

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

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