Project

General

Profile

« Previous | Next » 

Revision b455eb48

Added by Leszek Koltunski over 7 years ago

Rename various classes; fix a bug in Around the World.

View differences:

src/main/java/org/distorted/library/Distorted.java
75 75
   */
76 76
  public static final int CLONE_FRAGMENT= 0x8;
77 77
  /**
78
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
78
   * When creating an instance of a DistortedObjectTree from another instance, clone the children Nodes.
79 79
   * <p>
80 80
   * This is mainly useful for creating many similar sub-trees of Bitmaps and rendering then at different places
81 81
   * on the screen, with (optionally) different effects of the top-level root Bitmap.   
......
355 355
    GLES20.glEnableVertexAttribArray(mTextureCoordH);
356 356
   
357 357
    DistortedObject.reset();
358
    DistortedNode.reset();
358
    DistortedObjectTree.reset();
359 359
    EffectMessageSender.startSending();
360 360
    }
361 361
  
......
381 381
    {
382 382
    DistortedObject.release();
383 383
    DistortedFramebuffer.release();
384
    DistortedNode.release();
384
    DistortedObjectTree.release();
385 385
    EffectQueue.release();
386 386
    EffectMessageSender.stopSending();
387 387
   
src/main/java/org/distorted/library/DistortedBitmapGrid.java
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.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
public class DistortedBitmapGrid extends DistortedObjectGrid
28
  {
29
  private int mCols, mRows;
30
  private int remainingVert;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
// Create a flat, full grid. cols and rows is guaranteed (by DistortedBitmap constructor)
34
// to be in 1,2,...,256.
35

  
36
   private void computeNumberOfVertices(int cols, int rows)
37
     {
38
     mRows=rows;
39
     mCols=cols;
40

  
41
     if( cols==1 && rows==1 )
42
       {
43
       dataLength = 4;
44
       }
45
     else
46
       {
47
       dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
48
                    (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
49
       }
50

  
51
     //android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
52

  
53
     remainingVert = dataLength;
54
     }
55

  
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
   private int addVertex(int vertex, int col, int row, float[] position, float[] normal, float[] texture)
60
     {
61
     remainingVert--;
62

  
63
     float x= (float)col/mCols;
64
     float y= (float)row/mRows;
65

  
66
     position[3*vertex  ] = x-0.5f;
67
     position[3*vertex+1] = 0.5f-y;
68
     position[3*vertex+2] = 0;
69

  
70
     texture[2*vertex  ]  = x;
71
     texture[2*vertex+1]  = 1.0f-y;
72

  
73
     normal[3*vertex  ]   = 0.0f;
74
     normal[3*vertex+1]   = 0.0f;
75
     normal[3*vertex+2]   = 1.0f;
76

  
77
     return vertex+1;
78
     }
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

  
81
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
82
     {
83
     remainingVert--;
84

  
85
     //android.util.Log.e("BITMAP", "repeating last vertex!");
86

  
87
     if( vertex>0 )
88
       {
89
       position[3*vertex  ] = position[3*vertex-3];
90
       position[3*vertex+1] = position[3*vertex-2];
91
       position[3*vertex+2] = position[3*vertex-1];
92

  
93
       normal[3*vertex  ]   = normal[3*vertex-3];
94
       normal[3*vertex+1]   = normal[3*vertex-2];
95
       normal[3*vertex+2]   = normal[3*vertex-1];
96

  
97
       texture[2*vertex  ]  = texture[2*vertex-2];
98
       texture[2*vertex+1]  = texture[2*vertex-1];
99

  
100
       vertex++;
101
       }
102

  
103
     return vertex;
104
     }
105

  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

  
108
   private void buildGrid(float[] position, float[] normal, float[] texture)
109
     {
110
     boolean lastBlockIsNE = false;
111
     boolean currentBlockIsNE;
112
     int vertex = 0;
113

  
114
     //android.util.Log.d("BITMAP", "buildGrid");
115

  
116
     for(int row=0; row<mRows; row++)
117
       {
118
       for(int col=0; col<mCols; col++)
119
         {
120
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
121

  
122
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
123
           {
124
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
125
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?0:1), position, normal, texture);
126
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
127
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
128
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?1:0), position, normal, texture);
129
           }
130
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?0:1), position, normal, texture);
131
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?1:0), position, normal, texture);
132

  
133
         lastBlockIsNE = currentBlockIsNE;
134
         }
135
       }
136

  
137
     //android.util.Log.d("BITMAP", "buildGrid done");
138
     }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
/*
142
   private static String debug(float[] val, int stop)
143
     {
144
     String ret="";
145

  
146
     for(int i=0; i<val.length; i++)
147
        {
148
        if( i%stop==0 ) ret+="\n";
149
        ret+=(" "+val[i]);
150
        }
151

  
152
     return ret;
153
     }
154
*/
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// PUBLIC API
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
/**
159
 * Creates the underlying grid of vertices, normals and texture coords.
160
 *
161
 * @param cols Number of columns in the grid.
162
 * @param rows Number of rows in the grid.
163
 */
