Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 93718f1b

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 && numVertices>0 )
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 printVertex(int v)
684
     {
685
     String pos = "\n pos: (" + mVertAttribs1[VERT1_ATTRIBS*v +POS_ATTRIB  ] + ","
686
                              + mVertAttribs1[VERT1_ATTRIBS*v +POS_ATTRIB+1] + ","
687
                              + mVertAttribs1[VERT1_ATTRIBS*v +POS_ATTRIB+2];
688

    
689
     String nor = "\n nor: (" + mVertAttribs1[VERT1_ATTRIBS*v +NOR_ATTRIB  ] + ","
690
                              + mVertAttribs1[VERT1_ATTRIBS*v +NOR_ATTRIB+1] + ","
691
                              + mVertAttribs1[VERT1_ATTRIBS*v +NOR_ATTRIB+2];
692

    
693
     String tex = "\n tex: (" + mVertAttribs2[VERT2_ATTRIBS*v +TEX_ATTRIB  ] + ","
694
                              + mVertAttribs2[VERT2_ATTRIBS*v +TEX_ATTRIB+1];
695

    
696
     DistortedLibrary.logMessage("MeshBase: first vertex:"+pos+nor+tex);
697
     }
698

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

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

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

    
732
     for(int i=0; i<mNumVertices; i++)
733
       {
734
       sb.append('(');
735
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+NOR_ATTRIB  ]);
736
       sb.append(',');
737
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+NOR_ATTRIB+1]);
738
       sb.append(',');
739
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+NOR_ATTRIB+2]);
740
       sb.append(")\n");
741
       }
742
     DistortedLibrary.logMessage("MeshBase: normals:\n"+sb);
743
     }
744

    
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746
/**
747
 * Not part of public API, do not document (public only because has to be used from the main package)
748
 *
749
 * @y.exclude
750
 */
751
   public void printCom()
752
     {
753
     StringBuilder sb = new StringBuilder();
754

    
755
     for(int i=0; i<mNumVertices; i++)
756
       {
757
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
758
       sb.append(' ');
759
       }
760

    
761
     DistortedLibrary.logMessage("MeshBase: "+sb);
762
     }
763

    
764
///////////////////////////////////////////////////////////////////////////////////////////////////
765
/**
766
 * Not part of public API, do not document (public only because has to be used from the main package)
767
 *
768
 * @y.exclude
769
 */
770
   public void printTex()
