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.GLES30;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Class which represents a Node in a Tree of (InputSurface,Mesh,Effects) triplets.
|
30
|
* <p>
|
31
|
* Having organized such sets into a Tree, we can then render any Node to any OutputSurface.
|
32
|
* That recursively renders the set held in the Node and all its children.
|
33
|
* <p>
|
34
|
* The class takes special care to only render identical sub-trees once. Each Node holds a reference
|
35
|
* to sub-class 'NodeData'. Two identical sub-trees attached at different points of the main tree
|
36
|
* will point to the same NodeData; only the first of this is rendered (when mData.numRendered==0).
|
37
|
*/
|
38
|
public class DistortedNode implements DistortedAttacheable
|
39
|
{
|
40
|
private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
|
41
|
private static long mNextNodeID =0;
|
42
|
|
43
|
private MeshObject mMesh;
|
44
|
private DistortedEffects mEffects;
|
45
|
private DistortedInputSurface mSurface;
|
46
|
private NodeData mData;
|
47
|
|
48
|
private ArrayList<DistortedNode> mChildren;
|
49
|
private int[] mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
50
|
|
51
|
private class NodeData
|
52
|
{
|
53
|
long ID;
|
54
|
int numPointingNodes;
|
55
|
int numRender;
|
56
|
ArrayList<Long> key;
|
57
|
DistortedFramebuffer mFBO;
|
58
|
|
59
|
NodeData(long id, ArrayList<Long> k)
|
60
|
{
|
61
|
ID = id;
|
62
|
key = k;
|
63
|
numPointingNodes= 1;
|
64
|
numRender =-1;
|
65
|
mFBO = null;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
static synchronized void onDestroy()
|
72
|
{
|
73
|
mNextNodeID = 0;
|
74
|
mMapNodeID.clear();
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
private ArrayList<Long> generateIDList()
|
80
|
{
|
81
|
ArrayList<Long> ret = new ArrayList<>();
|
82
|
|
83
|
ret.add( mSurface.getID() );
|
84
|
|
85
|
if( mNumChildren[0]==0 )
|
86
|
{
|
87
|
ret.add(-mEffects.getID());
|
88
|
}
|
89
|
|
90
|
DistortedNode node;
|
91
|
|
92
|
for(int i=0; i<mNumChildren[0]; i++)
|
93
|
{
|
94
|
node = mChildren.get(i);
|
95
|
ret.add(node.mData.ID);
|
96
|
}
|
97
|
|
98
|
return ret;
|
99
|
}
|
100
|
|
101
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102
|
// Debug - print all the Node IDs
|
103
|
|
104
|
@SuppressWarnings("unused")
|
105
|
void debug(int depth)
|
106
|
{
|
107
|
String tmp="";
|
108
|
int i;
|
109
|
|
110
|
for(i=0; i<depth; i++) tmp +=" ";
|
111
|
tmp += ("NodeID="+mData.ID+" (nodes pointing: "+mData.numPointingNodes+" surfaceID="+mSurface.getID()+")");
|
112
|
|
113
|
android.util.Log.e("NODE", tmp);
|
114
|
|
115
|
for(i=0; i<mNumChildren[0]; i++)
|
116
|
mChildren.get(i).debug(depth+1);
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
// Debug - print contents of the HashMap
|
121
|
|
122
|
@SuppressWarnings("unused")
|
123
|
static void debugMap()
|
124
|
{
|
125
|
NodeData tmp;
|
126
|
|
127
|
for(ArrayList<Long> key: mMapNodeID.keySet())
|
128
|
{
|
129
|
tmp = mMapNodeID.get(key);
|
130
|
android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
void treeIsomorphism()
|
137
|
{
|
138
|
for(int i=0; i<mNumChildren[0]; i++)
|
139
|
{
|
140
|
mChildren.get(i).treeIsomorphism();
|
141
|
}
|
142
|
|
143
|
ArrayList<Long> newList = generateIDList();
|
144
|
NodeData newData = mMapNodeID.get(newList);
|
145
|
|
146
|
if( newData==null )
|
147
|
{
|
148
|
newData = new NodeData(++mNextNodeID,newList);
|
149
|
mMapNodeID.put(newList,newData);
|
150
|
}
|
151
|
else if( newData.ID != mData.ID )
|
152
|
{
|
153
|
newData.numPointingNodes++;
|
154
|
}
|
155
|
|
156
|
if( newData.ID != mData.ID )
|
157
|
{
|
158
|
boolean fboUsed = false;
|
159
|
|
160
|
if( mNumChildren[0]>0 && newData.mFBO==null )
|
161
|
{
|
162
|
if( mData.mFBO!=null )
|
163
|
{
|
164
|
android.util.Log.d("NODE", "copying over FBO of node surfaceID="+mSurface.getID());
|
165
|
|
166
|
newData.mFBO = mData.mFBO;
|
167
|
fboUsed = true;
|
168
|
}
|
169
|
else
|
170
|
{
|
171
|
android.util.Log.d("NODE", "creating new FBO of node surfaceID="+mSurface.getID());
|
172
|
newData.mFBO = new DistortedFramebuffer(mSurface.getWidth(),mSurface.getHeight());
|
173
|
}
|
174
|
}
|
175
|
if( mNumChildren[0]==0 && newData.mFBO!=null )
|
176
|
{
|
177
|
android.util.Log.d("NODE", "deleting FBO of newData node!!");
|
178
|
|
179
|
|
180
|
newData.mFBO.markForDeletion();
|
181
|
newData.mFBO = null;
|
182
|
}
|
183
|
|
184
|
if( --mData.numPointingNodes==0 )
|
185
|
{
|
186
|
mMapNodeID.remove(mData.key);
|
187
|
|
188
|
if( !fboUsed && mData.mFBO!=null )
|
189
|
{
|
190
|
android.util.Log.d("NODE", "deleting FBO of node surfaceID="+mSurface.getID());
|
191
|
|
192
|
mData.mFBO.markForDeletion();
|
193
|
mData.mFBO = null;
|
194
|
}
|
195
|
}
|
196
|
|
197
|
mData = newData;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
202
|
// return the total number of render calls issued
|
203
|
|
204
|
int drawRecursive(int render, long currTime, DistortedOutputSurface surface)
|
205
|
{
|
206
|
int ret = 0;
|
207
|
float halfX = mSurface.getWidth()/2.0f;
|
208
|
float halfY = mSurface.getHeight()/2.0f;
|
209
|
|
210
|
if( mNumChildren[0]<=0 )
|
211
|
{
|
212
|
mSurface.setAsInput();
|
213
|
}
|
214
|
else
|
215
|
{
|
216
|
if( mData.numRender!=render )
|
217
|
{
|
218
|
mData.numRender = render;
|
219
|
mData.mFBO.setAsOutput();
|
220
|
|
221
|
GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
222
|
GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
|
223
|
|
224
|
if( mSurface.setAsInput() )
|
225
|
{
|
226
|
ret++;
|
227
|
DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO);
|
228
|
}
|
229
|
|
230
|
for(int i=0; i<mNumChildren[0]; i++)
|
231
|
{
|
232
|
ret += mChildren.get(i).drawRecursive(render, currTime, mData.mFBO);
|
233
|
}
|
234
|
}
|
235
|
|
236
|
mData.mFBO.setAsInput();
|
237
|
}
|
238
|
|
239
|
mEffects.drawPriv(halfX, halfY, mMesh, surface, currTime);
|
240
|
|
241
|
return ret+1;
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
// PUBLIC API
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
/**
|
248
|
* Constructs new Node.
|
249
|
*
|
250
|
* @param surface InputSurface to put into the new Node.
|
251
|
* @param effects DistortedEffects to put into the new Node.
|
252
|
* @param mesh MeshObject to put into the new Node.
|
253
|
*/
|
254
|
public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
255
|
{
|
256
|
mSurface = surface;
|
257
|
mEffects = effects;
|
258
|
mMesh = mesh;
|
259
|
mChildren = null;
|
260
|
mNumChildren = new int[1];
|
261
|
mNumChildren[0]= 0;
|
262
|
|
263
|
ArrayList<Long> list = new ArrayList<>();
|
264
|
list.add(mSurface.getID());
|
265
|
list.add(-mEffects.getID());
|
266
|
|
267
|
mData = mMapNodeID.get(list);
|
268
|
|
269
|
if( mData!=null )
|
270
|
{
|
271
|
mData.numPointingNodes++;
|
272
|
}
|
273
|
else
|
274
|
{
|
275
|
mData = new NodeData(++mNextNodeID,list);
|
276
|
mMapNodeID.put(list, mData);
|
277
|
}
|
278
|
}
|
279
|
|
280
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281
|
/**
|
282
|
* Copy-constructs new Node from another Node.
|
283
|
*
|
284
|
* @param node The DistortedNode to copy data from.
|
285
|
* @param flags bit field composed of a subset of the following:
|
286
|
* {@link Distorted#CLONE_SURFACE}, {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
|
287
|
* {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
|
288
|
* For example flags = CLONE_SURFACE | CLONE_CHILDREN.
|
289
|
*/
|
290
|
public DistortedNode(DistortedNode node, int flags)
|
291
|
{
|
292
|
mEffects= new DistortedEffects(node.mEffects,flags);
|
293
|
mMesh = node.mMesh;
|
294
|
|
295
|
if( (flags & Distorted.CLONE_SURFACE) != 0 )
|
296
|
{
|
297
|
mSurface = node.mSurface;
|
298
|
}
|
299
|
else
|
300
|
{
|
301
|
int w = node.mSurface.getWidth();
|
302
|
int h = node.mSurface.getHeight();
|
303
|
|
304
|
if( node.mSurface instanceof DistortedTexture )
|
305
|
{
|
306
|
mSurface = new DistortedTexture(w,h);
|
307
|
}
|
308
|
else if( node.mSurface instanceof DistortedFramebuffer )
|
309
|
{
|
310
|
boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
|
311
|
mSurface = new DistortedFramebuffer(w,h,hasDepth);
|
312
|
}
|
313
|
}
|
314
|
if( (flags & Distorted.CLONE_CHILDREN) != 0 )
|
315
|
{
|
316
|
if( node.mChildren==null ) // do NOT copy over the NULL!
|
317
|
{
|
318
|
node.mChildren = new ArrayList<>(2);
|
319
|
}
|
320
|
|
321
|
mChildren = node.mChildren;
|
322
|
mNumChildren = node.mNumChildren;
|
323
|
}
|
324
|
else
|
325
|
{
|
326
|
mChildren = null;
|
327
|
mNumChildren = new int[1];
|
328
|
mNumChildren[0] = 0;
|
329
|
}
|
330
|
|
331
|
ArrayList<Long> list = generateIDList();
|
332
|
|
333
|
mData = mMapNodeID.get(list);
|
334
|
|
335
|
if( mData!=null )
|
336
|
{
|
337
|
mData.numPointingNodes++;
|
338
|
}
|
339
|
else
|
340
|
{
|
341
|
mData = new NodeData(++mNextNodeID,list);
|
342
|
mMapNodeID.put(list, mData);
|
343
|
}
|
344
|
}
|
345
|
|
346
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
347
|
/**
|
348
|
* Adds a new child to the last position in the list of our Node's children.
|
349
|
* <p>
|
350
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
351
|
* DistortedAttachDeamon (by calling attachNow())
|
352
|
*
|
353
|
* @param node The new Node to add.
|
354
|
*/
|
355
|
public void attach(DistortedNode node)
|
356
|
{
|
357
|
DistortedAttachDaemon.attach(this,node);
|
358
|
}
|
359
|
|
360
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
361
|
/**
|
362
|
* Adds a new child to the last position in the list of our Node's children.
|
363
|
* <p>
|
364
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
365
|
* DistortedAttachDeamon (by calling attachNow())
|
366
|
*
|
367
|
* @param surface InputSurface to initialize our child Node with.
|
368
|
* @param effects DistortedEffects to initialize our child Node with.
|
369
|
* @param mesh MeshObject to initialize our child Node with.
|
370
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
371
|
*/
|
372
|
public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
|
373
|
{
|
374
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
375
|
DistortedAttachDaemon.attach(this,node);
|
376
|
return node;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
/**
|
381
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
382
|
* DistortedAttacheable interface, which should really be a class that we extend here instead but
|
383
|
* Java has no multiple inheritance.
|
384
|
*
|
385
|
* @param node The new Node to add.
|
386
|
*/
|
387
|
public void attachNow(DistortedNode node)
|
388
|
{
|
389
|
if( mChildren==null ) mChildren = new ArrayList<>(2);
|
390
|
|
391
|
mChildren.add(node);
|
392
|
mNumChildren[0]++;
|
393
|
}
|
394
|
|
395
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
396
|
/**
|
397
|
* Removes the first occurrence of a specified child from the list of children of our Node.
|
398
|
* <p>
|
399
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
400
|
* DistortedAttachDeamon (by calling detachNow())
|
401
|
*
|
402
|
* @param node The Node to remove.
|
403
|
*/
|
404
|
public void detach(DistortedNode node)
|
405
|
{
|
406
|
DistortedAttachDaemon.detach(this,node);
|
407
|
}
|
408
|
|
409
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
410
|
/**
|
411
|
* Removes the first occurrence of a specified child from the list of children of our Node.
|
412
|
* <p>
|
413
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
414
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
415
|
* <p>
|
416
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
417
|
* DistortedAttachDeamon (by calling detachNow())
|
418
|
*
|
419
|
* @param effects DistortedEffects to remove.
|
420
|
*/
|
421
|
public void detach(DistortedEffects effects)
|
422
|
{
|
423
|
long id = effects.getID();
|
424
|
DistortedNode node;
|
425
|
|
426
|
for(int i=0; i<mNumChildren[0]; i++)
|
427
|
{
|
428
|
node = mChildren.get(i);
|
429
|
|
430
|
if( node.mEffects.getID()==id )
|
431
|
{
|
432
|
DistortedAttachDaemon.detach(this,node);
|
433
|
break;
|
434
|
}
|
435
|
}
|
436
|
}
|
437
|
|
438
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439
|
/**
|
440
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
441
|
* DistortedAttacheable interface, which should really be a class that we extend here instead but
|
442
|
* Java has no multiple inheritance.
|
443
|
*
|
444
|
* @param node The Node to remove.
|
445
|
*/
|
446
|
public void detachNow(DistortedNode node)
|
447
|
{
|
448
|
if( mNumChildren[0]>0 && mChildren.remove(node) )
|
449
|
{
|
450
|
mNumChildren[0]--;
|
451
|
|
452
|
if( mNumChildren[0]==0 && mData.mFBO!=null )
|
453
|
{
|
454
|
mData.mFBO.markForDeletion();
|
455
|
mData.mFBO = null;
|
456
|
}
|
457
|
}
|
458
|
}
|
459
|
|
460
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
461
|
/**
|
462
|
* Removes all children Nodes.
|
463
|
* <p>
|
464
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
465
|
* DistortedAttachDeamon (by calling detachAllNow())
|
466
|
*/
|
467
|
public void detachAll()
|
468
|
{
|
469
|
DistortedAttachDaemon.detachAll(this);
|
470
|
}
|
471
|
|
472
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
473
|
/**
|
474
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
475
|
* DistortedAttacheable interface, which should really be a class that we extend here instead but
|
476
|
* Java has no multiple inheritance.
|
477
|
*/
|
478
|
public void detachAllNow()
|
479
|
{
|
480
|
if( mNumChildren[0]>0 )
|
481
|
{
|
482
|
mNumChildren[0] = 0;
|
483
|
mChildren.clear();
|
484
|
|
485
|
if( mData.mFBO!=null )
|
486
|
{
|
487
|
mData.mFBO.markForDeletion();
|
488
|
mData.mFBO = null;
|
489
|
}
|
490
|
}
|
491
|
}
|
492
|
|
493
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
494
|
/**
|
495
|
* Returns the DistortedEffects object that's in the Node.
|
496
|
*
|
497
|
* @return The DistortedEffects contained in the Node.
|
498
|
*/
|
499
|
public DistortedEffects getEffects()
|
500
|
{
|
501
|
return mEffects;
|
502
|
}
|
503
|
|
504
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
505
|
/**
|
506
|
* Returns the DistortedInputSurface object that's in the Node.
|
507
|
*
|
508
|
* @return The DistortedInputSurface contained in the Node.
|
509
|
*/
|
510
|
public DistortedInputSurface getSurface()
|
511
|
{
|
512
|
return mSurface;
|
513
|
}
|
514
|
|
515
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
516
|
/**
|
517
|
* Returns the DistortedFramebuffer object that's in the Node.
|
518
|
*
|
519
|
* @return The DistortedFramebuffer contained in the Node.
|
520
|
*/
|
521
|
public DistortedFramebuffer getFramebuffer()
|
522
|
{
|
523
|
return mData.mFBO;
|
524
|
}
|
525
|
|
526
|
}
|