164
   public DistortedBitmapGrid(int cols, int rows)
165
      {
166
      computeNumberOfVertices(cols,rows);
167

  
168
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
169
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
170
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
171

  
172
      buildGrid(positionData,normalData,textureData);
173

  
174
      //android.util.Log.e("CUBES","dataLen="+dataLength);
175
      //android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
176
      //android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
177
      //android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
178

  
179
      if( remainingVert!=0 )
180
        android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
181

  
182
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
183
      mGridPositions.put(positionData).position(0);
184

  
185
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
186
      mGridNormals.put(normalData).position(0);
187

  
188
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
189
      mGridTexture.put(textureData).position(0);
190
      }
191
  }
src/main/java/org/distorted/library/DistortedCubesGrid.java
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.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24
import java.util.ArrayList;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
public class DistortedCubesGrid extends DistortedObjectGrid
29
   {
30
   private static final float R = 0.0f;//0.2f;
31
   private static final float FRONTZ = 0.5f;
32
   private static final float BACKZ  =-0.5f;
33
   
34
   private static final int NORTH = 0;
35
   private static final int WEST  = 1;
36
   private static final int EAST  = 2;
37
   private static final int SOUTH = 3;
38

  
39
   private static final boolean BACK  = true;
40
   private static final boolean FRONT = false;
41
   private static final boolean UPPER = false;
42
   private static final boolean LOWER = true;
43
   
44
   private static final float[] mNormalX = new float[4];
45
   private static final float[] mNormalY = new float[4];
46
   private static final float[] mNormalZ = new float[4];
47

  
48
   private class Edge
49
     {
50
     final int side; 
51
     final int row;
52
     final int col;
53
     
54
     Edge(int s, int r, int c)
55
       {
56
       side= s; 
57
       row = r;
58
       col = c;
59
       }
60
     }
61
   
62
   private int mCols, mRows;
63
   private short[][] mCubes;
64
   private ArrayList<Edge> mEdges = new ArrayList<>();
65

  
66
   private int remainingVert;
67
   private int mSideBends;
68
   private int mEdgeNum;
69
   private int mSideWalls;
70

  
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
73
// or bottom-left quadrant of the grid.
74

  
75
   private boolean isNE(int row,int col)
76
     {
77
     return ( (2*row<mRows)^(2*col<mCols) );
78
     }
79

  
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// return the number of vertices our grid will contain
82

  
83
   private int computeDataLength(boolean frontOnly)
84
      {
85
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
86
      int shiftCol = (mCols-1)/2;
87

  
88
      boolean lastBlockIsNE=false;
89
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
90
                                    // two triangles along the NE-SW line (rather than NW-SE)
91
      for(int row=0; row<mRows; row++)
92
        {
93
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
94

  
95
        for(int col=0; col<mCols; col++)
96
          {
97
          if( mCubes[row][col]%2 == 1 )  // land
98
            {
99
            thisBlockIsNE = isNE(row,col);
100
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
101
            lastBlockIsNE = thisBlockIsNE;
102
            frontWalls++;
103
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
104
            }
105
          }
106
        }
107

  
108
      int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
109
      int sideVert  = 2*( mSideWalls + mSideBends + mEdgeNum -1);
110
      int firstWinding= (!frontOnly && (frontVert+1)%2==1 ) ? 1:0;
111
      int dataL = frontOnly ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
112
/*
113
      android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
114
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
115
*/
116
      return dataL<0 ? 0:dataL;
117
      }
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
/*
121
   private static String debug(short[] val)
122
     {
123
     String ret="";j
124
     
125
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
126
     
127
     return ret;
128
     }
129
*/
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/*
132
   private static String debug(float[] val, int stop)
133
     {
134
     String ret="";
135

  
136
     for(int i=0; i<val.length; i++) 
137
        {
138
        if( i%stop==0 ) ret+="\n";
139
        ret+=(" "+val[i]);
140
        }
141

  
142
     return ret;
143
     }
144
*/  
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
/*
147
   private static String debug(Edge e)
148
     {
149
     String d = "";
150
     
151
     switch(e.side)
152
       {
153
       case NORTH: d+="NORTH "; break;
154
       case SOUTH: d+="SOUTH "; break;
155
       case WEST : d+="WEST  "; break;
156
       case EAST : d+="EAST  "; break;
157
       }
158
     
159
     d+=("("+e.row+","+e.col+")");
160
     
161
     return d;
162
     }   
163
*/
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// desc is guaranteed to be padded with 0s in the end (DistortedCubes constructor does it)
166

  
167
   private void prepareDataStructures(int cols, String desc, boolean frontOnly)
168
     {
169
     mRows     =0;
170
     mCols     =0;
171
     dataLength=0;
172
     
173
     if( cols>0 && desc.contains("1") )
174
       {
175
       mCols = cols;
176
       mRows = desc.length()/cols;
177

  
178
       mCubes = new short[mRows][mCols];
179
       
180
       for(int j=0; j<mCols; j++)
181
         for(int i=0; i<mRows; i++)
182
           mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0);
183
       
184
       markRegions();
185
       dataLength = computeDataLength(frontOnly);
186

  
187
       remainingVert = dataLength;
188
       }
189
     }
