Project

General

Profile

Download (13.8 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedTree.java @ e7a20702

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
    int numRendered;
53
    DistortedFramebuffer mDF;
54

    
55
    NodeData(long id)
56
      {
57
      ID              = id;
58
      numPointingNodes= 1;
59
      numRendered     = 0;
60
      mDF             = null;
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
      mTexture.setAsInput();
212
      }
213
    else
214
      {
215
      mData.mDF.createFBO();
216

    
217
      if( mData.numRendered==0 )
218
        {
219
        mData.mDF.setAsOutput();
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.setAsInput() )
225
          mEffects.drawNoEffectsPriv(mTexture.mHalfX, mTexture.mHalfY, mGrid, mData.mDF);
226

    
227
        synchronized(this)
228
          {
229
          for(int i=0; i<mNumChildren[0]; i++)
230
            {
231
            mChildren.get(i).drawRecursive(currTime, mData.mDF);
232
            }
233
          }
234
        }
235

    
236
      mData.numRendered++;
237
      mData.numRendered %= mData.numPointingNodes;
238

    
239
      df.setAsOutput();
240
      mData.mDF.setAsInput();
241
      }
242

    
243
    mEffects.drawPriv(mTexture.mHalfX, mTexture.mHalfY, mGrid, df, currTime);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
// PUBLIC API
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
/**
250
 * Constructs new Node of the Tree.
251
 *     
252
 * @param texture DistortedTexture to put into the new Node.
253
 * @param effects DistortedEffects to put into the new Node.
254
 * @param grid GridObject to put into the new Node.
255
 */
256
  public DistortedTree(DistortedTexture texture, DistortedEffects effects, GridObject grid)
