Project

General

Profile

« Previous | Next » 

Revision e458a4ba

Added by Leszek Koltunski over 8 years ago

1. new package 'message'
2. Rename the 'Grid' classes to match

View differences:

src/main/java/org/distorted/library/DistortedBitmap.java
80 80
     mSizeX= width;
81 81
     mSizeY= height;
82 82
     mSizeZ= 1;     
83
     mGrid = new GridBitmap(xsize,ysize);
83
     mGrid = new DistortedBitmapGrid(xsize,ysize);
84 84
     initializeData(gridSize);
85 85
     }
86 86

  
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
class DistortedBitmapGrid extends DistortedObjectGrid
28
  {
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

  
32
  DistortedBitmapGrid(int xLength, int yLength)
33
    {
34
    dataLength = 2*xLength*(yLength-1)+2*(yLength-2); // (yLength-1) strips, 2*xLength triangles in each, plus 2 degenerate triangles per each of (yLength-2) joins 
35
    final short[] indexData = new short[dataLength];
36
    
37
    int offset=0;    
38
    for(int y=0; y<yLength-1; y++) 
39
      {
40
      if (y>0) indexData[offset++] = (short) (y*xLength); // Degenerate begin: repeat first vertex
41

  
42
      for (int x = 0; x < xLength; x++) 
43
        {
44
        indexData[offset++] = (short) (( y   *xLength)+x);
45
        indexData[offset++] = (short) (((y+1)*xLength)+x);
46
        }
47

  
48
      if (y<yLength-2) indexData[offset++] = (short) (((y+1)*xLength) + (xLength-1)); // Degenerate end: repeat last vertex
49
      }
50

  
51
    // for(int g=0; g<dataLength; g++) Log.e(TAG_BACKGROUND, "index["+g+"]="+indexData[g]);
52

  
53
    float[] bufferData= new float[COLOR_DATA_SIZE*dataLength];
54

  
55
    offset=0;
56
    for(int i=0; i<dataLength; i++)
57
      {
58
      bufferData[offset++] = 1.0f; // r
59
      bufferData[offset++] = 1.0f; // g
60
      bufferData[offset++] = 1.0f; // b
61
      bufferData[offset++] = 1.0f; // a
62
      }
63
    mGridColors = ByteBuffer.allocateDirect(COLOR_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
64
    mGridColors.put(bufferData).position(0); 
65

  
66
    bufferData = new float[NORMAL_DATA_SIZE*dataLength];
67

  
68
    offset=0;
69
    for(int i=0; i<dataLength; i++)
70
      {
71
      bufferData[offset++] = 0.0f; // x
72
      bufferData[offset++] = 0.0f; // y
73
      bufferData[offset++] = 1.0f; // z
74
      }
75
    mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
76
    mGridNormals.put(bufferData).position(0); 
77

  
78
    float tmpx,tmpy,tmpz;
79
    bufferData = new float[TEX_DATA_SIZE*dataLength];
80

  
81
    offset=0;
82
    for(int i=0; i<dataLength; i++)
83
      {
84
      tmpx = ((float)(indexData[offset/2]%xLength))/(xLength-1);
85
      tmpy = ((float)(indexData[offset/2]/xLength))/(yLength-1);
86

  
87
      bufferData[offset++] = tmpx; // s=x
88
      bufferData[offset++] = tmpy; // t=y
89
      }
90
    mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
91
    mGridTexture.put(bufferData).position(0); 
92

  
93
    //for(int g=0; g<dataLength; g++) Log.e(TAG_BACKGROUND, "tex["+g+"]=("+bufferData[2*g]+","+bufferData[2*g+1]+")");
94
    //Log.e(TAG, "regWidth="+(2*mRegW)+" regHeight="+(2*mRegH)+" xLength="+xLength+" yLength="+yLength);
95

  
96
    offset=0;
97
    bufferData= new float[POSITION_DATA_SIZE*dataLength];
98

  
99
    for(int i=0; i<dataLength; i++)
100
      {
101
      tmpx = ((float)(indexData[offset/3]%xLength))/(xLength-1);
102
      tmpy = ((float)(indexData[offset/3]/xLength))/(yLength-1);
103
      tmpz = 0;
104

  
105
      bufferData[offset++] = (tmpx-0.5f); // x
106
      bufferData[offset++] = (0.5f-tmpy); // y
107
      bufferData[offset++] =        tmpz; // z
108
      }
109
    mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
110
    mGridPositions.put(bufferData).position(0); 
111

  
112
    //for(int g=0; g<dataLength; g++) android.util.Log.e("BACKGROUND", "pos["+g+"]=("+bufferData[3*g]+","+bufferData[3*g+1]+")");
113
    }
114
  };
115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedCubes.java
96 96
   mSizeX= gridSize*Cs;
97 97
   mSizeY= gridSize*Rs;
98 98
   mSizeZ= frontOnly ? 0 : gridSize;
99
   mGrid = new GridCubes(cols,desc, frontOnly);
99
   mGrid = new DistortedCubesGrid(cols,desc, frontOnly);
100 100
   initializeData(gridSize);
101 101
   }
