Project

General

Profile

« Previous | Next » 

Revision a51fe521

Added by Leszek Koltunski over 7 years ago

Move from unpacked to packad server-side Vertex attribute buffer.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
300 300
    mV.send(halfW,halfH,halfZ);
301 301
    mF.send(halfW,halfH);
302 302

  
303
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mPosVBO[0]);
304
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
305
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mNorVBO[0]);
306
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, 0);
307
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mTexVBO[0]);
308
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, 0);
309
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
303
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
304
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
305
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
306
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
307
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
310 308
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0 );
311 309

  
312 310
    /// DEBUG ONLY //////
src/main/java/org/distorted/library/MeshCubes.java
185 185

  
186 186
   private void prepareDataStructures(int cols, String desc, boolean frontOnly)
187 187
     {
188
     mRows     =0;
189
     mCols     =0;
190
     dataLength=0;
188
     mRows      =0;
189
     mCols      =0;
190
     numVertices=0;
191 191
     
192 192
     if( cols>0 && desc.contains("1") )
193 193
       {
......
205 205
//android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
206 206

  
207 207
       markRegions();
208
       dataLength = computeDataLength(frontOnly);
208
       numVertices = computeDataLength(frontOnly);
209 209

  
210
       remainingVert = dataLength;
210
       remainingVert = numVertices;
211 211
       }
212 212
     }
213 213

  
......
216 216

  
217 217
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
218 218
     {
219
     mRows     =rows;
220
     mCols     =cols;
221
     dataLength=   0;
219
     mRows       =rows;
220
     mCols       =cols;
221
     numVertices =   0;
222 222

  
223 223
     if( cols>0 && rows>0 )
224 224
       {
......
233 233
//android.util.Log.d("cubes", "VERT FULL:"+debug(mBoundingVert,3));
234 234

  
235 235
       markRegions();
236
       dataLength = computeDataLength(frontOnly);
236
       numVertices = computeDataLength(frontOnly);
237 237

  
238
       remainingVert = dataLength;
238
       remainingVert = numVertices;
239 239
       }
240 240
     }
241 241

  
......
267 267

  
268 268
///////////////////////////////////////////////////////////////////////////////////////////////////
269 269

  
270
 private float retLeftmost(int row)
270
  private float retLeftmost(int row)