257
    {
258
    mTexture= texture;
259
    mEffects= effects;
260
    mGrid   = grid;
261
    mParent = null;
262
    mChildren = null;
263
    mNumChildren = new int[1];
264
    mNumChildren[0] = 0;
265
   
266
    ArrayList<Long> list = new ArrayList<>();
267
    list.add(mTexture.getID());
268

    
269
    mData = mMapNodeID.get(list);
270
   
271
    if( mData!=null )
272
      {
273
      mData.numPointingNodes++;
274
      }
275
    else
276
      {
277
      mData = new NodeData(++mNextNodeID);   
278
      mMapNodeID.put(list, mData);
279
      }
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////  
283
/**
284
 * Copy-constructs new Node of the Tree from another Node.
285
 *     
286
 * @param node The DistortedTree to copy data from.
287
 * @param flags bit field composed of a subset of the following:
288
 *        {@link Distorted#CLONE_BITMAP},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
289
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
290
 *        For example flags = CLONE_BITMAP | CLONE_CHILDREN.
291
 */
292
  public DistortedTree(DistortedTree node, int flags)
293
    {
294
    mParent = null;
295
    mEffects= new DistortedEffects(node.mEffects,flags);
296
    mGrid   = node.mGrid;
297

    
298
    if( (flags & Distorted.CLONE_BITMAP) != 0 )
299
      {
300
      mTexture = node.mTexture;
301
      }
302
    else
303
      {
304
      mTexture = new DistortedTexture(node.mTexture.getWidth(), node.mTexture.getHeight());
305
      }
306
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
307
      {
308
      mChildren = node.mChildren;
309
      mNumChildren = node.mNumChildren;
310
      }
311
    else
312
      {
313
      mChildren = null;
314
      mNumChildren = new int[1];
315
      mNumChildren[0] = 0;
316
      }
317
   
318
    ArrayList<Long> list = generateIDList();
319
   
320
    mData = mMapNodeID.get(list);
321
   
322
    if( mData!=null )
323
      {
324
      mData.numPointingNodes++;
325
      }
326
    else
327
      {
328
      mData = new NodeData(++mNextNodeID);   
329
      mMapNodeID.put(list, mData);
330
      }
331
    }
332
  
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
/**
335
 * Adds a new child to the last position in the list of our Node's children.
336
 * 
337
 * @param node The new Node to add.
338
 */
339
  public synchronized void attach(DistortedTree node)
340
    {
341
    ArrayList<Long> prev = generateIDList(); 
342
   
343
    if( mChildren==null ) mChildren = new ArrayList<>(2);
344
     
345
    node.mParent = this;
346
    mChildren.add(node);
347
    mNumChildren[0]++;
348
     
349
    RecomputeNodeID(prev);
350
    }
351
   
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353
/**
354
 * Adds a new child to the last position in the list of our Node's children.
355
 * 
356
 * @param texture DistortedTexture to initialize our child Node with.
357
 * @param effects DistortedEffects to initialize our child Node with.
358
 * @param grid GridObject to initialize our child Node with.
359
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
360
 */
361
  public synchronized DistortedTree attach(DistortedTexture texture, DistortedEffects effects, GridObject grid)
362
    {
363
    ArrayList<Long> prev = generateIDList(); 
364
      
365
    if( mChildren==null ) mChildren = new ArrayList<>(2);
366
    DistortedTree node = new DistortedTree(texture,effects,grid);
367
    node.mParent = this;
368
    mChildren.add(node);
369
    mNumChildren[0]++;
370
   
371
    RecomputeNodeID(prev);
372

    
373
    return node;
374
    }
375
  
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377
/**
378
 * Removes the first occurrence of a specified child from the list of children of our Node.
379
 * 
380
 * @param node The Node to remove.
381
 * @return <code>true</code> if the child was successfully removed.
382
 */
383
  public synchronized boolean detach(DistortedTree node)
384
    {
385
    if( mNumChildren[0]>0 )
386
      {
387
      ArrayList<Long> prev = generateIDList();  
388
         
389
      if( mChildren.remove(node) )
390
        {
391
        node.mParent = null;  
392
        mNumChildren[0]--;
393
     
394
        RecomputeNodeID(prev);
395
     
396
        return true;
397
        }
398
      }
399
   
400
    return false;
401
    }
402
  
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404
/**
405
 * Removes the first occurrence of a specified child from the list of children of our Node.
406
 * 
407
 * @param effects DistortedEffects to remove.
408
 * @return <code>true</code> if the child was successfully removed.
409
 */
410
  public synchronized boolean detach(DistortedEffects effects)
411
    {
412
    long id = effects.getID();
413
    DistortedTree node;
414
   
415
    for(int i=0; i<mNumChildren[0]; i++)
416
      {
417
      node = mChildren.get(i);
418
     
419
      if( node.mEffects.getID()==id )
420
        {
421
        ArrayList<Long> prev = generateIDList();   
422
     
423
        node.mParent = null;  
424
        mChildren.remove(i);
425
        mNumChildren[0]--;
426
      
427
        RecomputeNodeID(prev);
428
      
429
        return true;
430
        }
431
      }
432
   
433
    return false;
434
    }
435
    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
/**
438
 * Removes all children Nodes.
439
 */
440
  public synchronized void detachAll()
441
    {
442
    for(int i=0; i<mNumChildren[0]; i++)
443
      {
444
      mChildren.get(i).mParent = null;
445
      }
446
   
447
    if( mNumChildren[0]>0 )
448
      {
449
      ArrayList<Long> prev = generateIDList();  
450
      
451
      mNumChildren[0] = 0;
452
      mChildren.clear();
453
      RecomputeNodeID(prev);
454
      }
455
    }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458
/**
459
 * Returns the DistortedEffects object that's in the Node.
460
 * 
461
 * @return The DistortedEffects contained in the Node.
462
 */
463
  public DistortedEffects getEffects()
464
    {
465
    return mEffects;
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469
/**
470
 * Returns the DistortedTexture object that's in the Node.
471
 *
472
 * @return The DistortedTexture contained in the Node.
473
 */
474
  public DistortedTexture getTexture()
475
    {
476
    return mTexture;
477
    }
478

    
479
  }
480

    
(5-5/16)