102 102

  
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
class DistortedCubesGrid extends DistortedObjectGrid
29
   {
30
   private static final float R = 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
   
47
   private class Edge
48
     {
49
     final int side; 
50
     final int row;
51
     final int col;
52
     
53
     public Edge(int s, int r, int c)
54
       {
55
       side= s; 
56
       row = r;
57
       col = c;
58
       }
59
     };
60
   
61
   private int frontVert, sideVert;
62
   private int mCols, mRows;
63
   private short[][] mCubes;
64
   private ArrayList<Edge> mEdges = new ArrayList<Edge>();
65
   
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

  
68
   private int computeDataLength(boolean frontOnly)
69
      {
70
      int frontWalls=0, frontSegments=0, sideWalls=0, sideBends=0;
71
      
72
      for(int i=0; i<mRows; i++)
73
         for(int j=0; j<mCols; j++)
74
            {
75
            if( mCubes[i][j]%2 == 1 )  // land
76
              {
77
              frontWalls++;
78
              if( j==mCols-1 || mCubes[i][j+1]%2 == 0 ) frontSegments++;
79
              }
80
              
81
            if( (i==0 && mCubes[i][j]!=2) || (i!=0 && mCubes[i][j] != mCubes[i-1][j  ]) ) sideWalls++; // up
82
            if( (j==0 && mCubes[i][j]!=2) || (j!=0 && mCubes[i][j] != mCubes[i  ][j-1]) ) sideWalls++; // left
83
            if( i==mRows-1 && mCubes[i][j]!=2                                           ) sideWalls++; // bottom
84
            if( j==mCols-1 && mCubes[i][j]!=2                                           ) sideWalls++; // right
85
            }
86

  
87
      int edges= mEdges.size();
88
      
89
      for(int i=0; i<edges; i++) 
90
        {
91
        Edge curr = mEdges.get(i);
92
        Edge next = getNextEdge(curr);
93
        int startX = curr.col;
94
        int startY = curr.row;
95
        int startS = curr.side;
96
        
97
        do
98
          {
99
          if( next.side != curr.side ) sideBends++; 
100
          curr  = next; 
101
          next = getNextEdge(curr);
102
          }
103
        while( curr.col!=startX || curr.row!=startY || curr.side!=startS );
104
        }
105
      
106
      frontVert = 2*( frontWalls + 2*frontSegments - 1);
107
      sideVert  = 2*( sideWalls + sideBends + edges -1);
108
      
109
      int dataL = frontOnly ? frontVert : (frontVert+1) + (1+sideVert+1) + (1+frontVert);
110
      
111
      android.util.Log.e("CUBES","frontVert="+frontVert+" sideVert="+sideVert);
112
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+sideWalls+" sSegments="+edges+" sideBends="+sideBends+" dataLen="+dataL );
113
      
114
      return dataL<0 ? 0:dataL;
115
      }
116

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

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

  
140
     return ret;
141
     }
142
*/  
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
/*
145
   private static String debug(Edge e)
146
     {
147
     String d = "";
148
     
149
     switch(e.side)
150
       {
151
       case NORTH: d+="NORTH "; break;
152
       case SOUTH: d+="SOUTH "; break;
153
       case WEST : d+="WEST  "; break;
154
       case EAST : d+="EAST  "; break;
155
       }
156
     
157
     d+=("("+e.row+","+e.col+")");
158
     
159
     return d;
160
     }   
161
*/ 
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

  
164
   private void buildGrid(int cols, String desc, boolean frontOnly)
165
     {
166
     mRows     =0;
167
     mCols     =0;
168
     dataLength=0;
169
     
170
     if( cols>0 )
171
       {
172
       int reallen = desc.length();
173
       int len = reallen;
174

  
175
       if( (reallen/cols)*cols != reallen )
176
         {
177
         len = ((reallen/cols)+1)*cols; 
178
         for(int i=reallen; i<len; i++) desc += "0";
179
         }
180
    
181
       if( desc.indexOf("1")>=0 )
182
         {
183
         mCols = cols;
184
         mRows = len/cols;
185

  
186
         mCubes = new short[mRows][mCols];
187
       
188
         for(int j=0; j<mCols; j++) 
189
           for(int i=0; i<mRows; i++)
190
             mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0); 
191
       
192
         markRegions();
193
         dataLength = computeDataLength(frontOnly);
194
         }
195
       }
196
     }
197
 
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
200
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
201
// gets a unique odd integer, each connected block of water a unique even integer.
202
//
203
// Water on the edges of the grid is also considered connected to itself!   
204
//   
205
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
206
// will start building the side walls of each connected black of land (and sides of holes of water 
207
// inside)   
208
   
209
   private void markRegions()
210
     {
211
     int i, j, numWater=1, numLand=0;
212
     
213
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
214
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
215
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
216
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
217
           
218
     for(i=0; i<mRows; i++)
219
        for(j=0; j<mCols; j++)
220
           {
221
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(NORTH,i,j)); }
222
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i,j)); }
223
           }
224
     
225
     // now we potentially need to kick out some Edges - precisely the edges with water inside -
226
     // which are surrounded by more than one type of land. Otherwise the following does not work:
227
     //
228
     // 0 1 0
229
     // 1 0 1
230
     // 0 1 0
231
     //
232
     // The 'water inside' edges that did not get kicked out by this procedure need to be transformed
233
     // with Edge(NORTH,row,col) -> Edge(SOUTH,row-1,col) so that later on normals work correctly
234
     // (Edge always needs to point out from land to water for that)
235
     
236
     int numEdges= mEdges.size();
237
     short initLand;
238
     int initCol, initRow;
239
     boolean kicked;
240
     Edge e;
241
     
242
     for(i=0; i<numEdges; i++) 
