Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 2ed1c692

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 7691a39f leszek
  private static int mRender = 0;
31
32 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
33
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
34
35 af4cc5db Leszek Koltunski
  private float mX, mY, mFOV;
36
  int mWidth,mHeight,mDepth;
37
  float mDistance;
38
  float[] mProjectionMatrix;
39
40 c7da4e65 leszek
  int mDepthCreated;
41 a436ccc5 leszek
  int[] mDepthH = new int[1];
42
  int[] mFBOH   = new int[1];
43
44 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 09ab7524 Leszek Koltunski
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
47 af4cc5db Leszek Koltunski
    {
48 09ab7524 Leszek Koltunski
    super(width,height,createColor,type);
49 af4cc5db Leszek Koltunski
50
    mProjectionMatrix = new float[16];
51
52
    mWidth = width;
53
    mHeight= height;
54
55
    mFOV = 60.0f;
56
    mX   =  0.0f;
57
    mY   =  0.0f;
58
59 c7da4e65 leszek
    mDepthCreated= createDepth;
60 a436ccc5 leszek
    mFBOH[0]     = fbo;
61 c7da4e65 leszek
    mDepthH[0]   = 0;
62 a436ccc5 leszek
63 af4cc5db Leszek Koltunski
    createProjection();
64
    }
65
66 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 af4cc5db Leszek Koltunski
  void createProjection()
