Project

General

Profile

« Previous | Next » 

Revision 0d4aae88

Added by Leszek Koltunski almost 5 years ago

New MeshJoined.
Test app says it's not working - bug in MeshBase.join().

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.java
76 76
     private int mEndIndex;
77 77
     private float[] mTextureMap;
78 78

  
79
     Component()
79
     Component(int end)
80 80
       {
81
       mTextureMap = new float[8];
82

  
83
       mTextureMap[ 0] = 0.0f;  // LD_X
84
       mTextureMap[ 1] = 0.0f;  // LD_Y
85
       mTextureMap[ 2] = 0.0f;  // LU_X
86
       mTextureMap[ 3] = 1.0f;  // LU_Y
87
       mTextureMap[ 4] = 1.0f;  // RU_X
88
       mTextureMap[ 5] = 1.0f;  // RU_Y
89
       mTextureMap[ 6] = 1.0f;  // RD_X
90
       mTextureMap[ 7] = 0.0f;  // RD_Y
81
       mEndIndex = end;
82

  
83
       mTextureMap    = new float[4];
84
       mTextureMap[0] = 0.0f;  // LowerLeft_X
85
       mTextureMap[1] = 0.0f;  // LowerLeft_Y
86
       mTextureMap[2] = 1.0f;  // Width
87
       mTextureMap[3] = 1.0f;  // Height
91 88
       }
92 89
     Component(Component original)
93 90
       {
94 91
       mEndIndex = original.mEndIndex;
95
       mTextureMap = new float[8];
96
       System.arraycopy(original.mTextureMap,0,mTextureMap,0,8);
92
       mTextureMap = new float[4];
93
       System.arraycopy(original.mTextureMap,0,mTextureMap,0,4);
94
       }
95

  
96
     void setMap(float[] newMap)
97
       {
98
       mTextureMap = newMap;
97 99
       }
98 100
     }
99 101

  
......
113 115

  
114 116
     mShowNormals = false;
115 117
     mInflate     = 0.0f;
116
     mComponent = new ArrayList<>();
117
     mComponent.add(new Component());
118
     mComponent   = new ArrayList<>();
118 119

  
119 120
     mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
120 121
     mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
......
151 152
     setAttribs(mVertAttribs);
152 153
     }
153 154

  
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// change mVertAttribs from index 'begin' to index 'end' (inclusive) to the new Texture Map.
157
// x varies from -mBoundingX to +mBoundingX; y accordingly.
158

  
159
   private void changeTextureMap( float[] newMap, float[] oldMap, int begin, int end)
160
     {
161

  
162
     }
163

  
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

  
166
   int numComponents()
167
     {
168
     return mComponent.size();
169
     }
170

  
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

  
173
   void setBounding(float bx, float by, float bz)
174
     {
175
     mBoundingX = bx/2;
176
     mBoundingY = by/2;
177
     mBoundingZ = bz/2;
178
     }
179

  
154 180
///////////////////////////////////////////////////////////////////////////////////////////////////
155 181
// when a derived class is done computing its mesh, it has to call this method.
156 182

  
......
159 185
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
160 186
     mVertAttribs = vertexAttribs;
161 187

  
162
     mComponent.get(0).mEndIndex = mNumVertices;
188
     mComponent.add(new Component(mNumVertices));
163 189

  
164 190
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
165 191
     attribs.put(vertexAttribs).position(0);
......
169 195
     }
170 196

  
171 197
///////////////////////////////////////////////////////////////////////////////////////////////////
172
/**
173
 * Not part of public API, do not document (public only because has to be used from the main package)
174
 *
175
 * @y.exclude
176
 */
177
   public int getTFO()
178
     {
179
     return mTFO.mIndex[0];
180
     }
198
// called from MeshJoined
181 199

  
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
/**
184
 * Not part of public API, do not document (public only because has to be used from the main package)
185
 *
186
 * @y.exclude
187
 */
188
   public int getNumVertices()
200
   void join(MeshBase[] meshes)
189 201
     {
190
     return mNumVertices;
191
     }
202
     MeshBase mesh;
203
     Component comp;
204
     int com, num, len = meshes.length;
205
     int origVertices = mNumVertices;
192 206

  
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
/**
195
 * Each mesh has its 'bounding box' - return half of its X-length.
196
 * <p>
197
 * In case of all 'simple' Meshes, the bounding box is always 1x1x1 (Sphere, Cubes) or 1x1x0
198
 * (Rectangles, Triangles, Quad - i.e. all 'flat' Meshes). But this can be something else in case of
199
 * MeshComponent.
200
 */