243
       {
244
       e = mEdges.get(i);
245
       initRow= e.row;
246
       initCol= e.col;
247
         
248
       //android.util.Log.e("CUBES", "checking edge "+debug(e));
249
             
250
       if( mCubes[initRow][initCol]%2==0 )
251
         {
252
         kicked = false; 
253
         initLand = mCubes[initRow-1][initCol];
254
         
255
         do
256
           {
257
           e = getNextEdge(e); 
258
           //android.util.Log.e("CUBES", " next edge "+debug(e));   
259
       
260
           switch(e.side)
261
             {
262
             case NORTH: if( initLand!=mCubes[e.row-1][e.col  ] ) kicked=true; break;
263
             case SOUTH: if( initLand!=mCubes[e.row+1][e.col  ] ) kicked=true; break;
264
             case WEST:  if( initLand!=mCubes[e.row  ][e.col-1] ) kicked=true; break;
265
             case EAST:  if( initLand!=mCubes[e.row  ][e.col+1] ) kicked=true; break;
266
             }
267
           
268
           if( kicked )
269
             {
270
             //android.util.Log.e("CUBES", "kicking out edge!");
271
             mEdges.remove(i);
272
             i--;
273
             numEdges--; 
274
             }
275
           }
276
         while( kicked==false && (e.col!=initCol || e.row!=initRow || e.side!=NORTH) );
277
         
278
         if( kicked==false )
279
           {
280
           mEdges.set(i, new Edge(SOUTH,e.row-1,e.col)); 
281
           }
282
         }
283
       }
284
     }
285

  
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
// when calling, make sure that newVal != val
288
   
289
   private void markRegion(short newVal, int row, int col)
290
     {
291
     short val = mCubes[row][col];
292
     mCubes[row][col] = newVal;
293
     
294
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
295
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
296
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
297
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
298
     }
299
   
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
   
302
   private void createNormals(int row, int col)
303
     {
304
     int td,lr; 
305
      
306
	  int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
307
	  int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
308
	  int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
309
	  int c  =                                (mCubes[row  ][col  ]%2);
310
	  int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
311
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
312
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
313
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
314
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
315
     
316
     td = nw+n-w-c;
317
     lr = c+n-w-nw;
318
     if( td<0 ) td=-1;
319
     if( td>0 ) td= 1;
320
     if( lr<0 ) lr=-1;
321
     if( lr>0 ) lr= 1;
322
     mNormalX[0] = lr*R;
323
     mNormalY[0] = td*R;
324
     
325
     td = w+c-sw-s;
326
     lr = c+s-w-sw;
327
     if( td<0 ) td=-1;
328
     if( td>0 ) td= 1;
329
     if( lr<0 ) lr=-1;
330
     if( lr>0 ) lr= 1;
331
     mNormalX[1] = lr*R;
332
     mNormalY[1] = td*R;
333
     
334
     td = n+ne-c-e;
335
     lr = e+ne-c-n;
336
     if( td<0 ) td=-1;
337
     if( td>0 ) td= 1;
338
     if( lr<0 ) lr=-1;
339
     if( lr>0 ) lr= 1;
340
     mNormalX[2] = lr*R;
341
     mNormalY[2] = td*R;
342
     
343
     td = c+e-s-se;
344
     lr = e+se-c-s;
345
     if( td<0 ) td=-1;
346
     if( td>0 ) td= 1;
347
     if( lr<0 ) lr=-1;
348
     if( lr>0 ) lr= 1;
349
     mNormalX[3] = lr*R;
350
     mNormalY[3] = td*R;
351
     /*
352
	  android.util.Log.d("CUBES", "row="+row+" col="+col);
353
	  android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
354
	  android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
355
	  android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
356
	  android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
357
     */
358
     }
359
   
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
   
