Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ b2939df4

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

    
105
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
106
    {
107
    int numRenders = 0;
108

    
109
    for(int i=0; i<num; i++)
110
      {
111
      numRenders += children.get(i).draw(time,this);
112
      }
113

    
114
    return numRenders;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// PUBLIC API
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
/**
121
 * Draws all the attached children to this OutputSurface.
122
 * <p>
123
 * Must be called from a thread holding OpenGL Context.
124
 *
125
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
126
 * @return Number of objects rendered.
127
 */
128
  public int render(long time)
129
    {
130
    // change tree topology (attach and detach children)
131
/*
132
    boolean changed =
133
*/
134
    DistortedAttachDaemon.toDo();
135
/*
136
    // debugging only
137
    if( changed )
138
      {
139
      for(int i=0; i<mNumChildren; i++)
140
        {
141
        mChildren.get(i).debug(0);
142
        }
143

    
144
      DistortedNode.debugMap();
145
      }
146
*/
147
    // create and delete all underlying OpenGL resources
148
    // Watch out: FIRST change topology, only then deal
149
    // with OpenGL resources. That's because changing Tree
150
    // can result in additional Framebuffers that would need
151
    // to be created immediately, before the calls to drawRecursive()
152
    toDo();
153
/*
154
    // debugging only
155
    if( changed )
156
      {
157
      DistortedSurface.debugLists();
158
      }
159
*/
160
    // mark OpenGL state as unknown
161
    DistortedRenderState.reset();
162

    
163
    int numRenders=0;
164

    
165
    for(int i=0; i<mNumChildren; i++)
166
      {
167
      numRenders += mChildren.get(i).renderRecursive(time);
168
      }
169

    
170
    setAsOutput();
171
    numRenders += renderChildren(time,mNumChildren,mChildren);
172

    
173
    return numRenders;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
/**
178
 * Bind this Surface as a Framebuffer we can render to.
179
 */
180
  public void setAsOutput()
181
    {
182
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
/**
187
 * Create new Projection matrix.
188
 *
189
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
190
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
191
 * @param near Distance between the screen plane and the near plane.
192
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
193
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
194
 */
195
  public void setProjection(float fov, float near)
196
    {
197
    if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
198
    if( near<   1.0f && near> 0.0f ) mNear= near;
199

    
200
    createProjection();
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
/**
205
 * Resize the underlying Framebuffer.
206
 * <p>
207
 * This method can be safely called mid-render as it doesn't interfere with rendering.
208
 *
209
 * @param width The new width.
210
 * @param height The new height.
211
 */
212
  public void resize(int width, int height)
213
    {
214
    if( mWidth!=width || mHeight!=height )
215
      {
216
      mWidth = width;
217
      mHeight= height;
218
      mSizeX = width;
219
      mSizeY = height;
220

    
221
      createProjection();
222

    
223
      if( mColorCreated==CREATED )
224
        {
225
        markForCreation();
226
        recreate();
227
        }
228
      }
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
/**
233
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
234
 *
235
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
236
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
237
 */
238
  public void enableDepth(boolean enable)
239
    {
240
    if( enable && mDepthCreated==DONT_CREATE )
241
      {
242
      mDepthCreated = NOT_CREATED_YET;
243
      markForCreation();
244
      }
245
    if( !enable && mDepthCreated!=DONT_CREATE )
246
      {
247
      mDepthCreated = DONT_CREATE;
248
      markForCreation();
249
      }
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
/**
254
 * Return true if the Surface contains a DEPTH attachment.
255
 *
256
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
257
 */
258
  public boolean hasDepth()
259
    {
260
    return mDepthCreated==CREATED;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
/**
265
 * Adds a new child to the last position in the list of our Surface's children.
266
 * <p>
267
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
268
 * DistortedAttachDeamon (by calling attachNow())
269
 *
270
 * @param node The new Node to add.
271
 */
272
  public void attach(DistortedNode node)
273
    {
274
    DistortedAttachDaemon.attach(this,node);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
/**
279
 * Adds a new child to the last position in the list of our Surface's children.
280
 * <p>
281
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
282
 * DistortedAttachDeamon (by calling attachNow())
283
 *
284
 * @param surface InputSurface to initialize our child Node with.
285
 * @param effects DistortedEffects to initialize our child Node with.
286
 * @param mesh MeshObject to initialize our child Node with.
287
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
288
 */
289
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
290
    {
291
    DistortedNode node = new DistortedNode(surface,effects,mesh);
292
    DistortedAttachDaemon.attach(this,node);
293
    return node;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
/**
298
 * This is not really part of the public API. Has to be public only because it is a part of the
299
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
300
 * Java has no multiple inheritance.
301
 *
302
 * @y.exclude
303
 * @param node new Node to add.
304
 */
305
  public void attachNow(DistortedNode node)
306
    {
307
    if( mChildren==null ) mChildren = new ArrayList<>(2);
308
    mChildren.add(node);
309
    mNumChildren++;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
/**
314
 * Removes the first occurrence of a specified child from the list of children of our Surface.
315
 * <p>
316
 * A bit questionable method as there can be many different Nodes attached as children, some
317
 * of them having the same Effects but - for instance - different Mesh. Use with care.
318
 * <p>
319
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
320
 * DistortedAttachDeamon (by calling detachNow())
321
 *
322
 * @param effects DistortedEffects to remove.
323
 */
324
  public void detach(DistortedEffects effects)
325
    {
326
    long id = effects.getID();
327
    DistortedNode node;
328

    
329
    for(int i=0; i<mNumChildren; i++)
330
      {
331
      node = mChildren.get(i);
332

    
333
      if( node.getEffects().getID()==id )
334
        {
335
        DistortedAttachDaemon.detach(this,node);
336
        break;
337
        }
338
      }
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
/**
343
 * Removes the first occurrence of a specified child from the list of children of our Surface.
344
 * <p>
345
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
346
 * DistortedAttachDeamon (by calling detachNow())
347
 *
348
 * @param node The Node to remove.
349
 */
350
  public void detach(DistortedNode node)
351
    {
352
    DistortedAttachDaemon.detach(this,node);
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
/**
357
 * This is not really part of the public API. Has to be public only because it is a part of the
358
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
359
 * Java has no multiple inheritance.
360
 *
361
 * @y.exclude
362
 * @param node The Node to remove.
363
 */
364
  public void detachNow(DistortedNode node)
365
    {
366
    if( mNumChildren>0 && mChildren.remove(node) )
367
      {
368
      mNumChildren--;
369
      }
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
/**
374
 * Removes all children Nodes.
375
 * <p>
376
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
377
 * DistortedAttachDeamon (by calling detachAllNow())
378
 */
379
  public void detachAll()
380
    {
381
    DistortedAttachDaemon.detachAll(this);
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
/**
386
 * This is not really part of the public API. Has to be public only because it is a part of the
387
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
388
 * Java has no multiple inheritance.
389
 *
390
 * @y.exclude
391
 */
392
  public void detachAllNow()
393
    {
394
    if( mNumChildren>0 )
395
      {
396
      mNumChildren = 0;
397
      mChildren.clear();
398
      }
399
    }
400
}
(8-8/23)