190

  
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
// full grid
193

  
194
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
195
     {
196
     mRows     =rows;
197
     mCols     =cols;
198
     dataLength=   0;
199

  
200
     if( cols>0 && rows>0 )
201
       {
202
       mCubes = new short[mRows][mCols];
203

  
204
       for(int j=0; j<mCols; j++)
205
         for(int i=0; i<mRows; i++)
206
           mCubes[i][j] = (short)1;
207

  
208
       markRegions();
209
       dataLength = computeDataLength(frontOnly);
210

  
211
       remainingVert = dataLength;
212
       }
213
     }
214

  
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
217
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
218
// gets a unique odd integer, each connected block of water a unique even integer.
219
//
220
// Water on the edges of the grid is also considered connected to itself!   
221
//   
222
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
223
// will start building the side walls of each connected block of land (and sides of holes of water
224
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
225
// later on setting up normal vectors wouldn't work.
226
   
227
   private void markRegions()
228
     {
229
     int i, j, numWater=1, numLand=0;
230
     
231
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
232
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
233
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
234
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
235
           
236
     for(i=0; i<mRows; i++)
237
        for(j=0; j<mCols; j++)
238
           {
239
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
240
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
241
           }
242
     
243
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
244
     //
245
     // 0 1 0
246
     // 1 0 1
247
     // 0 1 0
248
     
249
     mEdgeNum= mEdges.size();
250
     int initCol, initRow, initSide, lastSide;
251
     Edge e1,e2;
252
     
253
     for(i=0; i<mEdgeNum; i++)
254
       {
255
       e1 = mEdges.get(i);
256
       initRow= e1.row;
257
       initCol= e1.col;
258
       initSide=e1.side;
259

  
260
       do
261
         {
262
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
263

  
264
         mSideWalls++;
265

  
266
         if( e1.side==NORTH || e1.side==SOUTH )
267
           {
268
           for(j=i+1;j<mEdgeNum;j++)
269
             {
270
             e2 = mEdges.get(j);
271

  
272
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
273
               {
274
               mEdges.remove(j);
275
               mEdgeNum--;
276
               j--;
277

  
278
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
279
               }
280
             }
281
           }
282

  
283
         lastSide = e1.side;
284
         e1 = getNextEdge(e1);
285
         if( e1.side!=lastSide ) mSideBends++;
286
         }
287
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
288
       }
289
     }
290

  
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
// when calling, make sure that newVal != val
293
   
294
   private void markRegion(short newVal, int row, int col)