362
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
363
     {
364
     short last, current;
365
     boolean seenland=false;
366
     float centerX, centerY;
367
    
368
     for(int i=0; i<mRows; i++)
369
       {
370
       last =0;
371
         
372
       for(int j=0; j<mCols; j++)
373
         {
374
         current = mCubes[i][j];
375
            
376
         if( current%2 == 1 )
377
           {
378
           centerX = j-(mCols-1.0f)/2.0f;
379
           centerY = (mRows-1.0f)/2.0f-i;
380
      
381
           createNormals(i,j);
382
          
383
           if( last != current )
384
             {
385
             if( seenland ) vertex = repeatLast(vertex,position,normal,texture);    
386

  
387
             if( front ) // NW corner
388
               {
389
               position[3*vertex+0] = (centerX-0.5f)/mCols;
390
               position[3*vertex+1] = (centerY+0.5f)/mRows;
391
               position[3*vertex+2] = FRONTZ;
392
               normal[3*vertex+0]   = mNormalX[0];
393
               normal[3*vertex+1]   = mNormalY[0];
394
               normal[3*vertex+2]   = 1.0f;
395
               texture[2*vertex+0]  = (float)j/mCols;
396
               texture[2*vertex+1]  = (float)i/mRows;     
397
               vertex++;
398
               }
399
             else  // SW corner
400
               { 
401
               position[3*vertex+0] = (centerX-0.5f)/mCols;
402
               position[3*vertex+1] = (centerY-0.5f)/mRows; 
403
               position[3*vertex+2] = BACKZ; 
404
               normal[3*vertex+0]   = mNormalX[1];
405
               normal[3*vertex+1]   = mNormalY[1];
406
               normal[3*vertex+2]   =-1.0f;
407
               texture[2*vertex+0]  = (float)j/mCols;
408
               texture[2*vertex+1]  = (float)(i+1)/mRows;
409
               vertex++;
410
               
411
               if( !seenland ) vertex = repeatLast(vertex,position,normal,texture);   //  if drawing the back, repeat the very first vertex
412
               }
413
             
414
             if( seenland ) vertex = repeatLast(vertex,position,normal,texture);    
415

  
416
             if( front ) // SW corner
417
               {
418
               position[3*vertex+0] = (centerX-0.5f)/mCols;
419
               position[3*vertex+1] = (centerY-0.5f)/mRows; 
420
               position[3*vertex+2] = FRONTZ; 
421
               normal[3*vertex+0]   = mNormalX[1];
422
               normal[3*vertex+1]   = mNormalY[1];
423
               normal[3*vertex+2]   = 1.0f;
424
               texture[2*vertex+0]  = (float)j/mCols;
425
               texture[2*vertex+1]  = (float)(i+1)/mRows;
426
               vertex++; 
427
               }
428
             else  // NW corner
429
               {
430
               position[3*vertex+0] = (centerX-0.5f)/mCols;
431
               position[3*vertex+1] = (centerY+0.5f)/mRows;
432
               position[3*vertex+2] = BACKZ;
433
               normal[3*vertex+0]   = mNormalX[0];
434
               normal[3*vertex+1]   = mNormalY[0];
435
               normal[3*vertex+2]   =-1.0f;
436
               texture[2*vertex+0]  = (float)j/mCols;
437
               texture[2*vertex+1]  = (float)i/mRows;     
438
               vertex++; 
439
               }
440
             }
441
              
442
           if( front )  // NE corner
443
             {
444
             position[3*vertex+0] = (centerX+0.5f)/mCols;
445
             position[3*vertex+1] = (centerY+0.5f)/mRows;
446
             position[3*vertex+2] = FRONTZ; 
447
             normal[3*vertex+0]   = mNormalX[2];
448
             normal[3*vertex+1]   = mNormalY[2];
449
             normal[3*vertex+2]   = 1.0f;
450
             texture[2*vertex+0]  = (float)(j+1)/mCols;
451
             texture[2*vertex+1]  = (float)i/mRows;
452
             vertex++;
453
             }
454
           else // SE corner
455
             {
456
             position[3*vertex+0] = (centerX+0.5f)/mCols;
457
             position[3*vertex+1] = (centerY-0.5f)/mRows;
458
             position[3*vertex+2] = BACKZ; 
459
             normal[3*vertex+0]   = mNormalX[3];
460
             normal[3*vertex+1]   = mNormalY[3];
461
             normal[3*vertex+2]   =-1.0f;
462
             texture[2*vertex+0]  = (float)(j+1)/mCols;
463
             texture[2*vertex+1]  = (float)(i+1)/mRows;
464
             vertex++; 
465
             }
466
           
467
           if( front )  // SE corner
468
             {
469
             position[3*vertex+0] = (centerX+0.5f)/mCols;
470
             position[3*vertex+1] = (centerY-0.5f)/mRows;
471
             position[3*vertex+2] = FRONTZ; 
472
             normal[3*vertex+0]   = mNormalX[3];
473
             normal[3*vertex+1]   = mNormalY[3];
474
             normal[3*vertex+2]   = 1.0f;
475
             texture[2*vertex+0]  = (float)(j+1)/mCols;
476
             texture[2*vertex+1]  = (float)(i+1)/mRows;
477
             vertex++;
478
             }
479
           else // NE corner
480
             {
481
             position[3*vertex+0] = (centerX+0.5f)/mCols;
482
             position[3*vertex+1] = (centerY+0.5f)/mRows;
483
             position[3*vertex+2] = BACKZ; 
484
             normal[3*vertex+0]   = mNormalX[2];
485
             normal[3*vertex+1]   = mNormalY[2];
486
             normal[3*vertex+2]   =-1.0f;
487
             texture[2*vertex+0]  = (float)(j+1)/mCols;
488
             texture[2*vertex+1]  = (float)i/mRows;
489
             vertex++; 
490
             }
491
           
492
           seenland = true;
493
           }
494
            
495
         last = current;
496
         }
497
       }
498
     
499
     return vertex;
500
     }
501

  
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

  
504
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
505
     {
506
     if( vertex>0 )
507
       {
508
       position[3*vertex+0] = position[3*vertex-3]; 
509
       position[3*vertex+1] = position[3*vertex-2];
510
       position[3*vertex+2] = position[3*vertex-1];
511

  
512
       normal[3*vertex+0]   = normal[3*vertex-3]; 
513
       normal[3*vertex+1]   = normal[3*vertex-2];
514
       normal[3*vertex+2]   = normal[3*vertex-1];
515

  
516
       texture[2*vertex+0]  = texture[2*vertex-2];
517
       texture[2*vertex+1]  = texture[2*vertex-1];
518
         
519
       vertex++;     
520
       }
521
     
522
     return vertex;
523
     }
524
   
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

  
527
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
528
     {
529
     int edges= mEdges.size();
530
     
531
     for(int i=0; i<edges; i++) 
532
       {
533
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
534
       } 
535
      
536
     return vertex;
537
     }
538

  
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

  
541
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
542
     {
543
     Edge prev; 
544
     
545
     if( curr.side==NORTH ) // water outside
546
       {
547
       prev = new Edge(WEST,curr.row,curr.col);
548
       }
549
     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.
550
       {
551
       prev = curr;
552
       curr = new Edge(EAST,curr.row+1,curr.col-1);
553
       }
554
     
555
     int col = curr.col;
556
     int row = curr.row;
557
     int side= curr.side;  
558
     Edge next = getNextEdge(curr);
559
     
560
     addVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
561
     vertex++;
562
     
563
     do
564
       {
565
       if( prev.side!=curr.side )
566
         {
567
         addVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
568
         vertex++;
569
         addVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
570
         vertex++;
571
         }
572
       
573
       addVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
574
       vertex++;
575
       addVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
576
       vertex++;
577
       
578
       prev = curr;
579
       curr = next; 
580
       next = getNextEdge(curr);
581
       }
582
     while( curr.col!=col || curr.row!=row || curr.side!=side );
583
     
584
     vertex = repeatLast(vertex,position,normal,texture);
585
     
586
     return vertex;
587
     }
