Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24
import java.util.ArrayList;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedAttacheable
29
{
30
  private ArrayList<DistortedNode> mChildren;
31
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
32

    
33
  private float mFOV;
34
  int mWidth,mHeight,mDepth;
35
  float mDistance, mNear;
36
  float[] mProjectionMatrix;
37

    
38
  int mDepthCreated;
39
  int[] mDepthH = new int[1];
40
  int[] mFBOH   = new int[1];
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
45
    {
46
    super(width,height,createColor,type);
47

    
48
    mProjectionMatrix = new float[16];
49

    
50
    mWidth = width;
51
    mHeight= height;
52

    
53
    mFOV = 60.0f;
54
    mNear=  0.5f;
55

    
56
    mDepthCreated= createDepth;
57
    mFBOH[0]     = fbo;
58
    mDepthH[0]   = 0;
59

    
60
    createProjection();
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  void createProjection()
66
    {
67
    if( mWidth>0 && mHeight>1 )
68
      {
69
      if( mFOV>0.0f )  // perspective projection
70
        {
71
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
72
        float q = mWidth*mNear;
73
        float c = mHeight*mNear;
74

    
75
        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

    
81
        mDistance    = mHeight/a;
82
        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
        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

    
98
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
99
        }
100
      }
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
// PUBLIC API
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
/**
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
  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
/**
140
 * Draws all the attached children to this OutputSurface.
141
 * <p>
142
 * Must be called from a thread holding OpenGL Context.
143
 *
144
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
145
 * @return Number of objects rendered.
146
 */
147
  public int renderOld(long time)
148
    {
149
    // change tree topology (attach and detach children)
150
/*
151
    boolean changed =
152
*/
153
    DistortedAttachDaemon.toDo();
154
/*
155
    // debugging only
156
    if( changed )
157
      {
158
      for(int i=0; i<mNumChildren; i++)
159
        {
160
        mChildren.get(i).debug(0);
161
        }
162

    
163
      DistortedNode.debugMap();
164
      }
165
*/
166
    // 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
    toDo();
172
/*
173
    // debugging only
174
    if( changed )
175
      {
176
      DistortedSurface.debugLists();
177
      }
178
*/
179
    // mark OpenGL state as unknown
180
    DistortedRenderState.reset();
181

    
182
    int numRenders = 0;
183

    
184
    for(int i=0; i<mNumChildren; i++)
185
      {
186
      numRenders += mChildren.get(i).drawRecursive(time,this);
187
      }
188

    
189
    return numRenders;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
/**
194
 * Bind this Surface as a Framebuffer we can render to.
195
 */
196
  public void setAsOutput()
197
    {
198
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
/**
203
 * Create new Projection matrix.
204
 *
205
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
206
 *            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
 */
211
  public void setProjection(float fov, float near)
212
    {
213
    if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
214
    if( near<   1.0f && near> 0.0f ) mNear= near;
215

    
216
    createProjection();
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
/**
221
 * Resize the underlying Framebuffer.
222
 * <p>
223
 * This method can be safely called mid-render as it doesn't interfere with rendering.
224
 *
225
 * @param width The new width.
226
 * @param height The new height.
227
 */
228
  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

    
237
      createProjection();
238

    
239
      if( mColorCreated==CREATED )
240
        {
241
        markForCreation();
242
        recreate();
243
        }
244
      }
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    if( enable && mDepthCreated==DONT_CREATE )
257
      {
258
      mDepthCreated = NOT_CREATED_YET;
259
      markForCreation();
260
      }
261
    if( !enable && mDepthCreated!=DONT_CREATE )
262
      {
263
      mDepthCreated = DONT_CREATE;
264
      markForCreation();
265
      }
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
    return mDepthCreated==CREATED;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280
/**
281
 * Adds a new child to the last position in the list of our Surface's children.
282
 * <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
 *
286
 * @param node The new Node to add.
287
 */
288
  public void attach(DistortedNode node)
289
    {
290
    DistortedAttachDaemon.attach(this,node);
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
/**
295
 * Adds a new child to the last position in the list of our Surface's children.
296
 * <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
 *
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
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
306
    {
307
    DistortedNode node = new DistortedNode(surface,effects,mesh);
308
    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
 * @y.exclude
319
 * @param node new Node to add.
320
 */
321
  public void attachNow(DistortedNode node)
322
    {
323
    if( mChildren==null ) mChildren = new ArrayList<>(2);
324
    mChildren.add(node);
325
    mNumChildren++;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
358
/**
359
 * Removes the first occurrence of a specified child from the list of children of our Surface.
360
 * <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
 *
377
 * @y.exclude
378
 * @param node The Node to remove.
379
 */
380
  public void detachNow(DistortedNode node)
381
    {
382
    if( mNumChildren>0 && mChildren.remove(node) )
383
      {
384
      mNumChildren--;
385
      }
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
/**
390
 * Removes all children Nodes.
391
 * <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
 *
406
 * @y.exclude
407
 */
408
  public void detachAllNow()
409
    {
410
    if( mNumChildren>0 )
411
      {
412
      mNumChildren = 0;
413
      mChildren.clear();
414
      }
415
    }
416
}
(8-8/23)