295
     {
296
     short val = mCubes[row][col];
297
     mCubes[row][col] = newVal;
298
     
299
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
300
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
301
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
302
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
303
     }
304
   
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
   
307
   private void createNormals(boolean front, int row, int col)
308
     {
309
     int td,lr; 
310
      
311
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
312
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
313
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
314
     int c  =                                (mCubes[row  ][col  ]%2);
315
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
316
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
317
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
318
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
319
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
320

  
321
     if(front)
322
       {
323
       mNormalZ[0] = 1.0f;
324
       mNormalZ[1] = 1.0f;
325
       mNormalZ[2] = 1.0f;
326
       mNormalZ[3] = 1.0f;
327
       }
328
     else
329
       {
330
       mNormalZ[0] =-1.0f;
331
       mNormalZ[1] =-1.0f;
332
       mNormalZ[2] =-1.0f;
333
       mNormalZ[3] =-1.0f;
334
       }
335

  
336
     td = nw+n-w-c;
337
     lr = c+n-w-nw;
338
     if( td<0 ) td=-1;
339
     if( td>0 ) td= 1;
340
     if( lr<0 ) lr=-1;
341
     if( lr>0 ) lr= 1;
342
     mNormalX[0] = lr*R;
343
     mNormalY[0] = td*R;
344
     
345
     td = w+c-sw-s;
346
     lr = c+s-w-sw;
347
     if( td<0 ) td=-1;
348
     if( td>0 ) td= 1;
349
     if( lr<0 ) lr=-1;
350
     if( lr>0 ) lr= 1;
351
     mNormalX[1] = lr*R;
352
     mNormalY[1] = td*R;
353
     
354
     td = n+ne-c-e;
355
     lr = e+ne-c-n;
356
     if( td<0 ) td=-1;
357
     if( td>0 ) td= 1;
358
     if( lr<0 ) lr=-1;
359
     if( lr>0 ) lr= 1;
360
     mNormalX[2] = lr*R;
361
     mNormalY[2] = td*R;
362
     
363
     td = c+e-s-se;
364
     lr = e+se-c-s;
365
     if( td<0 ) td=-1;
366
     if( td>0 ) td= 1;
367
     if( lr<0 ) lr=-1;
368
     if( lr>0 ) lr= 1;
369
     mNormalX[3] = lr*R;
370
     mNormalY[3] = td*R;
371
     /*
372
     android.util.Log.d("CUBES", "row="+row+" col="+col);
373
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
374
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
375
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
376
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
377
     */
378
     }
379
   
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

  
382
   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
383
     {
384
     remainingVert--;
385

  
386
     float x = (float)col/mCols;
387
     float y = (float)row/mRows;
388

  
389
     position[3*vertex  ] = x-0.5f;
390
     position[3*vertex+1] = 0.5f-y;
391
     position[3*vertex+2] = vectZ;
392
     normal[3*vertex  ]   = mNormalX[index];
393
     normal[3*vertex+1]   = mNormalY[index];
394
     normal[3*vertex+2]   = mNormalZ[index];
395
     texture[2*vertex  ]  = x;
396
     texture[2*vertex+1]  = 1.0f-y;
397

  
398
     return vertex+1;
399
     }
400

  
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

  
403
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
404
     {
405
     short last, current;
406
     boolean seenLand=false;
407
     boolean lastBlockIsNE = false;
408
     boolean currentBlockIsNE;
409
     float vectZ = front?FRONTZ:BACKZ;
410

  
411
     //android.util.Log.d("CUBES", "buildFrontBack");
412

  
413
     for(int row=0; row<mRows; row++)
414
       {
415
       last =0;
416
         
417
       for(int col=0; col<mCols; col++)
418
         {
419
         current = mCubes[row][col];
420

  
421
         if( current%2 == 1 )
422
           {
423
           currentBlockIsNE = isNE(row,col);
424

  
425
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
426
             {
427
             //android.util.Log.d("CUBES","repeating winding2 vertex");
428

  
429
             vertex = repeatLast(vertex,position,normal,texture);
430
             }
431

  
432
           createNormals(front,row,col);
433

  
434
           if( currentBlockIsNE )
435
             {
436
             if( (last!=current) || !lastBlockIsNE )
437
               {
438
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
439
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
440
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
441
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
442
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
443
               }
444
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
445
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
446
             }
447
           else
448
             {
449
             if( (last!=current) || lastBlockIsNE )
450
               {
451
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
452
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
453
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
454
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
455
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
456
               }
457
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
458
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
459
             }
460

  
461
           seenLand = true;
462
           lastBlockIsNE = currentBlockIsNE;
463
           }
464
            
465
         last = current;
466
         }
467
       }
468
     
469
     return vertex;
470
     }
