Project

General

Profile

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

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

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)
40
    {
41
    super(width,height,color);
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
    if( DistortedAttachDaemon.toDo() )
110
      {
111
      for(int i=0; i<mNumChildren; i++)
112
        {
113
        mChildren.get(i).treeIsomorphism();
114
        }
115
      }
116

    
117
    toDo();
118

    
119
    for(int i=0; i<mNumChildren; i++)
120
      {
121
      mChildren.get(i).drawRecursive(time,this);
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
/**
127
 * Bind this Surface as a Framebuffer we can render to.
128
 */
129
  public abstract void setAsOutput();
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Create new Projection matrix.
134
 *
135
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
136
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
137
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
138
 */
139
  public void setProjection(float fov, float x, float y)
140
    {
141
    mFOV = fov;
142
    mX = x;
143
    mY = y;
144

    
145
    createProjection();
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
/**
150
 * Resize the underlying Framebuffer.
151
 *
152
 * @param width The new width.
153
 * @param height The new height.
154
 */
155
  public void resize(int width, int height)
156
    {
157
    if( mWidth!=width || mHeight!=height )
158
      {
159
      mWidth = width;
160
      mHeight= height;
161
      mSizeX = width;
162
      mSizeY = height;
163

    
164
      createProjection();
165

    
166
      if( mColorH[0]>0 )
167
        {
168
        moveToToDo();
169
        recreate();
170
        }
171
      }
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
/**
176
 * Adds a new child to the last position in the list of our Surface's children.
177
 * <p>
178
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
179
 * DistortedAttachDeamon (by calling attachNow())
180
 *
181
 * @param node The new Node to add.
182
 */
183
  public void attach(DistortedNode node)
184
    {
185
    DistortedAttachDaemon.attach(this,node);
186
    }
187

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
/**
209
 * This is not really part of the public API. Has to be public only because it is a part of the
210
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
211
 * Java has no multiple inheritance.
212
 *
213
 * @param node new Node to add.
214
 */
215
  public void attachNow(DistortedNode node)
216
    {
217
    if( mChildren==null ) mChildren = new ArrayList<>(2);
218
    mChildren.add(node);
219
    mNumChildren++;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
/**
224
 * Removes the first occurrence of a specified child from the list of children of our Surface.
225
 * <p>
226
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
227
 * DistortedAttachDeamon (by calling detachNow())
228
 *
229
 * @param node The Node to remove.
230
 */
231
  public void detach(DistortedNode node)
232
    {
233
    DistortedAttachDaemon.detach(this,node);
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
/**
238
 * This is not really part of the public API. Has to be public only because it is a part of the
239
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
240
 * Java has no multiple inheritance.
241
 *
242
 * @param node The Node to remove.
243
 */
244
  public void detachNow(DistortedNode node)
245
    {
246
    if( mNumChildren>0 && mChildren.remove(node) )
247
      {
248
      mNumChildren--;
249
      }
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
/**
254
 * Removes all children Nodes.
255
 * <p>
256
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
257
 * DistortedAttachDeamon (by calling detachAllNow())
258
 */
259
  public void detachAll()
260
    {
261
    DistortedAttachDaemon.detachAll(this);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
/**
266
 * This is not really part of the public API. Has to be public only because it is a part of the
267
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
268
 * Java has no multiple inheritance.
269
 */
270
  public void detachAllNow()
271
    {
272
    if( mNumChildren>0 )
273
      {
274
      mNumChildren = 0;
275
      mChildren.clear();
276
      }
277
    }
278
}
(8-8/22)