201
   public float getBoundingX()
202
    {
203
    return mBoundingX*mStretchX;
204
    }
207
     // compute new numVertices; take care of Components
205 208

  
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
/**
208
 * Each mesh has its 'bounding box' - return half of its Y-length.
209
 */
210
   public float getBoundingY()
211
    {
212
    return mBoundingY*mStretchY;
213
    }
209
     if( origVertices>0 )
210
       {
211
       com = mComponent.size();
212
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
213
       mComponent.get(com-1).mEndIndex = mNumVertices;
214
       }
214 215

  
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
/**
217
 * Each mesh has its 'bounding box' - return half of its Z-length.
218
 */
219
   public float getBoundingZ()
220
    {
221
    return mBoundingZ*mStretchZ;
222
    }
216
     for(int i=0; i<len; i++)
217
       {
218
       mesh = meshes[i];
219
       com = mesh.mComponent.size();
223 220

  
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
/**
226
 * Sometimes we want to display a Mesh on a rectangular screen. Then we need to stretch it by
227
 * different factors in x and y (or z) directions. If we also wanted do display some vertex effects
228
 * done on this mesh, let's say a bulge done by a Distort effect, and wanted the bulge to be round,
229
 * (i.e the same in x and y directions) then doing so without this method would be impossible.
230
 *
231
 * This sets 'stretch' factors in each 3 dimensions. All vertices of this Mesh will be premultiplied
232
 * by those factors in the very first line of the Vertex Shader, before any Effects are done on it.
233
 * Using this we can thus pre-stretch the mesh to aspect ratio equal to the surface we eventually
234
 * want to display the Mesh on, and this way we can achieve a round Distort bulge!
235
 *
236
 * This could also be used to pre-stretch a Rectangles Mesh to a size equal (in pixels) to the bitmap
237
 * this mesh is textured with - and this lets us work with all Effects in natural, pixel units.
238
 *
239
 * @param sx stretch factor in x.
240
 * @param sy stretch factor in y.
241
 * @param sz stretch factor in z.
242
 */
243
   public void setStretch(float sx, float sy, float sz)
244
     {
245
     mStretchX = sx;
246
     mStretchY = sy;
247
     mStretchZ = sz;
248
     }
221
       for(int j=0; j<com; j++)
222
         {
223
         comp = new Component(mesh.mComponent.get(j));
224
         comp.mEndIndex += mNumVertices;
225
         mComponent.add(comp);
226
         }
249 227

  
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
/**
252
 * Returns the x-factor set by setStretch().
253
 */
254
   public float getStretchX()
255
     {
256
     return mStretchX;
228
       num = mesh.mNumVertices;
229
       mNumVertices+= (i<len-1 ? ( num%2==1 ? num+2 : num+1 ) : num);
230
       }
231

  
232
     // allocate new attrib array
233
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
234
     num = origVertices;
235

  
236
     if( origVertices>0 )
237
       {
238
       System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*num);
239
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
240
       origVertices++;
241

  
242
       if( num%2==1 )
243
         {
244
         System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
245
         origVertices++;
246
         }
247
       }
248

  
249
     for(int i=0; i<len; i++)
250
       {
251
       mesh = meshes[i];
252
       num = mesh.mNumVertices;
253

  
254
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
255
       origVertices++;
256
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*num);
257
       origVertices+=num;
258

  
259
       if( i<len-1 )
260
         {
261
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
262
         origVertices++;
263

  
264
         if( num%2==1 )
265
           {
266
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
267
           origVertices++;
268
           }
269
         }
270
       }
271

  
272
     if( origVertices!=mNumVertices )
273
       {
274
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
275
       }
276

  
277
     mVertAttribs = newAttribs;
278

  
279
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
280
     attribs.put(mVertAttribs).position(0);
281

  
282
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
283
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
257 284
     }
258 285

  
259 286
///////////////////////////////////////////////////////////////////////////////////////////////////
260 287
/**
261
 * Returns the y-factor set by setStretch().
288
 * Not part of public API, do not document (public only because has to be used from the main package)
289
 *
290
 * @y.exclude
262 291
 */
263
   public float getStretchY()
292
   public int getTFO()
264 293
     {
265
     return mStretchY;
294
     return mTFO.mIndex[0];
266 295
     }