471

  
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

  
474
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
475
     {
476
     //android.util.Log.e("CUBES", "repeating last vertex!");
477

  
478
     if( vertex>0 )
479
       {
480
       remainingVert--;
481

  
482
       position[3*vertex  ] = position[3*vertex-3];
483
       position[3*vertex+1] = position[3*vertex-2];
484
       position[3*vertex+2] = position[3*vertex-1];
485

  
486
       normal[3*vertex  ]   = normal[3*vertex-3];
487
       normal[3*vertex+1]   = normal[3*vertex-2];
488
       normal[3*vertex+2]   = normal[3*vertex-1];
489

  
490
       texture[2*vertex  ]  = texture[2*vertex-2];
491
       texture[2*vertex+1]  = texture[2*vertex-1];
492
         
493
       vertex++;     
494
       }
495
     
496
     return vertex;
497
     }
498
   
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

  
501
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
502
     {
503
     //android.util.Log.d("CUBES", "buildSide");
504

  
505
     for(int i=0; i<mEdgeNum; i++)
506
       {
507
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
508
       } 
509
      
510
     return vertex;
511
     }
512

  
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

  
515
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
516
     {
517
     Edge prev; 
518
     
519
     if( curr.side==NORTH ) // water outside
520
       {
521
       prev = new Edge(WEST,curr.row,curr.col);
522
       }
523
     else                   // land outside; we need to move forward one link because we are going in opposite direction and we need to start from a bend.
524
       {
525
       prev = curr;
526
       curr = new Edge(EAST,curr.row+1,curr.col-1);
527
       }
528
     
529
     int col = curr.col;
530
     int row = curr.row;
531
     int side= curr.side;  
532
     Edge next = getNextEdge(curr);
533
     
534
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
535
     vertex++;
536
     
537
     do
538
       {
539
       if( prev.side!=curr.side )
540
         {
541
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
542
         vertex++;
543
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
544
         vertex++;
545
         }
546
       
547
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
548
       vertex++;
549
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
550
       vertex++;
551
       
552
       prev = curr;
553
       curr = next; 
554
       next = getNextEdge(curr);
555
       }
556
     while( curr.col!=col || curr.row!=row || curr.side!=side );
557
     
558
     vertex = repeatLast(vertex,position,normal,texture);
559
     
560
     return vertex;
561
     }
562

  
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

  
565
   private Edge getNextEdge(Edge curr)
566
     {
567
     int col = curr.col;
568
     int row = curr.row;
569
      
570
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
571
                       
572
     switch(curr.side) 
573
       {
574
       case NORTH: if( col==mCols-1 ) 
575
                     return new Edge(EAST,row,col);
576
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
577
                     return new Edge(WEST,row-1,col+1);
578
                   if( mCubes[row][col+1]==mCubes[row][col] )
579
                     return new Edge(NORTH,row,col+1);
580
                   else  
581
                     return new Edge(EAST,row,col);
582
                   
583
       case SOUTH: if( col==0 ) 
584
                     return new Edge(WEST,row,col);
585
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
586
                     return new Edge(EAST,row+1,col-1); 
587
                   if( mCubes[row][col-1]==mCubes[row][col] )
588
                     return new Edge(SOUTH,row,col-1);
589
                   else
590
                     return new Edge(WEST,row,col); 
591
                     
592
       case EAST : if( row==mRows-1 ) 
593
                     return new Edge(SOUTH,row,col);
594
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
595
                     return new Edge(NORTH,row+1,col+1);
596
                   if( mCubes[row+1][col]==mCubes[row][col] )
597
                     return new Edge(EAST,row+1,col);
598
                   else 
599
                     return new Edge(SOUTH,row,col);
600
                   
601
       default   : if( row==0 )
602
                     return new Edge(NORTH,row,col);
603
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
604
                     return new Edge(SOUTH,row-1,col-1);
605
                   if( mCubes[row-1][col]==mCubes[row][col] )
606
                     return new Edge(WEST,row-1,col);
607
                   else
608
                     return new Edge(NORTH,row,col);     
609
       }
610
     }