588

  
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

  
591
   private Edge getNextEdge(Edge curr)
592
     {
593
     int col = curr.col;
594
     int row = curr.row;
595
      
596
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
597
                       
598
     switch(curr.side) 
599
       {
600
       case NORTH: if( col==mCols-1 ) 
601
                     return new Edge(EAST,row,col);
602
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
603
                     return new Edge(WEST,row-1,col+1);
604
                   if( mCubes[row][col+1]==mCubes[row][col] )
605
                     return new Edge(NORTH,row,col+1);
606
                   else  
607
                     return new Edge(EAST,row,col);
608
                   
609
       case SOUTH: if( col==0 ) 
610
                     return new Edge(WEST,row,col);
611
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
612
                     return new Edge(EAST,row+1,col-1); 
613
                   if( mCubes[row][col-1]==mCubes[row][col] )
614
                     return new Edge(SOUTH,row,col-1);
615
                   else
616
                     return new Edge(WEST,row,col); 
617
                     
618
       case EAST : if( row==mRows-1 ) 
619
                     return new Edge(SOUTH,row,col);
620
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
621
                     return new Edge(NORTH,row+1,col+1);
622
                   if( mCubes[row+1][col]==mCubes[row][col] )
623
                     return new Edge(EAST,row+1,col);
624
                   else 
625
                     return new Edge(SOUTH,row,col);
626
                   
627
       case WEST : if( row==0 )
628
                     return new Edge(NORTH,row,col);
629
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
630
                     return new Edge(SOUTH,row-1,col-1);
631
                   if( mCubes[row-1][col]==mCubes[row][col] )
632
                     return new Edge(WEST,row-1,col);
633
                   else
634
                     return new Edge(NORTH,row,col);     
635
       }
636
     
637
     return null;
638
     }
639

  
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641
   
642
   private void addVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
643
     {
644
     float centerX = curr.col-(mCols-1.0f)/2.0f;
645
     float centerY = (mRows-1.0f)/2.0f-curr.row;
646
  
647
     switch(curr.side)
648
       {
649
       case NORTH: position[3*vertex+0] = (back ? (centerX-0.5f) : (centerX+0.5f))/mCols; 
650
                   position[3*vertex+1] = (centerY+0.5f)/mRows;
651
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
652

  
653
                   normal[3*vertex+0]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
654
                   normal[3*vertex+1]   = 1.0f;
655
                   normal[3*vertex+2]   = lower ? -R:R;
656

  
657
                   texture[2*vertex+0]  = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
658
                   texture[2*vertex+1]  = (float)(lower? (curr.row-1):(curr.row  ))/mRows;  
659
                   break;
660
       case SOUTH: position[3*vertex+0] = (back ? (centerX+0.5f) : (centerX-0.5f))/mCols;
661
                   position[3*vertex+1] = (centerY-0.5f)/mRows;
662
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
663
            
664
                   normal[3*vertex+0]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
665
                   normal[3*vertex+1]   =-1.0f;
666
                   normal[3*vertex+2]   = lower ? -R:R;
667

  
668
                   texture[2*vertex+0]  = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
669
                   texture[2*vertex+1]  = (float)(lower? (curr.row+2):(curr.row+1))/mRows;
670
                   break;
671
       case WEST : position[3*vertex+0] = (centerX-0.5f)/mCols;
672
                   position[3*vertex+1] = (back ? (centerY-0.5f):(centerY+0.5f))/mRows;
673
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
674

  
675
                   normal[3*vertex+0]   =-1.0f;
676
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
677
                   normal[3*vertex+2]   = lower ? -R:R;
678
 
679
                   texture[2*vertex+0]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
680
                   texture[2*vertex+1]  = (float)(back  ? (curr.row+1):(curr.row  ))/mRows;
681
                   break;
682
       case EAST : position[3*vertex+0] = (centerX+0.5f)/mCols;
683
                   position[3*vertex+1] = (back ? (centerY+0.5f):(centerY-0.5f))/mRows;
684
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
685

  
686
                   normal[3*vertex+0]   = 1.0f;
687
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
688
                   normal[3*vertex+2]   = lower ? -R:R; 
689

  
690
                   texture[2*vertex+0]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
691
                   texture[2*vertex+1]  = (float)(back  ? (curr.row  ):(curr.row+1))/mRows;
692
                   break;
693
       }
694
     
695
     if(texture[2*vertex+0]>1.0f) texture[2*vertex+0] =2.0f-texture[2*vertex+0];
696
     if(texture[2*vertex+0]<0.0f) texture[2*vertex+0] =    -texture[2*vertex+0];
697
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
698
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
699
     }
700

  
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702
// PUBLIC API
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704
   
705
/**
706
 * Creates the underlying grid of vertices, normals, texture coords and colors.
707
 *    
708
 * @param rows See {@link DistortedCubes#DistortedCubes(String)} 
709
 * @param desc See {@link DistortedCubes#DistortedCubes(String)}
710
 */
711
   public DistortedCubesGrid(int cols, String desc, boolean frontOnly)