271 271
    {
272 272
    if( row==0 )
273 273
      {
......
439 439
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
440 440
// later on setting up normal vectors wouldn't work.
441 441
   
442
   private void markRegions()
442
  private void markRegions()
443 443
     {
444 444
     int i, j, numWater=1, numLand=0;
445 445
     
......
506 506
///////////////////////////////////////////////////////////////////////////////////////////////////
507 507
// when calling, make sure that newVal != val
508 508
   
509
   private void markRegion(short newVal, int row, int col)
509
  private void markRegion(short newVal, int row, int col)
510 510
     {
511 511
     int val = mCubes[row][col];
512 512
     mCubes[row][col] = newVal;
......
519 519
   
520 520
///////////////////////////////////////////////////////////////////////////////////////////////////
521 521
   
522
   private void createNormals(boolean front, int row, int col)
522
  private void createNormals(boolean front, int row, int col)
523 523
     {
524 524
     int td,lr; 
525 525
      
......
591 591
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
592 592
     */
593 593
     }
594
   
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

  
597
   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
598
     {
599
     remainingVert--;
600

  
601
     float x = (float)col/mCols;
602
     float y = (float)row/mRows;
603

  
604
     position[3*vertex  ] = x-0.5f;
605
     position[3*vertex+1] = 0.5f-y;
606
     position[3*vertex+2] = vectZ;
607
     normal[3*vertex  ]   = mNormalX[index];
608
     normal[3*vertex+1]   = mNormalY[index];
609
     normal[3*vertex+2]   = mNormalZ[index];
610
     texture[2*vertex  ]  = x;
611
     texture[2*vertex+1]  = 1.0f-y;
612

  
613
     return vertex+1;
614
     }
615 594

  
616 595
///////////////////////////////////////////////////////////////////////////////////////////////////
617 596

  
618
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
597
  private int buildFrontBackGrid(boolean front, int vertex, float[] attribs)
619 598
     {
620 599
     int last, current;
621 600
     boolean seenLand=false;
......
641 620
             {
642 621
             //android.util.Log.d("CUBES","repeating winding2 vertex");
643 622

  
644
             vertex = repeatLast(vertex,position,normal,texture);
623
             vertex = repeatLast(vertex,attribs);
645 624
             }
646 625

  
647 626
           createNormals(front,row,col);
......
650 629
             {
651 630
             if( (last!=current) || !lastBlockIsNE )
652 631
               {
653
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
654
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
655
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
656
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
657
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
632
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
633
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
634
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
635
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
636
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
658 637
               }
659
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
660
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
638
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, attribs);
639
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
661 640
             }
662 641
           else
663 642
             {
664 643
             if( (last!=current) || lastBlockIsNE )
665 644
               {
666
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
667
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
668
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
669
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
670
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
645
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
646
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
647
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
648
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
649
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
671 650
               }
672
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
673
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
651
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
652
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , attribs);
674 653
             }
675 654

  
676 655
           seenLand = true;
......
686 665

  
687 666
///////////////////////////////////////////////////////////////////////////////////////////////////
688 667

  
689
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
668
  private int repeatLast(int vertex, float[] attribs)
690 669
     {
691 670
     //android.util.Log.e("CUBES", "repeating last vertex!");
692 671

  
......
694 673
       {
695 674
       remainingVert--;
696 675

  
697
       position[3*vertex  ] = position[3*vertex-3];
698
       position[3*vertex+1] = position[3*vertex-2];
699
       position[3*vertex+2] = position[3*vertex-1];
700

  
701
       normal[3*vertex  ]   = normal[3*vertex-3];
702
       normal[3*vertex+1]   = normal[3*vertex-2];
703
       normal[3*vertex+2]   = normal[3*vertex-1];
704

  
705
       texture[2*vertex  ]  = texture[2*vertex-2];
706
       texture[2*vertex+1]  = texture[2*vertex-1];
676
       attribs[8*vertex  ] = attribs[8*vertex-8];
677
       attribs[8*vertex+1] = attribs[8*vertex-7];
678
       attribs[8*vertex+2] = attribs[8*vertex-6];
679
       attribs[8*vertex+3] = attribs[8*vertex-5];
680
       attribs[8*vertex+4] = attribs[8*vertex-4];
681
       attribs[8*vertex+5] = attribs[8*vertex-3];
682
       attribs[8*vertex+6] = attribs[8*vertex-2];
683
       attribs[8*vertex+7] = attribs[8*vertex-1];
707 684
         
708 685
       vertex++;     
709 686
       }
......
713 690
   
714 691
///////////////////////////////////////////////////////////////////////////////////////////////////
715 692

  
716
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
693
  private int buildSideGrid(int vertex, float[] attribs)
717 694
     {
718 695
     //android.util.Log.d("CUBES", "buildSide");
719 696

  
720 697
     for(int i=0; i<mEdgeNum; i++)
721 698
       {
722
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
699
       vertex = buildIthSide(mEdges.get(i), vertex, attribs);
723 700
       } 
724 701
      
725 702
     return vertex;
......
727 704

  
728 705
///////////////////////////////////////////////////////////////////////////////////////////////////
729 706

  
730
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
707
  private int buildIthSide(Edge curr, int vertex, float[] attribs)
731 708
     {
732 709
     Edge prev; 
733 710
     
......
746 723
     int side= curr.side;  
747 724
     Edge next = getNextEdge(curr);
748 725
     
749
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
726
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
750 727
     vertex++;
751 728
     
752 729
     do
753 730
       {
754 731
       if( prev.side!=curr.side )
755 732
         {
756
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
733
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
757 734
         vertex++;
758
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
735
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,attribs);
759 736
         vertex++;
760 737
         }
761 738
       
762
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
739
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,attribs);
763 740
       vertex++;
764
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
741
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,attribs);
765 742
       vertex++;
766 743
       
767 744
       prev = curr;
......
770 747
       }
771 748
     while( curr.col!=col || curr.row!=row || curr.side!=side );
772 749
     
773
     vertex = repeatLast(vertex,position,normal,texture);
750
     vertex = repeatLast(vertex,attribs);
774 751
     
775 752
     return vertex;
776 753
     }
777 754

  
778 755
///////////////////////////////////////////////////////////////////////////////////////////////////
779 756

  
780
   private Edge getNextEdge(Edge curr)
757
  private Edge getNextEdge(Edge curr)
781 758
     {
782 759
     int col = curr.col;
783 760
     int row = curr.row;
......
824 801
       }
825 802
     }
826 803

  
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805

  
806
  private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs)
