Project

General

Profile

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

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

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
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
 * Draw the (texture,mesh,effects) object to the Framebuffer.
95
 * <p>
96
 * Must be called from a thread holding OpenGL Context.
97
 *
98
 * @param surface InputSurface to skin our Mesh with.
99
 * @param mesh Class descendant from MeshObject
100
 * @param effects The DistortedEffects to use when rendering
101
 * @param time Current time, in milliseconds.
102
 */
103
  public void renderTo(DistortedInputSurface surface, MeshObject mesh, DistortedEffects effects, long time)
104
    {
105
    surface.create();  // Watch out  - this needs to be before
106
    create();          // the 'setAsInput' because this has side-effects!
107

    
108
    if( surface.setAsInput() )
109
      {
110
      DistortedSurface.deleteAllMarked();
111
      effects.drawPriv(surface.getWidth()/2.0f, surface.getHeight()/2.0f, mesh, this, time);
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
/**
117
 * Draws the Tree, and all its children, to the Framebuffer.
118
 * <p>
119
 * Must be called from a thread holding OpenGL Context.
120
 *
121
 * @param dt DistortedNode to render.
122
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the Tree.
123
 */
124
  public void renderTo(DistortedNode dt, long time)
125
    {
126
    DistortedSurface.deleteAllMarked();
127
    create();
128
    dt.drawRecursive(time,this);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Bind this Surface as a Framebuffer we can render to.
134
 */
135
  public abstract void setAsOutput();
136

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

    
151
    createProjection();
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
/**
156
 * Resize the underlying Framebuffer.
157
 *
158
 * @param width The new width.
159
 * @param height The new height.
160
 */
161
  public void resize(int width, int height)
162
    {
163
    if( mWidth!=width || mHeight!=height )
164
      {
165
      mWidth = width;
166
      mHeight= height;
167
      createProjection();
168
      mSizeX = width;
169
      mSizeY = height;
170
      if( mColorH[0]>0 ) markForDeletion();
171
      }
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
/**
176
 * Adds a new child to the last position in the list of our Surface's children.
177
 *
178
 * @param node The new Node to add.
179
 */
180
  public synchronized void attach(DistortedNode node)
181
    {
182
    if( mChildren==null ) mChildren = new ArrayList<>(2);
183
    mChildren.add(node);
184
    mNumChildren++;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
/**
189
 * Adds a new child to the last position in the list of our Surface's children.
190
 *
191
 * @param surface InputSurface to initialize our child Node with.
192
 * @param effects DistortedEffects to initialize our child Node with.
193
 * @param mesh MeshObject to initialize our child Node with.
194
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
195
 */
196
  public synchronized DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
197
    {
198
    if( mChildren==null ) mChildren = new ArrayList<>(2);
199
    DistortedNode node = new DistortedNode(surface,effects,mesh);
200
    mChildren.add(node);
201
    mNumChildren++;
202

    
203
    return node;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
/**
208
 * Removes the first occurrence of a specified child from the list of children of our Surface.
209
 *
210
 * @param node The Node to remove.
211
 * @return <code>true</code> if the child was successfully removed.
212
 */
213
  public synchronized boolean detach(DistortedNode node)
214
    {
215
    if( mChildren.remove(node) )
216
      {
217
      mNumChildren--;
218
      return true;
219
      }
220

    
221
    return false;
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
/**
226
 * Removes all children Nodes.
227
 */
228
  public synchronized void detachAll()
229
    {
230
    mNumChildren = 0;
231
    mChildren.clear();
232
    }
233
}
(6-6/20)