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 |
d9706fd2
|
Leszek Koltunski
|
DistortedSurface.deleteAllMarked();
|
103 |
|
|
create();
|
104 |
c5369f1b
|
leszek
|
|
105 |
d9706fd2
|
Leszek Koltunski
|
for(int i=0; i<mNumChildren; i++)
|
106 |
af4cc5db
|
Leszek Koltunski
|
{
|
107 |
d9706fd2
|
Leszek Koltunski
|
mChildren.get(i).drawRecursive(time,this);
|
108 |
af4cc5db
|
Leszek Koltunski
|
}
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112 |
c5369f1b
|
leszek
|
/**
|
113 |
|
|
* Bind this Surface as a Framebuffer we can render to.
|
114 |
|
|
*/
|
115 |
af4cc5db
|
Leszek Koltunski
|
public abstract void setAsOutput();
|
116 |
|
|
|
117 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
|
/**
|
119 |
|
|
* Create new Projection matrix.
|
120 |
|
|
*
|
121 |
|
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
122 |
|
|
* @param x X-coordinate of the point at which our camera looks at. 0 is the center.
|
123 |
|
|
* @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
|
124 |
|
|
*/
|
125 |
|
|
public void setProjection(float fov, float x, float y)
|
126 |
|
|
{
|
127 |
|
|
mFOV = fov;
|
128 |
|
|
mX = x;
|
129 |
|
|
mY = y;
|
130 |
|
|
|
131 |
|
|
createProjection();
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135 |
c5369f1b
|
leszek
|
/**
|
136 |
af4cc5db
|
Leszek Koltunski
|
* Resize the underlying Framebuffer.
|
137 |
|
|
*
|
138 |
|
|
* @param width The new width.
|
139 |
|
|
* @param height The new height.
|
140 |
c5369f1b
|
leszek
|
*/
|
141 |
af4cc5db
|
Leszek Koltunski
|
public void resize(int width, int height)
|
142 |
|
|
{
|
143 |
|
|
if( mWidth!=width || mHeight!=height )
|
144 |
|
|
{
|
145 |
|
|
mWidth = width;
|
146 |
|
|
mHeight= height;
|
147 |
|
|
createProjection();
|
148 |
|
|
mSizeX = width;
|
149 |
|
|
mSizeY = height;
|
150 |
|
|
if( mColorH[0]>0 ) markForDeletion();
|
151 |
|
|
}
|
152 |
|
|
}
|
153 |
a09ada4c
|
Leszek Koltunski
|
|
154 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155 |
|
|
/**
|
156 |
|
|
* Adds a new child to the last position in the list of our Surface's children.
|
157 |
|
|
*
|
158 |
|
|
* @param node The new Node to add.
|
159 |
|
|
*/
|
160 |
|
|
public synchronized void attach(DistortedNode node)
|
161 |
|
|
{
|
162 |
|
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
163 |
|
|
mChildren.add(node);
|
164 |
|
|
mNumChildren++;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168 |
|
|
/**
|
169 |
|
|
* Adds a new child to the last position in the list of our Surface's children.
|
170 |
|
|
*
|
171 |
|
|
* @param surface InputSurface to initialize our child Node with.
|
172 |
|
|
* @param effects DistortedEffects to initialize our child Node with.
|
173 |
|
|
* @param mesh MeshObject to initialize our child Node with.
|
174 |
|
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
175 |
|
|
*/
|
176 |
|
|
public synchronized DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
177 |
|
|
{
|
178 |
|
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
179 |
|
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
180 |
|
|
mChildren.add(node);
|
181 |
|
|
mNumChildren++;
|
182 |
|
|
|
183 |
|
|
return node;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187 |
|
|
/**
|
188 |
|
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
189 |
|
|
*
|
190 |
|
|
* @param node The Node to remove.
|
191 |
|
|
* @return <code>true</code> if the child was successfully removed.
|
192 |
|
|
*/
|
193 |
|
|
public synchronized boolean detach(DistortedNode node)
|
194 |
|
|
{
|
195 |
d9706fd2
|
Leszek Koltunski
|
if( mNumChildren>0 && mChildren.remove(node) )
|
196 |
a09ada4c
|
Leszek Koltunski
|
{
|
197 |
|
|
mNumChildren--;
|
198 |
|
|
return true;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
return false;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205 |
|
|
/**
|
206 |
|
|
* Removes all children Nodes.
|
207 |
|
|
*/
|
208 |
|
|
public synchronized void detachAll()
|
209 |
|
|
{
|
210 |
d9706fd2
|
Leszek Koltunski
|
if( mNumChildren>0 )
|
211 |
|
|
{
|
212 |
|
|
mNumChildren = 0;
|
213 |
|
|
mChildren.clear();
|
214 |
|
|
}
|
215 |
a09ada4c
|
Leszek Koltunski
|
}
|
216 |
af4cc5db
|
Leszek Koltunski
|
}
|