807
     {
808
     remainingVert--;
809

  
810
     float x = (float)col/mCols;
811
     float y = (float)row/mRows;
812

  
813
     attribs[8*vertex  ] = x-0.5f;
814
     attribs[8*vertex+1] = 0.5f-y;
815
     attribs[8*vertex+2] = vectZ;
816
     attribs[8*vertex+3] = mNormalX[index];
817
     attribs[8*vertex+4] = mNormalY[index];
818
     attribs[8*vertex+5] = mNormalZ[index];
819
     attribs[8*vertex+6] = x;
820
     attribs[8*vertex+7] = 1.0f-y;
821

  
822
     return vertex+1;
823
     }
824

  
827 825
///////////////////////////////////////////////////////////////////////////////////////////////////
828 826
   
829
   private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
827
  private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] attribs)
830 828
     {
831 829
     //android.util.Log.e("CUBES", "adding Side vertex!");
832 830

  
......
838 836
       {
839 837
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
840 838

  
841
                   position[3*vertex  ] = x - 0.5f;
842
                   position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
843
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
844

  
845
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
846
                   normal[3*vertex+1]   = 1.0f;
847
                   normal[3*vertex+2]   = lower ? -R:R;
848

  
849
                   texture[2*vertex  ]  = x;
850
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
839
                   attribs[8*vertex  ] = x - 0.5f;
840
                   attribs[8*vertex+1] = 0.5f - (float)curr.row/mRows;
841
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
842
                   attribs[8*vertex+3] = side==NORTH ? 0.0f : (side==WEST?-R:R);
843
                   attribs[8*vertex+4] = 1.0f;
844
                   attribs[8*vertex+5] = lower ? -R:R;
845
                   attribs[8*vertex+6] = x;
846
                   attribs[8*vertex+7] = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
851 847
                   break;
852 848
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
853 849

  
854
                   position[3*vertex  ] = x - 0.5f;
855
                   position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
856
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
857
            
858
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
859
                   normal[3*vertex+1]   =-1.0f;
860
                   normal[3*vertex+2]   = lower ? -R:R;
861

  
862
                   texture[2*vertex  ]  = x;
863
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
850
                   attribs[8*vertex  ] = x - 0.5f;
851
                   attribs[8*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
852
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
853
                   attribs[8*vertex+3] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
854
                   attribs[8*vertex+4] =-1.0f;
855
                   attribs[8*vertex+5] = lower ? -R:R;
856
                   attribs[8*vertex+6] = x;
857
                   attribs[8*vertex+7] = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
864 858
                   break;
865 859
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
866 860

  
867
                   position[3*vertex  ] = (float)curr.col/mCols -0.5f;
868
                   position[3*vertex+1] = 0.5f - y;
869
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
870

  
871
                   normal[3*vertex  ]   =-1.0f;
872
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
873
                   normal[3*vertex+2]   = lower ? -R:R;
874
 
875
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
876
                   texture[2*vertex+1]  = 1.0f - y;
861
                   attribs[8*vertex  ] = (float)curr.col/mCols -0.5f;
862
                   attribs[8*vertex+1] = 0.5f - y;
863
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
864
                   attribs[8*vertex+3] =-1.0f;
865
                   attribs[8*vertex+4] = side==WEST ? 0.0f : (side==NORTH?-R:R);
866
                   attribs[8*vertex+5] = lower ? -R:R;
867
                   attribs[8*vertex+6] = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
868
                   attribs[8*vertex+7] = 1.0f - y;
877 869
                   break;
878 870
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
879 871

  
880
                   position[3*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
881
                   position[3*vertex+1] = 0.5f - y;
882
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
883

  
884
                   normal[3*vertex  ]   = 1.0f;
885
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
886
                   normal[3*vertex+2]   = lower ? -R:R; 
887

  
888
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
889
                   texture[2*vertex+1]  = 1.0f - y;
872
                   attribs[8*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
873
                   attribs[8*vertex+1] = 0.5f - y;
874
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
875
                   attribs[8*vertex+3] = 1.0f;
876
                   attribs[8*vertex+4] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
877
                   attribs[8*vertex+5] = lower ? -R:R;
878
                   attribs[8*vertex+6] = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
879
                   attribs[8*vertex+7] = 1.0f - y;
890 880
                   break;
891 881
       }
892 882
     
893
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
894
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
895
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
896
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
883
     if(attribs[8*vertex+6]>1.0f) attribs[8*vertex+6] = 2.0f-attribs[8*vertex+6];
884
     if(attribs[8*vertex+6]<0.0f) attribs[8*vertex+6] =     -attribs[8*vertex+6];
885
     if(attribs[8*vertex+7]>1.0f) attribs[8*vertex+7] = 2.0f-attribs[8*vertex+7];
886
     if(attribs[8*vertex+7]<0.0f) attribs[8*vertex+7] =     -attribs[8*vertex+7];
897 887
     }
898 888

  
899 889
///////////////////////////////////////////////////////////////////////////////////////////////////
900 890

  
901
   private void build(boolean frontOnly)
891
  private void build(boolean frontOnly)
902 892
     {
903
     int numVertices=0;
904
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
905
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
906
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
893
     int vertSoFar=0;
894
     float[] attribs= new float[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
907 895

  
908
     //android.util.Log.d("CUBES","building front grid...");
896
     //android.util.Log.d("MeshCubes","building front grid...");
909 897

  
910
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
898
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
911 899

  
912 900
     if( !frontOnly )
913 901
       {
914
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
915
       if( numVertices%2==1 )
902
       vertSoFar = repeatLast(vertSoFar,attribs);
903
       if( vertSoFar%2==1 )
916 904
         {
917
         //android.util.Log.d("CUBES","repeating winding1 vertex");
905
         //android.util.Log.d("MeshCubes","repeating winding1 vertex");
918 906

  
919
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
907
         vertSoFar = repeatLast(vertSoFar,attribs);
920 908
         }
921 909

  
922
       //android.util.Log.d("CUBES","building side grid...");
910
       //android.util.Log.d("MeshCubes","building side grid...");
923 911

  
924
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
912
       vertSoFar = buildSideGrid (vertSoFar,attribs);
925 913

  
926
       //android.util.Log.d("CUBES","building back grid...");
914
       //android.util.Log.d("MeshCubes","building back grid...");
927 915

  
928
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
916
       buildFrontBackGrid (false,vertSoFar,attribs);
929 917
       }
930 918

  
931
     /*
932
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
933
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
934
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
935
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
936
     */
919
     //android.util.Log.e("MeshCubes", "dataLen="+numVertices);
920
     //android.util.Log.d("MeshCubes", "attribs: "+debug(attribs,8) );
937 921

  
938 922
     mEdges.clear();
939 923
     mEdges = null;
940 924
     mCubes = null;
941 925

  
942 926
     if( remainingVert!=0 )
943
       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
927
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
944 928

  
945
     mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
946
     mMeshPositions.put(positionData).position(0);
947

  
948
     mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
949
     mMeshNormals.put(normalData).position(0);
950

  
951
     mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
952
     mMeshTexture.put(textureData).position(0);
929
     mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
930
     mVertAttribs.put(attribs).position(0);
953 931
     }
954 932

  
955 933
///////////////////////////////////////////////////////////////////////////////////////////////////
956 934

  
957
   float[] getBoundingVertices()
935
  float[] getBoundingVertices()
958 936
     {
959 937
     return mBoundingVert;
960 938
     }
......
986 964
 *                  </p>
987 965
 * @param frontOnly Only create the front wall or side and back as well?
988 966
 */
989
   public MeshCubes(int cols, String desc, boolean frontOnly)
990
      {
991
      super(frontOnly ? 0.0f:1.0f/cols);
992
      prepareDataStructures(cols,desc,frontOnly);
993
      build(frontOnly);
994
      }
967
 public MeshCubes(int cols, String desc, boolean frontOnly)
968
   {
969
   super(frontOnly ? 0.0f:1.0f/cols);
970
   prepareDataStructures(cols,desc,frontOnly);
971
   build(frontOnly);
972
   }
995 973

  
996 974
///////////////////////////////////////////////////////////////////////////////////////////////////
997 975
/**
......
1001 979
 * @param rows      Number of rows.
1002 980
 * @param frontOnly Only create the front wall or side and back as well?
1003 981
 */
1004
   public MeshCubes(int cols, int rows, boolean frontOnly)
1005
      {
1006
      super(frontOnly ? 0.0f:1.0f/cols);
1007
      prepareDataStructures(cols,rows,frontOnly);
1008
      build(frontOnly);
1009
      }
982
 public MeshCubes(int cols, int rows, boolean frontOnly)
983
   {
984
   super(frontOnly ? 0.0f:1.0f/cols);
985
   prepareDataStructures(cols,rows,frontOnly);
986
   build(frontOnly);
1010 987
   }
1011
///////////////////////////////////////////////////////////////////////////////////////////////////
1012

  
988
 }
src/main/java/org/distorted/library/MeshFlat.java
42 42
///////////////////////////////////////////////////////////////////////////////////////////////////
43 43
// Create a flat, full grid.
44 44

  
45
   private void computeNumberOfVertices(int cols, int rows)
45
  private void computeNumberOfVertices(int cols, int rows)
46 46
     {
47 47
     mRows=rows;
48 48
     mCols=cols;
49 49

  
50 50
     if( cols==1 && rows==1 )
51 51
       {
52
       dataLength = 4;
52
       numVertices = 4;
53 53
       }
54 54
     else
55 55
       {
56
       dataLength = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
57
                    (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
56
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
57
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
58 58
       }
59 59

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

  
62
     remainingVert = dataLength;
62
     remainingVert = numVertices;
63 63
     }
64 64

  
65 65

  
66 66
///////////////////////////////////////////////////////////////////////////////////////////////////
67 67

  
68
   private int addVertex(int vertex, float x, float y, float[] position, float[] normal, float[] texture)
68
  private int addVertex(int vertex, float x, float y, float[] attribs)
69 69
     {
70 70
     remainingVert--;
71 71

  
72
     position[3*vertex  ] = x-0.5f;
73
     position[3*vertex+1] = 0.5f-y;
74
     position[3*vertex+2] = 0;
72
     attribs[8*vertex  ] = x-0.5f;
73
     attribs[8*vertex+1] = 0.5f-y;
74
     attribs[8*vertex+2] = 0;
75 75

  
76
     texture[2*vertex  ]  = x;
77
     texture[2*vertex+1]  = 1.0f-y;
76
     attribs[8*vertex+3] = 0.0f;
77
     attribs[8*vertex+4] = 0.0f;
78
     attribs[8*vertex+5] = 1.0f;
78 79

  
79
     normal[3*vertex  ]   = 0.0f;
80
     normal[3*vertex+1]   = 0.0f;
81
     normal[3*vertex+2]   = 1.0f;
80
     attribs[8*vertex+6] = x;
81
     attribs[8*vertex+7] = 1.0f-y;
82 82

  
83 83
     return vertex+1;
84 84
     }
85 85
///////////////////////////////////////////////////////////////////////////////////////////////////
86 86

  
87
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
87
  private int repeatLast(int vertex, float[] attribs)
88 88
     {
89 89
     remainingVert--;
90 90

  
......
92 92

  
93 93
     if( vertex>0 )
94 94
       {
95
       position[3*vertex  ] = position[3*vertex-3];
96
       position[3*vertex+1] = position[3*vertex-2];
97
       position[3*vertex+2] = position[3*vertex-1];
98

  
99
       normal[3*vertex  ]   = normal[3*vertex-3];
100
       normal[3*vertex+1]   = normal[3*vertex-2];
101
       normal[3*vertex+2]   = normal[3*vertex-1];
102

  
103
       texture[2*vertex  ]  = texture[2*vertex-2];
104
       texture[2*vertex+1]  = texture[2*vertex-1];
95
       attribs[8*vertex  ] = attribs[8*vertex-8];
96
       attribs[8*vertex+1] = attribs[8*vertex-7];
97
       attribs[8*vertex+2] = attribs[8*vertex-6];
98
       attribs[8*vertex+3] = attribs[8*vertex-5];
99
       attribs[8*vertex+4] = attribs[8*vertex-4];
100
       attribs[8*vertex+5] = attribs[8*vertex-3];
101
       attribs[8*vertex+6] = attribs[8*vertex-2];
102
       attribs[8*vertex+7] = attribs[8*vertex-1];
105 103

  
106 104
       vertex++;
107 105
       }
......
111 109

  
112 110
///////////////////////////////////////////////////////////////////////////////////////////////////
113 111

  
114
   private void buildGrid(float[] position, float[] normal, float[] texture)
112
  private void buildGrid(float[] attribs)
115 113
     {
116 114
     boolean lastBlockIsNE = false;
117 115
     boolean currentBlockIsNE;
......
135 133

  
136 134
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
137 135
           {
138
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
139
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), position, normal, texture);
140
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
141
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
142
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), position, normal, texture);
136
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
137
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
138
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
139
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
140
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
143 141
           }
144
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), position, normal, texture);
145
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), position, normal, texture);
142
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
143
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
146 144

  
147 145
         lastBlockIsNE = currentBlockIsNE;
