Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ f8377ef8

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
 *  
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
 */
34
public class DistortedNode
35
  {
36
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
37
  private static long mNextNodeID =0;
38

    
39
  private MeshObject mMesh;
40
  private DistortedEffects mEffects;
41
  private DistortedInputSurface mSurface;
42
  private NodeData mData;
43

    
44
  private DistortedNode mParent;
45
  private ArrayList<DistortedNode> 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 mFBO;
54

    
55
    NodeData(long id)
56
      {
57
      ID              = id;
58
      numPointingNodes= 1;
59
      numRendered     = 0;
60
      mFBO            = null;
61

    
62
      android.util.Log.e("DistortedNode", "new NodeData");
63
      }
64
    }
65
 
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  static synchronized void onDestroy()
69
    {
70
    mNextNodeID = 0;
71
    mMapNodeID.clear();
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
// tree isomorphism
76
  
77
  private void RecomputeNodeID(ArrayList<Long> prev)
78
    {
79
    ArrayList<Long> curr = generateIDList();
80
     
81
    if( mParent==null )
82
      {
83
      adjustNodeData(prev,curr);
84
      }
85
    else
86
      {
87
      ArrayList<Long> parentPrev = mParent.generateIDList();
88
      adjustNodeData(prev,curr);
89
      mParent.RecomputeNodeID(parentPrev);
90
      }
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  private ArrayList<Long> generateIDList()
96
    {
97
    ArrayList<Long> ret = new ArrayList<>();
98
     
99
    ret.add( mSurface.getID() );
100
    DistortedNode node;
101
   
102
    for(int i=0; i<mNumChildren[0]; i++)
103
      {
104
      node = mChildren.get(i);
105
      ret.add(node.mData.ID);
106
      }
107
   
108
    return ret;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void adjustNodeData(ArrayList<Long> oldList, ArrayList<Long> newList)
114
    {
115
    boolean otherNodesPoint = (mData.numPointingNodes>1);
116

    
117
    if( otherNodesPoint ) mData.numPointingNodes--;
118
    else                  mMapNodeID.remove(oldList);
119

    
120
    NodeData newData = mMapNodeID.get(newList);
121
    
122
    if( newData==null )
123
      {
124
      if( otherNodesPoint )  mData = new NodeData(++mNextNodeID);
125
      else                   mData.ID = ++mNextNodeID;  // numPointingNodes must be 1 already
126

    
127
      if( newList.size()>1 )
128
        {
129
        if( mData.mFBO ==null )
130
          {
131
          android.util.Log.e("DistortedNode", "creating a new FBO");
132

    
133
          mData.mFBO = new DistortedFramebuffer(mSurface.getWidth(), mSurface.getHeight());
134
          }
135
        }
136
      else
137
        {
138
        if( mData.mFBO !=null )
139
          {
140
          android.util.Log.e("DistortedNode", "marking FBO for deletion and setting it to NULL");
141

    
142
          mData.mFBO.markForDeletion();
143
          mData.mFBO = null;
144
          }
145
        else
146
          {
147
          android.util.Log.e("DistortedNode", "adjustNodeData: impossible situation??");
148
          }
149
        }
150

    
151
      mMapNodeID.put(newList, mData);
152
      }
153
    else
154
      {
155
      newData.numPointingNodes++;
156
      mData = newData;
157
      }
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////  
161
// this will be called on startup and every time OpenGL context has been lost
162

    
163
  static void reset()
164
    {
165
    NodeData tmp;   
166
     
167
    for(ArrayList<Long> key: mMapNodeID.keySet())
168
      {
169
      tmp = mMapNodeID.get(key);
170
          
171
      if( tmp.mFBO != null ) tmp.numRendered = 0;
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// Debug - print all the Node IDs
177
/*
178
  void debug(int depth)
179
    {
180
    String tmp="";
181
    int i;
182

    
183
    for(i=0; i<depth; i++) tmp +="   ";
184
    tmp += (mData.ID+" (nodes: "+mData.numPointingNodes+")");
185

    
186
    android.util.Log.e("NODE", tmp);
187

    
188
    for(i=0; i<mNumChildren[0]; i++)
189
      mChildren.get(i).debug(depth+1);
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// Debug - print contents of the HashMap
194

    
195
  static void debugMap()
196
    {
197
    NodeData tmp;
198

    
199
    for(ArrayList<Long> key: mMapNodeID.keySet())
200
      {
201
      tmp = mMapNodeID.get(key);
202

    
203
      android.util.Log.e("NODE", "key="+key+" NodeID: "+tmp.ID);
204
      }
205
    }
206
*/
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  void drawRecursive(long currTime, DistortedOutputSurface surface)
210
    {
211
    float halfX = mSurface.getWidth()/2.0f;
212
    float halfY = mSurface.getHeight()/2.0f;
213

    
214
    if( mNumChildren[0]<=0 )
215
      {
216
      mSurface.setAsInput();
217
      }
218
    else
219
      {
220
      if( mData.numRendered==0 )
221
        {
222
        mData.mFBO.setAsOutput();
223

    
224
        GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
225
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
226

    
227
        if( mSurface.setAsInput() )
228
          DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO );
229

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

    
236
      mData.numRendered++;
237
      mData.numRendered %= mData.numPointingNodes;
238
      mData.mFBO.setAsInput();
239
      }
240

    
241
    mEffects.drawPriv(halfX, halfY, mMesh, surface, currTime);
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
    mParent        = null;
260
    mChildren      = null;
261
    mNumChildren   = new int[1];
262
    mNumChildren[0]= 0;
263
   
264
    ArrayList<Long> list = new ArrayList<>();
265
    list.add(mSurface.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);   
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
    mParent = null;
293
    mEffects= new DistortedEffects(node.mEffects,flags);
294
    mMesh = node.mMesh;
295

    
296
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
297
      {
298
      mSurface = node.mSurface;
299
      }
300
    else
301
      {
302
      int w = node.mSurface.getWidth();
303
      int h = node.mSurface.getHeight();
304

    
305
      if( node.mSurface instanceof DistortedTexture )
306
        {
307
        mSurface = new DistortedTexture(w,h);
308
        }
309
      else if( node.mSurface instanceof DistortedFramebuffer )
310
        {
311
        boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
312
        mSurface = new DistortedFramebuffer(w,h,hasDepth);
313
        }
314
      }
315
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
316
      {
317
      mChildren = node.mChildren;
318
      mNumChildren = node.mNumChildren;
319
      }
320
    else
321
      {
322
      mChildren = null;
323
      mNumChildren = new int[1];
324
      mNumChildren[0] = 0;
325
      }
326
   
327
    ArrayList<Long> list = generateIDList();
328
   
329
    mData = mMapNodeID.get(list);
330
   
331
    if( mData!=null )
332
      {
333
      mData.numPointingNodes++;
334
      }
335
    else
336
      {
337
      mData = new NodeData(++mNextNodeID);   
338
      mMapNodeID.put(list, mData);
339
      }
340
    }
341
  
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
/**
344
 * Adds a new child to the last position in the list of our Node's children.
345
 * 
346
 * @param node The new Node to add.
347
 */
348
  public synchronized void attach(DistortedNode node)
349
    {
350
    ArrayList<Long> prev = generateIDList(); 
351
   
352
    if( mChildren==null ) mChildren = new ArrayList<>(2);
353

    
354
    android.util.Log.e("DistortedNode", "ATTACH1");
355

    
356

    
357
    node.mParent = this;
358
    mChildren.add(node);
359
    mNumChildren[0]++;
360
     
361
    RecomputeNodeID(prev);
362
    }
363
   
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
/**
366
 * Adds a new child to the last position in the list of our Node's children.
367
 * 
368
 * @param surface InputSurface to initialize our child Node with.
369
 * @param effects DistortedEffects to initialize our child Node with.
370
 * @param mesh MeshObject to initialize our child Node with.
371
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
372
 */
373
  public synchronized DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
374
    {
375
    ArrayList<Long> prev = generateIDList(); 
376

    
377
    android.util.Log.e("DistortedNode", "ATTACH2");
378

    
379
    if( mChildren==null ) mChildren = new ArrayList<>(2);
380
    DistortedNode node = new DistortedNode(surface,effects,mesh);
381
    node.mParent = this;
382
    mChildren.add(node);
383
    mNumChildren[0]++;
384
   
385
    RecomputeNodeID(prev);
386

    
387
    return node;
388
    }
389
  
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
/**
392
 * Removes the first occurrence of a specified child from the list of children of our Node.
393
 * 
394
 * @param node The Node to remove.
395
 * @return <code>true</code> if the child was successfully removed.
396
 */
397
  public synchronized boolean detach(DistortedNode node)
398
    {
399
    if( mNumChildren[0]>0 )
400
      {
401
      ArrayList<Long> prev = generateIDList();  
402
         
403
      if( mChildren.remove(node) )
404
        {
405
android.util.Log.e("DistortedNode", "DETACH1");
406

    
407
        node.mParent = null;
408
        mNumChildren[0]--;
409
     
410
        RecomputeNodeID(prev);
411
     
412
        return true;
413
        }
414
      }
415
   
416
    return false;
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420
/**
421
 * Removes the first occurrence of a specified child from the list of children of our Node.
422
 * <p>
423
 * A bit questionable method as there can be many different Nodes attached as children, some
424
 * of them having the same Effects but - for instance - different Mesh. Use with care.
425
 *
426
 * @param effects DistortedEffects to remove.
427
 * @return <code>true</code> if the child was successfully removed.
428
 */
429
  public synchronized boolean detach(DistortedEffects effects)
430
    {
431
    long id = effects.getID();
432
    DistortedNode node;
433

    
434
    for(int i=0; i<mNumChildren[0]; i++)
435
      {
436
      node = mChildren.get(i);
437

    
438
      if( node.mEffects.getID()==id )
439
        {
440
android.util.Log.e("DistortedNode", "DETACH2");
441

    
442

    
443
        ArrayList<Long> prev = generateIDList();
444

    
445
        node.mParent = null;
446
        mChildren.remove(i);
447
        mNumChildren[0]--;
448

    
449
        RecomputeNodeID(prev);
450

    
451
        return true;
452
        }
453
      }
454

    
455
    return false;
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459
/**
460
 * Removes all children Nodes.
461
 */
462
  public synchronized void detachAll()
463
    {
464
    for(int i=0; i<mNumChildren[0]; i++)
465
      {
466
      mChildren.get(i).mParent = null;
467
      }
468
   
469
    if( mNumChildren[0]>0 )
470
      {
471
      ArrayList<Long> prev = generateIDList();  
472
      
473
      mNumChildren[0] = 0;
474
      mChildren.clear();
475
      RecomputeNodeID(prev);
476
      }
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480
/**
481
 * Returns the DistortedEffects object that's in the Node.
482
 * 
483
 * @return The DistortedEffects contained in the Node.
484
 */
485
  public DistortedEffects getEffects()
486
    {
487
    return mEffects;
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491
/**
492
 * Returns the DistortedInputSurface object that's in the Node.
493
 *
494
 * @return The DistortedInputSurface contained in the Node.
495
 */
496
  public DistortedInputSurface getSurface()
497
    {
498
    return mSurface;
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502
/**
503
 * Returns the DistortedFramebuffer object that's in the Node.
504
 *
505
 * @return The DistortedFramebuffer contained in the Node.
506
 */
507
  public DistortedFramebuffer getFramebuffer()
508
    {
509
    return mData.mFBO;
510
    }
511

    
512
  }
(5-5/20)