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 |
c204c69d
|
leszek
|
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedAttacheable
|
28 |
af4cc5db
|
Leszek Koltunski
|
{
|
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 |
c204c69d
|
leszek
|
// 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 |
f8377ef8
|
leszek
|
toDo();
|
118 |
c5369f1b
|
leszek
|
|
119 |
d9706fd2
|
Leszek Koltunski
|
for(int i=0; i<mNumChildren; i++)
|
120 |
af4cc5db
|
Leszek Koltunski
|
{
|
121 |
d9706fd2
|
Leszek Koltunski
|
mChildren.get(i).drawRecursive(time,this);
|
122 |
af4cc5db
|
Leszek Koltunski
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126 |
c5369f1b
|
leszek
|
/**
|
127 |
|
|
* Bind this Surface as a Framebuffer we can render to.
|
128 |
|
|
*/
|
129 |
af4cc5db
|
Leszek Koltunski
|
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 |
c5369f1b
|
leszek
|
/**
|
150 |
af4cc5db
|
Leszek Koltunski
|
* Resize the underlying Framebuffer.
|
151 |
|
|
*
|
152 |
|
|
* @param width The new width.
|
153 |
|
|
* @param height The new height.
|
154 |
c5369f1b
|
leszek
|
*/
|
155 |
af4cc5db
|
Leszek Koltunski
|
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 |
f8377ef8
|
leszek
|
|
164 |
|
|
createProjection();
|
165 |
|
|
|
166 |
|
|
if( mColorH[0]>0 )
|
167 |
|
|
{
|
168 |
|
|
moveToToDo();
|
169 |
|
|
recreate();
|
170 |
|
|
}
|
171 |
af4cc5db
|
Leszek Koltunski
|
}
|
172 |
|
|
}
|
173 |
a09ada4c
|
Leszek Koltunski
|
|
174 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175 |
|
|
/**
|
176 |
|
|
* Adds a new child to the last position in the list of our Surface's children.
|
177 |
c204c69d
|
leszek
|
* <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 |
a09ada4c
|
Leszek Koltunski
|
*
|
181 |
|
|
* @param node The new Node to add.
|
182 |
|
|
*/
|
183 |
c204c69d
|
leszek
|
public void attach(DistortedNode node)
|
184 |
a09ada4c
|
Leszek Koltunski
|
{
|
185 |
c204c69d
|
leszek
|
DistortedAttachDaemon.attach(this,node);
|
186 |
a09ada4c
|
Leszek Koltunski
|
}
|
187 |
|
|
|
188 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189 |
|
|
/**
|
190 |
|
|
* Adds a new child to the last position in the list of our Surface's children.
|
191 |
c204c69d
|
leszek
|
* <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 |
a09ada4c
|
Leszek Koltunski
|
*
|
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 |
c204c69d
|
leszek
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
201 |
a09ada4c
|
Leszek Koltunski
|
{
|
202 |
|
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
203 |
c204c69d
|
leszek
|
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 |
a09ada4c
|
Leszek Koltunski
|
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 |
c204c69d
|
leszek
|
* <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 |
a09ada4c
|
Leszek Koltunski
|
*
|
242 |
|
|
* @param node The Node to remove.
|
243 |
|
|
*/
|
244 |
c204c69d
|
leszek
|
public void detachNow(DistortedNode node)
|
245 |
a09ada4c
|
Leszek Koltunski
|
{
|
246 |
d9706fd2
|
Leszek Koltunski
|
if( mNumChildren>0 && mChildren.remove(node) )
|
247 |
a09ada4c
|
Leszek Koltunski
|
{
|
248 |
|
|
mNumChildren--;
|
249 |
|
|
}
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253 |
|
|
/**
|
254 |
|
|
* Removes all children Nodes.
|
255 |
c204c69d
|
leszek
|
* <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 |
a09ada4c
|
Leszek Koltunski
|
*/
|
270 |
c204c69d
|
leszek
|
public void detachAllNow()
|
271 |
a09ada4c
|
Leszek Koltunski
|
{
|
272 |
d9706fd2
|
Leszek Koltunski
|
if( mNumChildren>0 )
|
273 |
|
|
{
|
274 |
|
|
mNumChildren = 0;
|
275 |
|
|
mChildren.clear();
|
276 |
|
|
}
|
277 |
a09ada4c
|
Leszek Koltunski
|
}
|
278 |
af4cc5db
|
Leszek Koltunski
|
}
|