148 146
         x+=X;
......
156 154

  
157 155
///////////////////////////////////////////////////////////////////////////////////////////////////
158 156
/*
159
   private static String debug(float[] val, int stop)
157
  private static String debug(float[] val, int stop)
160 158
     {
161 159
     String ret="";
162 160

  
......
169 167
     return ret;
170 168
     }
171 169
*/
172

  
173 170
///////////////////////////////////////////////////////////////////////////////////////////////////
174 171

  
175
   float[] getBoundingVertices()
172
  float[] getBoundingVertices()
176 173
     {
177 174
     return mBoundingVert;
178 175
     }
......
186 183
 * @param cols Number of columns in the grid.
187 184
 * @param rows Number of rows in the grid.
188 185
 */
189
   public MeshFlat(int cols, int rows)
190
      {
191
      super(0.0f);
192
      computeNumberOfVertices(cols,rows);
193

  
194
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
195
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
196
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
197

  
198
      buildGrid(positionData,normalData,textureData);
186
 public MeshFlat(int cols, int rows)
187
    {
188
    super(0.0f);
189
    computeNumberOfVertices(cols,rows);
199 190

  
200
      //android.util.Log.e("CUBES","dataLen="+dataLength);
201
      //android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
202
      //android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
203
      //android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
191
    float[] attribs= new float[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
204 192

  
205
      if( remainingVert!=0 )
206
        android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
193
    buildGrid(attribs);
207 194

  
208
      mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
209
      mMeshPositions.put(positionData).position(0);
195
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
196
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
210 197

  
211
      mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
212
      mMeshNormals.put(normalData).position(0);
198
    if( remainingVert!=0 )
199
      android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
213 200

  
214
      mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
215
      mMeshTexture.put(textureData).position(0);
216
      }
217
  }
201
    mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
202
    mVertAttribs.put(attribs).position(0);
203
    }