712
      {
713
      buildGrid(cols,desc,frontOnly);
714
       
715
      int numVertices=0;
716
      float[] colorData   = new float[COLOR_DATA_SIZE   *dataLength];
717
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
718
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
719
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
720
      
721
      for(int i=0; i<dataLength; i++)
722
        {
723
        colorData[COLOR_DATA_SIZE*i+0] = 1.0f; // r
724
        colorData[COLOR_DATA_SIZE*i+1] = 1.0f; // g
725
        colorData[COLOR_DATA_SIZE*i+2] = 1.0f; // b
726
        colorData[COLOR_DATA_SIZE*i+3] = 1.0f; // a
727
        }
728

  
729
      numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
730
      
731
      if( !frontOnly )
732
        {
733
        numVertices = repeatLast(numVertices,positionData,normalData,textureData);
734
        numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
735
        numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
736
        }
737
      
738
      /*
739
      android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
740
      android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
741
      android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
742
      android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
743
      */
744
      mGridColors = ByteBuffer.allocateDirect(COLOR_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
745
      mGridColors.put(colorData).position(0); 
746

  
747
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
748
      mGridPositions.put(positionData).position(0); 
749
      
750
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
751
      mGridNormals.put(normalData).position(0); 
752

  
753
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
754
      mGridTexture.put(textureData).position(0); 
755
      }
756
   }
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758

  
src/main/java/org/distorted/library/DistortedObject.java
23 23
import android.opengl.GLES20;
24 24
import android.opengl.GLUtils;
25 25

  
26
import org.distorted.library.message.EffectListener;
26 27
import org.distorted.library.type.Float1D;
27 28
import org.distorted.library.type.Float2D;
28 29
import org.distorted.library.type.Float3D;
......
52 53

  
53 54
    protected boolean matrixCloned, vertexCloned, fragmentCloned;
54 55
 
55
    protected GridObject mGrid = null;
56
    protected DistortedObjectGrid mGrid = null;
56 57
    protected long mID;
57 58
    protected int mSizeX, mSizeY, mSizeZ, mSize; // in screen space
58 59

  
src/main/java/org/distorted/library/DistortedObjectGrid.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.FloatBuffer;
23

  
24
import android.opengl.GLES20;
25

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

  
28
abstract class DistortedObjectGrid
29
   {
30
   protected static final int BYTES_PER_FLOAT   = 4; //
31
   protected static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
32
   protected static final int COLOR_DATA_SIZE   = 4; // Size of the color data in elements 
33
   protected static final int NORMAL_DATA_SIZE  = 3; // Size of the normal data in elements.
34
   protected static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements. 
35

  
36
   protected int dataLength;                       
37
      
38
   protected FloatBuffer mGridPositions,mGridColors,mGridNormals,mGridTexture;
39
 
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
   
42
   void draw()
43
     { 
44
     GLES20.glVertexAttribPointer(Distorted.mPositionH    , POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mGridPositions);          
45
     GLES20.glVertexAttribPointer(Distorted.mColorH       , COLOR_DATA_SIZE   , GLES20.GL_FLOAT, false, 0, mGridColors   );   
46
     GLES20.glVertexAttribPointer(Distorted.mNormalH      , NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mGridNormals  );
47
     GLES20.glVertexAttribPointer(Distorted.mTextureCoordH, TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mGridTexture  );  
48

  
49
     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, dataLength); 
50
     }
