Project

General

Profile

« Previous | Next » 

Revision 7a9edb92

Added by Leszek Koltunski over 4 years ago

Add component centers to dmesh version 2.

View differences:

src/main/java/org/distorted/library/mesh/DeferredJobs.java
45 45
  private static final int JOB_TYPE_COPY     = 5;
46 46
  private static final int JOB_TYPE_TEXTURE  = 6;
47 47
  private static final int JOB_TYPE_ASSOC    = 7;
48
  private static final int JOB_TYPE_CENTER   = 8;
48 49

  
49 50
  private static ArrayList<JobNode> mJobs = new ArrayList<>();
50 51

  
......
59 60
    private MatrixEffect mMatrixEffect;
60 61
    private Static4D[] mMaps;
61 62
    private int mComp, mAndAssoc, mEquAssoc;
63
    private float mX,mY,mZ;
62 64

  
63
    Job(int type, MeshBase target, MeshBase[] source, VertexEffect vEff, MatrixEffect mEff, Static4D[] maps, int comp, int and, int equ)
65
    Job(int type, MeshBase target, MeshBase[] source, VertexEffect vEff, MatrixEffect mEff, Static4D[] maps, int comp, int and, int equ, float x, float y, float z)
64 66
      {
65 67
      mType     = type;
66 68
      mTarget   = target;
......
69 71
      mComp     = comp;
70 72
      mAndAssoc = and;
71 73
      mEquAssoc = equ;
74
      mX        = x;
75
      mY        = y;
76
      mZ        = z;
72 77

  
73 78
      if( vEff!=null )
74 79
        {
......
104 109
                                 break;
105 110
        case JOB_TYPE_ASSOC    : mTarget.setEffectAssociationNow(mComp,mAndAssoc,mEquAssoc);
106 111
                                 break;
112
        case JOB_TYPE_CENTER   : mTarget.setComponentCenterNow(mComp,mX,mY,mZ);
113
                                 break;
107 114
        }
108 115
/*
109 116
      Log.e("jobs", "executing "+print()+" job");
......
128 135
        case JOB_TYPE_COPY     : return "COPY";
129 136
        case JOB_TYPE_TEXTURE  : return "TEXTURE";
130 137
        case JOB_TYPE_ASSOC    : return "ASSOC";
138
        case JOB_TYPE_CENTER   : return "CENTER";
131 139
        }
132 140

  
133 141
      return null;
......
231 239

  
232 240
    if( jn==null )
233 241
      {
234
      Job job = new Job(JOB_TYPE_VERTEX,target,null,effect,null,null,0,0,0);
242
      Job job = new Job(JOB_TYPE_VERTEX,target,null,effect,null,null,0,0,0,0,0,0);
235 243
      JobNode node = new JobNode(job);
236 244
      mJobs.add(node);
237 245
      return node;
......
245 253
        }
246 254
      else
247 255
        {
248
        Job job = new Job(JOB_TYPE_VERTEX,target,null,effect,null,null,0,0,0);
256
        Job job = new Job(JOB_TYPE_VERTEX,target,null,effect,null,null,0,0,0,0,0,0);
249 257
        JobNode node = new JobNode(job);
250 258
        node.mPrevJobs.add(jn);
251 259
        jn.mNextJobs.add(node);
......
260 268
  static JobNode matrix(MeshBase target, MatrixEffect effect, int andAssoc, int ecuAssoc)
261 269
    {
262 270
    JobNode jn = target.mJobNode[0];
263
    Job job = new Job(JOB_TYPE_MATRIX,target,null,null,effect,null,0,andAssoc,ecuAssoc);
271
    Job job = new Job(JOB_TYPE_MATRIX,target,null,null,effect,null,0,andAssoc,ecuAssoc,0,0,0);
264 272
    JobNode node = new JobNode(job);
265 273
    node.mPrevJobs.add(jn);
266 274
    jn.mNextJobs.add(node);
......
273 281
  static JobNode mergeTex(MeshBase target)
274 282
    {
275 283
    JobNode jn = target.mJobNode[0];
276
    Job job = new Job(JOB_TYPE_MERGE_TEX,target,null,null,null,null,0,0,0);
284
    Job job = new Job(JOB_TYPE_MERGE_TEX,target,null,null,null,null,0,0,0,0,0,0);
277 285
    JobNode node = new JobNode(job);
278 286
    node.mPrevJobs.add(jn);
279 287
    jn.mNextJobs.add(node);
......
286 294
  static JobNode mergeEff(MeshBase target)
287 295
    {
288 296
    JobNode jn = target.mJobNode[0];
289
    Job job = new Job(JOB_TYPE_MERGE_EFF,target,null,null,null,null,0,0,0);
297
    Job job = new Job(JOB_TYPE_MERGE_EFF,target,null,null,null,null,0,0,0,0,0,0);
290 298
    JobNode node = new JobNode(job);
291 299
    node.mPrevJobs.add(jn);
292 300
    jn.mNextJobs.add(node);
......
300 308
    {
301 309
    JobNode jn;
302 310

  
303
    Job job = new Job(JOB_TYPE_JOIN,target,meshes,null,null,null,0,0,0);
311
    Job job = new Job(JOB_TYPE_JOIN,target,meshes,null,null,null,0,0,0,0,0,0);
304 312
    JobNode node = new JobNode(job);
305 313

  
306 314
    for (MeshBase mesh : meshes)
......
325 333
    JobNode jn = mesh.mJobNode[0];
326 334
    MeshBase[] meshes = new MeshBase[1];
327 335
    meshes[0] = mesh;
328
    Job job = new Job(JOB_TYPE_COPY,target,meshes,null,null,null,0,0,0);
336
    Job job = new Job(JOB_TYPE_COPY,target,meshes,null,null,null,0,0,0,0,0,0);
329 337
    JobNode node = new JobNode(job);
330 338
    node.mPrevJobs.add(jn);
331 339
    jn.mNextJobs.add(node);
......
338 346
  static JobNode textureMap(MeshBase target, Static4D[] maps, int comp)
339 347
    {
340 348
    JobNode jn = target.mJobNode[0];
341
    Job job = new Job(JOB_TYPE_TEXTURE,target,null,null,null,maps,comp,0,0);
349
    Job job = new Job(JOB_TYPE_TEXTURE,target,null,null,null,maps,comp,0,0,0,0,0);
342 350
    JobNode node = new JobNode(job);
343 351
    node.mPrevJobs.add(jn);
344 352
    jn.mNextJobs.add(node);
......
351 359
  static JobNode effectAssoc(MeshBase target, int comp, int andAssoc, int equAssoc)
352 360
    {
353 361
    JobNode jn = target.mJobNode[0];
354
    Job job = new Job(JOB_TYPE_ASSOC,target,null,null,null,null,comp,andAssoc,equAssoc);
362
    Job job = new Job(JOB_TYPE_ASSOC,target,null,null,null,null,comp,andAssoc,equAssoc,0,0,0);
363
    JobNode node = new JobNode(job);
364
    node.mPrevJobs.add(jn);
365
    jn.mNextJobs.add(node);
366
    mJobs.add(node);
367
    return node;
368
    }
369

  
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

  
372
  static JobNode componentCenter(MeshBase target, int comp, float x, float y, float z)
373
    {
374
    JobNode jn = target.mJobNode[0];
375
    Job job = new Job(JOB_TYPE_CENTER,target,null,null,null,null,comp,0,0,x,y,z);
355 376
    JobNode node = new JobNode(job);
356 377
    node.mPrevJobs.add(jn);
357 378
    jn.mNextJobs.add(node);
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
src/main/java/org/distorted/library/mesh/UniformBlockCenter.java
55 55

  
56 56
///////////////////////////////////////////////////////////////////////////////////////////////////
57 57

  
58
   void setEffectCenterNow(int comp, float x, float y, float z)
59
     {
60
     mCenter[4*comp  ] = x;
61
     mCenter[4*comp+1] = y;
62
     mCenter[4*comp+2] = z;
58
  void setEffectCenterNow(int comp, float x, float y, float z)
59
    {
60
    mCenter[4*comp  ] = x;
61
    mCenter[4*comp+1] = y;
62
    mCenter[4*comp+2] = z;
63 63

  
64
     mUBO.invalidate();
65
     }
64
    mUBO.invalidate();
65
    }
66 66

  
67 67
///////////////////////////////////////////////////////////////////////////////////////////////////
68 68

  
69
   int getIndex()
70
     {
71
     return mUBO.createImmediatelyFloat( BLOCK_SIZE, mCenter);
72
     }
69
  int getIndex()
70
    {
71
    return mUBO.createImmediatelyFloat( BLOCK_SIZE, mCenter);
72
    }
73 73

  
74 74
///////////////////////////////////////////////////////////////////////////////////////////////////
75 75

  
76
   void copy(int compTo, UniformBlockCenter blockFrom, int compFrom)
77
     {
78
     mCenter[4*compTo  ] = blockFrom.mCenter[4*compFrom  ];
79
     mCenter[4*compTo+1] = blockFrom.mCenter[4*compFrom+1];
80
     mCenter[4*compTo+2] = blockFrom.mCenter[4*compFrom+2];
81
     }
76
  void copy(int compTo, UniformBlockCenter blockFrom, int compFrom)
77
    {
78
    mCenter[4*compTo  ] = blockFrom.mCenter[4*compFrom  ];
79
    mCenter[4*compTo+1] = blockFrom.mCenter[4*compFrom+1];
80
    mCenter[4*compTo+2] = blockFrom.mCenter[4*compFrom+2];
81
    }
82

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

  
85
  float[] getBackingArray()
86
    {
87
    return mCenter;
88
    }
82 89
  }

Also available in: Unified diff