611

  
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
   
614
   private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
615
     {
616
     //android.util.Log.e("CUBES", "adding Side vertex!");
617

  
618
     remainingVert--;
619

  
620
     float x, y;
621

  
622
     switch(curr.side)
623
       {
624
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
625

  
626
                   position[3*vertex  ] = x - 0.5f;
627
                   position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
628
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
629

  
630
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
631
                   normal[3*vertex+1]   = 1.0f;
632
                   normal[3*vertex+2]   = lower ? -R:R;
633

  
634
                   texture[2*vertex  ]  = x;
635
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
636
                   break;
637
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
638

  
639
                   position[3*vertex  ] = x - 0.5f;
640
                   position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
641
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
642
            
643
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
644
                   normal[3*vertex+1]   =-1.0f;
645
                   normal[3*vertex+2]   = lower ? -R:R;
646

  
647
                   texture[2*vertex  ]  = x;
648
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
649
                   break;
650
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
651

  
652
                   position[3*vertex  ] = (float)curr.col/mCols -0.5f;
653
                   position[3*vertex+1] = 0.5f - y;
654
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
655

  
656
                   normal[3*vertex  ]   =-1.0f;
657
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
658
                   normal[3*vertex+2]   = lower ? -R:R;
659
 
660
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
661
                   texture[2*vertex+1]  = 1.0f - y;
662
                   break;
663
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
664

  
665
                   position[3*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
666
                   position[3*vertex+1] = 0.5f - y;
667
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
668

  
669
                   normal[3*vertex  ]   = 1.0f;
670
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
671
                   normal[3*vertex+2]   = lower ? -R:R; 
672

  
673
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
674
                   texture[2*vertex+1]  = 1.0f - y;
675
                   break;
676
       }
677
     
678
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
679
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
680
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
681
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
682
     }
683

  
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685

  
686
   private void build(boolean frontOnly)
687
     {
688
     int numVertices=0;
689
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
690
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
691
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
692

  
693
     //android.util.Log.d("CUBES","building front grid...");
694

  
695
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
696

  
697
     if( !frontOnly )
698
       {
699
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
700
       if( numVertices%2==1 )
701
         {
702
         //android.util.Log.d("CUBES","repeating winding1 vertex");
703

  
704
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
705
         }
706

  
707
       //android.util.Log.d("CUBES","building side grid...");
708

  
709
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
710

  
711
       //android.util.Log.d("CUBES","building back grid...");
712

  
713
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
714
       }
715

  
716
     /*
717
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
718
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
719
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
720
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
721
     */
722

  
723
     mEdges.clear();
724
     mEdges = null;
725
     mCubes = null;
726

  
727
     if( remainingVert!=0 )
728
       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
729

  
730
     mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
731
     mGridPositions.put(positionData).position(0);
732

  
733
     mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
734
     mGridNormals.put(normalData).position(0);
735

  
736
     mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
737
     mGridTexture.put(textureData).position(0);
738
     }
739

  
740
///////////////////////////////////////////////////////////////////////////////////////////////////
741
// PUBLIC API
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743
/**
744
 * Creates the underlying grid of vertices, normals, texture coords and colors.
745
 *    
746
 * @param cols      Integer helping to parse the next parameter.
747
 * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
748
 *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
749
 *                  <p></p>
750
 *                  <p>
751
 *                  <pre>
752
 *                  For example, (cols=2, desc="111010") describes the following shape:
753
 *
754
 *                  XX
755
 *                  X
756
 *                  X
757
 *
758
 *                  whereas (cols=2,desc="110001") describes
759
 *
760
 *                  XX
761
 *
762
 *                   X
763
 *                  </pre>
764
 *                  </p>
765
 * @param frontOnly Only create the front wall or side and back as well?
766
 */
767
   public DistortedCubesGrid(int cols, String desc, boolean frontOnly)
768
      {
769
      prepareDataStructures(cols,desc,frontOnly);
770
      build(frontOnly);
771
      }
772

  
773
///////////////////////////////////////////////////////////////////////////////////////////////////
774
/**
775
 * Creates a full, hole-less underlying grid of vertices, normals, texture coords and colors.
776
 *
777
 * @param cols      Number of columns.
778
 * @param rows      Number of rows.
779
 * @param frontOnly Only create the front wall or side and back as well?
780
 */
781
   public DistortedCubesGrid(int cols, int rows, boolean frontOnly)
782
      {
783
      prepareDataStructures(cols,rows,frontOnly);
784
      build(frontOnly);
785
      }
786
   }
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788

  
src/main/java/org/distorted/library/DistortedFramebuffer.java
31 31
 * <p>
