Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 7691a39f

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 a436ccc5 leszek
  boolean mDepthEnabled;
41
  int[] mDepthH = new int[1];
42
  int[] mFBOH   = new int[1];
43
44 af4cc5db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 a436ccc5 leszek
  DistortedOutputSurface(int width, int height, int color, int fbo, boolean system, boolean depth)
47 af4cc5db Leszek Koltunski
    {
48 af27df87 leszek
    super(width,height,color,system);
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 a436ccc5 leszek
    mDepthEnabled= depth;
60
    mFBOH[0]     = fbo;
61
    mDepthH[0]   = color;
62
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
    // create and delete all underlying OpenGL resources
118
    // Watch out: FIRST change topology, only then deal
119
    // with OpenGL resources. That's because changing Tree
120
    // can result in additional Framebuffers that would need
121
    // to be created immediately, before the calls to drawRecursive()
122
123 af27df87 leszek
    boolean changed = DistortedAttachDaemon.toDo();
124
125
    if( changed )
126 c204c69d leszek
      {
127
      for(int i=0; i<mNumChildren; i++)
128
        {
129
        mChildren.get(i).treeIsomorphism();
130 af27df87 leszek
        mChildren.get(i).debug(0);
131 c204c69d leszek
        }
132 af27df87 leszek
133 7691a39f leszek
      DistortedNode.debugMap();
134 c204c69d leszek
      }
135
136 f8377ef8 leszek
    toDo();
137 c5369f1b leszek
138 af27df87 leszek
    if( changed )
139
      {
140
      DistortedSurface.debugLists();
141
      }
142
143 7691a39f leszek
    int numRenders = 0;
144
145 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
146 af4cc5db Leszek Koltunski
      {
147 7691a39f leszek
      numRenders += mChildren.get(i).drawRecursive(mRender,time,this);
148 af4cc5db Leszek Koltunski
      }
149 7691a39f leszek
150
    return numRenders;
151 af4cc5db Leszek Koltunski
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154 c5369f1b leszek
/**
155
 * Bind this Surface as a Framebuffer we can render to.
156
 */
157 a436ccc5 leszek
  public void setAsOutput()
158
    {
159
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
160
161
    if( mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET )
162
      {
163
      GLES30.glEnable(GLES30.GL_DEPTH_TEST);
164
      GLES30.glDepthMask(true);
165
      }
166
    else
167
      {
168
      GLES30.glDisable(GLES30.GL_DEPTH_TEST);
169
      GLES30.glDepthMask(false);
170
      }
171
    }
172 af4cc5db Leszek Koltunski
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
/**
175
 * Create new Projection matrix.
176
 *
177
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
178
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
179
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
180
 */
181
  public void setProjection(float fov, float x, float y)
182
    {
183
    mFOV = fov;
184
    mX = x;
185
    mY = y;
186
187
    createProjection();
188
    }
189
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191 c5369f1b leszek
/**
192 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
193
 *
194
 * @param width The new width.
195
 * @param height The new height.
196 c5369f1b leszek
 */
197 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
198
    {
199
    if( mWidth!=width || mHeight!=height )
200
      {
201
      mWidth = width;
202
      mHeight= height;
203
      mSizeX = width;
204
      mSizeY = height;
205 f8377ef8 leszek
206
      createProjection();
207
208
      if( mColorH[0]>0 )
209
        {
210
        moveToToDo();
211
        recreate();
212
        }
213 af4cc5db Leszek Koltunski
      }
214
    }
215 a09ada4c Leszek Koltunski
216 a436ccc5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
217
/**
218
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
219
 *
220
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
221
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
222
 */
223
  public void enableDepth(boolean enable)
224
    {
225
    if( mDepthEnabled!=enable )
226
      {
227
      mDepthEnabled = enable;
228
      moveToToDo();
229
      }
230
    }
231
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
/**
234
 * Return true if the Surface contains a DEPTH attachment.
235
 *
236
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
237
 */
238
  public boolean hasDepth()
239
    {
240
    return mDepthEnabled;
241
    }
242
243 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
/**
245
 * Adds a new child to the last position in the list of our Surface's children.
246 c204c69d leszek
 * <p>
247
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
248
 * DistortedAttachDeamon (by calling attachNow())
249 a09ada4c Leszek Koltunski
 *
250
 * @param node The new Node to add.
251
 */
252 c204c69d leszek
  public void attach(DistortedNode node)
253 a09ada4c Leszek Koltunski
    {
254 c204c69d leszek
    DistortedAttachDaemon.attach(this,node);
255 a09ada4c Leszek Koltunski
    }
256
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
/**
259
 * Adds a new child to the last position in the list of our Surface's children.
260 c204c69d leszek
 * <p>
261
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
262
 * DistortedAttachDeamon (by calling attachNow())
263 a09ada4c Leszek Koltunski
 *
264
 * @param surface InputSurface to initialize our child Node with.
265
 * @param effects DistortedEffects to initialize our child Node with.
266
 * @param mesh MeshObject to initialize our child Node with.
267
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
268
 */
269 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
270 a09ada4c Leszek Koltunski
    {
271
    DistortedNode node = new DistortedNode(surface,effects,mesh);
272 c204c69d leszek
    DistortedAttachDaemon.attach(this,node);
273
    return node;
274
    }
275
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
/**
278
 * This is not really part of the public API. Has to be public only because it is a part of the
279
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
280
 * Java has no multiple inheritance.
281
 *
282
 * @param node new Node to add.
283
 */
284
  public void attachNow(DistortedNode node)
285
    {
286
    if( mChildren==null ) mChildren = new ArrayList<>(2);
287 a09ada4c Leszek Koltunski
    mChildren.add(node);
288
    mNumChildren++;
289
    }
290
291 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
292
/**
293
 * Removes the first occurrence of a specified child from the list of children of our Surface.
294
 * <p>
295
 * A bit questionable method as there can be many different Nodes attached as children, some
296
 * of them having the same Effects but - for instance - different Mesh. Use with care.
297
 * <p>
298
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
299
 * DistortedAttachDeamon (by calling detachNow())
300
 *
301
 * @param effects DistortedEffects to remove.
302
 */
303
  public void detach(DistortedEffects effects)
304
    {
305
    long id = effects.getID();
306
    DistortedNode node;
307
308
    for(int i=0; i<mNumChildren; i++)
309
      {
310
      node = mChildren.get(i);
311
312
      if( node.getEffects().getID()==id )
313
        {
314
        DistortedAttachDaemon.detach(this,node);
315
        break;
316
        }
317
      }
318
    }
319
320 a09ada4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
321
/**
322
 * Removes the first occurrence of a specified child from the list of children of our Surface.
323 c204c69d leszek
 * <p>
324
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
325
 * DistortedAttachDeamon (by calling detachNow())
326
 *
327
 * @param node The Node to remove.
328
 */
329
  public void detach(DistortedNode node)
330
    {
331
    DistortedAttachDaemon.detach(this,node);
332
    }
333
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
/**
336
 * This is not really part of the public API. Has to be public only because it is a part of the
337
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
338
 * Java has no multiple inheritance.
339 a09ada4c Leszek Koltunski
 *
340
 * @param node The Node to remove.
341
 */
342 c204c69d leszek
  public void detachNow(DistortedNode node)
343 a09ada4c Leszek Koltunski
    {
344 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 && mChildren.remove(node) )
345 a09ada4c Leszek Koltunski
      {
346
      mNumChildren--;
347
      }
348
    }
349
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351
/**
352
 * Removes all children Nodes.
353 c204c69d leszek
 * <p>
354
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
355
 * DistortedAttachDeamon (by calling detachAllNow())
356
 */
357
  public void detachAll()
358
    {
359
    DistortedAttachDaemon.detachAll(this);
360
    }
361
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
/**
364
 * This is not really part of the public API. Has to be public only because it is a part of the
365
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
366
 * Java has no multiple inheritance.
367 a09ada4c Leszek Koltunski
 */
368 c204c69d leszek
  public void detachAllNow()
369 a09ada4c Leszek Koltunski
    {
370 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 )
371
      {
372
      mNumChildren = 0;
373
      mChildren.clear();
374
      }
375 a09ada4c Leszek Koltunski
    }
376 af4cc5db Leszek Koltunski
}