Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 54fe333a

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 static int mRender = 0;
31

    
32
  private ArrayList<DistortedNode> mChildren;
33
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
34

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

    
40
  int mDepthCreated;
41
  int[] mDepthH = new int[1];
42
  int[] mFBOH   = new int[1];
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
50
    mProjectionMatrix = new float[16];
51

    
52
    mWidth = width;
53
    mHeight= height;
54

    
55
    mFOV = 60.0f;
56
    mNear=  0.5f;
57

    
58
    mDepthCreated= createDepth;
59
    mFBOH[0]     = fbo;
60
    mDepthH[0]   = 0;
61

    
62
    createProjection();
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

    
77
        float left   = -q*c/2;
78
        float right  = +q*c/2;
79
        float bottom =   -c/2;
80
        float top    =   +c/2;
81
        float near   =    c/a;
82

    
83
        mDistance    = mHeight/a;
84
        float far    = 2*mDistance-near;
85
        mDepth       = (int)((far-near)/2);
86

    
87
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
88
        }
89
      else             // parallel projection
90
        {
91
        float left   = -mWidth /2.0f;
92
        float right  = +mWidth /2.0f;
93
        float bottom = -mHeight/2.0f;
94
        float top    = +mHeight/2.0f;
95
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
96
        mDistance    = mWidth+mHeight;
97
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
98
        mDepth       = (int)((far-near)/2);
99

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
// PUBLIC API
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
/**
109
 * Draws all the attached children to this OutputSurface.
110
 * <p>
111
 * Must be called from a thread holding OpenGL Context.
112
 *
113
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
114
 * @return Number of objects rendered.
115
 */
116
  public int render(long time)
117
    {
118
    mRender++;
119

    
120
    // change tree topology (attach and detach children)
121
/*
122
    boolean changed =
123
*/
124
    DistortedAttachDaemon.toDo();
125
/*
126
    // debugging only
127
    if( changed )
128
      {
129
      for(int i=0; i<mNumChildren; i++)
130
        {
131
        mChildren.get(i).debug(0);
132
        }
133

    
134
      DistortedNode.debugMap();
135
      }
136
*/
137
    // create and delete all underlying OpenGL resources
138
    // Watch out: FIRST change topology, only then deal
139
    // with OpenGL resources. That's because changing Tree
140
    // can result in additional Framebuffers that would need
141
    // to be created immediately, before the calls to drawRecursive()
142
    toDo();
143
/*
144
    // debugging only
145
    if( changed )
146
      {
147
      DistortedSurface.debugLists();
148
      }
149
*/
150
    // mark OpenGL state as unknown
151
    DistortedRenderState.reset();
152

    
153
    int numRenders = 0;
154

    
155
    for(int i=0; i<mNumChildren; i++)
156
      {
157
      numRenders += mChildren.get(i).drawRecursive(mRender,time,this);
158
      }
159

    
160
    return numRenders;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
/**
165
 * Bind this Surface as a Framebuffer we can render to.
166
 */
167
  public void setAsOutput()
168
    {
169
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
/**
174
 * Create new Projection matrix.
175
 *
176
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
177
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
178
 * @param near Distance between the screen plane and the near plane.
179
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
180
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
181
 */
182
  public void setProjection(float fov, float near)
183
    {
184
    if( fov < 180.0f && fov >=0.0f ) mFOV = fov;
185
    if( near<   1.0f && near> 0.0f ) mNear= near;
186

    
187
    createProjection();
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
/**
192
 * Resize the underlying Framebuffer.
193
 * <p>
194
 * This method can be safely called mid-render as it doesn't interfere with rendering.
195
 *
196
 * @param width The new width.
197
 * @param height The new height.
198
 */
199
  public void resize(int width, int height)
200
    {
201
    if( mWidth!=width || mHeight!=height )
202
      {
203
      mWidth = width;
204
      mHeight= height;
205
      mSizeX = width;
206
      mSizeY = height;
207

    
208
      createProjection();
209

    
210
      if( mColorCreated==CREATED )
211
        {
212
        markForCreation();
213
        recreate();
214
        }
215
      }
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
/**
220
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
221
 *
222
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
223
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
224
 */
225
  public void enableDepth(boolean enable)
226
    {
227
    if( enable && mDepthCreated==DONT_CREATE )
228
      {
229
      mDepthCreated = NOT_CREATED_YET;
230
      markForCreation();
231
      }
232
    if( !enable && mDepthCreated!=DONT_CREATE )
233
      {
234
      mDepthCreated = DONT_CREATE;
235
      markForCreation();
236
      }
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
/**
241
 * Return true if the Surface contains a DEPTH attachment.
242
 *
243
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
244
 */
245
  public boolean hasDepth()
246
    {
247
    return mDepthCreated==CREATED;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
/**
252
 * Adds a new child to the last position in the list of our Surface's children.
253
 * <p>
254
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
255
 * DistortedAttachDeamon (by calling attachNow())
256
 *
257
 * @param node The new Node to add.
258
 */
259
  public void attach(DistortedNode node)
260
    {
261
    DistortedAttachDaemon.attach(this,node);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
/**
266
 * Adds a new child to the last position in the list of our Surface's children.
267
 * <p>
268
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
269
 * DistortedAttachDeamon (by calling attachNow())
270
 *
271
 * @param surface InputSurface to initialize our child Node with.
272
 * @param effects DistortedEffects to initialize our child Node with.
273
 * @param mesh MeshObject to initialize our child Node with.
274
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
275
 */
276
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
277
    {
278
    DistortedNode node = new DistortedNode(surface,effects,mesh);
279
    DistortedAttachDaemon.attach(this,node);
280
    return node;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
/**
285
 * This is not really part of the public API. Has to be public only because it is a part of the
286
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
287
 * Java has no multiple inheritance.
288
 *
289
 * @y.exclude
290
 * @param node new Node to add.
291
 */
292
  public void attachNow(DistortedNode node)
293
    {
294
    if( mChildren==null ) mChildren = new ArrayList<>(2);
295
    mChildren.add(node);
296
    mNumChildren++;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
/**
301
 * Removes the first occurrence of a specified child from the list of children of our Surface.
302
 * <p>
303
 * A bit questionable method as there can be many different Nodes attached as children, some
304
 * of them having the same Effects but - for instance - different Mesh. Use with care.
305
 * <p>
306
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
307
 * DistortedAttachDeamon (by calling detachNow())
308
 *
309
 * @param effects DistortedEffects to remove.
310
 */
311
  public void detach(DistortedEffects effects)
312
    {
313
    long id = effects.getID();
314
    DistortedNode node;
315

    
316
    for(int i=0; i<mNumChildren; i++)
317
      {
318
      node = mChildren.get(i);
319

    
320
      if( node.getEffects().getID()==id )
321
        {
322
        DistortedAttachDaemon.detach(this,node);
323
        break;
324
        }
325
      }
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
/**
330
 * Removes the first occurrence of a specified child from the list of children of our Surface.
331
 * <p>
332
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
333
 * DistortedAttachDeamon (by calling detachNow())
334
 *
335
 * @param node The Node to remove.
336
 */
337
  public void detach(DistortedNode node)
338
    {
339
    DistortedAttachDaemon.detach(this,node);
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
/**
344
 * This is not really part of the public API. Has to be public only because it is a part of the
345
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
346
 * Java has no multiple inheritance.
347
 *
348
 * @y.exclude
349
 * @param node The Node to remove.
350
 */
351
  public void detachNow(DistortedNode node)
352
    {
353
    if( mNumChildren>0 && mChildren.remove(node) )
354
      {
355
      mNumChildren--;
356
      }
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
/**
361
 * Removes all children Nodes.
362
 * <p>
363
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
364
 * DistortedAttachDeamon (by calling detachAllNow())
365
 */
366
  public void detachAll()
367
    {
368
    DistortedAttachDaemon.detachAll(this);
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
 */
379
  public void detachAllNow()
380
    {
381
    if( mNumChildren>0 )
382
      {
383
      mNumChildren = 0;
384
      mChildren.clear();
385
      }
386
    }
387
}
(8-8/23)