1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
6a06a912
|
Leszek Koltunski
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import java.util.ArrayList;
|
23 |
|
|
import java.util.HashMap;
|
24 |
|
|
|
25 |
|
|
import android.opengl.GLES20;
|
26 |
|
|
|
27 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28 |
|
|
/**
|
29 |
cb0e1940
|
Leszek Koltunski
|
* Class which represents a Node in a Tree of DistortedObjects.
|
30 |
6a06a912
|
Leszek Koltunski
|
*
|
31 |
cb0e1940
|
Leszek Koltunski
|
* Having organized a set of DistortedObjects into a Tree, we can then render any Node to any Framebuffer.
|
32 |
|
|
* That recursively renders the Object held in the Node and all its children, along with whatever effects
|
33 |
6a06a912
|
Leszek Koltunski
|
* each one of them has.
|
34 |
|
|
*/
|
35 |
|
|
public class DistortedNode
|
36 |
|
|
{
|
37 |
|
|
private static final int TEXTURE_FAILED_TO_CREATE = -1;
|
38 |
|
|
private static final int TEXTURE_NOT_CREATED_YET = -2;
|
39 |
|
|
|
40 |
9361b337
|
Leszek Koltunski
|
private DistortedObject mObject;
|
41 |
6a06a912
|
Leszek Koltunski
|
private NodeData mData;
|
42 |
|
|
|
43 |
|
|
private DistortedNode mParent;
|
44 |
|
|
private ArrayList<DistortedNode> mChildren;
|
45 |
|
|
private int[] mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
46 |
|
|
|
47 |
9361b337
|
Leszek Koltunski
|
private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
|
48 |
6a06a912
|
Leszek Koltunski
|
private static long mNextNodeID =0;
|
49 |
|
|
|
50 |
|
|
//////////////////////////////////////////////////////////////////
|
51 |
|
|
|
52 |
|
|
private class NodeData
|
53 |
|
|
{
|
54 |
|
|
long ID;
|
55 |
|
|
int numPointingNodes;
|
56 |
|
|
int mFramebufferID;
|
57 |
|
|
int mTextureID;
|
58 |
|
|
DistortedProjection mProjection;
|
59 |
|
|
boolean mRendered;
|
60 |
|
|
|
61 |
|
|
//////////////////////////////////////////////////////////////////
|
62 |
|
|
|
63 |
|
|
public NodeData(long id)
|
64 |
|
|
{
|
65 |
|
|
ID = id;
|
66 |
|
|
numPointingNodes= 1;
|
67 |
|
|
mFramebufferID = 0;
|
68 |
|
|
mTextureID = TEXTURE_NOT_CREATED_YET;
|
69 |
|
|
mProjection = null;
|
70 |
|
|
mRendered = false;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
//////////////////////////////////////////////////////////////////
|
74 |
|
|
|
75 |
|
|
boolean createFBO()
|
76 |
|
|
{
|
77 |
|
|
int[] textureIds = new int[1];
|
78 |
|
|
GLES20.glGenTextures(1, textureIds, 0);
|
79 |
|
|
mTextureID = textureIds[0];
|
80 |
|
|
int[] mFBORenderToTexture = new int[1];
|
81 |
|
|
|
82 |
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
|
83 |
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
|
84 |
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
|
85 |
|
|
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
|
86 |
|
|
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
|
87 |
|
|
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mProjection.width, mProjection.height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
|
88 |
|
|
|
89 |
|
|
GLES20.glGenFramebuffers(1, mFBORenderToTexture, 0);
|
90 |
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBORenderToTexture[0]);
|
91 |
|
|
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mTextureID, 0);
|
92 |
|
|
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
|
93 |
|
|
|
94 |
|
|
if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
|
95 |
|
|
{
|
96 |
|
|
android.util.Log.e("Node", "failed to create framebuffer, error="+status);
|
97 |
|
|
|
98 |
|
|
GLES20.glDeleteTextures(1, textureIds, 0);
|
99 |
|
|
GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
|
100 |
|
|
mFramebufferID = 0;
|
101 |
|
|
mTextureID = TEXTURE_FAILED_TO_CREATE;
|
102 |
|
|
return false;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
mFramebufferID = mFBORenderToTexture[0];
|
106 |
|
|
|
107 |
|
|
return true;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
//////////////////////////////////////////////////////////////////
|
111 |
|
|
|
112 |
|
|
void deleteFBO()
|
113 |
|
|
{
|
114 |
|
|
int[] textureIds = new int[1];
|
115 |
|
|
int[] mFBORenderToTexture = new int[1];
|
116 |
|
|
|
117 |
|
|
textureIds[0] = mTextureID;
|
118 |
|
|
mFBORenderToTexture[0] = mFramebufferID;
|
119 |
|
|
|
120 |
|
|
GLES20.glDeleteTextures(1, textureIds, 0);
|
121 |
|
|
GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
|
122 |
|
|
|
123 |
|
|
mFramebufferID = 0;
|
124 |
|
|
mTextureID = TEXTURE_NOT_CREATED_YET;
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
|
128 |
436899f2
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129 |
|
|
|
130 |
f2a0d837
|
Leszek Koltunski
|
static void release()
|
131 |
436899f2
|
Leszek Koltunski
|
{
|
132 |
|
|
mNextNodeID = 0;
|
133 |
|
|
mMapNodeID.clear();
|
134 |
|
|
}
|
135 |
|
|
|
136 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137 |
|
|
|
138 |
|
|
private void markRecursive()
|
139 |
|
|
{
|
140 |
|
|
mData.mRendered = false;
|
141 |
|
|
|
142 |
|
|
synchronized(this)
|
143 |
|
|
{
|
144 |
|
|
for(int i=0; i<mNumChildren[0]; i++) mChildren.get(i).markRecursive();
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149 |
|
|
|
150 |
|
|
private void drawRecursive(long currTime, DistortedProjection dp, int mFBO)
|
151 |
|
|
{
|
152 |
|
|
if( mNumChildren[0]<=0 )
|
153 |
|
|
{
|
154 |
9361b337
|
Leszek Koltunski
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
|
155 |
6a06a912
|
Leszek Koltunski
|
|
156 |
|
|
if( mData.mProjection!=null )
|
157 |
|
|
{
|
158 |
|
|
mData.deleteFBO();
|
159 |
|
|
mData.mProjection = null;
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
else
|
163 |
|
|
{
|
164 |
|
|
if( mData.mRendered==false )
|
165 |
|
|
{
|
166 |
|
|
mData.mRendered = true;
|
167 |
|
|
|
168 |
|
|
if( mData.mTextureID==TEXTURE_NOT_CREATED_YET ) mData.createFBO();
|
169 |
|
|
|
170 |
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mData.mFramebufferID);
|
171 |
|
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
172 |
|
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
173 |
|
|
|
174 |
9361b337
|
Leszek Koltunski
|
if( mObject.mBitmapSet[0] )
|
175 |
6a06a912
|
Leszek Koltunski
|
{
|
176 |
9361b337
|
Leszek Koltunski
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
|
177 |
|
|
mObject.drawNoEffectsPriv(mData.mProjection);
|
178 |
6a06a912
|
Leszek Koltunski
|
}
|
179 |
|
|
|
180 |
|
|
synchronized(this)
|
181 |
|
|
{
|
182 |
|
|
for(int i=0; i<mNumChildren[0]; i++)
|
183 |
|
|
{
|
184 |
|
|
mChildren.get(i).drawRecursive(currTime, mData.mProjection, mData.mFramebufferID);
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBO);
|
190 |
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mData.mTextureID); // this is safe because we must have called createFBO() above before.
|
191 |
|
|
}
|
192 |
|
|
|
193 |
9361b337
|
Leszek Koltunski
|
mObject.drawPriv(currTime, dp);
|
194 |
6a06a912
|
Leszek Koltunski
|
}
|
195 |
|
|
|
196 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197 |
|
|
// tree isomorphism
|
198 |
|
|
|
199 |
|
|
private void RecomputeNodeID(ArrayList<Long> prev)
|
200 |
|
|
{
|
201 |
|
|
ArrayList<Long> curr = generateIDList();
|
202 |
|
|
|
203 |
|
|
if( mParent==null )
|
204 |
|
|
{
|
205 |
|
|
adjustNodeData(prev,curr);
|
206 |
|
|
}
|
207 |
|
|
else
|
208 |
|
|
{
|
209 |
|
|
ArrayList<Long> parentPrev = mParent.generateIDList();
|
210 |
|
|
adjustNodeData(prev,curr);
|
211 |
|
|
mParent.RecomputeNodeID(parentPrev);
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216 |
|
|
|
217 |
|
|
private ArrayList<Long> generateIDList()
|
218 |
|
|
{
|
219 |
9361b337
|
Leszek Koltunski
|
ArrayList<Long> ret = new ArrayList<>();
|
220 |
6a06a912
|
Leszek Koltunski
|
|
221 |
9361b337
|
Leszek Koltunski
|
ret.add( mNumChildren[0]>0 ? mObject.getBitmapID() : mObject.getID() );
|
222 |
6a06a912
|
Leszek Koltunski
|
DistortedNode node;
|
223 |
|
|
|
224 |
|
|
for(int i=0; i<mNumChildren[0]; i++)
|
225 |
|
|
{
|
226 |
|
|
node = mChildren.get(i);
|
227 |
|
|
ret.add(node.mData.ID);
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
return ret;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
234 |
|
|
|
235 |
|
|
private void adjustNodeData(ArrayList<Long> oldList, ArrayList<Long> newList)
|
236 |
|
|
{
|
237 |
fee0865c
|
Leszek Koltunski
|
boolean otherNodesPoint = (mData.numPointingNodes>1);
|
238 |
|
|
|
239 |
|
|
if( otherNodesPoint ) mData.numPointingNodes--;
|
240 |
|
|
else mMapNodeID.remove(oldList);
|
241 |
6a06a912
|
Leszek Koltunski
|
|
242 |
|
|
NodeData newData = mMapNodeID.get(newList);
|
243 |
|
|
|
244 |
|
|
if( newData==null )
|
245 |
|
|
{
|
246 |
fee0865c
|
Leszek Koltunski
|
if( otherNodesPoint ) mData = new NodeData(++mNextNodeID);
|
247 |
|
|
else mData.ID = ++mNextNodeID; // numPointingNodes must be 1 already
|
248 |
|
|
|
249 |
6a06a912
|
Leszek Koltunski
|
if( newList.size()>1 && mData.mProjection==null )
|
250 |
|
|
{
|
251 |
|
|
mData.mProjection = new DistortedProjection(true);
|
252 |
9361b337
|
Leszek Koltunski
|
mData.mProjection.onSurfaceChanged(mObject.getWidth(), mObject.getHeight());
|
253 |
6a06a912
|
Leszek Koltunski
|
mData.mFramebufferID = 0;
|
254 |
|
|
mData.mTextureID = TEXTURE_NOT_CREATED_YET;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
mMapNodeID.put(newList, mData);
|
258 |
|
|
}
|
259 |
|
|
else
|
260 |
fee0865c
|
Leszek Koltunski
|
{
|
261 |
6a06a912
|
Leszek Koltunski
|
newData.numPointingNodes++;
|
262 |
|
|
mData = newData;
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
267 |
|
|
// this will be called on startup and every time OpenGL context has been lost
|
268 |
|
|
// also call this from the constructor if the OpenGL context has been created already.
|
269 |
|
|
|
270 |
|
|
static void reset()
|
271 |
|
|
{
|
272 |
|
|
NodeData tmp;
|
273 |
|
|
|
274 |
|
|
for(ArrayList<Long> key: mMapNodeID.keySet())
|
275 |
|
|
{
|
276 |
|
|
tmp = mMapNodeID.get(key);
|
277 |
|
|
|
278 |
|
|
if( tmp.mProjection!=null )
|
279 |
|
|
{
|
280 |
|
|
tmp.mTextureID = TEXTURE_NOT_CREATED_YET;
|
281 |
|
|
tmp.mRendered = false;
|
282 |
|
|
}
|
283 |
|
|
}
|
284 |
|
|
}
|
285 |
fee0865c
|
Leszek Koltunski
|
|
286 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287 |
|
|
// Debug - print all the Node IDs
|
288 |
|
|
|
289 |
|
|
void debug(int depth)
|
290 |
|
|
{
|
291 |
|
|
String tmp="";
|
292 |
|
|
int i;
|
293 |
|
|
|
294 |
|
|
for(i=0; i<depth; i++) tmp +=" ";
|
295 |
|
|
tmp += (""+mData.ID);
|
296 |
|
|
|
297 |
|
|
android.util.Log.e("node", tmp);
|
298 |
|
|
|
299 |
|
|
for(i=0; i<mNumChildren[0]; i++)
|
300 |
|
|
mChildren.get(i).debug(depth+1);
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304 |
|
|
// Debug - print contents of the HashMap
|
305 |
|
|
|
306 |
|
|
static void debugMap()
|
307 |
|
|
{
|
308 |
|
|
NodeData tmp;
|
309 |
|
|
|
310 |
|
|
for(ArrayList<Long> key: mMapNodeID.keySet())
|
311 |
|
|
{
|
312 |
|
|
tmp = mMapNodeID.get(key);
|
313 |
|
|
|
314 |
|
|
android.util.Log.e("NODE", "key="+key+" NodeID: "+tmp.ID);
|
315 |
|
|
}
|
316 |
|
|
}
|
317 |
|
|
|
318 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319 |
|
|
// PUBLIC API
|
320 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
321 |
|
|
/**
|
322 |
|
|
* Constructs new Node of the Tree.
|
323 |
|
|
*
|
324 |
9361b337
|
Leszek Koltunski
|
* @param obj DistortedObject to put into the new Node.
|
325 |
6a06a912
|
Leszek Koltunski
|
*/
|
326 |
9361b337
|
Leszek Koltunski
|
public DistortedNode(DistortedObject obj)
|
327 |
6a06a912
|
Leszek Koltunski
|
{
|
328 |
9361b337
|
Leszek Koltunski
|
mObject = obj;
|
329 |
6a06a912
|
Leszek Koltunski
|
mParent = null;
|
330 |
|
|
mChildren = null;
|
331 |
|
|
mNumChildren = new int[1];
|
332 |
|
|
mNumChildren[0] = 0;
|
333 |
|
|
|
334 |
9361b337
|
Leszek Koltunski
|
ArrayList<Long> list = new ArrayList<>();
|
335 |
|
|
list.add(obj.getID());
|
336 |
6a06a912
|
Leszek Koltunski
|
|
337 |
|
|
mData = mMapNodeID.get(list);
|
338 |
|
|
|
339 |
|
|
if( mData!=null )
|
340 |
|
|
{
|
341 |
|
|
mData.numPointingNodes++;
|
342 |
|
|
}
|
343 |
|
|
else
|
344 |
|
|
{
|
345 |
|
|
mData = new NodeData(++mNextNodeID);
|
346 |
|
|
mMapNodeID.put(list, mData);
|
347 |
|
|
}
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
351 |
|
|
/**
|
352 |
|
|
* Copy-constructs new Node of the Tree from another Node.
|
353 |
|
|
*
|
354 |
|
|
* @param node The DistortedNode to copy data from.
|
355 |
|
|
* @param flags bit field composed of a subset of the following:
|
356 |
015642fb
|
Leszek Koltunski
|
* {@link Distorted#CLONE_BITMAP}, {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
|
357 |
6a06a912
|
Leszek Koltunski
|
* {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
|
358 |
|
|
* For example flags = CLONE_BITMAP | CLONE_CHILDREN.
|
359 |
|
|
*/
|
360 |
|
|
public DistortedNode(DistortedNode node, int flags)
|
361 |
|
|
{
|
362 |
9361b337
|
Leszek Koltunski
|
mParent = null;
|
363 |
ada90d33
|
Leszek Koltunski
|
mObject = node.mObject.deepCopy(flags);
|
364 |
9361b337
|
Leszek Koltunski
|
|
365 |
|
|
if( (flags & Distorted.CLONE_CHILDREN) != 0 )
|
366 |
6a06a912
|
Leszek Koltunski
|
{
|
367 |
|
|
mChildren = node.mChildren;
|
368 |
|
|
mNumChildren = node.mNumChildren;
|
369 |
|
|
}
|
370 |
|
|
else
|
371 |
|
|
{
|
372 |
|
|
mChildren = null;
|
373 |
|
|
mNumChildren = new int[1];
|
374 |
|
|
mNumChildren[0] = 0;
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
ArrayList<Long> list = generateIDList();
|
378 |
|
|
|
379 |
|
|
mData = mMapNodeID.get(list);
|
380 |
|
|
|
381 |
|
|
if( mData!=null )
|
382 |
|
|
{
|
383 |
|
|
mData.numPointingNodes++;
|
384 |
|
|
}
|
385 |
|
|
else
|
386 |
|
|
{
|
387 |
|
|
mData = new NodeData(++mNextNodeID);
|
388 |
|
|
mMapNodeID.put(list, mData);
|
389 |
|
|
}
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
393 |
|
|
/**
|
394 |
|
|
* Adds a new child to the last position in the list of our Node's children.
|
395 |
|
|
*
|
396 |
|
|
* @param node The new Node to add.
|
397 |
|
|
*/
|
398 |
|
|
public synchronized void attach(DistortedNode node)
|
399 |
|
|
{
|
400 |
|
|
ArrayList<Long> prev = generateIDList();
|
401 |
|
|
|
402 |
9361b337
|
Leszek Koltunski
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
403 |
6a06a912
|
Leszek Koltunski
|
|
404 |
|
|
node.mParent = this;
|
405 |
|
|
mChildren.add(node);
|
406 |
|
|
mNumChildren[0]++;
|
407 |
|
|
|
408 |
|
|
RecomputeNodeID(prev);
|
409 |
|
|
}
|
410 |
|
|
|
411 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
412 |
|
|
/**
|
413 |
|
|
* Adds a new child to the last position in the list of our Node's children.
|
414 |
|
|
*
|
415 |
cb0e1940
|
Leszek Koltunski
|
* @param obj DistortedObject to initialize our child Node with.
|
416 |
6a06a912
|
Leszek Koltunski
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
417 |
|
|
*/
|
418 |
cb0e1940
|
Leszek Koltunski
|
public synchronized DistortedNode attach(DistortedObject obj)
|
419 |
6a06a912
|
Leszek Koltunski
|
{
|
420 |
|
|
ArrayList<Long> prev = generateIDList();
|
421 |
|
|
|
422 |
9361b337
|
Leszek Koltunski
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
423 |
cb0e1940
|
Leszek Koltunski
|
DistortedNode node = new DistortedNode(obj);
|
424 |
6a06a912
|
Leszek Koltunski
|
node.mParent = this;
|
425 |
|
|
mChildren.add(node);
|
426 |
|
|
mNumChildren[0]++;
|
427 |
|
|
|
428 |
|
|
RecomputeNodeID(prev);
|
429 |
fee0865c
|
Leszek Koltunski
|
|
430 |
6a06a912
|
Leszek Koltunski
|
return node;
|
431 |
|
|
}
|
432 |
|
|
|
433 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
434 |
|
|
/**
|
435 |
|
|
* Removes the first occurrence of a specified child from the list of children of our Node.
|
436 |
|
|
*
|
437 |
|
|
* @param node The Node to remove.
|
438 |
|
|
* @return <code>true</code> if the child was successfully removed.
|
439 |
|
|
*/
|
440 |
|
|
public synchronized boolean detach(DistortedNode node)
|
441 |
|
|
{
|
442 |
|
|
if( mNumChildren[0]>0 )
|
443 |
|
|
{
|
444 |
|
|
ArrayList<Long> prev = generateIDList();
|
445 |
|
|
|
446 |
|
|
if( mChildren.remove(node) )
|
447 |
|
|
{
|
448 |
|
|
node.mParent = null;
|
449 |
|
|
mNumChildren[0]--;
|
450 |
|
|
|
451 |
|
|
RecomputeNodeID(prev);
|
452 |
|
|
|
453 |
|
|
return true;
|
454 |
|
|
}
|
455 |
|
|
}
|
456 |
|
|
|
457 |
|
|
return false;
|
458 |
|
|
}
|
459 |
|
|
|
460 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
461 |
|
|
/**
|
462 |
|
|
* Removes the first occurrence of a specified child from the list of children of our Node.
|
463 |
|
|
*
|
464 |
9361b337
|
Leszek Koltunski
|
* @param obj DistortedObject to remove.
|
465 |
6a06a912
|
Leszek Koltunski
|
* @return <code>true</code> if the child was successfully removed.
|
466 |
|
|
*/
|
467 |
9361b337
|
Leszek Koltunski
|
public synchronized boolean detach(DistortedObject obj)
|
468 |
6a06a912
|
Leszek Koltunski
|
{
|
469 |
9361b337
|
Leszek Koltunski
|
long id = obj.getID();
|
470 |
6a06a912
|
Leszek Koltunski
|
DistortedNode node;
|
471 |
|
|
|
472 |
|
|
for(int i=0; i<mNumChildren[0]; i++)
|
473 |
|
|
{
|
474 |
|
|
node = mChildren.get(i);
|
475 |
|
|
|
476 |
9361b337
|
Leszek Koltunski
|
if( node.mObject.getID()==id )
|
477 |
6a06a912
|
Leszek Koltunski
|
{
|
478 |
|
|
ArrayList<Long> prev = generateIDList();
|
479 |
|
|
|
480 |
|
|
node.mParent = null;
|
481 |
|
|
mChildren.remove(i);
|
482 |
|
|
mNumChildren[0]--;
|
483 |
|
|
|
484 |
|
|
RecomputeNodeID(prev);
|
485 |
|
|
|
486 |
|
|
return true;
|
487 |
|
|
}
|
488 |
|
|
}
|
489 |
|
|
|
490 |
|
|
return false;
|
491 |
|
|
}
|
492 |
|
|
|
493 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
494 |
|
|
/**
|
495 |
|
|
* Removes all children Nodes.
|
496 |
|
|
*/
|
497 |
|
|
public synchronized void detachAll()
|
498 |
|
|
{
|
499 |
|
|
for(int i=0; i<mNumChildren[0]; i++)
|
500 |
|
|
{
|
501 |
|
|
mChildren.get(i).mParent = null;
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
if( mNumChildren[0]>0 )
|
505 |
|
|
{
|
506 |
|
|
ArrayList<Long> prev = generateIDList();
|
507 |
|
|
|
508 |
|
|
mNumChildren[0] = 0;
|
509 |
|
|
mChildren.clear();
|
510 |
|
|
RecomputeNodeID(prev);
|
511 |
|
|
}
|
512 |
|
|
}
|
513 |
|
|
|
514 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
515 |
|
|
/**
|
516 |
|
|
* Draws the Node, and all its children, to the default framebuffer 0 (i.e. the screen).
|
517 |
|
|
*
|
518 |
|
|
* @param currTime Current time, in milliseconds.
|
519 |
|
|
*/
|
520 |
|
|
public void draw(long currTime)
|
521 |
|
|
{
|
522 |
|
|
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
523 |
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
524 |
|
|
GLES20.glUniform1i(Distorted.mTextureUniformH, 0);
|
525 |
|
|
|
526 |
|
|
markRecursive();
|
527 |
|
|
drawRecursive(currTime,Distorted.mProjection,0);
|
528 |
|
|
}
|
529 |
|
|
|
530 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
531 |
|
|
/**
|
532 |
9361b337
|
Leszek Koltunski
|
* Returns the DistortedObject object that's in the Node.
|
533 |
6a06a912
|
Leszek Koltunski
|
*
|
534 |
9361b337
|
Leszek Koltunski
|
* @return The DistortedObject contained in the Node.
|
535 |
6a06a912
|
Leszek Koltunski
|
*/
|
536 |
9361b337
|
Leszek Koltunski
|
public DistortedObject getObject()
|
537 |
6a06a912
|
Leszek Koltunski
|
{
|
538 |
9361b337
|
Leszek Koltunski
|
return mObject;
|
539 |
6a06a912
|
Leszek Koltunski
|
}
|
540 |
|
|
|
541 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
542 |
|
|
}
|