204
 }
src/main/java/org/distorted/library/MeshObject.java
28 28
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
29 29
 * 2) normals 3) texture coordinates.
30 30
 * <p>
31
 * If you want to render to a particular shape, extend from here, construct the three FloatBuffers and
32
 * provide correct dataLength, i.e. the number of vertices.
31
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
32
 * and provide correct numVertices.
33 33
 */
34 34
public abstract class MeshObject extends DistortedObject
35 35
   {
36
   static final int BYTES_PER_FLOAT   = 4; //
37
   static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
38
   static final int NORMAL_DATA_SIZE  = 3; // Size of the normal data in elements.
39
   static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
36
   private static final int BYTES_PER_FLOAT = 4;
40 37

  
41
   int dataLength;
42
   FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture;
43
   int[] mPosVBO = new int[1];
44
   int[] mNorVBO = new int[1];
45
   int[] mTexVBO = new int[1];
38
   static final int POSITION_DATA_SIZE= 3;
39
   static final int NORMAL_DATA_SIZE  = 3;
40
   static final int TEX_DATA_SIZE     = 2;
46 41

  
47
   final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
48
                        // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
42
   static final int OFFSET0 =                                                                   0;
43
   static final int OFFSET1 = (POSITION_DATA_SIZE                               )*BYTES_PER_FLOAT;
44
   static final int OFFSET2 = (POSITION_DATA_SIZE+NORMAL_DATA_SIZE              )*BYTES_PER_FLOAT;
45
   static final int VERTSIZE= (POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
46

  
47
   int numVertices;
48
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX, NorY,NorZ, TexS, TexT
49
   int[] mAttVBO = new int[1]; // server-side packed vertex attributes
50

  
51
   final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
52
                               // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
49 53

  
50 54
///////////////////////////////////////////////////////////////////////////////////////////////////
51 55

  
......
60 64
///////////////////////////////////////////////////////////////////////////////////////////////////
61 65
// must be called from a thread holding OpenGL Context
62 66
//
63
// Do NOT release mMeshPositions etc as we will need them when we need to re-create the buffers after
67
// Do NOT release mVertAttribs etc as we will need them when we need to re-create the buffers after
64 68
// a loss of OpenGL context!
65 69

  
66 70
   void create()
67 71
     {
68
     if( mPosVBO[0]<0 )
69
       {
70
       GLES30.glGenBuffers(1, mPosVBO, 0);
71
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mPosVBO[0]);
72
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, mMeshPositions, GLES30.GL_STATIC_READ);
73
       }
74
     if( mNorVBO[0]<0 )
75
       {
76
       GLES30.glGenBuffers(1, mNorVBO, 0);
77
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mNorVBO[0]);
78
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*  NORMAL_DATA_SIZE*BYTES_PER_FLOAT, mMeshNormals  , GLES30.GL_STATIC_READ);
79
       }
