Project

General

Profile

Download (49.2 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 3273dce2

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.mesh;
22

    
23
import android.opengl.GLES30;
24

    
25
import org.distorted.library.effect.MatrixEffect;
26
import org.distorted.library.effect.VertexEffect;
27
import org.distorted.library.effectqueue.EffectQueue;
28
import org.distorted.library.main.DistortedLibrary;
29
import org.distorted.library.main.InternalBuffer;
30
import org.distorted.library.program.DistortedProgram;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.library.uniformblock.UniformBlockAssociation;
33
import org.distorted.library.uniformblock.UniformBlockCenter;
34

    
35
import java.io.DataOutputStream;
36
import java.io.IOException;
37
import java.io.DataInputStream;
38
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41
import java.util.ArrayList;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
/**
45
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
46
 * <p>
47
 * If you want to render to a particular shape, extend from here, construct a float array
48
 * containing per-vertex attributes, and call back setAttribs().
49
 */
50
public abstract class MeshBase
51
   {
52
   private static final int ASSOC_UBO_BINDING  = 3;
53
   private static final int CENTER_UBO_BINDING = 4;
54

    
55
   // sizes of attributes of an individual vertex.
56
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
57
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
58
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
59
   private static final int COM_DATA_SIZE= 1; // component number, a single float
60

    
61
   static final int POS_ATTRIB   = 0;
62
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
63
   static final int TEX_ATTRIB   = 0;
64
   static final int COM_ATTRIB   = TEX_DATA_SIZE;
65

    
66
   static final int VERT1_ATTRIBS= POS_DATA_SIZE + NOR_DATA_SIZE;  // number of attributes of a vertex (the part changed by preapply)
67
   static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE;  // number of attributes of a vertex (the 'preapply invariant' part)
68
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE;  // number of attributes of a transform feedback vertex
69

    
70
   private static final int BYTES_PER_FLOAT = 4;
71

    
72
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
73
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
74
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
75
   private static final int OFFSET_COM = COM_ATTRIB*BYTES_PER_FLOAT;
76

    
77
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
78
   private static final int VERT1_SIZE = VERT1_ATTRIBS*BYTES_PER_FLOAT;
79
   private static final int VERT2_SIZE = VERT2_ATTRIBS*BYTES_PER_FLOAT;
80

    
81
   private static final int[] mCenterBlockIndex = new int[EffectQueue.MAIN_VARIANTS];
82
   private static final int[] mAssocBlockIndex  = new int[EffectQueue.MAIN_VARIANTS];
83

    
84
   private static final int TEX_COMP_SIZE = 5; // 5 four-byte entities inside the component
85

    
86
   private static boolean mUseCenters;
87
   private static int mStride;
88
   private static int mMaxComponents = 100;
89

    
90
   private boolean mShowNormals;              // when rendering this mesh, draw normal vectors?
91
   private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
92
   private int mNumVertices;
93
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ
94
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
95
   private float mInflate;
96
   private final UniformBlockAssociation mUBA;
97
   private UniformBlockCenter mUBC;
98
   private boolean mStrideCorrected;
99

    
100
   DeferredJobs.JobNode[] mJobNode;
101

    
102
   private static class TexComponent
103
     {
104
     private int mEndIndex;
105
     private final Static4D mTextureMap;
106

    
107
     TexComponent(int end)
108
       {
109
       mEndIndex  = end;
110
       mTextureMap= new Static4D(0,0,1,1);
111
       }
112
     TexComponent(TexComponent original)
113
       {
114
       mEndIndex = original.mEndIndex;
115

    
116
       float x = original.mTextureMap.get0();
117
       float y = original.mTextureMap.get1();
118
       float z = original.mTextureMap.get2();
119
       float w = original.mTextureMap.get3();
120
       mTextureMap = new Static4D(x,y,z,w);
121
       }
122

    
123
     void setMap(Static4D map)
124
       {
125
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
126
       }
127
     }
128

    
129
   private ArrayList<TexComponent> mTexComponent;
130
   private ArrayList<Integer> mEffComponent;
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
   MeshBase()
135
     {
136
     mShowNormals  = false;
137
     mInflate      = 0.0f;
138
     mTexComponent = new ArrayList<>();
139
     mEffComponent = new ArrayList<>();
140

    
141
     mJobNode = new DeferredJobs.JobNode[1];
142

    
143
     mUBA = new UniformBlockAssociation();
144

    
145
     if( mUseCenters ) mUBC = new UniformBlockCenter();
146

    
147
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_DRAW);
148
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_DRAW);
149
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
150
     }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// copy constructor
154

    
155
   MeshBase(MeshBase original, boolean deep)
156
     {
157
     mShowNormals= original.mShowNormals;
158
     mInflate    = original.mInflate;
159
     mNumVertices= original.mNumVertices;
160

    
161
     mUBA = new UniformBlockAssociation(original.mUBA);
162

    
163
     if( mUseCenters ) mUBC = new UniformBlockCenter(original.mUBC);
164

    
165
     if( deep )
166
       {
167
       mJobNode = new DeferredJobs.JobNode[1];
168
       if( original.mJobNode[0]==null ) copy(original);
169
       else mJobNode[0] = DeferredJobs.copy(this,original);
170
       }
171
     else
172
       {
173
       mJobNode      = original.mJobNode;
174
       mVBO1         = original.mVBO1;
175
       mVertAttribs1 = original.mVertAttribs1;
176
       shallowCopy(original);
177
       }
178

    
179
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
180
     }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
   void copy(MeshBase original)