267 296

  
268 297
///////////////////////////////////////////////////////////////////////////////////////////////////
269 298
/**
270
 * Returns the z-factor set by setStretch().
299
 * Not part of public API, do not document (public only because has to be used from the main package)
300
 *
301
 * @y.exclude
271 302
 */
272
   public float getStretchZ()
303
   public int getNumVertices()
273 304
     {
274
     return mStretchZ;
305
     return mNumVertices;
275 306
     }
276 307

  
277 308
///////////////////////////////////////////////////////////////////////////////////////////////////
......
378 409
     {
379 410
     float[][] matrix = new float[effects.length][16];
380 411
     float[] tmp;
381
     float[] array = new float[4];
412
     float[] array = new float[7];
382 413
     float x,y,z;
383 414
     int numEffects = 0;
384 415

  
......
434 465

  
435 466
///////////////////////////////////////////////////////////////////////////////////////////////////
436 467
/**
437
 * Join a list of Meshes into this one.
468
 * Sets texture maps for all components of this mesh.
438 469
 * <p>
439
 * Please note that calling this once with the complete list of Meshes will be much faster than
440
 * calling it repeatedly with one Mesh at a time, as we have to reallocate the array of vertices
470
 * Please note that calling this once with the complete list of Maps will be much faster than
471
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
441 472
 * each time.
473
 * 'maps' needs to be maps[NumComponentsInThisMesh][4]. [0] is the lower-left corner's X, [1]- its Y,
474
 * [2] - width, [3] - height of the map.
475
 * For example map[0] = new float { 0.0, 0.5, 0.5, 0.5 } sets the 0th component texture map to the
476
 * upper-left quadrant of the texture.
442 477
 */
443
   public void join(MeshBase[] meshes)
478
   public void setTextureMap(float[][] maps)
444 479
     {
445
     MeshBase mesh;
446
     Component comp;
447
     int com, num, len = meshes.length;
448
     int origVertices = mNumVertices;
449

  
450
     // compute new numVertices; take care of Components
451
     mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
452
     com = mComponent.size();
453
     mComponent.get(com-1).mEndIndex = mNumVertices;
454

  
455
     for(int i=0; i<len; i++)
456
       {
457
       mesh = meshes[i];
458
       com = mesh.mComponent.size();
459

  
460
       for(int j=0; j<com; j++)
461
         {
462
         comp = new Component(mesh.mComponent.get(j));
463
         comp.mEndIndex += mNumVertices;
464
         mComponent.add(comp);
465
         }
466

  
467
       num = mesh.mNumVertices;
468
       mNumVertices+= (i<len-1 ? ( num%2==1 ? num+2 : num+1 ) : num);
469
       }
470

  
471
     // allocate new attrib array
472
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
473
     num = origVertices;
480
     int num_comp = mComponent.size();
481
     int num_maps = maps.length;
482
     int min = num_comp<num_maps ? num_comp : num_maps;
483
     int lastEnd = 0;
474 484

  
475
     System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*num);
476
     System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
477
     origVertices++;
478

  
479
     if( num%2==1 )
480
       {
481
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
482
       origVertices++;
483
       }
484

  
485
     for(int i=0; i<len; i++)
485
     for(int i=0; i<min; i++)
486 486
       {
487
       mesh = meshes[i];
488
       num = mesh.mNumVertices;
489

  
490
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
491
       origVertices++;
492
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*num);
493
       origVertices+=num;
494

  
495
       if( i<len-1 )
487
       if( maps[i]!=null )
496 488
         {
497
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
498
         origVertices++;
499

  
500
         if( num%2==1 )
501
           {
502
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
503
           origVertices++;
504
           }
489
         Component comp = mComponent.get(i);
490
         changeTextureMap(maps[i],comp.mTextureMap,lastEnd,comp.mEndIndex);
491
         comp.setMap(maps[i]);
492
         lastEnd = comp.mEndIndex;
505 493
         }
506 494
       }
507 495

  
508
     if( origVertices!=mNumVertices )
509
       {
510
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
511
       }
512

  
513
     mVertAttribs = newAttribs;
514

  
515 496
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
516 497
     attribs.put(mVertAttribs).position(0);
517 498

  
......
521 502

  
522 503
///////////////////////////////////////////////////////////////////////////////////////////////////
523 504
/**
524
 * Sets texture maps for all components of this mesh.
505
 * Each mesh has its 'bounding box' - return half of its X-length.
525 506
 * <p>
526
 * Please note that calling this once with the complete list of Maps will be much faster than
527
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
528
 * each time.
507
 * In case of all 'simple' Meshes, the bounding box is always 1x1x1 (Sphere, Cubes) or 1x1x0
508
 * (Rectangles, Triangles, Quad - i.e. all 'flat' Meshes). But this can be something else in case of
509
 * MeshComponent.
529 510
 */
