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 java.util.ArrayList;
|
23
|
import java.util.HashMap;
|
24
|
|
25
|
import android.opengl.GLES20;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Class which represents a Node in a Tree of (Texture,Grid,Effects) objects.
|
30
|
*
|
31
|
* Having organized such sets into a Tree, we can then render any Node to any Framebuffer.
|
32
|
* That recursively renders the set held in the Node and all its children.
|
33
|
*/
|
34
|
public class DistortedTree
|
35
|
{
|
36
|
private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
|
37
|
private static long mNextNodeID =0;
|
38
|
|
39
|
private GridObject mGrid;
|
40
|
private DistortedEffects mEffects;
|
41
|
private DistortedTexture mTexture;
|
42
|
private NodeData mData;
|
43
|
|
44
|
private DistortedTree mParent;
|
45
|
private ArrayList<DistortedTree> mChildren;
|
46
|
private int[] mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
47
|
|
48
|
private class NodeData
|
49
|
{
|
50
|
long ID;
|
51
|
int numPointingNodes;
|
52
|
DistortedFramebuffer mDF;
|
53
|
int numRendered;
|
54
|
|
55
|
NodeData(long id)
|
56
|
{
|
57
|
ID = id;
|
58
|
numPointingNodes= 1;
|
59
|
mDF = null;
|
60
|
numRendered = 0;
|
61
|
}
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
static synchronized void onDestroy()
|
67
|
{
|
68
|
mNextNodeID = 0;
|
69
|
mMapNodeID.clear();
|
70
|
}
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
// tree isomorphism
|
74
|
|
75
|
private void RecomputeNodeID(ArrayList<Long> prev)
|
76
|
{
|
77
|
ArrayList<Long> curr = generateIDList();
|
78
|
|
79
|
if( mParent==null )
|
80
|
{
|
81
|
adjustNodeData(prev,curr);
|
82
|
}
|
83
|
else
|
84
|
{
|
85
|
ArrayList<Long> parentPrev = mParent.generateIDList();
|
86
|
adjustNodeData(prev,curr);
|
87
|
mParent.RecomputeNodeID(parentPrev);
|
88
|
}
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
private ArrayList<Long> generateIDList()
|
94
|
{
|
95
|
ArrayList<Long> ret = new ArrayList<>();
|
96
|
|
97
|
ret.add( mTexture.getID() );
|
98
|
DistortedTree node;
|
99
|
|
100
|
for(int i=0; i<mNumChildren[0]; i++)
|
101
|
{
|
102
|
node = mChildren.get(i);
|
103
|
ret.add(node.mData.ID);
|
104
|
}
|
105
|
|
106
|
return ret;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
private void adjustNodeData(ArrayList<Long> oldList, ArrayList<Long> newList)
|
112
|
{
|
113
|
boolean otherNodesPoint = (mData.numPointingNodes>1);
|
114
|
|
115
|
if( otherNodesPoint ) mData.numPointingNodes--;
|
116
|
else mMapNodeID.remove(oldList);
|
117
|
|
118
|
NodeData newData = mMapNodeID.get(newList);
|
119
|
|
120
|
if( newData==null )
|
121
|
{
|
122
|
if( otherNodesPoint ) mData = new NodeData(++mNextNodeID);
|
123
|
else mData.ID = ++mNextNodeID; // numPointingNodes must be 1 already
|
124
|
|
125
|
if( newList.size()>1 )
|
126
|
{
|
127
|
if( mData.mDF==null )
|
128
|
mData.mDF = new DistortedFramebuffer(mTexture.getWidth(), mTexture.getHeight());
|
129
|
}
|
130
|
else
|
131
|
{
|
132
|
if( mData.mDF!=null )
|
133
|
{
|
134
|
mData.mDF.markForDeletion();
|
135
|
mData.mDF = null;
|
136
|
}
|
137
|
else
|
138
|
{
|
139
|
android.util.Log.e("DistortedTree", "adjustNodeData: impossible situation??");
|
140
|
}
|
141
|
}
|
142
|
|
143
|
mMapNodeID.put(newList, mData);
|
144
|
}
|
145
|
else
|
146
|
{
|
147
|
newData.numPointingNodes++;
|
148
|
mData = newData;
|
149
|
}
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
// this will be called on startup and every time OpenGL context has been lost
|
154
|
|
155
|
static void reset()
|
156
|
{
|
157
|
NodeData tmp;
|
158
|
|
159
|
for(ArrayList<Long> key: mMapNodeID.keySet())
|
160
|
{
|
161
|
tmp = mMapNodeID.get(key);
|
162
|
|
163
|
if( tmp.mDF != null )
|
164
|
{
|
165
|
tmp.mDF.reset();
|
166
|
tmp.numRendered = 0;
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
// Debug - print all the Node IDs
|
173
|
|
174
|
void debug(int depth)
|
175
|
{
|
176
|
String tmp="";
|
177
|
int i;
|
178
|
|
179
|
for(i=0; i<depth; i++) tmp +=" ";
|
180
|
tmp += (mData.ID+" (nodes: "+mData.numPointingNodes+")");
|
181
|
|
182
|
android.util.Log.e("NODE", tmp);
|
183
|
|
184
|
for(i=0; i<mNumChildren[0]; i++)
|
185
|
mChildren.get(i).debug(depth+1);
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
// Debug - print contents of the HashMap
|
190
|
|
191
|
static void debugMap()
|
192
|
{
|
193
|
NodeData tmp;
|
194
|
|
195
|
for(ArrayList<Long> key: mMapNodeID.keySet())
|
196
|
{
|
197
|
tmp = mMapNodeID.get(key);
|
198
|
|
199
|
android.util.Log.e("NODE", "key="+key+" NodeID: "+tmp.ID);
|
200
|
}
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
void drawRecursive(long currTime, DistortedFramebuffer df)
|
206
|
{
|
207
|
mTexture.createTexture();
|
208
|
|
209
|
if( mNumChildren[0]<=0 )
|
210
|
{
|
211
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture.mTextureDataH[0]);
|
212
|
}
|
213
|
else
|
214
|
{
|
215
|
mData.mDF.createFBO();
|
216
|
|
217
|
if( mData.numRendered==0 )
|
218
|
{
|
219
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mData.mDF.fboIds[0]);
|
220
|
|
221
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
222
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
223
|
|
224
|
if( mTexture.mBitmapSet[0] )
|
225
|
{
|
226
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture.mTextureDataH[0]);
|
227
|
mEffects.drawNoEffectsPriv(mTexture, mGrid, mData.mDF);
|
228
|
}
|
229
|
|
230
|
synchronized(this)
|
231
|
{
|
232
|
for(int i=0; i<mNumChildren[0]; i++)
|
233
|
{
|
234
|
mChildren.get(i).drawRecursive(currTime, mData.mDF);
|
235
|
}
|
236
|
}
|
237
|
}
|
238
|
|
239
|
mData.numRendered++;
|
240
|
mData.numRendered %= mData.numPointingNodes;
|
241
|
|
242
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, df.fboIds[0]);
|
243
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mData.mDF.texIds[0]);
|
244
|
}
|
245
|
|
246
|
mEffects.drawPriv(currTime, mTexture, mGrid, df);
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
// PUBLIC API
|
251
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252
|
/**
|
253
|
* Constructs new Node of the Tree.
|
254
|
*
|
255
|
* @param texture DistortedTexture to put into the new Node.
|
256
|
* @param effects DistortedEffects to put into the new Node.
|
257
|
* @param grid GridObject to put into the new Node.
|
258
|
*/
|
259
|
public DistortedTree(DistortedTexture texture, DistortedEffects effects, GridObject grid)
|
260
|
{
|
261
|
mTexture= texture;
|
262
|
mEffects= effects;
|
263
|
mGrid = grid;
|
264
|
mParent = null;
|
265
|
mChildren = null;
|
266
|
mNumChildren = new int[1];
|
267
|
mNumChildren[0] = 0;
|
268
|
|
269
|
ArrayList<Long> list = new ArrayList<>();
|
270
|
list.add(mTexture.getID());
|
271
|
|
272
|
mData = mMapNodeID.get(list);
|
273
|
|
274
|
if( mData!=null )
|
275
|
{
|
276
|
mData.numPointingNodes++;
|
277
|
}
|
278
|
else
|
279
|
{
|
280
|
mData = new NodeData(++mNextNodeID);
|
281
|
mMapNodeID.put(list, mData);
|
282
|
}
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
/**
|
287
|
* Copy-constructs new Node of the Tree from another Node.
|
288
|
*
|
289
|
* @param node The DistortedTree to copy data from.
|
290
|
* @param flags bit field composed of a subset of the following:
|
291
|
* {@link Distorted#CLONE_BITMAP}, {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
|
292
|
* {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
|
293
|
* For example flags = CLONE_BITMAP | CLONE_CHILDREN.
|
294
|
*/
|
295
|
public DistortedTree(DistortedTree node, int flags)
|
296
|
{
|
297
|
mParent = null;
|
298
|
mTexture= new DistortedTexture(node.mTexture,flags);
|
299
|
mEffects = new DistortedEffects(node.mEffects, flags);
|
300
|
mGrid = node.mGrid;
|
301
|
|
302
|
if( (flags & Distorted.CLONE_CHILDREN) != 0 )
|
303
|
{
|
304
|
mChildren = node.mChildren;
|
305
|
mNumChildren = node.mNumChildren;
|
306
|
}
|
307
|
else
|
308
|
{
|
309
|
mChildren = null;
|
310
|
mNumChildren = new int[1];
|
311
|
mNumChildren[0] = 0;
|
312
|
}
|
313
|
|
314
|
ArrayList<Long> list = generateIDList();
|
315
|
|
316
|
mData = mMapNodeID.get(list);
|
317
|
|
318
|
if( mData!=null )
|
319
|
{
|
320
|
mData.numPointingNodes++;
|
321
|
}
|
322
|
else
|
323
|
{
|
324
|
mData = new NodeData(++mNextNodeID);
|
325
|
mMapNodeID.put(list, mData);
|
326
|
}
|
327
|
}
|
328
|
|
329
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
330
|
/**
|
331
|
* Adds a new child to the last position in the list of our Node's children.
|
332
|
*
|
333
|
* @param node The new Node to add.
|
334
|
*/
|
335
|
public synchronized void attach(DistortedTree node)
|
336
|
{
|
337
|
ArrayList<Long> prev = generateIDList();
|
338
|
|
339
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
340
|
|
341
|
node.mParent = this;
|
342
|
mChildren.add(node);
|
343
|
mNumChildren[0]++;
|
344
|
|
345
|
RecomputeNodeID(prev);
|
346
|
}
|
347
|
|
348
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
349
|
/**
|
350
|
* Adds a new child to the last position in the list of our Node's children.
|
351
|
*
|
352
|
* @param texture DistortedTexture to initialize our child Node with.
|
353
|
* @param effects DistortedEffects to initialize our child Node with.
|
354
|
* @param grid GridObject to initialize our child Node with.
|
355
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
356
|
*/
|
357
|
public synchronized DistortedTree attach(DistortedTexture texture, DistortedEffects effects, GridObject grid)
|
358
|
{
|
359
|
ArrayList<Long> prev = generateIDList();
|
360
|
|
361
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
362
|
DistortedTree node = new DistortedTree(texture,effects,grid);
|
363
|
node.mParent = this;
|
364
|
mChildren.add(node);
|
365
|
mNumChildren[0]++;
|
366
|
|
367
|
RecomputeNodeID(prev);
|
368
|
|
369
|
return node;
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
/**
|
374
|
* Removes the first occurrence of a specified child from the list of children of our Node.
|
375
|
*
|
376
|
* @param node The Node to remove.
|
377
|
* @return <code>true</code> if the child was successfully removed.
|
378
|
*/
|
379
|
public synchronized boolean detach(DistortedTree node)
|
380
|
{
|
381
|
if( mNumChildren[0]>0 )
|
382
|
{
|
383
|
ArrayList<Long> prev = generateIDList();
|
384
|
|
385
|
if( mChildren.remove(node) )
|
386
|
{
|
387
|
node.mParent = null;
|
388
|
mNumChildren[0]--;
|
389
|
|
390
|
RecomputeNodeID(prev);
|
391
|
|
392
|
return true;
|
393
|
}
|
394
|
}
|
395
|
|
396
|
return false;
|
397
|
}
|
398
|
|
399
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
400
|
/**
|
401
|
* Removes the first occurrence of a specified child from the list of children of our Node.
|
402
|
*
|
403
|
* @param effects DistortedEffects to remove.
|
404
|
* @return <code>true</code> if the child was successfully removed.
|
405
|
*/
|
406
|
public synchronized boolean detach(DistortedEffects effects)
|
407
|
{
|
408
|
long id = effects.getID();
|
409
|
DistortedTree node;
|
410
|
|
411
|
for(int i=0; i<mNumChildren[0]; i++)
|
412
|
{
|
413
|
node = mChildren.get(i);
|
414
|
|
415
|
if( node.mEffects.getID()==id )
|
416
|
{
|
417
|
ArrayList<Long> prev = generateIDList();
|
418
|
|
419
|
node.mParent = null;
|
420
|
mChildren.remove(i);
|
421
|
mNumChildren[0]--;
|
422
|
|
423
|
RecomputeNodeID(prev);
|
424
|
|
425
|
return true;
|
426
|
}
|
427
|
}
|
428
|
|
429
|
return false;
|
430
|
}
|
431
|
|
432
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
433
|
/**
|
434
|
* Removes all children Nodes.
|
435
|
*/
|
436
|
public synchronized void detachAll()
|
437
|
{
|
438
|
for(int i=0; i<mNumChildren[0]; i++)
|
439
|
{
|
440
|
mChildren.get(i).mParent = null;
|
441
|
}
|
442
|
|
443
|
if( mNumChildren[0]>0 )
|
444
|
{
|
445
|
ArrayList<Long> prev = generateIDList();
|
446
|
|
447
|
mNumChildren[0] = 0;
|
448
|
mChildren.clear();
|
449
|
RecomputeNodeID(prev);
|
450
|
}
|
451
|
}
|
452
|
|
453
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
454
|
/**
|
455
|
* Returns the DistortedEffects object that's in the Node.
|
456
|
*
|
457
|
* @return The DistortedEffects contained in the Node.
|
458
|
*/
|
459
|
public DistortedEffects getEffects()
|
460
|
{
|
461
|
return mEffects;
|
462
|
}
|
463
|
|
464
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
465
|
/**
|
466
|
* Returns the DistortedTexture object that's in the Node.
|
467
|
*
|
468
|
* @return The DistortedTexture contained in the Node.
|
469
|
*/
|
470
|
public DistortedTexture getTexture()
|
471
|
{
|
472
|
return mTexture;
|
473
|
}
|
474
|
|
475
|
}
|
476
|
|