Project

General

Profile

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

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

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.Matrix;
23
import java.util.ArrayList;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

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

    
32
  private float mX, mY, mFOV;
33
  int mWidth,mHeight,mDepth;
34
  float mDistance;
35
  float[] mProjectionMatrix;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  DistortedOutputSurface(int width, int height, int color, boolean system)
40
    {
41
    super(width,height,color,system);
42

    
43
    mProjectionMatrix = new float[16];
44

    
45
    mWidth = width;
46
    mHeight= height;
47

    
48
    mFOV = 60.0f;
49
    mX   =  0.0f;
50
    mY   =  0.0f;
51

    
52
    createProjection();
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  void createProjection()
58
    {
59
    if( mWidth>0 && mHeight>1 )
60
      {
61
      if( mFOV>0.0f )  // perspective projection
62
        {
63
        float left   = (-mX-mWidth /2.0f)/mHeight;
64
        float right  = (-mX+mWidth /2.0f)/mHeight;
65
        float bottom = (-mY-mHeight/2.0f)/mHeight;
66
        float top    = (-mY+mHeight/2.0f)/mHeight;
67
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
68
        mDistance    = mHeight*near/(top-bottom);
69
        float far    = 2*mDistance-near;
70
        mDepth       = (int)((far-near)/2);
71

    
72
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
73
        }
74
      else             // parallel projection
75
        {
76
        float left   = -mX-mWidth /2.0f;
77
        float right  = -mX+mWidth /2.0f;
78
        float bottom = -mY-mHeight/2.0f;
79
        float top    = -mY+mHeight/2.0f;
80
        float near   = (mWidth+mHeight)/2;
81
        mDistance    = 2*near;
82
        float far    = 3*near;
83
        mDepth       = (int)near;
84

    
85
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
86
        }
87
      }
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
// PUBLIC API
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
/**
94
 * Draws all the attached children to this OutputSurface.
95
 * <p>
96
 * Must be called from a thread holding OpenGL Context.
97
 *
98
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
99
 */
100
  public void render(long time)
101
    {
102
    // change tree topology (attach and detach children)
103
    // create and delete all underlying OpenGL resources
104
    // Watch out: FIRST change topology, only then deal
105
    // with OpenGL resources. That's because changing Tree
106
    // can result in additional Framebuffers that would need
107
    // to be created immediately, before the calls to drawRecursive()
108

    
109
    boolean changed = DistortedAttachDaemon.toDo();
110

    
111
    if( changed )
112
      {
113
      for(int i=0; i<mNumChildren; i++)
114
        {
115
        mChildren.get(i).treeIsomorphism();
116
        mChildren.get(i).debug(0);
117
        }
118

    
119
      //DistortedNode.debugMap();
120
      }
121

    
122
    toDo();
123

    
124
    if( changed )
125
      {
126
      DistortedSurface.debugLists();
127
      }
128

    
129
    for(int i=0; i<mNumChildren; i++)
130
      {
131
      mChildren.get(i).drawRecursive(time,this);
132
      }
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/**
137
 * Bind this Surface as a Framebuffer we can render to.
138
 */
139
  public abstract void setAsOutput();
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
/**
143
 * Create new Projection matrix.
144
 *
145
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
146
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
147
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
148
 */
149
  public void setProjection(float fov, float x, float y)
150
    {
151
    mFOV = fov;
152
    mX = x;
153
    mY = y;
154

    
155
    createProjection();
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
/**
160
 * Resize the underlying Framebuffer.
161
 *
162
 * @param width The new width.
163
 * @param height The new height.
164
 */
165
  public void resize(int width, int height)
166
    {
167
    if( mWidth!=width || mHeight!=height )
168
      {
169
      mWidth = width;
170
      mHeight= height;
171
      mSizeX = width;
172
      mSizeY = height;
173

    
174
      createProjection();
175

    
176
      if( mColorH[0]>0 )
177
        {
178
        moveToToDo();
179
        recreate();
180
        }
181
      }
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
/**
186
 * Adds a new child to the last position in the list of our Surface's children.
187
 * <p>
188
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
189
 * DistortedAttachDeamon (by calling attachNow())
190
 *
191
 * @param node The new Node to add.
192
 */
193
  public void attach(DistortedNode node)
194
    {
195
    DistortedAttachDaemon.attach(this,node);
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
/**
200
 * Adds a new child to the last position in the list of our Surface's children.
201
 * <p>
202
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
203
 * DistortedAttachDeamon (by calling attachNow())
204
 *
205
 * @param surface InputSurface to initialize our child Node with.
206
 * @param effects DistortedEffects to initialize our child Node with.
207
 * @param mesh MeshObject to initialize our child Node with.
208
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
209
 */
210
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
211
    {
212
    DistortedNode node = new DistortedNode(surface,effects,mesh);
213
    DistortedAttachDaemon.attach(this,node);
214
    return node;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
/**
219
 * This is not really part of the public API. Has to be public only because it is a part of the
220
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
221
 * Java has no multiple inheritance.
222
 *
223
 * @param node new Node to add.
224
 */
225
  public void attachNow(DistortedNode node)
226
    {
227
    if( mChildren==null ) mChildren = new ArrayList<>(2);
228
    mChildren.add(node);
229
    mNumChildren++;
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
/**
234
 * Removes the first occurrence of a specified child from the list of children of our Surface.
235
 * <p>
236
 * A bit questionable method as there can be many different Nodes attached as children, some
237
 * of them having the same Effects but - for instance - different Mesh. Use with care.
238
 * <p>
239
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
240
 * DistortedAttachDeamon (by calling detachNow())
241
 *
242
 * @param effects DistortedEffects to remove.
243
 */
244
  public void detach(DistortedEffects effects)
245
    {
246
    long id = effects.getID();
247
    DistortedNode node;
248

    
249
    for(int i=0; i<mNumChildren; i++)
250
      {
251
      node = mChildren.get(i);
252

    
253
      if( node.getEffects().getID()==id )
254
        {
255
        DistortedAttachDaemon.detach(this,node);
256
        break;
257
        }
258
      }
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
/**
263
 * Removes the first occurrence of a specified child from the list of children of our Surface.
264
 * <p>
265
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
266
 * DistortedAttachDeamon (by calling detachNow())
267
 *
268
 * @param node The Node to remove.
269
 */
270
  public void detach(DistortedNode node)
271
    {
272
    DistortedAttachDaemon.detach(this,node);
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
/**
277
 * This is not really part of the public API. Has to be public only because it is a part of the
278
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
279
 * Java has no multiple inheritance.
280
 *
281
 * @param node The Node to remove.
282
 */
283
  public void detachNow(DistortedNode node)
284
    {
285
    if( mNumChildren>0 && mChildren.remove(node) )
286
      {
287
      mNumChildren--;
288
      }
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
/**
293
 * Removes all children Nodes.
294
 * <p>
295
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
296
 * DistortedAttachDeamon (by calling detachAllNow())
297
 */
298
  public void detachAll()
299
    {
300
    DistortedAttachDaemon.detachAll(this);
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
/**
305
 * This is not really part of the public API. Has to be public only because it is a part of the
306
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
307
 * Java has no multiple inheritance.
308
 */
309
  public void detachAllNow()
310
    {
311
    if( mNumChildren>0 )
312
      {
313
      mNumChildren = 0;
314
      mChildren.clear();
315
      }
316
    }
317
}
(8-8/22)