530
   public void setTextureMap(float[][] maps)
511
   public float getBoundingX()
512
    {
513
    return mBoundingX*mStretchX;
514
    }
515

  
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Each mesh has its 'bounding box' - return half of its Y-length.
519
 */
520
   public float getBoundingY()
521
    {
522
    return mBoundingY*mStretchY;
523
    }
524

  
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
/**
527
 * Each mesh has its 'bounding box' - return half of its Z-length.
528
 */
529
   public float getBoundingZ()
530
    {
531
    return mBoundingZ*mStretchZ;
532
    }
533

  
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
/**
536
 * Sometimes we want to display a Mesh on a rectangular screen. Then we need to stretch it by
537
 * different factors in x and y (or z) directions. If we also wanted do display some vertex effects
538
 * done on this mesh, let's say a bulge done by a Distort effect, and wanted the bulge to be round,
539
 * (i.e the same in x and y directions) then doing so without this method would be impossible.
540
 *
541
 * This sets 'stretch' factors in each 3 dimensions. All vertices of this Mesh will be premultiplied
542
 * by those factors in the very first line of the Vertex Shader, before any Effects are done on it.
543
 * Using this we can thus pre-stretch the mesh to aspect ratio equal to the surface we eventually
544
 * want to display the Mesh on, and this way we can achieve a round Distort bulge!
545
 *
546
 * This could also be used to pre-stretch a Rectangles Mesh to a size equal (in pixels) to the bitmap
547
 * this mesh is textured with - and this lets us work with all Effects in natural, pixel units.
548
 *
549
 * @param sx stretch factor in x.
550
 * @param sy stretch factor in y.
551
 * @param sz stretch factor in z.
552
 */
553
   public void setStretch(float sx, float sy, float sz)
531 554
     {
532
     int components = mComponent.size();
555
     mStretchX = sx;
556
     mStretchY = sy;
557
     mStretchZ = sz;
558
     }
533 559

  
534
     for(int comp=0; comp<components; comp++)
535
       {
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561
/**
562
 * Returns the x-factor set by setStretch().
563
 */
564
   public float getStretchX()
565
     {
566
     return mStretchX;
567
     }
536 568

  
537
       }
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570
/**
571
 * Returns the y-factor set by setStretch().
572
 */
573
   public float getStretchY()
574
     {
575
     return mStretchY;
576
     }
577

  
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579
/**
580
 * Returns the z-factor set by setStretch().
581
 */
582
   public float getStretchZ()
583
     {
584
     return mStretchZ;
538 585
     }
539 586
   }
540 587

  
src/main/java/org/distorted/library/mesh/MeshJoined.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.mesh;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
public class MeshJoined extends MeshBase
25
  {
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Join a list of (probably already changed by Matrix Effects) Meshes into one.
29
 * <p>
30
 * Try to keep the origin (0,0,0) in the center of gravity of the whole thing.
31
 */
32
  public MeshJoined(MeshBase[] meshes)
33
    {
34
    super(1,1,1);
35
    join(meshes);
36
    }
37

  
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
/**
40
 * Sets the lengths of the bounding box.
41
 * <p>
42
 * The 'bounding box' is an imaginary rectanguloid of size (bx,by,bz) inside of which this Mesh
43
 * fits tightly.
44
 * 'Normal' 3D meshes (e.g. Sphere) have this equal to 1,1,1. 'Normal' flat meshes (e.g. Quad) -
45
 * to 1,1,0. Here however we are joining several, probably already changed by MatrixEffects,
46
 * Meshes together. If the result fits into the standard 1,1,1 (recommended!) - we don't need to
47
 * call this function at all.
48
 *
49
 * @param bx x-length of the bounding box
50
 * @param by y-length of the bounding box
51
 * @param bz z-length of the bounding box
52
 */
53
   public void setBounding(float bx, float by, float bz)
54
     {
55
     super.setBounding(bx,by,bz);
56
     }
57

  
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
/**
60
 * Return how many basic Meshes is this Mesh joined from.
61
 */
62
   public int getNumComponents()
63
     {
64
     return numComponents();
65
     }
66
  }

Also available in: Unified diff