185
     {
186
     shallowCopy(original);
187

    
188
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_DRAW);
189
     mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
190
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
191
     }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
   private void shallowCopy(MeshBase original)
196
     {
197
     int texComSize = original.mTexComponent.size();
198
     mTexComponent = new ArrayList<>();
199

    
200
     for(int i=0; i<texComSize; i++)
201
       {
202
       TexComponent comp = new TexComponent(original.mTexComponent.get(i));
203
       mTexComponent.add(comp);
204
       }
205

    
206
     mEffComponent = new ArrayList<>();
207
     mEffComponent.addAll(original.mEffComponent);
208

    
209
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_DRAW);
210
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
211
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
212
     }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
   void mergeTexComponentsNow()
217
     {
218
     int num = mTexComponent.size();
219

    
220
     if( num>1 )
221
       {
222
       mTexComponent.clear();
223
       mTexComponent.add(new TexComponent(mNumVertices-1));
224

    
225
       mVBO2.invalidate();
226
       }
227
     }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
   void addEmptyTexComponentNow()
232
     {
233
     mTexComponent.add(new TexComponent(mNumVertices-1));
234
     }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
   void mergeEffComponentsNow()
239
     {
240
     int num = mEffComponent.size();
241

    
242
     if( num>1 )
243
       {
244
       mEffComponent.clear();
245
       mEffComponent.add(mNumVertices-1);
246

    
247
       for(int index=0; index<mNumVertices; index++)
248
         {
249
         mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0;
250
         }
251

    
252
       mVBO2.invalidate();
253
       }
254
     }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
   void applyMatrix(MatrixEffect effect, int andAssoc, int equAssoc)
259
     {
260
     float[] matrixP  = new float[16];
261
     float[] matrixV  = new float[16];
262
     float[] uniforms = new float[7];
263
     int start, end=-1, numComp = mEffComponent.size();
264

    
265
     matrixP[0] = matrixP[5] = matrixP[10] = matrixP[15] = 1;
266
     matrixV[0] = matrixV[5] = matrixV[10] = matrixV[15] = 1;
267

    
268
     effect.compute(uniforms,0,0,0);
269
     effect.apply(matrixP, matrixV, uniforms, 0);
270

    
271
     for(int comp=0; comp<numComp; comp++)
272
       {
273
       start = end+1;
274
       end   = mEffComponent.get(comp);
275

    
276
       if( mUBA.matchesAssociation(comp, andAssoc, equAssoc) )
277
         {
278
         applyMatrixToComponent(matrixP, matrixV, start, end);
279
         }
280
       }
281

    
282
     mVBO1.invalidate();
283
     }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
   private void applyMatrixToComponent(float[] matrixP, float[] matrixV, int start, int end)
