Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 39086ebb

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