Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 86782a25

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