32 32
 * User is able to create either Framebuffers from objects already constructed outside
33 33
 * of the library (the first constructor; primary use case: the screen) or an offscreen
34
 * FBOs (used by the DistortedNode, but also can be used by external users of the library)
34
 * FBOs (used by the DistortedObjectTree, but also can be used by external users of the library)
35 35
 * <p>
36 36
 * Also, keep all objects created in a static LinkedList. The point: we need to be able to mark
37 37
 * Framebuffers for deletion, and delete all marked objects later at a convenient time (that's
src/main/java/org/distorted/library/DistortedNode.java
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 DistortedObjects.
30
 *  
31
 * 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
 * each one of them has. 
34
 */
35
public class DistortedNode 
36
  {
37
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
38
  private static long mNextNodeID =0;
39

  
40
  private DistortedObjectGrid mGrid;
41
  private DistortedObject mObject;
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
    DistortedFramebuffer mDF;
53
    boolean mRendered;
54

  
55
    NodeData(long id)
56
      {
57
      ID              = id;
58
      numPointingNodes= 1;
59
      mDF             = null;
60
      mRendered       = false;
61
      }
62
    }
63
 
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
  static void release()
67
    {
68
    mNextNodeID = 0;
69
    mMapNodeID.clear();
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

  
74
  private void markRecursive()
75
    {
76
    mData.mRendered = false;
77
   
78
    synchronized(this)
79
      {
80
      for(int i=0; i<mNumChildren[0]; i++) mChildren.get(i).markRecursive();
81
      }
82
    }
83
  
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
  
86
  private void drawRecursive(long currTime, DistortedFramebuffer df)
87
    {
88
    if( mNumChildren[0]<=0 )
89
      {
90
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
91
      }
92
    else
93
      {
94
      if( !mData.mRendered )
95
        {
96
        mData.mRendered = true;
97
        mData.mDF.setAsOutput();
98

  
99
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
100
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
101
      
102
        if( mObject.mBitmapSet[0] )
103
          {
104
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
105
          mObject.drawNoEffectsPriv(mGrid, mData.mDF);
106
          }
107
      
108
        synchronized(this)
109
          {
110
          for(int i=0; i<mNumChildren[0]; i++)
111
            {
112
            mChildren.get(i).drawRecursive(currTime, mData.mDF);
113
            }
114
          }
115
        }
116

  
117
      df.setAsOutput();
118
      mData.mDF.setAsInput();
119
      }
120
    
121
    mObject.drawPriv(currTime, mGrid, df);
122
    }
123
  
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
// tree isomorphism
126
  
127
  private void RecomputeNodeID(ArrayList<Long> prev)
128
    {
129
    ArrayList<Long> curr = generateIDList();
130
     
131
    if( mParent==null )
132
      {
133
      adjustNodeData(prev,curr);
134
      }
135
    else
136
      {
137
      ArrayList<Long> parentPrev = mParent.generateIDList();
138
      adjustNodeData(prev,curr);
139
      mParent.RecomputeNodeID(parentPrev);
140
      }
141
    }
142

  
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

  
145
  private ArrayList<Long> generateIDList()
146
    {
147
    ArrayList<Long> ret = new ArrayList<>();
148
     
149
    ret.add( mNumChildren[0]>0 ? mObject.getBitmapID() : mObject.getID() );
150
    DistortedNode node;
151
   
152
    for(int i=0; i<mNumChildren[0]; i++)
153
      {
154
      node = mChildren.get(i);
155
      ret.add(node.mData.ID);
156
      }
157
   
158
    return ret;
159
    }
160

  
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

  
163
  private void adjustNodeData(ArrayList<Long> oldList, ArrayList<Long> newList)
164
    {
165
    boolean otherNodesPoint = (mData.numPointingNodes>1);
166

  
167
    if( otherNodesPoint ) mData.numPointingNodes--;
168
    else                  mMapNodeID.remove(oldList);
169
   
170
    NodeData newData = mMapNodeID.get(newList);
171
    
172
    if( newData==null )
173
      {
174
      if( otherNodesPoint )  mData = new NodeData(++mNextNodeID);
175
      else                   mData.ID = ++mNextNodeID;  // numPointingNodes must be 1 already
176

  
177
      if( newList.size()>1 )
178
        {
179
        if( mData.mDF==null )
180
          mData.mDF = new DistortedFramebuffer(mObject.getWidth(), mObject.getHeight());
181
        }
182
      else
183
        {
184
        if( mData.mDF!=null )
185
          {
186
          mData.mDF.markForDeletion();
187
          mData.mDF = null;
188
          }
189
        else
190
          {
191
          android.util.Log.e("DistortedNode", "adjustNodeData: impossible situation??");
192
          }
193
        }
194

  
195
      mMapNodeID.put(newList, mData);
196
      }
197
    else
198
      {
199
      newData.numPointingNodes++;
200
      mData = newData;
201
      }
202
    }
203

  
204
///////////////////////////////////////////////////////////////////////////////////////////////////  
205
// this will be called on startup and every time OpenGL context has been lost
206
// also call this from the constructor if the OpenGL context has been created already.
207
    
208
  static void reset()
209
    {
210
    NodeData tmp;   
211
     
212
    for(ArrayList<Long> key: mMapNodeID.keySet())
213
      {
214
      tmp = mMapNodeID.get(key);
215
          
216
      if( tmp.mDF != null )
217
        {
218
    	  tmp.mDF.reset();
219
        tmp.mRendered  = false;
220
        }
221
      }
222
    }
223

  
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
// Debug - print all the Node IDs
226
/*
227
  void debug(int depth)
228
    {
229
    String tmp="";
230
    int i;
231

  
232
    for(i=0; i<depth; i++) tmp +="   ";
233
    tmp += (""+mData.ID);
234

  
235
    android.util.Log.e("node", tmp);
236

  
237
    for(i=0; i<mNumChildren[0]; i++)
238
      mChildren.get(i).debug(depth+1);
239
    }
240

  
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// Debug - print contents of the HashMap
243

  
244
  static void debugMap()
245
    {
246
    NodeData tmp;
247

  
248
    for(ArrayList<Long> key: mMapNodeID.keySet())
249
      {
250
      tmp = mMapNodeID.get(key);
251

  
252
      android.util.Log.e("NODE", "key="+key+" NodeID: "+tmp.ID);
253
      }
254
    }
255
*/
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
// PUBLIC API
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Constructs new Node of the Tree.
261
 *     
262
 * @param obj DistortedObject to put into the new Node.
263
 */
264
  public DistortedNode(DistortedObject obj, DistortedObjectGrid grid)
265
    {
266
    mObject = obj;
267
    mGrid   = grid;
268
    mParent = null;
269
    mChildren = null;
270
    mNumChildren = new int[1];
271
    mNumChildren[0] = 0;
272
   
273
    ArrayList<Long> list = new ArrayList<>();
274
    list.add(obj.getID());
275
      
276
    mData = mMapNodeID.get(list);
277
   
278
    if( mData!=null )
279
      {
280
      mData.numPointingNodes++;
281
      }
282
    else
283
      {
284
      mData = new NodeData(++mNextNodeID);   
285
      mMapNodeID.put(list, mData);  
286
      }
287
    }
288

  
289
///////////////////////////////////////////////////////////////////////////////////////////////////  
290
/**
291
 * Copy-constructs new Node of the Tree from another Node.
292
 *     
293
 * @param node The DistortedNode to copy data from.
294
 * @param flags bit field composed of a subset of the following:
295
 *        {@link Distorted#CLONE_BITMAP},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
296
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
297
 *        For example flags = CLONE_BITMAP | CLONE_CHILDREN.
298
 */
299
  public DistortedNode(DistortedNode node, int flags)
300
    {
301
    mParent = null;
302
    mObject = new DistortedObject(node.mObject,flags);
303
    mGrid   = node.mGrid;
304

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

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

Also available in: Unified diff