288
     {
289
     float x,y,z;
290

    
291
     for(int index=start*VERT1_ATTRIBS; index<=end*VERT1_ATTRIBS; index+=VERT1_ATTRIBS )
292
       {
293
       x = mVertAttribs1[index+POS_ATTRIB  ];
294
       y = mVertAttribs1[index+POS_ATTRIB+1];
295
       z = mVertAttribs1[index+POS_ATTRIB+2];
296

    
297
       mVertAttribs1[index+POS_ATTRIB  ] = matrixP[0]*x + matrixP[4]*y + matrixP[ 8]*z + matrixP[12];
298
       mVertAttribs1[index+POS_ATTRIB+1] = matrixP[1]*x + matrixP[5]*y + matrixP[ 9]*z + matrixP[13];
299
       mVertAttribs1[index+POS_ATTRIB+2] = matrixP[2]*x + matrixP[6]*y + matrixP[10]*z + matrixP[14];
300

    
301
       x = mVertAttribs1[index+NOR_ATTRIB  ];
302
       y = mVertAttribs1[index+NOR_ATTRIB+1];
303
       z = mVertAttribs1[index+NOR_ATTRIB+2];
304

    
305
       mVertAttribs1[index+NOR_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
306
       mVertAttribs1[index+NOR_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
307
       mVertAttribs1[index+NOR_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
308

    
309
       x = mVertAttribs1[index+NOR_ATTRIB  ];
310
       y = mVertAttribs1[index+NOR_ATTRIB+1];
311
       z = mVertAttribs1[index+NOR_ATTRIB+2];
312

    
313
       float len1 = (float)Math.sqrt(x*x + y*y + z*z);
314

    
315
       if( len1>0.0f )
316
         {
317
         mVertAttribs1[index+NOR_ATTRIB  ] /= len1;
318
         mVertAttribs1[index+NOR_ATTRIB+1] /= len1;
319
         mVertAttribs1[index+NOR_ATTRIB+2] /= len1;
320
         }
321
       }
322
     }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
327
     {
328
     mUBA.setEffectAssociationNow(component, andAssociation, equAssociation);
329
     }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
   void setComponentCenterNow(int component, float x, float y, float z)
334
     {
335
     if( mUBC!=null )
336
       {
337
       mUBC.setEffectCenterNow(component, x, y, z);
338
       }
339
     }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
// when a derived class is done computing its mesh, it has to call this method.
343

    
344
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
345
     {
346
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
347
     mVertAttribs1= vert1Attribs;
348
     mVertAttribs2= vert2Attribs;
349

    
350
     mTexComponent.add(new TexComponent(mNumVertices-1));
351
     mEffComponent.add(mNumVertices-1);
352

    
353
     mVBO1.invalidate();
354
     mVBO2.invalidate();
355
     }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
   void joinAttribs(MeshBase[] meshes)
360
     {
361
     MeshBase mesh;
362
     TexComponent comp;
363
     int numMeshes = meshes.length;
364
     int numVertices,origVertices = mNumVertices;
365
     int origTexComponents,numTexComponents;
366
     int origEffComponents=0,numEffComponents;
367

    
368
     if( origVertices>0 )
369
       {
370
       origTexComponents = mTexComponent.size();
371
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
372
       mTexComponent.get(origTexComponents-1).mEndIndex = mNumVertices-1;
373
       origEffComponents = mEffComponent.size();
374
       mEffComponent.set(origEffComponents-1,mNumVertices-1);
375
       }
376

    
377
     for(int i=0; i<numMeshes; i++)
378
       {
379
       mesh = meshes[i];
380
       numTexComponents = mesh.mTexComponent.size();
381
       numEffComponents = mesh.mEffComponent.size();
382
       numVertices = mesh.mNumVertices;
383

    
384
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
385
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
386

    
387
       for(int j=0; j<numTexComponents; j++)
388
         {
389
         comp = new TexComponent(mesh.mTexComponent.get(j));
390
         comp.mEndIndex += (extraVerticesBefore+mNumVertices);
391
         if( j==numTexComponents-1) comp.mEndIndex += extraVerticesAfter;
392
         mTexComponent.add(comp);
393
         }
394

    
395
       for(int j=0; j<numEffComponents; j++)
396
         {
397
         int index = mesh.mEffComponent.get(j);
398
         index += (extraVerticesBefore+mNumVertices);
399
         if( j==numEffComponents-1 ) index += extraVerticesAfter;
400
         mEffComponent.add(index);
401

    
402
         if( origEffComponents<mMaxComponents )
403
           {
404
           mUBA.copy(origEffComponents, mesh.mUBA, j);
405
           if( mUseCenters ) mUBC.copy(origEffComponents, mesh.mUBC, j);
406
           origEffComponents++;
407
           }
408
         }
409

    
410
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
411
       }
412

    
413
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
414
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
415
     numVertices = origVertices;
416

    
417
     if( origVertices>0 )
418
       {
419
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                          0, VERT1_ATTRIBS*numVertices);
420
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS            );
421
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                          0, VERT2_ATTRIBS*numVertices);
422
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS            );
423
       origVertices++;
424

    
425
       if( numVertices%2==1 )
426
         {
427
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
428
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
429
         origVertices++;
430
         }
431
       }
432

    
433
     for(int i=0; i<numMeshes; i++)
434
       {
435
       mesh = meshes[i];
436
       numVertices = mesh.mNumVertices;
437

    
438
       if( origVertices>0 )
439
         {
440
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
441
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
442
         origVertices++;
443
         }
444
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
445
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
446
       origVertices+=numVertices;
447

    
448
       if( i<numMeshes-1 )
449
         {
450
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
451
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
452
         origVertices++;
453

    
454
         if( numVertices%2==1 )
455
           {
456
           System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
457
           System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
458
           origVertices++;
459
           }
460
         }
461
       }
462

    
463
     if( origVertices!=mNumVertices )
464
       {
465
       DistortedLibrary.logMessage("MeshBase: join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
466
       }
467

    
468
     int endIndex, index=0, numEffComp = mEffComponent.size();
469

    
470
     for(int component=0; component<numEffComp; component++)
471
       {
472
       endIndex = mEffComponent.get(component);
473

    
474
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
475
       }
476

    
477
     mVertAttribs1 = newAttribs1;
478
     mVertAttribs2 = newAttribs2;
479
     mVBO1.invalidate();
480
     mVBO2.invalidate();
481
     }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
// called from MeshJoined
485

    
486
   void join(MeshBase[] meshes)
487
     {
488
     boolean immediateJoin = true;
489

    
490
     for (MeshBase mesh : meshes)
491
       {
492
       if (mesh.mJobNode[0] != null)
493
         {
494
         immediateJoin = false;
495
         break;
496
         }
497
       }
498

    
499
     if( immediateJoin ) joinAttribs(meshes);
500
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
501
     }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
   void textureMap(Static4D[] maps, int startComponent)