69
    {
70
    if( mWidth>0 && mHeight>1 )
71
      {
72
      if( mFOV>0.0f )  // perspective projection
73
        {
74
        float left   = (-mX-mWidth /2.0f)/mHeight;
75
        float right  = (-mX+mWidth /2.0f)/mHeight;
76
        float bottom = (-mY-mHeight/2.0f)/mHeight;
77
        float top    = (-mY+mHeight/2.0f)/mHeight;
78
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
79
        mDistance    = mHeight*near/(top-bottom);
80
        float far    = 2*mDistance-near;
81
        mDepth       = (int)((far-near)/2);
82
83
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
84
        }
85
      else             // parallel projection
86
        {
87
        float left   = -mX-mWidth /2.0f;
88
        float right  = -mX+mWidth /2.0f;
89
        float bottom = -mY-mHeight/2.0f;
90
        float top    = -mY+mHeight/2.0f;
91
        float near   = (mWidth+mHeight)/2;
92
        mDistance    = 2*near;
93
        float far    = 3*near;
94
        mDepth       = (int)near;
95
96
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
97
        }
98
      }
99
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// PUBLIC API
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104 c5369f1b leszek
/**
105 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
106 af4cc5db Leszek Koltunski
 * <p>
107
 * Must be called from a thread holding OpenGL Context.
108
 *
109 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
110 7691a39f leszek
 * @return Number of objects rendered.
111 c5369f1b leszek
 */
112 7691a39f leszek
  public int render(long time)
113 af4cc5db Leszek Koltunski
    {
114 7691a39f leszek
    mRender++;
115
116 c204c69d leszek
    // change tree topology (attach and detach children)
117 889cce10 Leszek Koltunski
/*
118
    boolean changed =
119
*/
120
    DistortedAttachDaemon.toDo();
121 eadf0859 leszek
/*
122 f28fffc2 Leszek Koltunski
    // debugging only
123 af27df87 leszek
    if( changed )
124 c204c69d leszek
      {
125
      for(int i=0; i<mNumChildren; i++)
126
        {
127 af27df87 leszek
        mChildren.get(i).debug(0);
128 c204c69d leszek
        }
129 af27df87 leszek
130 7691a39f leszek
      DistortedNode.debugMap();
131 c204c69d leszek
      }
132 eadf0859 leszek
*/
133 09ab7524 Leszek Koltunski
    // create and delete all underlying OpenGL resources
134
    // Watch out: FIRST change topology, only then deal
135
    // with OpenGL resources. That's because changing Tree
136
    // can result in additional Framebuffers that would need
137
    // to be created immediately, before the calls to drawRecursive()
138 f8377ef8 leszek
    toDo();
139 eadf0859 leszek
/*
140 f28fffc2 Leszek Koltunski
    // debugging only
141 af27df87 leszek
    if( changed )
142
      {
143
      DistortedSurface.debugLists();
144
      }
145 eadf0859 leszek
*/
146 2ed1c692 leszek
    GLES30.glDepthFunc(GLES30.GL_LEQUAL);
147
    GLES30.glEnable(GLES30.GL_BLEND);
148
    GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
149
    GLES30.glDisable(GLES30.GL_CULL_FACE);
150
151 7691a39f leszek
    int numRenders = 0;
152
153 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
154 af4cc5db Leszek Koltunski
      {
155 7691a39f leszek
      numRenders += mChildren.get(i).drawRecursive(mRender,time,this);
156 af4cc5db Leszek Koltunski
      }
157 7691a39f leszek
158
    return numRenders;
159 af4cc5db Leszek Koltunski
    }
160
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162 c5369f1b leszek
/**
163
 * Bind this Surface as a Framebuffer we can render to.
164
 */
165 a436ccc5 leszek
  public void setAsOutput()
166
    {
167
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
168
169 c7da4e65 leszek
    if( mDepthCreated==CREATED )
170 a436ccc5 leszek
      {
171
      GLES30.glEnable(GLES30.GL_DEPTH_TEST);
172
      GLES30.glDepthMask(true);
173
      }
174
    else
175
      {
176
      GLES30.glDisable(GLES30.GL_DEPTH_TEST);
177
      GLES30.glDepthMask(false);
178
      }
179
    }
180 af4cc5db Leszek Koltunski
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
/**
183
 * Create new Projection matrix.
184
 *
185
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
186
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
187
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
188
 */
189
  public void setProjection(float fov, float x, float y)
190
    {
191
    mFOV = fov;
192
    mX = x;
193
    mY = y;
194
195
    createProjection();
196
    }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199 c5369f1b leszek
/**
200 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
201 c7da4e65 leszek
 * <p>
202
 * This method can be safely called mid-render as it doesn't interfere with rendering.
203 af4cc5db Leszek Koltunski
 *
204
 * @param width The new width.
205
 * @param height The new height.
206 c5369f1b leszek
 */
207 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
208
    {
209
    if( mWidth!=width || mHeight!=height )
210
      {
211
      mWidth = width;
212
      mHeight= height;
213
      mSizeX = width;
214
      mSizeY = height;
215 f8377ef8 leszek
216
      createProjection();
217
218 c7da4e65 leszek
      if( mColorCreated==CREATED )
219 f8377ef8 leszek
        {
220 f28fffc2 Leszek Koltunski
        markForCreation();
221 f8377ef8 leszek
        recreate();
222
        }
223 af4cc5db Leszek Koltunski
      }
224
    }
225 a09ada4c Leszek Koltunski
226 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
227
/**
228
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
229
 *
230
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
231
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
232
 */
233
  public void enableDepth(boolean enable)
234
    {
235 c7da4e65 leszek
    if( enable && mDepthCreated==DONT_CREATE )
236
      {
237
      mDepthCreated = NOT_CREATED_YET;
238 f28fffc2 Leszek Koltunski
      markForCreation();
239 c7da4e65 leszek
      }
240
    if( !enable && mDepthCreated!=DONT_CREATE )
241 a436ccc5 leszek
      {
242 c7da4e65 leszek
      mDepthCreated = DONT_CREATE;
243 f28fffc2 Leszek Koltunski
      markForCreation();
244 a436ccc5 leszek
      }
245
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
/**
249
 * Return true if the Surface contains a DEPTH attachment.
250
 *
251
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
252
 */
253
  public boolean hasDepth()
254
    {
255 c7da4e65 leszek
    return mDepthCreated==CREATED;
256 a436ccc5 leszek
    }
257
258 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Adds a new child to the last position in the list of our Surface's children.
261 c204c69d leszek
 * <p>
262
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
263
 * DistortedAttachDeamon (by calling attachNow())
264 a09ada4c Leszek Koltunski
 *
265
 * @param node The new Node to add.
266
 */
267 c204c69d leszek
  public void attach(DistortedNode node)
268 a09ada4c Leszek Koltunski
    {
269 c204c69d leszek
    DistortedAttachDaemon.attach(this,node);
270 a09ada4c Leszek Koltunski
    }
271
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
/**
274
 * Adds a new child to the last position in the list of our Surface's children.
275 c204c69d leszek
 * <p>
276
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
277
 * DistortedAttachDeamon (by calling attachNow())
278 a09ada4c Leszek Koltunski
 *
279
 * @param surface InputSurface to initialize our child Node with.
280
 * @param effects DistortedEffects to initialize our child Node with.
281
 * @param mesh MeshObject to initialize our child Node with.
282
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
283
 */
284 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
285 a09ada4c Leszek Koltunski
    {
286
    DistortedNode node = new DistortedNode(surface,effects,mesh);
287 c204c69d leszek
    DistortedAttachDaemon.attach(this,node);
288
    return node;
289
    }
290
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
/**
293
 * This is not really part of the public API. Has to be public only because it is a part of the
294
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
295
 * Java has no multiple inheritance.
296
 *
297 43fbf0dd leszek
 * @y.exclude
298 c204c69d leszek
 * @param node new Node to add.
299
 */
300
  public void attachNow(DistortedNode node)
301
    {
302
    if( mChildren==null ) mChildren = new ArrayList<>(2);
303 a09ada4c Leszek Koltunski
    mChildren.add(node);
304
    mNumChildren++;
305
    }
306
307 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
308
/**
309
 * Removes the first occurrence of a specified child from the list of children of our Surface.
310
 * <p>
311
 * A bit questionable method as there can be many different Nodes attached as children, some
312
 * of them having the same Effects but - for instance - different Mesh. Use with care.
313
 * <p>
314
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
315
 * DistortedAttachDeamon (by calling detachNow())
316
 *
317
 * @param effects DistortedEffects to remove.
318
 */
319
  public void detach(DistortedEffects effects)
320
    {
321
    long id = effects.getID();
322
    DistortedNode node;
323
324
    for(int i=0; i<mNumChildren; i++)
325
      {
326
      node = mChildren.get(i);
327
328
      if( node.getEffects().getID()==id )
329
        {
330
        DistortedAttachDaemon.detach(this,node);
331
        break;
332
        }
333
      }
334
    }
335
336 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
337
/**
338
 * Removes the first occurrence of a specified child from the list of children of our Surface.
339 c204c69d leszek
 * <p>
340
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
341
 * DistortedAttachDeamon (by calling detachNow())
342
 *
343
 * @param node The Node to remove.
344
 */
345
  public void detach(DistortedNode node)
346
    {
347
    DistortedAttachDaemon.detach(this,node);
348
    }
349
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351
/**
352
 * This is not really part of the public API. Has to be public only because it is a part of the
353
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
354
 * Java has no multiple inheritance.
355 a09ada4c Leszek Koltunski
 *
356 43fbf0dd leszek
 * @y.exclude
357 a09ada4c Leszek Koltunski
 * @param node The Node to remove.
358
 */
359 c204c69d leszek
  public void detachNow(DistortedNode node)
360 a09ada4c Leszek Koltunski
    {
361 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 && mChildren.remove(node) )
362 a09ada4c Leszek Koltunski
      {
363
      mNumChildren--;
364
      }
365
    }
366
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
/**
369
 * Removes all children Nodes.
370 c204c69d leszek
 * <p>
371
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
372
 * DistortedAttachDeamon (by calling detachAllNow())
373
 */
374
  public void detachAll()
375
    {
376
    DistortedAttachDaemon.detachAll(this);
377
    }
378
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
/**
381
 * This is not really part of the public API. Has to be public only because it is a part of the
382
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
383
 * Java has no multiple inheritance.
384 43fbf0dd leszek
 *
385
 * @y.exclude
386 a09ada4c Leszek Koltunski
 */
387 c204c69d leszek
  public void detachAllNow()
388 a09ada4c Leszek Koltunski
    {
389 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 )
390
      {
391
      mNumChildren = 0;
392
      mChildren.clear();
393
      }
394 a09ada4c Leszek Koltunski
    }
395 af4cc5db Leszek Koltunski
}