Project

General

Profile

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

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

1 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 af4cc5db Leszek Koltunski
import android.opengl.Matrix;
23 a09ada4c Leszek Koltunski
import java.util.ArrayList;
24 af4cc5db Leszek Koltunski
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
27
abstract class DistortedOutputSurface extends DistortedSurface
28
{
29 a09ada4c Leszek Koltunski
  private ArrayList<DistortedNode> mChildren;
30
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
31
32 af4cc5db Leszek Koltunski
  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 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 af4cc5db Leszek Koltunski
  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 c5369f1b leszek
/**
94 d9706fd2 Leszek Koltunski
 * Draws all the attached children to this OutputSurface.
95 af4cc5db Leszek Koltunski
 * <p>
96
 * Must be called from a thread holding OpenGL Context.
97
 *
98 d9706fd2 Leszek Koltunski
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
99 c5369f1b leszek
 */
100 d9706fd2 Leszek Koltunski
  public void render(long time)
101 af4cc5db Leszek Koltunski
    {
102 f8377ef8 leszek
    toDo();
103 c5369f1b leszek
104 d9706fd2 Leszek Koltunski
    for(int i=0; i<mNumChildren; i++)
105 af4cc5db Leszek Koltunski
      {
106 d9706fd2 Leszek Koltunski
      mChildren.get(i).drawRecursive(time,this);
107 af4cc5db Leszek Koltunski
      }
108
    }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111 c5369f1b leszek
/**
112
 * Bind this Surface as a Framebuffer we can render to.
113
 */
114 af4cc5db Leszek Koltunski
  public abstract void setAsOutput();
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
/**
118
 * Create new Projection matrix.
119
 *
120
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
121
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
122
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
123
 */
124
  public void setProjection(float fov, float x, float y)
125
    {
126
    mFOV = fov;
127
    mX = x;
128
    mY = y;
129
130
    createProjection();
131
    }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134 c5369f1b leszek
/**
135 af4cc5db Leszek Koltunski
 * Resize the underlying Framebuffer.
136
 *
137
 * @param width The new width.
138
 * @param height The new height.
139 c5369f1b leszek
 */
140 af4cc5db Leszek Koltunski
  public void resize(int width, int height)
141
    {
142
    if( mWidth!=width || mHeight!=height )
143
      {
144
      mWidth = width;
145
      mHeight= height;
146
      mSizeX = width;
147
      mSizeY = height;
148 f8377ef8 leszek
149
      createProjection();
150
151
      if( mColorH[0]>0 )
152
        {
153
        moveToToDo();
154
        recreate();
155
        }
156 af4cc5db Leszek Koltunski
      }
157
    }
158 a09ada4c Leszek Koltunski
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * Adds a new child to the last position in the list of our Surface's children.
162
 *
163
 * @param node The new Node to add.
164
 */
165
  public synchronized void attach(DistortedNode node)
166
    {
167
    if( mChildren==null ) mChildren = new ArrayList<>(2);
168
    mChildren.add(node);
169
    mNumChildren++;
170
    }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
/**
174
 * Adds a new child to the last position in the list of our Surface's children.
175
 *
176
 * @param surface InputSurface to initialize our child Node with.
177
 * @param effects DistortedEffects to initialize our child Node with.
178
 * @param mesh MeshObject to initialize our child Node with.
179
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
180
 */
181
  public synchronized DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
182
    {
183
    if( mChildren==null ) mChildren = new ArrayList<>(2);
184
    DistortedNode node = new DistortedNode(surface,effects,mesh);
185
    mChildren.add(node);
186
    mNumChildren++;
187
188
    return node;
189
    }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
/**
193
 * Removes the first occurrence of a specified child from the list of children of our Surface.
194
 *
195
 * @param node The Node to remove.
196
 * @return <code>true</code> if the child was successfully removed.
197
 */
198
  public synchronized boolean detach(DistortedNode node)
199
    {
200 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 && mChildren.remove(node) )
201 a09ada4c Leszek Koltunski
      {
202
      mNumChildren--;
203
      return true;
204
      }
205
206
    return false;
207
    }
208
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
/**
211
 * Removes all children Nodes.
212
 */
213
  public synchronized void detachAll()
214
    {
215 d9706fd2 Leszek Koltunski
    if( mNumChildren>0 )
216
      {
217
      mNumChildren = 0;
218
      mChildren.clear();
219
      }
220 a09ada4c Leszek Koltunski
    }
221 af4cc5db Leszek Koltunski
}