51
   }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectListener.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
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * This interface lets users of the Distorted library get notified when something happens to one of the effects.
25
 * To receive the notifications, we first have to register with a call to {@link DistortedBitmap#addEventListener(EffectListener)}.
26
 * List of all possible events that can happen is defined in {@link EffectMessage}
27
 */
28

  
29
public interface EffectListener 
30
  {
31
/**
32
 * Gets called when event of type 'em' happens to effect 'effectID'. 
33
 * 
34
 * @param eventType  Type of event that happened.
35
 * @param effectID   ID of the effect the event happened to. This ID must have been previously returned by one
36
 *                   of the DistortedBitmap.{deform,distort,move,...} functions.
37
 * @param effectName Name of the effect as defined in EffectNames, e.g. if effectType==EffectNames.MOVE.ordinal(),
38
 *                   then the event happened to a MOVE effect.
39
 * @param bitmapID   the ID of the DistortedBitmap object, as returned by {@link DistortedBitmap#getID()}, this event
40
 *                   happened to. If the object has been created using a copy constructor from another instance of
41
 *                   DistortedBitmap, the ID here will be the one of the original object.
42
 * @param message    Any message string associated with it. 'Failed' event types have one.
43
 * @see EffectMessage
44
 * @see EffectNames
45
 */
46
   
47
  void effectMessage(final EffectMessage eventType, final long effectID, final int effectName, final long bitmapID, final String message);
48
  }
49

  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectMessage.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
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
import org.distorted.library.type.Interpolator;
25

  
26
/**
27
* Defines all possible events a class implementing the {@link EffectListener} interface can receive.
28
*/
29

  
30
public enum EffectMessage 
31
  {
32
/**
33
 * The effect has been removed. This can happen if:
34
 * <ul>
35
 * <li> someone explicitly removed the effect with a call to {@link DistortedBitmap#abortEffect(long)} (or one of the other 'abort' methods)
36
 * <li> the interpolation of the effect has finished and the end result is equal to the effect's unity.
37
 * </ul>    
38
 */
39
  EFFECT_REMOVED,
40
  
41
/**
42
 * Interpolation of the effect has finished. 
43
 * <p>
44
 * If you set up an interpolated effect and set its Interpolator to do 3.5 interpolations of 1000 ms each
45
 * with calls to {@link Interpolator#setCount(float)} and {@link Interpolator#setDuration(long)},
46
 * then you are going to get this message exactly once after 3.5*1000 = 3500 milliseconds when the interpolation 
47
 * finishes. You will never get this message if you set the effect to go on indefinitely with a call to 
48
 * {@link Interpolator#setCount(float)}.
49
 * <p>  
50
 * If then the end effect is equal to the effect's unity, then immediately after this message you
51
 * will also get a EFFECT_REMOVED message.
52
 */
53
  EFFECT_FINISHED,
54

  
55
  /**
56
 * The effect has failed to properly execute.
57
 * <p>
58
 * Currently only OTHER effects (saving to PNG file and to a MP4 movie) can fail.
59
 */
60
  EFFECT_FAILED
61
  }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectMessageSender.java
19 19

  
20 20
package org.distorted.library;
21 21

  
22
import org.distorted.library.message.EffectListener;
23
import org.distorted.library.message.EffectMessage;
24

  
22 25
import java.util.Vector;
23 26

  
24 27
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectQueue.java
19 19

  
20 20
package org.distorted.library;
21 21

  
22
import org.distorted.library.message.EffectListener;
23
import org.distorted.library.message.EffectMessage;
22 24
import org.distorted.library.type.Interpolator;
23 25
import org.distorted.library.type.Interpolator2D;
24 26

  
......
247 249
   
248 250
    for(int i=0; i<mNumListeners; i++) 
249 251
      EffectMessageSender.newMessage( mListeners.elementAt(i),
250
                                      EffectMessage.EFFECT_REMOVED, 
252
                                      EffectMessage.EFFECT_REMOVED,
251 253
                                      (removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedType).type,
252 254
                                      removedType,
253 255
                                      mBitmapID,
src/main/java/org/distorted/library/EffectQueueFragment.java
21 21

  
22 22
import android.opengl.GLES20;
23 23

  
24
import org.distorted.library.message.EffectMessage;
24 25
import org.distorted.library.type.Float2D;
25 26
import org.distorted.library.type.Float3D;
26 27
import org.distorted.library.type.Float4D;
......
78 79
        {
79 80
        for(int j=0; j<mNumListeners; j++)   
80 81
          EffectMessageSender.newMessage( mListeners.elementAt(j),
81
                                          EffectMessage.EFFECT_FINISHED, 
82
                                          EffectMessage.EFFECT_FINISHED,
82 83
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
83 84
                                          mType[i], 
84 85
                                          mBitmapID,
src/main/java/org/distorted/library/EffectQueueMatrix.java
22 22
import android.opengl.GLES20;
23 23
import android.opengl.Matrix;
24 24

  
25
import org.distorted.library.message.EffectMessage;
25 26
import org.distorted.library.type.Float3D;
26 27
import org.distorted.library.type.Interpolator;
27 28
import org.distorted.library.type.Interpolator1D;
......
109 110
        {   
110 111
        for(int j=0; j<mNumListeners; j++)   
111 112
          EffectMessageSender.newMessage( mListeners.elementAt(j),
112
                                          EffectMessage.EFFECT_FINISHED, 
113
                                          EffectMessage.EFFECT_FINISHED,
113 114
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.MATRIX.type,
114 115
                                          mType[i], 
115 116
                                          mBitmapID,
src/main/java/org/distorted/library/EffectQueueOther.java
24 24
import android.graphics.Bitmap;
25 25
import android.opengl.GLES20;
26 26

  
27
import org.distorted.library.message.EffectMessage;
28

  
27 29
import java.io.BufferedOutputStream;
28 30
import java.io.FileOutputStream;
29 31
import java.io.IOException;
src/main/java/org/distorted/library/EffectQueueVertex.java
21 21

  
22 22
import android.opengl.GLES20;
23 23

  
24
import org.distorted.library.message.EffectMessage;
24 25
import org.distorted.library.type.Float2D;
25 26
import org.distorted.library.type.Float4D;
26 27
import org.distorted.library.type.Interpolator;
......
76 77
        {
77 78
        for(int j=0; j<mNumListeners; j++)   
78 79
          EffectMessageSender.newMessage( mListeners.elementAt(j),
79
                                          EffectMessage.EFFECT_FINISHED, 
80
                                          EffectMessage.EFFECT_FINISHED,
80 81
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.VERTEX.type,
81 82
                                          mType[i], 
82 83
                                          mBitmapID,
src/main/java/org/distorted/library/GridBitmap.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
class GridBitmap extends GridObject
28
  {
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

  
32
  GridBitmap(int xLength, int yLength)
33
    {
34
    dataLength = 2*xLength*(yLength-1)+2*(yLength-2); // (yLength-1) strips, 2*xLength triangles in each, plus 2 degenerate triangles per each of (yLength-2) joins 
35
    final short[] indexData = new short[dataLength];
36
    
37
    int offset=0;    
38
    for(int y=0; y<yLength-1; y++) 
39
      {
40
      if (y>0) indexData[offset++] = (short) (y*xLength); // Degenerate begin: repeat first vertex
41

  
42
      for (int x = 0; x < xLength; x++) 
43
        {
44
        indexData[offset++] = (short) (( y   *xLength)+x);
45
        indexData[offset++] = (short) (((y+1)*xLength)+x);
46
        }
47

  
48
      if (y<yLength-2) indexData[offset++] = (short) (((y+1)*xLength) + (xLength-1)); // Degenerate end: repeat last vertex
49
      }
50

  
51
    // for(int g=0; g<dataLength; g++) Log.e(TAG_BACKGROUND, "index["+g+"]="+indexData[g]);
52

  
53
    float[] bufferData= new float[COLOR_DATA_SIZE*dataLength];
54

  
55
    offset=0;
56
    for(int i=0; i<dataLength; i++)
57
      {
58
      bufferData[offset++] = 1.0f; // r
59
      bufferData[offset++] = 1.0f; // g
60
      bufferData[offset++] = 1.0f; // b
61
      bufferData[offset++] = 1.0f; // a
62
      }
63
    mGridColors = ByteBuffer.allocateDirect(COLOR_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
64
    mGridColors.put(bufferData).position(0); 
65

  
66
    bufferData = new float[NORMAL_DATA_SIZE*dataLength];
67

  
68
    offset=0;
69
    for(int i=0; i<dataLength; i++)
70
      {
71
      bufferData[offset++] = 0.0f; // x
72
      bufferData[offset++] = 0.0f; // y
73
      bufferData[offset++] = 1.0f; // z
74
      }
75
    mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
76
    mGridNormals.put(bufferData).position(0); 
77

  
78
    float tmpx,tmpy,tmpz;
79
    bufferData = new float[TEX_DATA_SIZE*dataLength];
80

  
81
    offset=0;
82
    for(int i=0; i<dataLength; i++)
83
      {
84
      tmpx = ((float)(indexData[offset/2]%xLength))/(xLength-1);
85
      tmpy = ((float)(indexData[offset/2]/xLength))/(yLength-1);
86

  
87
      bufferData[offset++] = tmpx; // s=x
88
      bufferData[offset++] = tmpy; // t=y
89
      }
90
    mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
91
    mGridTexture.put(bufferData).position(0); 
92

  
93
    //for(int g=0; g<dataLength; g++) Log.e(TAG_BACKGROUND, "tex["+g+"]=("+bufferData[2*g]+","+bufferData[2*g+1]+")");
94
    //Log.e(TAG, "regWidth="+(2*mRegW)+" regHeight="+(2*mRegH)+" xLength="+xLength+" yLength="+yLength);
95

  
96
    offset=0;
97
    bufferData= new float[POSITION_DATA_SIZE*dataLength];
98

  
99
    for(int i=0; i<dataLength; i++)
100
      {
101
      tmpx = ((float)(indexData[offset/3]%xLength))/(xLength-1);
102
      tmpy = ((float)(indexData[offset/3]/xLength))/(yLength-1);
103
      tmpz = 0;
104

  
105
      bufferData[offset++] = (tmpx-0.5f); // x
106
      bufferData[offset++] = (0.5f-tmpy); // y
107
      bufferData[offset++] =        tmpz; // z
108
      }
109
    mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
110
    mGridPositions.put(bufferData).position(0); 
111

  
112
    //for(int g=0; g<dataLength; g++) android.util.Log.e("BACKGROUND", "pos["+g+"]=("+bufferData[3*g]+","+bufferData[3*g+1]+")");
113
    }
114
  };
115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/GridCubes.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
class GridCubes extends GridObject
29
   {
30
   private static final float R = 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
   
47
   private class Edge
48
     {
49
     final int side; 
50
     final int row;
51
     final int col;
52
     
53
     public Edge(int s, int r, int c)
54
       {
55
       side= s; 
56
       row = r;
57
       col = c;
58
       }
59
     };
60
   
61
   private int frontVert, sideVert;
62
   private int mCols, mRows;
63
   private short[][] mCubes;
64
   private ArrayList<Edge> mEdges = new ArrayList<Edge>();
65
   
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

  
68
   private int computeDataLength(boolean frontOnly)
69
      {
70
      int frontWalls=0, frontSegments=0, sideWalls=0, sideBends=0;
71
      
72
      for(int i=0; i<mRows; i++)
73
         for(int j=0; j<mCols; j++)
74
            {
75
            if( mCubes[i][j]%2 == 1 )  // land
76
              {
77
              frontWalls++;
78
              if( j==mCols-1 || mCubes[i][j+1]%2 == 0 ) frontSegments++;
79
              }
80
              
81
            if( (i==0 && mCubes[i][j]!=2) || (i!=0 && mCubes[i][j] != mCubes[i-1][j  ]) ) sideWalls++; // up
82
            if( (j==0 && mCubes[i][j]!=2) || (j!=0 && mCubes[i][j] != mCubes[i  ][j-1]) ) sideWalls++; // left
83
            if( i==mRows-1 && mCubes[i][j]!=2                                           ) sideWalls++; // bottom
84
            if( j==mCols-1 && mCubes[i][j]!=2                                           ) sideWalls++; // right
85
            }
86

  
87
      int edges= mEdges.size();
88
      
89
      for(int i=0; i<edges; i++) 
90
        {
91
        Edge curr = mEdges.get(i);
92
        Edge next = getNextEdge(curr);
93
        int startX = curr.col;
94
        int startY = curr.row;
95
        int startS = curr.side;
96
        
97
        do
98
          {
99
          if( next.side != curr.side ) sideBends++; 
100
          curr  = next; 
101
          next = getNextEdge(curr);
102
          }
103
        while( curr.col!=startX || curr.row!=startY || curr.side!=startS );
104
        }
105
      
106
      frontVert = 2*( frontWalls + 2*frontSegments - 1);
107
      sideVert  = 2*( sideWalls + sideBends + edges -1);
108
      
109
      int dataL = frontOnly ? frontVert : (frontVert+1) + (1+sideVert+1) + (1+frontVert);
110
      
111
      android.util.Log.e("CUBES","frontVert="+frontVert+" sideVert="+sideVert);
112
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+sideWalls+" sSegments="+edges+" sideBends="+sideBends+" dataLen="+dataL );
113
      
114
      return dataL<0 ? 0:dataL;
115
      }
116

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

  
134
     for(int i=0; i<val.length; i++) 
135
        {
136
        if( i%stop==0 ) ret+="\n";
137
        ret+=(" "+val[i]);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff