Project

General

Profile

« Previous | Next » 

Revision 7a9edb92

Added by Leszek Koltunski over 3 years ago

Add component centers to dmesh version 2.

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.java
312 312

  
313 313
///////////////////////////////////////////////////////////////////////////////////////////////////
314 314

  
315
   void setEffectCenterNow(int component, float x, float y, float z)
315
   void setComponentCenterNow(int component, float x, float y, float z)
316 316
     {
317 317
     mUBC.setEffectCenterNow(component, x, y, z);
318 318
     }
......
702 702
       try
703 703
         {
704 704
         String name = "/sdcard/"+mNumVertices+".dmesh";
705
         DataOutputStream dos = new DataOutputStream(new FileOutputStream(name));
705
         DataOutputStream dos = new DataOutputStream(new java.io.FileOutputStream(name));
706 706
         write(dos);
707 707
         android.util.Log.e("mesh", "file written: "+name);
708 708
         }
709
       catch(FileNotFoundException ex)
709
       catch(java.io.FileNotFoundException ex)
710 710
         {
711 711
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
712 712
         }
......
770 770
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
771 771
     int version, numEff, numTex;
772 772

  
773
     //////////////////////////////////////////////////////////////////////////////////////////
774
     // read version, number of vertices, number of tex components, number of effect components
775
     //////////////////////////////////////////////////////////////////////////////////////////
776

  
773 777
     try
774 778
       {
775 779
       stream.read(buffer);
......
795 799
       return 0;
796 800
       }
797 801

  
802
     //////////////////////////////////////////////////////////////////////////////////////////
803
     // read contents of all texture and effect components
804
     //////////////////////////////////////////////////////////////////////////////////////////
805

  
798 806
     if( mNumVertices>0 && numEff>0 && numTex>0 )
799 807
       {
800 808
       int size = numEff+TEX_COMP_SIZE*numTex;
......
803 811
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
804 812

  
805 813
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
806
       int vert1InFile = version==1 ? VERT1_ATTRIBS+3 : VERT1_ATTRIBS;
814
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
815
       // ... and there was no center.
816
       int centerSize  = version==2 ? 3*numEff : 0;
807 817

  
808
       buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(vert1InFile+VERT2_ATTRIBS))];
818
       buffer = new byte[BYTES_PER_FLOAT*(size + centerSize + mNumVertices*(vert1InFile+VERT2_ATTRIBS))];
809 819

  
810 820
       try
811 821
         {
......
822 832

  
823 833
       floatBuf.get(tmp,0,size);
824 834

  
825
       switch(version)
826
         {
827
         case 1 : int index = floatBuf.position();
828

  
829
                  for(int vert=0; vert<mNumVertices; vert++)
830
                    {
831
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
832
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
833
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
834
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
835
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
836
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
837
                    index+=vert1InFile;
838
                    }
839
                  floatBuf.position(index);
840
                  break;
841
         case 2 : floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
842
                  break;
843
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
844
                  return 0;
845
         }
846

  
847
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
848

  
849 835
       TexComponent tex;
850 836
       int index, texComp;
851 837
       float x, y, z, w;
......
870 856
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
871 857
         mEffComponent.add(index);
872 858
         }
859

  
860
       //////////////////////////////////////////////////////////////////////////////////////////
861
       // read vert1 array, vert2 array and (if version>1) the component centers.
862
       //////////////////////////////////////////////////////////////////////////////////////////
863

  
864
       switch(version)
865
         {
866
         case 1 : index = floatBuf.position();
867

  
868
                  for(int vert=0; vert<mNumVertices; vert++)
869
                    {
870
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
871
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
872
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
873
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
874
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
875
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
876
                    index+=vert1InFile;
877
                    }
878
                  floatBuf.position(index);
879
                  break;
880
         case 2 : float[] centers = new float[3*numEff];
881
                  floatBuf.get(centers,0,3*numEff);
882

  
883
                  for(int eff=0; eff<numEff; eff++)
884
                    {
885
                    mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
886
                    }
887

  
888
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
889
                  break;
890
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
891
                  return 0;
892
         }
893

  
894
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
873 895
       }
874 896

  
875 897
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
......
939 961
         stream.writeFloat((float)mEffComponent.get(i));
940 962
         }
941 963

  
964
       float[] centers = mUBC.getBackingArray();
965

  
966
       for(int i=0; i<numEff; i++)
967
         {
968
         stream.writeFloat(centers[4*i  ]);
969
         stream.writeFloat(centers[4*i+1]);
970
         stream.writeFloat(centers[4*i+2]);
971
         }
972

  
942 973
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
943 974
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
944 975
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
......
1139 1170
       }
1140 1171
     }
1141 1172

  
1173
///////////////////////////////////////////////////////////////////////////////////////////////////
1174
/**
1175
 * Set center of a Component.
1176
 *
1177
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1178
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1179
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1180
 * add the 'halo' around an object.
1181
 *
1182
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1183
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1184
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1185
 */
1186
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1187
     {
1188
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1189
       {
1190
       if( mJobNode[0]==null )
1191
         {
1192
         setComponentCenterNow(component, centerX, centerY, centerZ);
1193
         }
1194
       else
1195
         {
1196
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1197
         }
1198
       }
1199
     }
1200

  
1142 1201
///////////////////////////////////////////////////////////////////////////////////////////////////
1143 1202
/**
1144 1203
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices

Also available in: Unified diff