Project

General

Profile

« Previous | Next » 

Revision 665e2c45

Added by Leszek Koltunski about 8 years ago

New constructor to DistortedCubes - easily create a hole-less Cuboid.

View differences:

src/main/java/org/distorted/library/DistortedCubes.java
103 103
   initializeData();
104 104
   }
105 105

  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
/**
108
 * Creates internal memory representation of a full, hole-less cuboid subset.
109
 *
110
 * @param cols Number of columns
111
 * @param rows Number of rows
112
 * @param cubeSize size, in pixels, of the single 1x1x1 cube our cuboid is built from
113
 * @param frontOnly Only create the front wall or side and back as well?
114
 */
115
 public DistortedCubes(int cols, int rows, int cubeSize, boolean frontOnly)
116
   {
117
   mSizeX= cubeSize*cols;
118
   mSizeY= cubeSize*rows;
119
   mSizeZ= frontOnly ? 1 : cubeSize;
120
   mGrid = DistortedGridFactory.getGrid(cols,rows, frontOnly);
121
   initializeData();
122
   }
123

  
106 124
///////////////////////////////////////////////////////////////////////////////////////////////////
107 125
/**
108 126
 * Convenience constructor.
src/main/java/org/distorted/library/DistortedCubesGrid.java
228 228
       remainingVert = dataLength;
229 229
       }
230 230
     }
231
 
231

  
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
// full grid
234

  
235
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
236
     {
237
     mRows     =rows;
238
     mCols     =cols;
239
     dataLength=   0;
240

  
241
     if( cols>0 && rows>0 )
242
       {
243
       mCubes = new short[mRows][mCols];
244

  
245
       for(int j=0; j<mCols; j++)
246
         for(int i=0; i<mRows; i++)
247
           mCubes[i][j] = (short)1;
248

  
249
       markRegions();
250
       dataLength = computeDataLength(frontOnly);
251

  
252
       remainingVert = dataLength;
253
       }
254
     }
255

  
232 256
///////////////////////////////////////////////////////////////////////////////////////////////////
233 257
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
234 258
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
......
714 738
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
715 739
     }
716 740

  
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742

  
743
   private void build(boolean frontOnly)
744
     {
745
     int numVertices=0;
746
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
747
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
748
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
749

  
750
     //android.util.Log.d("CUBES","building front grid...");
751

  
752
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
753

  
754
     if( !frontOnly )
755
       {
756
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
757
       if( numVertices%2==1 )
758
         {
759
         //android.util.Log.d("CUBES","repeating winding1 vertex");
760

  
761
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
762
         }
763

  
764
       //android.util.Log.d("CUBES","building side grid...");
765

  
766
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
767

  
768
       //android.util.Log.d("CUBES","building back grid...");
769

  
770
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
771
       }
772

  
773
     /*
774
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
775
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
776
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
777
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
778
     */
779
     android.util.Log.d("CUBES", "remainingVert " +remainingVert );
780

  
781
     mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
782
     mGridPositions.put(positionData).position(0);
783

  
784
     mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
785
     mGridNormals.put(normalData).position(0);
786

  
787
     mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
788
     mGridTexture.put(textureData).position(0);
789
     }
790

  
717 791
///////////////////////////////////////////////////////////////////////////////////////////////////
718 792
// PUBLIC API
719 793
///////////////////////////////////////////////////////////////////////////////////////////////////
720
   
721 794
/**
722 795
 * Creates the underlying grid of vertices, normals, texture coords and colors.
723 796
 *    
......
725 798
 * @param desc      See {@link DistortedCubes#DistortedCubes(int,String,int,boolean)}
726 799
 * @param frontOnly See {@link DistortedCubes#DistortedCubes(int,String,int,boolean)}
727 800
 */
728
   public DistortedCubesGrid(int cols, String desc, boolean frontOnly)
801
   DistortedCubesGrid(int cols, String desc, boolean frontOnly)
729 802
      {
730
      //android.util.Log.d("CUBES","calculating dataLength...");
731

  
732 803
      prepareDataStructures(cols,desc,frontOnly);
733
       
734
      int numVertices=0;
735
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
736
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
737
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
738

  
739
      //android.util.Log.d("CUBES","building front grid...");
740

  
741
      numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
742
      
743
      if( !frontOnly )
744
        {
745
        numVertices = repeatLast(numVertices,positionData,normalData,textureData);
746
        if( numVertices%2==1 )
747
          {
748
          //android.util.Log.d("CUBES","repeating winding1 vertex");
749

  
750
          numVertices = repeatLast(numVertices,positionData,normalData,textureData);
751
          }
752

  
753
        //android.util.Log.d("CUBES","building side grid...");
754

  
755
        numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
756

  
757
        //android.util.Log.d("CUBES","building back grid...");
758

  
759
        numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
760
        }
761
      
762
      /*
763
      android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
764
      android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
765
      android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
766
      android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
767
      */
768
      android.util.Log.d("CUBES", "remainingVert " +remainingVert );
769

  
770
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
771
      mGridPositions.put(positionData).position(0); 
772
      
773
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
774
      mGridNormals.put(normalData).position(0); 
804
      build(frontOnly);
805
      }
775 806

  
776
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
777
      mGridTexture.put(textureData).position(0); 
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
/**
809
 * Creates a full, hole-less underlying grid of vertices, normals, texture coords and colors.
810
 *
811
 * @param cols      See {@link DistortedCubes#DistortedCubes(int,int,int,boolean)}
812
 * @param rows      See {@link DistortedCubes#DistortedCubes(int,int,int,boolean)}
813
 * @param frontOnly See {@link DistortedCubes#DistortedCubes(int,int,int,boolean)}
814
 */
815
   DistortedCubesGrid(int cols, int rows, boolean frontOnly)
816
      {
817
      prepareDataStructures(cols,rows,frontOnly);
818
      build(frontOnly);
778 819
      }
779 820
   }
780 821
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedGridFactory.java
35 35

  
36 36
  static synchronized DistortedObjectGrid getGrid(int cols, String desc, boolean frontOnly)
37 37
    {
38
    String d = desc+("_"+cols+"_"+(frontOnly?"1":"0"));
38
    String d = "1_"+cols+"_"+desc+"_"+(frontOnly?"1":"0");
39 39
    Object o = mGrids.get(d);
40 40

  
41 41
    if( o!=null )
......
50 50
      }
51 51
    }
52 52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// DistortedCubesGrid (full)
55

  
56
  static synchronized DistortedObjectGrid getGrid(int cols, int rows, boolean frontOnly)
57
    {
58
    String d = "2_"+cols+"_"+rows+"_"+(frontOnly?"1":"0");
59
    Object o = mGrids.get(d);
60

  
61
    if( o!=null )
62
      {
63
      return (DistortedObjectGrid)o;
64
      }
65
    else
66
      {
67
      DistortedObjectGrid grid = new DistortedCubesGrid(cols,rows,frontOnly);
68
      mGrids.put(d,grid);
69
      return grid;
70
      }
71
    }
72

  
53 73
///////////////////////////////////////////////////////////////////////////////////////////////////
54 74
// DistortedBitmapGrid
55 75

  

Also available in: Unified diff