506
     {
507
     int num_comp = mTexComponent.size();
508
     if( startComponent>num_comp ) return;
509

    
510
     int num_maps = maps.length;
511
     int min = Math.min(num_comp-startComponent, num_maps);
512
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
513
     Static4D newMap, oldMap;
514
     TexComponent comp;
515
     float newW, newH, ratW, ratH, movX, movY;
516

    
517
     for(int i=0; i<min; i++)
518
       {
519
       newMap = maps[i];
520
       comp = mTexComponent.get(i+startComponent);
521

    
522
       if( newMap!=null )
523
         {
524
         newW = newMap.get2();
525
         newH = newMap.get3();
526

    
527
         if( newW!=0.0f && newH!=0.0f )
528
           {
529
           oldMap = comp.mTextureMap;
530
           ratW = newW/oldMap.get2();
531
           ratH = newH/oldMap.get3();
532
           movX = newMap.get0() - ratW*oldMap.get0();
533
           movY = newMap.get1() - ratH*oldMap.get1();
534

    
535
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
536
             {
537
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
538
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
539
             }
540
           comp.setMap(newMap);
541
           }
542
         }
543

    
544
       vertex= comp.mEndIndex+1;
545
       }
546

    
547
     mVBO2.invalidate();
548
     }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551
/**
552
 * Are we going to need per-component centers (needed for correct postprocessing of concave meshes)
553
 * Switching this on allocates 16*MAX_EFFECT_COMPONENTS bytes for uniforms in the vertex shader.
554
 */
555
   public static void setUseCenters()
556
     {
557
     mUseCenters = true;
558
     }
559

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561
/**
562
 * Are we using per-component centers?
563
 */
564
   public static boolean getUseCenters()
565
     {
566
     return mUseCenters;
567
     }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

    
571
   public static int getMaxEffComponents()
572
     {
573
     return mMaxComponents;
574
     }
575

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577
/**
578
 * How many mesh components are we going to need? Call before compilation of the shaders.
579
 */
580
   public static void setMaxEffComponents(int max)
581
     {
582
     mMaxComponents = max;
583
     }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
   public static void getUniforms(int programH, int variant)
588
     {
589
     mCenterBlockIndex[variant]= GLES30.glGetUniformBlockIndex(programH, "componentCenter");
590
     mAssocBlockIndex[variant] = GLES30.glGetUniformBlockIndex(programH, "componentAssociation");
591

    
592
     if( mStride==0 )
593
       {
594
       String[] uniformNames = { "vComAssoc" };
595
       int[] indices = new int[1];
596
       int[] params  = new int[1];
597

    
598
       GLES30.glGetUniformIndices(programH, uniformNames, indices, 0);
599
       GLES30.glGetActiveUniformsiv( programH, 1, indices, 0, GLES30.GL_UNIFORM_ARRAY_STRIDE, params,0 );
600

    
601
       mStride = params[0]/4;
602
       }
603
     }
604

    
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606
/**
607
 * Not part of public API, do not document (public only because has to be used from the main package)
608
 *
609
 * @y.exclude
610
 */
611
   public void copyTransformToVertex()
612
     {
613
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
614
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
615
     if( buffer!=null )
616
       {
617
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
618
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
619
       mVBO1.updateFloat(mVertAttribs1);
620
       }
621
     else
622
       {
623
       int error = GLES30.glGetError();
624
       DistortedLibrary.logMessage("MeshBase: failed to map tf buffer, error="+error);
625
       }
626

    
627
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
628
     }
629

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631
/**
632
 * Not part of public API, do not document (public only because has to be used from the main package)
633
 *
634
 * @y.exclude
635
 */
636
   public void debugJobs()
637
     {
638
     if( mJobNode[0]!=null )
639
       {
640
       mJobNode[0].print(0);
641
       }
642
     else
643
       {
644
       DistortedLibrary.logMessage("MeshBase: JobNode null");
645
       }
646
     }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
/**
650
 * Not part of public API, do not document (public only because has to be used from the main package)
651
 *
652
 * @y.exclude
653
 */
654
   public void printTexComponent(int comp)
655
     {
656
     if( comp>=0 && comp<getNumTexComponents() )
657
       {
658
       int beg = comp>0 ? mTexComponent.get(comp-1).mEndIndex+1 : 0;
659
       int end = mTexComponent.get(comp).mEndIndex;
660
       StringBuilder sb = new StringBuilder();
661

    
662
       for( int vert=beg; vert<=end; vert++)
663
         {
664
         sb.append('(');
665
         sb.append(mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ]);
666
         sb.append(',');
667
         sb.append(mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1]);
668
         sb.append(',');
669
         sb.append(mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2]);
670
         sb.append(") ");
671
         }
672

    
673
       DistortedLibrary.logMessage("MeshBase: "+sb);
674
       }
675
     }
676

    
677
///////////////////////////////////////////////////////////////////////////////////////////////////
678
/**
679
 * Not part of public API, do not document (public only because has to be used from the main package)
680
 *
681
 * @y.exclude
682
 */
683
   public void printPos()
684
     {
685
     StringBuilder sb = new StringBuilder();
686

    
687
     for(int i=0; i<mNumVertices; i++)
688
       {
689
       sb.append('(');
690
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
691
       sb.append(',');
692
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
693
       sb.append(',');
694
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
695
       sb.append(")\n");
696
       }
697
     DistortedLibrary.logMessage("MeshBase: vertices: \n"+sb);
698
     }
699

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701
   /**
702
    * Not part of public API, do not document (public only because has to be used from the main package)
703
    *
704
    * @y.exclude
705
    */
706
   public void printNor()
707
     {
708
     StringBuilder sb = new StringBuilder();
709

    
710
     for(int i=0; i<mNumVertices; i++)
711
       {
712
       sb.append('(');
713
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+NOR_ATTRIB  ]);
714
       sb.append(',');
715
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+NOR_ATTRIB+1]);
716
       sb.append(',');
717
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+NOR_ATTRIB+2]);
718
       sb.append(")\n");
719
       }
720
     DistortedLibrary.logMessage("MeshBase: normals:\n"+sb);
721
     }
722

    
723
///////////////////////////////////////////////////////////////////////////////////////////////////
724
/**
725
 * Not part of public API, do not document (public only because has to be used from the main package)
726
 *
727
 * @y.exclude
728
 */
729
   public void printCom()
730
     {
731
     StringBuilder sb = new StringBuilder();
732

    
733
     for(int i=0; i<mNumVertices; i++)
734
       {
735
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
736
       sb.append(' ');
737
       }
738

    
739
     DistortedLibrary.logMessage("MeshBase: "+sb);
740
     }
741

    
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743
/**
744
 * Not part of public API, do not document (public only because has to be used from the main package)
745
 *
746
 * @y.exclude
747
 */
748
   public void printTex()
749
     {
750
     int vert=0;
751
     int num = getNumTexComponents();
752
     StringBuilder sb = new StringBuilder();
753
     sb.append("tex components: ").append(num);
754

    
755
     for(int i=0; i<num; i++)
756
       {
757
       int end = mTexComponent.get(i).mEndIndex;
758
       sb.append("\n");
759

    
760
       for( ; vert<=end; vert++)
761
         {
762
         sb.append(' ');
763
         sb.append('(');
764
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
765
         sb.append(',');
766
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
767
         sb.append(')');
768
         }
769
       }
770

    
771
     DistortedLibrary.logMessage("MeshBase: "+sb);
772
     }
773

    
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775
/**
776
 * Not part of public API, do not document (public only because has to be used from the main package)
777
 *
778
 * @y.exclude
779
 */
780
   public int getTFO()
781
     {
782
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
783
     }
784

    
785
///////////////////////////////////////////////////////////////////////////////////////////////////
786
/**
787
 * Not part of public API, do not document (public only because has to be used from the main package)
788
 *
789
 * @y.exclude
790
 */
791
   public int getNumVertices()
792
     {
793
     return mNumVertices;
794
     }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797
/**
798
 * Not part of public API, do not document (public only because has to be used from the main package)
799
 *
800
 * @y.exclude
801
 */
802
   public void send(int programH, int variant)
803
     {
804
     if( !mStrideCorrected )
805
       {
806
       mStrideCorrected = true;
807
       mUBA.correctStride(mStride);
808
       }
809

    
810
     int indexA = mUBA.getIndex();
811
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
812
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
813

    
814
     if( mUseCenters )
815
       {
816
       int indexC = mUBC.getIndex();
817
       GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
818
       GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
819
       }
820
     }
821

    
822
///////////////////////////////////////////////////////////////////////////////////////////////////
823
/**
824
 * Not part of public API, do not document (public only because has to be used from the main package)
825
 *
826
 * @y.exclude
827
 */
828
   public void bindVertexAttribs(DistortedProgram program)
829
     {
830
     if( mJobNode[0]!=null )
831
       {
832
       mJobNode[0].execute();  // this will set itself to null
833
       }
834

    
835
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
836
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
837
     int[] attr = program.mAttribute;
838

    
839
     switch( attr.length )
840
       {
841
       // 'normal' case
842
       case 4: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
843
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
844
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
845
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
846
               GLES30.glVertexAttribPointer(attr[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
847
               GLES30.glVertexAttribPointer(attr[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
848
               break;
849
       // if we are not using component centers and there are no vertex effects enabled,
850
       // then component attribute does not exist in the Vertex Shader
851
       case 3: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
852
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
853
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
854
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
855
               GLES30.glVertexAttribPointer(attr[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
856
               break;
857
       // pre program used in EffectQueuePostprocessing does not have v_Normal attribute
858
       // (it is not used in the fragment shader)
859
       case 2: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
860
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
861
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
862
               GLES30.glVertexAttribPointer(attr[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
863
               break;
864
       }
865

    
866
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
867
     }
868

    
869
///////////////////////////////////////////////////////////////////////////////////////////////////
870
/**
871
 * Not part of public API, do not document (public only because has to be used from the main package)
872
 *
873
 * @y.exclude
874
 */
875
   public void bindTransformAttribs(DistortedProgram program)
876
     {
877
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
878

    
879
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
880
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
881
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
882
     }
883

    
884
///////////////////////////////////////////////////////////////////////////////////////////////////
885
/**
886
 * Not part of public API, do not document (public only because has to be used from the main package)
887
 *
888
 * @y.exclude
889
 */
890
   public void setInflate(float inflate)
891
     {
892
     mInflate = inflate;
893
     }
894

    
895
///////////////////////////////////////////////////////////////////////////////////////////////////
896
/**
897
 * Not part of public API, do not document (public only because has to be used from the main package)
898
 *
899
 * @y.exclude
900
 */
901
   public float getInflate()
902
     {
903
     return mInflate;
904
     }
905

    
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907
// Return the number of bytes read.
908

    
909
   int read(DataInputStream stream)
910
     {
911
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
912
     int version, numEff, numTex;
913

    
914
     //////////////////////////////////////////////////////////////////////////////////////////
915
     // read version, number of vertices, number of tex components, number of effect components
916
     //////////////////////////////////////////////////////////////////////////////////////////
917

    
918
     try
919
       {
920
       stream.read(buffer);
921
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
922

    
923
       version = byteBuf.getInt(0);
924

    
925
       if( version==1 || version==2 )
926
         {
927
         mNumVertices= byteBuf.getInt(4);
928
         numTex      = byteBuf.getInt(8);
929
         numEff      = byteBuf.getInt(12);
930
         }
931
       else
932
         {
933
         DistortedLibrary.logMessage("MeshBase: Error: unknown mesh file version "+String.format("0x%08X", version));
934
         return 0;
935
         }
936
       }
937
     catch(Exception e)
938
       {
939
       DistortedLibrary.logMessage("MeshBase: Exception1 trying to read file: "+e.toString());
940
       return 0;
941
       }
942

    
943
     //////////////////////////////////////////////////////////////////////////////////////////
944
     // read contents of all texture and effect components
945
     //////////////////////////////////////////////////////////////////////////////////////////
946

    
947
     if( mNumVertices>0 && numEff>0 && numTex>0 )
948
       {
949
       int size = numEff+TEX_COMP_SIZE*numTex;
950
       float[] tmp = new float[size];
951
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
952
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
953

    
954
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
955
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
956
       // ... and there was no center.
957
       int centerSize  = version==2 ? 3*numEff : 0;
958

    
959
       buffer = new byte[BYTES_PER_FLOAT*(size + centerSize + mNumVertices*(vert1InFile+VERT2_ATTRIBS))];
960

    
961
       try
962
         {
963
         stream.read(buffer);
964
         }
965
       catch(Exception e)
966
         {
967
         DistortedLibrary.logMessage("MeshBase: Exception2 trying to read file: "+e.toString());
968
         return 0;
969
         }
970

    
971
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
972
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
973

    
974
       floatBuf.get(tmp,0,size);
975

    
976
       TexComponent tex;
977
       int index, texComp;
978
       float x, y, z, w;
979

    
980
       for(texComp=0; texComp<numTex; texComp++)
981
         {
982
         index = (int)tmp[TEX_COMP_SIZE*texComp];
983

    
984
         x= tmp[TEX_COMP_SIZE*texComp+1];
985
         y= tmp[TEX_COMP_SIZE*texComp+2];
986
         z= tmp[TEX_COMP_SIZE*texComp+3];
987
         w= tmp[TEX_COMP_SIZE*texComp+4];
988

    
989
         tex = new TexComponent(index);
990
         tex.setMap(new Static4D(x,y,z,w));
991

    
992
         mTexComponent.add(tex);
993
         }
994

    
995
       for(int effComp=0; effComp<numEff; effComp++)
996
         {
997
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
998
         mEffComponent.add(index);
999
         }
1000

    
1001
       //////////////////////////////////////////////////////////////////////////////////////////
1002
       // read vert1 array, vert2 array and (if version>1) the component centers.
1003
       //////////////////////////////////////////////////////////////////////////////////////////
1004

    
1005
       switch(version)
1006
         {
1007
         case 1 : index = floatBuf.position();
1008

    
1009
                  for(int vert=0; vert<mNumVertices; vert++)
1010
                    {
1011
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
1012
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
1013
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
1014
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
1015
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
1016
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
1017
                    index+=vert1InFile;
1018
                    }
1019
                  floatBuf.position(index);
1020
                  break;
1021
         case 2 : float[] centers = new float[3*numEff];
1022
                  floatBuf.get(centers,0,3*numEff);
1023

    
1024
                  if( mUseCenters )
1025
                    {
1026
                    for(int eff=0; eff<numEff; eff++)
1027
                      {
1028
                      mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
1029
                      }
1030
                    }
1031

    
1032
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
1033
                  break;
1034
         default: DistortedLibrary.logMessage("MeshBase: Error: unknown mesh file version "+String.format("0x%08X", version));
1035
                  return 0;
1036
         }
1037

    
1038
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
1039
       }
1040

    
1041
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
1042
     }
1043

    
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045
/**
1046
 * @y.exclude
1047
 */
1048
   public int getLastVertexEff(int effComponent)
1049
     {
1050
     if( effComponent>=0 && effComponent<mTexComponent.size() )
1051
       {
1052
       return mEffComponent.get(effComponent);
1053
       }
1054

    
1055
     return 0;
1056
     }
1057

    
1058
///////////////////////////////////////////////////////////////////////////////////////////////////
1059
/**
1060
 * @y.exclude
1061
 */
1062
   public int getLastVertexTex(int texComponent)
1063
     {
1064
     if( texComponent>=0 && texComponent<mTexComponent.size() )
1065
       {
1066
       return mTexComponent.get(texComponent).mEndIndex;
1067
       }
1068

    
1069
     return 0;
1070
     }
1071

    
1072
///////////////////////////////////////////////////////////////////////////////////////////////////
1073
// PUBLIC API
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075
/**
1076
 * Write the Mesh to a File.
1077
 */
1078
   public void write(DataOutputStream stream)
1079
     {
1080
     TexComponent tex;
1081

    
1082
     int numTex = mTexComponent.size();
1083
     int numEff = mEffComponent.size();
1084

    
1085
     try
1086
       {
1087
       stream.writeInt(2);  // version
1088
       stream.writeInt(mNumVertices);
1089
       stream.writeInt(numTex);
1090
       stream.writeInt(numEff);
1091

    
1092
       for(int i=0; i<numTex; i++)
1093
         {
1094
         tex = mTexComponent.get(i);
1095

    
1096
         stream.writeFloat((float)tex.mEndIndex);
1097
         stream.writeFloat(tex.mTextureMap.get0());
1098
         stream.writeFloat(tex.mTextureMap.get1());
1099
         stream.writeFloat(tex.mTextureMap.get2());
1100
         stream.writeFloat(tex.mTextureMap.get3());
1101
         }
1102

    
1103
       for(int i=0; i<numEff; i++)
1104
         {
1105
         stream.writeFloat((float)mEffComponent.get(i));
1106
         }
1107

    
1108
       float[] centers = mUseCenters ? mUBC.getBackingArray() : new float[4*numEff];
1109

    
1110
       for(int i=0; i<numEff; i++)
1111
         {
1112
         stream.writeFloat(centers[4*i  ]);
1113
         stream.writeFloat(centers[4*i+1]);
1114
         stream.writeFloat(centers[4*i+2]);
1115
         }
1116

    
1117
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
1118
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
1119
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
1120
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
1121

    
1122
       stream.write(vertBuf1.array());
1123
       stream.write(vertBuf2.array());
1124
       }
1125
     catch(IOException ex)
1126
       {
1127
       DistortedLibrary.logMessage("MeshBase: IOException trying to write a mesh: "+ex.toString());
1128
       }
1129
     }
1130

    
1131
///////////////////////////////////////////////////////////////////////////////////////////////////
1132
/**
1133
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1134
 * <p>
1135
 * Will work only on OpenGL ES >= 3.0 devices.
1136
 *
1137
 * @param show Controls if we render the Normal vectors or not.
1138
 */
1139
   public void setShowNormals(boolean show)
1140
     {
1141
     mShowNormals = show;
1142
     }
1143

    
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145
/**
1146
 * When rendering this mesh, should we also draw the normal vectors?
1147
 *
1148
 * @return <i>true</i> if we do render normal vectors
1149
 */
1150
   public boolean getShowNormals()
1151
     {
1152
     return mShowNormals;
1153
     }
1154

    
1155
///////////////////////////////////////////////////////////////////////////////////////////////////
1156
/**
1157
 * Merge all texture components of this Mesh into a single one.
1158
 */
1159
   public void mergeTexComponents()
1160
     {
1161
     if( mJobNode[0]==null )
1162
       {
1163
       mergeTexComponentsNow();
1164
       }
1165
     else
1166
       {
1167
       mJobNode[0] = DeferredJobs.mergeTex(this);
1168
       }
1169
     }
1170

    
1171
///////////////////////////////////////////////////////////////////////////////////////////////////
1172
/**
1173
 * Merge all effect components of this Mesh into a single one.
1174
 */
1175
   public void mergeEffComponents()
1176
     {
1177
     if( mJobNode[0]==null )
1178
       {
1179
       mergeEffComponentsNow();
1180
       }
1181
     else
1182
       {
1183
       mJobNode[0] = DeferredJobs.mergeEff(this);
1184
       }
1185
     }
1186

    
1187
///////////////////////////////////////////////////////////////////////////////////////////////////
1188
/**
1189
 * Release all internal resources.
1190
 */
1191
   public void markForDeletion()
1192
     {
1193
     mVertAttribs1 = null;
1194
     mVertAttribs2 = null;
1195

    
1196
     mVBO1.markForDeletion();
1197
     mVBO2.markForDeletion();
1198
     mTFO.markForDeletion();
1199
     mUBA.markForDeletion();
1200

    
1201
     if( mUBC!=null )
1202
       {
1203
       mUBC.markForDeletion();
1204
       }
1205
     }
1206

    
1207
///////////////////////////////////////////////////////////////////////////////////////////////////
1208
/**
1209
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1210
 * <p>
1211
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1212
 * contains any Dynamics, they will be evaluated at 0.
1213
 *
1214
 * @param effect List of Matrix Effects to apply to the Mesh.
1215
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1216
 * @param equAssoc 'equality' association which defines which components will be affected.
1217
 */
1218
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1219
     {
1220
     if( mJobNode[0]==null )
1221
       {
1222
       applyMatrix(effect,andAssoc,equAssoc);
1223
       }
1224
     else
1225
       {
1226
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1227
       }
1228
     }
1229

    
1230
///////////////////////////////////////////////////////////////////////////////////////////////////
1231
/**
1232
 * Apply a Vertex Effect to the vertex mesh.
1233
 * <p>
1234
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1235
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1236
 * We would call this several times building up a list of Effects to do. This list of effects gets
1237
 * lazily executed only when the Mesh is used for rendering for the first time.
1238
 *
1239
 * @param effect Vertex Effect to apply to the Mesh.
1240
 */
1241
   public void apply(VertexEffect effect)
1242
     {
1243
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1244
     }
1245

    
1246
///////////////////////////////////////////////////////////////////////////////////////////////////
1247
/**
1248
 * Sets texture maps for (some of) the components of this mesh.
1249
 * <p>
1250
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1251
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1252
 * upper-left quadrant of the texture.
1253
 * <p>
1254
 * Probably the most common user case would be sending as many maps as there are components in this
1255
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1256
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1257
 * of the 2nd and 4rd one, call this with
1258
 * maps = new Static4D[4]
1259
 * maps[0] = null
1260
 * maps[1] = the map for the 2nd component
1261
 * maps[2] = null
1262
 * maps[3] = the map for the 4th component
1263
 * A map's width and height have to be non-zero (but can be negative!)
1264
 *
1265
 * @param maps            List of texture maps to apply to the texture's components.
1266
 * @param startComponent  the component the first of the maps refers to.
1267
 */
1268
   public void setTextureMap(Static4D[] maps, int startComponent)
1269
     {
1270
     if( mJobNode[0]==null )
1271
       {
1272
       textureMap(maps,startComponent);
1273
       }
1274
     else
1275
       {
1276
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1277
       }
1278
     }
1279

    
1280
///////////////////////////////////////////////////////////////////////////////////////////////////
1281
/**
1282
 * Return the texture map of one of the components.
1283
 *
1284
 * @param component The component number whose texture map we want to retrieve.
1285
 */
1286
   public Static4D getTextureMap(int component)
1287
     {
1288
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1289
     }
1290

    
1291
///////////////////////////////////////////////////////////////////////////////////////////////////
1292
/**
1293
 * Set Effect association.
1294
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1295
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1296
 * will only be active on vertices of Components such that
1297
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1298
 * (see main_vertex_shader)
1299
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1300
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1301
 */
1302
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1303
     {
1304
     if( component>=0 && component<mMaxComponents )
1305
       {
1306
       if( mJobNode[0]==null )
1307
         {
1308
         setEffectAssociationNow(component, andAssociation, equAssociation);
1309
         }
1310
       else
1311
         {
1312
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1313
         }
1314
       }
1315
     }
1316

    
1317
///////////////////////////////////////////////////////////////////////////////////////////////////
1318
/**
1319
 * Set center of a Component.
1320
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1321
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1322
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1323
 * add the 'halo' around an object.
1324
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1325
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1326
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1327
 */
1328
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1329
     {
1330
     if( component>=0 && component<mMaxComponents )
1331
       {
1332
       if( mJobNode[0]==null )
1333
         {
1334
         setComponentCenterNow(component, centerX, centerY, centerZ);
1335
         }
1336
       else
1337
         {
1338
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1339
         }
1340
       }
1341
     }
1342

    
1343
///////////////////////////////////////////////////////////////////////////////////////////////////
1344
/**
1345
 * Set the centers of a all components to the same value in one go.
1346
 */
1347
   public void setAllComponentCenters(float centerX, float centerY, float centerZ)
1348
     {
1349
     if( mJobNode[0]==null )
1350
       {
1351
       setComponentCenterNow(-1, centerX, centerY, centerZ);
1352
       }
1353
     else
1354
       {
1355
       mJobNode[0] = DeferredJobs.componentCenter(this,-1,centerX, centerY, centerZ);
1356
       }
1357
     }
1358

    
1359
///////////////////////////////////////////////////////////////////////////////////////////////////
1360
/**
1361
 * Adds an empty (no vertices) texture component to the end of the text component list.
1362
 * Sometimes we want to do this to have several Meshes with equal number of tex components each.
1363
 */
1364
   public void addEmptyTexComponent()
1365
     {
1366
      if( mJobNode[0]==null )
1367
       {
1368
       addEmptyTexComponentNow();
1369
       }
1370
     else
1371
       {
1372
       mJobNode[0] = DeferredJobs.addEmptyTex(this);
1373
       }
1374
     }
1375

    
1376
///////////////////////////////////////////////////////////////////////////////////////////////////
1377
/**
1378
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1379
 * which can be independently textured.
1380
 *
1381
 * @return The number of Texture Components of this Mesh.
1382
 */
1383
   public int getNumTexComponents()
1384
     {
1385
     return mTexComponent.size();
1386
     }
1387

    
1388
///////////////////////////////////////////////////////////////////////////////////////////////////
1389
/**
1390
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1391
 * to which a VertexEffect can be addressed, independently of other vertices.
1392
 *
1393
 * @return The number of Effect Components of this Mesh.
1394
 */
1395
   public int getNumEffComponents()
1396
     {
1397
     return mEffComponent.size();
1398
     }
1399

    
1400
///////////////////////////////////////////////////////////////////////////////////////////////////
1401
/**
1402
 * Copy the Mesh.
1403
 *
1404
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1405
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1406
 *             coordinates and effect associations, is always deep copied)
1407
 */
1408
   public abstract MeshBase copy(boolean deep);
1409
   }
(3-3/12)