771
     {
772
     int vert=0;
773
     int num = getNumTexComponents();
774
     StringBuilder sb = new StringBuilder();
775
     sb.append("tex components: ").append(num);
776

    
777
     for(int i=0; i<num; i++)
778
       {
779
       int end = mTexComponent.get(i).mEndIndex;
780
       sb.append("\n");
781

    
782
       for( ; vert<=end; vert++)
783
         {
784
         sb.append(' ');
785
         sb.append('(');
786
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
787
         sb.append(',');
788
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
789
         sb.append(')');
790
         }
791
       }
792

    
793
     DistortedLibrary.logMessage("MeshBase: "+sb);
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 int getTFO()
803
     {
804
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
805
     }
806

    
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
/**
809
 * Not part of public API, do not document (public only because has to be used from the main package)
810
 *
811
 * @y.exclude
812
 */
813
   public int getNumVertices()
814
     {
815
     return mNumVertices;
816
     }
817

    
818
///////////////////////////////////////////////////////////////////////////////////////////////////
819
/**
820
 * Not part of public API, do not document (public only because has to be used from the main package)
821
 *
822
 * @y.exclude
823
 */
824
   public void send(int programH, int variant)
825
     {
826
     if( !mStrideCorrected )
827
       {
828
       mStrideCorrected = true;
829
       mUBA.correctStride(mStride);
830
       }
831

    
832
     int indexA = mUBA.getIndex();
833
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
834
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
835

    
836
     if( mUseCenters )
837
       {
838
       int indexC = mUBC.getIndex();
839
       GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
840
       GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
841
       }
842
     }
843

    
844
///////////////////////////////////////////////////////////////////////////////////////////////////
845
/**
846
 * Not part of public API, do not document (public only because has to be used from the main package)
847
 *
848
 * @y.exclude
849
 */
850
   public void bindVertexAttribs(DistortedProgram program)
851
     {
852
     if( mJobNode[0]!=null )
853
       {
854
       mJobNode[0].execute();  // this will set itself to null
855
       }
856

    
857
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
858
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
859
     int[] attr = program.mAttribute;
860

    
861
     switch( attr.length )
862
       {
863
       // 'normal' case
864
       case 4: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
865
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
866
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
867
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
868
               GLES30.glVertexAttribPointer(attr[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
869
               GLES30.glVertexAttribPointer(attr[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
870
               break;
871
       // if we are not using component centers and there are no vertex effects enabled,
872
       // then component attribute does not exist in the Vertex Shader
873
       case 3: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
874
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
875
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
876
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
877
               GLES30.glVertexAttribPointer(attr[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
878
               break;
879
       // pre program used in EffectQueuePostprocessing does not have v_Normal attribute
880
       // (it is not used in the fragment shader)
881
       case 2: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
882
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
883
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
884
               GLES30.glVertexAttribPointer(attr[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
885
               break;
886
       }
887

    
888
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
889
     }
890

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892
/**
893
 * Not part of public API, do not document (public only because has to be used from the main package)
894
 *
895
 * @y.exclude
896
 */
897
   public void bindTransformAttribs(DistortedProgram program)
898
     {
899
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
900

    
901
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
902
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
903
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
904
     }
905

    
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907
/**
908
 * Not part of public API, do not document (public only because has to be used from the main package)
909
 *
910
 * @y.exclude
911
 */
912
   public void setInflate(float inflate)
913
     {
914
     mInflate = inflate;
915
     }
916

    
917
///////////////////////////////////////////////////////////////////////////////////////////////////
918
/**
919
 * Not part of public API, do not document (public only because has to be used from the main package)
920
 *
921
 * @y.exclude
922
 */
923
   public float getInflate()
924
     {
925
     return mInflate;
926
     }
927

    
928
///////////////////////////////////////////////////////////////////////////////////////////////////
929
// Return the number of bytes read.
930

    
931
   int read(DataInputStream stream)
932
     {
933
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
934
     int version, numEff, numTex;
935

    
936
     //////////////////////////////////////////////////////////////////////////////////////////
937
     // read version, number of vertices, number of tex components, number of effect components
938
     //////////////////////////////////////////////////////////////////////////////////////////
939

    
940
     try
941
       {
942
       stream.read(buffer);
943
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
944

    
945
       version = byteBuf.getInt(0);
946

    
947
       if( version==1 || version==2 )
948
         {
949
         mNumVertices= byteBuf.getInt(4);
950
         numTex      = byteBuf.getInt(8);
951
         numEff      = byteBuf.getInt(12);
952
         }
953
       else
954
         {
955
         DistortedLibrary.logMessage("MeshBase: Error: unknown mesh file version "+String.format("0x%08X", version));
956
         return 0;
957
         }
958
       }
959
     catch(Exception e)
960
       {
961
       DistortedLibrary.logMessage("MeshBase: Exception1 trying to read file: "+e.toString());
962
       return 0;
963
       }
964

    
965
     //////////////////////////////////////////////////////////////////////////////////////////
966
     // read contents of all texture and effect components
967
     //////////////////////////////////////////////////////////////////////////////////////////
968

    
969
     if( mNumVertices>0 && numEff>0 && numTex>0 )
970
       {
971
       int size = numEff+TEX_COMP_SIZE*numTex;
972
       float[] tmp = new float[size];
973
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
974
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
975

    
976
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
977
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
978
       // ... and there was no center.
979
       int centerSize  = version==2 ? 3*numEff : 0;
980

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

    
983
       try
984
         {
985
         stream.read(buffer);
986
         }
987
       catch(Exception e)
988
         {
989
         DistortedLibrary.logMessage("MeshBase: Exception2 trying to read file: "+e.toString());
990
         return 0;
991
         }
992

    
993
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
994
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
995

    
996
       floatBuf.get(tmp,0,size);
997

    
998
       TexComponent tex;
999
       int index, texComp;
1000
       float x, y, z, w;
1001

    
1002
       for(texComp=0; texComp<numTex; texComp++)
1003
         {
1004
         index = (int)tmp[TEX_COMP_SIZE*texComp];
1005

    
1006
         x= tmp[TEX_COMP_SIZE*texComp+1];
1007
         y= tmp[TEX_COMP_SIZE*texComp+2];
1008
         z= tmp[TEX_COMP_SIZE*texComp+3];
1009
         w= tmp[TEX_COMP_SIZE*texComp+4];
1010

    
1011
         tex = new TexComponent(index);
1012
         tex.setMap(new Static4D(x,y,z,w));
1013

    
1014
         mTexComponent.add(tex);
1015
         }
1016

    
1017
       for(int effComp=0; effComp<numEff; effComp++)
1018
         {
1019
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
1020
         mEffComponent.add(index);
1021
         }
1022

    
1023
       //////////////////////////////////////////////////////////////////////////////////////////
1024
       // read vert1 array, vert2 array and (if version>1) the component centers.
1025
       //////////////////////////////////////////////////////////////////////////////////////////
1026

    
1027
       switch(version)
1028
         {
1029
         case 1 : index = floatBuf.position();
1030

    
1031
                  for(int vert=0; vert<mNumVertices; vert++)
1032
                    {
1033
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
1034
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
1035
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
1036
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
1037
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
1038
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
1039
                    index+=vert1InFile;
1040
                    }
1041
                  floatBuf.position(index);
1042
                  break;
1043
         case 2 : float[] centers = new float[3*numEff];
1044
                  floatBuf.get(centers,0,3*numEff);
1045

    
1046
                  if( mUseCenters )
1047
                    {
1048
                    for(int eff=0; eff<numEff; eff++)
1049
                      {
1050
                      mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
1051
                      }
1052
                    }
1053

    
1054
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
1055
                  break;
1056
         default: DistortedLibrary.logMessage("MeshBase: Error: unknown mesh file version "+String.format("0x%08X", version));
1057
                  return 0;
1058
         }
1059

    
1060
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
1061
       }
1062

    
1063
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
1064
     }
1065

    
1066
///////////////////////////////////////////////////////////////////////////////////////////////////
1067
/**
1068
 * @y.exclude
1069
 */
1070
   public int getLastVertexEff(int effComponent)
1071
     {
1072
     if( effComponent>=0 && effComponent<mTexComponent.size() )
1073
       {
1074
       return mEffComponent.get(effComponent);
1075
       }
1076

    
1077
     return 0;
1078
     }
1079

    
1080
///////////////////////////////////////////////////////////////////////////////////////////////////
1081
/**
1082
 * @y.exclude
1083
 */
1084
   public int getLastVertexTex(int texComponent)
1085
     {
1086
     if( texComponent>=0 && texComponent<mTexComponent.size() )
1087
       {
1088
       return mTexComponent.get(texComponent).mEndIndex;
1089
       }
1090

    
1091
     return 0;
1092
     }
1093

    
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095
// PUBLIC API
1096
///////////////////////////////////////////////////////////////////////////////////////////////////
1097
/**
1098
 * Write the Mesh to a File.
1099
 */
1100
   public void write(DataOutputStream stream)
1101
     {
1102
     TexComponent tex;
1103

    
1104
     int numTex = mTexComponent.size();
1105
     int numEff = mEffComponent.size();
1106

    
1107
     try
1108
       {
1109
       stream.writeInt(2);  // version
1110
       stream.writeInt(mNumVertices);
1111
       stream.writeInt(numTex);
1112
       stream.writeInt(numEff);
1113

    
1114
       for(int i=0; i<numTex; i++)
1115
         {
1116
         tex = mTexComponent.get(i);
1117

    
1118
         stream.writeFloat((float)tex.mEndIndex);
1119
         stream.writeFloat(tex.mTextureMap.get0());
1120
         stream.writeFloat(tex.mTextureMap.get1());
1121
         stream.writeFloat(tex.mTextureMap.get2());
1122
         stream.writeFloat(tex.mTextureMap.get3());
1123
         }
1124

    
1125
       for(int i=0; i<numEff; i++)
1126
         {
1127
         stream.writeFloat((float)mEffComponent.get(i));
1128
         }
1129

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

    
1132
       for(int i=0; i<numEff; i++)
1133
         {
1134
         stream.writeFloat(centers[4*i  ]);
1135
         stream.writeFloat(centers[4*i+1]);
1136
         stream.writeFloat(centers[4*i+2]);
1137
         }
1138

    
1139
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
1140
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
1141
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
1142
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
1143

    
1144
       stream.write(vertBuf1.array());
1145
       stream.write(vertBuf2.array());
1146
       }
1147
     catch(IOException ex)
1148
       {
1149
       DistortedLibrary.logMessage("MeshBase: IOException trying to write a mesh: "+ex.toString());
1150
       }
1151
     }
1152

    
1153
///////////////////////////////////////////////////////////////////////////////////////////////////
1154
/**
1155
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1156
 * <p>
1157
 * Will work only on OpenGL ES >= 3.0 devices.
1158
 *
1159
 * @param show Controls if we render the Normal vectors or not.
1160
 */
1161
   public void setShowNormals(boolean show)
1162
     {
1163
     mShowNormals = show;
1164
     }
1165

    
1166
///////////////////////////////////////////////////////////////////////////////////////////////////
1167
/**
1168
 * When rendering this mesh, should we also draw the normal vectors?
1169
 *
1170
 * @return <i>true</i> if we do render normal vectors
1171
 */
1172
   public boolean getShowNormals()
1173
     {
1174
     return mShowNormals;
1175
     }
1176

    
1177
///////////////////////////////////////////////////////////////////////////////////////////////////
1178
/**
1179
 * Merge all texture components of this Mesh into a single one.
1180
 */
1181
   public void mergeTexComponents()
1182
     {
1183
     if( mJobNode[0]==null )
1184
       {
1185
       mergeTexComponentsNow();
1186
       }
1187
     else
1188
       {
1189
       mJobNode[0] = DeferredJobs.mergeTex(this);
1190
       }
1191
     }
1192

    
1193
///////////////////////////////////////////////////////////////////////////////////////////////////
1194
/**
1195
 * Merge all effect components of this Mesh into a single one.
1196
 */
1197
   public void mergeEffComponents()
1198
     {
1199
     if( mJobNode[0]==null )
1200
       {
1201
       mergeEffComponentsNow();
1202
       }
1203
     else
1204
       {
1205
       mJobNode[0] = DeferredJobs.mergeEff(this);
1206
       }
1207
     }
1208

    
1209
///////////////////////////////////////////////////////////////////////////////////////////////////
1210
/**
1211
 * Release all internal resources.
1212
 */
1213
   public void markForDeletion()
1214
     {
1215
     mVertAttribs1 = null;
1216
     mVertAttribs2 = null;
1217

    
1218
     mVBO1.markForDeletion();
1219
     mVBO2.markForDeletion();
1220
     mTFO.markForDeletion();
1221
     mUBA.markForDeletion();
1222

    
1223
     if( mUBC!=null )
1224
       {
1225
       mUBC.markForDeletion();
1226
       }
1227
     }
1228

    
1229
///////////////////////////////////////////////////////////////////////////////////////////////////
1230
/**
1231
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1232
 * <p>
1233
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1234
 * contains any Dynamics, they will be evaluated at 0.
1235
 *
1236
 * @param effect List of Matrix Effects to apply to the Mesh.
1237
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1238
 * @param equAssoc 'equality' association which defines which components will be affected.
1239
 */
1240
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1241
     {
1242
     if( mJobNode[0]==null )
1243
       {
1244
       applyMatrix(effect,andAssoc,equAssoc);
1245
       }
1246
     else
1247
       {
1248
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1249
       }
1250
     }
1251

    
1252
///////////////////////////////////////////////////////////////////////////////////////////////////
1253
/**
1254
 * Apply a Vertex Effect to the vertex mesh.
1255
 * <p>
1256
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1257
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1258
 * We would call this several times building up a list of Effects to do. This list of effects gets
1259
 * lazily executed only when the Mesh is used for rendering for the first time.
1260
 *
1261
 * @param effect Vertex Effect to apply to the Mesh.
1262
 */
1263
   public void apply(VertexEffect effect)
1264
     {
1265
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1266
     }
1267

    
1268
///////////////////////////////////////////////////////////////////////////////////////////////////
1269
/**
1270
 * Sets texture maps for (some of) the components of this mesh.
1271
 * <p>
1272
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1273
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1274
 * upper-left quadrant of the texture.
1275
 * <p>
1276
 * Probably the most common user case would be sending as many maps as there are components in this
1277
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1278
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1279
 * of the 2nd and 4rd one, call this with
1280
 * maps = new Static4D[4]
1281
 * maps[0] = null
1282
 * maps[1] = the map for the 2nd component
1283
 * maps[2] = null
1284
 * maps[3] = the map for the 4th component
1285
 * A map's width and height have to be non-zero (but can be negative!)
1286
 *
1287
 * @param maps            List of texture maps to apply to the texture's components.
1288
 * @param startComponent  the component the first of the maps refers to.
1289
 */
1290
   public void setTextureMap(Static4D[] maps, int startComponent)
1291
     {
1292
     if( mJobNode[0]==null )
1293
       {
1294
       textureMap(maps,startComponent);
1295
       }
1296
     else
1297
       {
1298
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1299
       }
1300
     }
1301

    
1302
///////////////////////////////////////////////////////////////////////////////////////////////////
1303
/**
1304
 * Return the texture map of one of the components.
1305
 *
1306
 * @param component The component number whose texture map we want to retrieve.
1307
 */
1308
   public Static4D getTextureMap(int component)
1309
     {
1310
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1311
     }
1312

    
1313
///////////////////////////////////////////////////////////////////////////////////////////////////
1314
/**
1315
 * Set Effect association.
1316
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1317
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1318
 * will only be active on vertices of Components such that
1319
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1320
 * (see main_vertex_shader)
1321
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1322
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1323
 */
1324
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1325
     {
1326
     if( component>=0 && component<mMaxComponents )
1327
       {
1328
       if( mJobNode[0]==null )
1329
         {
1330
         setEffectAssociationNow(component, andAssociation, equAssociation);
1331
         }
1332
       else
1333
         {
1334
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1335
         }
1336
       }
1337
     }
1338

    
1339
///////////////////////////////////////////////////////////////////////////////////////////////////
1340
/**
1341
 * Set center of a Component.
1342
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1343
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1344
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1345
 * add the 'halo' around an object.
1346
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1347
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1348
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1349
 */
1350
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1351
     {
1352
     if( component>=0 && component<mMaxComponents )
1353
       {
1354
       if( mJobNode[0]==null )
1355
         {
1356
         setComponentCenterNow(component, centerX, centerY, centerZ);
1357
         }
1358
       else
1359
         {
1360
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1361
         }
1362
       }
1363
     }
1364

    
1365
///////////////////////////////////////////////////////////////////////////////////////////////////
1366
/**
1367
 * Set the centers of a all components to the same value in one go.
1368
 */
1369
   public void setAllComponentCenters(float centerX, float centerY, float centerZ)
1370
     {
1371
     if( mJobNode[0]==null )
1372
       {
1373
       setComponentCenterNow(-1, centerX, centerY, centerZ);
1374
       }
1375
     else
1376
       {
1377
       mJobNode[0] = DeferredJobs.componentCenter(this,-1,centerX, centerY, centerZ);
1378
       }
1379
     }
1380

    
1381
///////////////////////////////////////////////////////////////////////////////////////////////////
1382
/**
1383
 * Adds an empty (no vertices) texture component to the end of the text component list.
1384
 * Sometimes we want to do this to have several Meshes with equal number of tex components each.
1385
 */
1386
   public void addEmptyTexComponent()
1387
     {
1388
      if( mJobNode[0]==null )
1389
       {
1390
       addEmptyTexComponentNow();
1391
       }
1392
     else
1393
       {
1394
       mJobNode[0] = DeferredJobs.addEmptyTex(this);
1395
       }
1396
     }
1397

    
1398
///////////////////////////////////////////////////////////////////////////////////////////////////
1399
/**
1400
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1401
 * which can be independently textured.
1402
 *
1403
 * @return The number of Texture Components of this Mesh.
1404
 */
1405
   public int getNumTexComponents()
1406
     {
1407
     return mTexComponent.size();
1408
     }
1409

    
1410
///////////////////////////////////////////////////////////////////////////////////////////////////
1411
/**
1412
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1413
 * to which a VertexEffect can be addressed, independently of other vertices.
1414
 *
1415
 * @return The number of Effect Components of this Mesh.
1416
 */
1417
   public int getNumEffComponents()
1418
     {
1419
     return mEffComponent.size();
1420
     }
1421

    
1422
///////////////////////////////////////////////////////////////////////////////////////////////////
1423
/**
1424
 * Copy the Mesh.
1425
 *
1426
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1427
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1428
 *             coordinates and effect associations, is always deep copied)
1429
 */
1430
   public abstract MeshBase copy(boolean deep);
1431
   }
(3-3/12)