80
     if( mTexVBO[0]<0 )
72
     if( mAttVBO[0]<0 )
81 73
       {
82
       GLES30.glGenBuffers(1, mTexVBO, 0);
83
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mTexVBO[0]);
84
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*    TEX_DATA_SIZE*BYTES_PER_FLOAT, mMeshTexture  , GLES30.GL_STATIC_READ);
74
       GLES30.glGenBuffers(1, mAttVBO, 0);
75
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mAttVBO[0]);
76
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, numVertices*VERTSIZE, mVertAttribs, GLES30.GL_STATIC_READ);
77
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
85 78
       }
86

  
87
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
88 79
     }
89 80

  
90 81
///////////////////////////////////////////////////////////////////////////////////////////////////
......
92 83

  
93 84
   void delete()
94 85
     {
95
     if( mPosVBO[0]>=0 )
96
       {
97
       GLES30.glDeleteBuffers(1, mPosVBO, 0);
98
       mPosVBO[0] = -1;
99
       }
100
     if( mNorVBO[0]>=0 )
101
       {
102
       GLES30.glDeleteBuffers(1, mNorVBO, 0);
103
       mNorVBO[0] = -1;
104
       }
105
     if( mTexVBO[0]>=0 )
86
     if( mAttVBO[0]>=0 )
106 87
       {
107
       GLES30.glDeleteBuffers(1, mTexVBO, 0);
108
       mTexVBO[0] = -1;
88
       GLES30.glDeleteBuffers(1, mAttVBO, 0);
89
       mAttVBO[0] = -1;
109 90
       }
110 91
     }
111 92

  
......
113 94

  
114 95
   void recreate()
115 96
     {
116
     mPosVBO[0] = -1;
117
     mNorVBO[0] = -1;
118
     mTexVBO[0] = -1;
97
     mAttVBO[0] = -1;
119 98
     }
120 99

  
121 100
///////////////////////////////////////////////////////////////////////////////////////////////////
......
123 102

  
124 103
   String printDetails()
125 104
     {
126
     return getClass().getSimpleName()+" vertices:"+dataLength;
105
     return getClass().getSimpleName()+" vertices:"+ numVertices;
127 106
     }
128 107

  
129 108
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff