Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 81433569

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.mesh;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24
import android.util.Log;
25

    
26
import org.distorted.library.effect.MatrixEffect;
27
import org.distorted.library.effect.VertexEffect;
28
import org.distorted.library.effectqueue.EffectQueue;
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
     Matrix.setIdentityM(matrixP,0);
266
     Matrix.setIdentityM(matrixV,0);
267
     effect.compute(uniforms,0,0,0);
268
     effect.apply(matrixP, matrixV, uniforms, 0);
269

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

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

    
281
     mVBO1.invalidate();
282
     }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

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

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

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

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

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

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

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

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

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

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

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

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

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
462
     if( origVertices!=mNumVertices )
463
       {
464
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
465
       }
466

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

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

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

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

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483
// called from MeshJoined
484

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

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

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

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

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

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

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

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

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

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

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

    
546
     mVBO2.invalidate();
547
     }
548

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

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

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

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

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

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

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

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

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

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

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

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

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

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

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

    
672
       Log.d("mesh", sb.toString() );
673
       }
674
     }
675

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

    
686
     for(int i=0; i<mNumVertices; i++)
687
       {
688
       sb.append('(');
689
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
690
       sb.append(',');
691
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
692
       sb.append(',');
693
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
694
       sb.append(") ");
695
       }
696
     Log.d("mesh", sb.toString());
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 printCom()
706
     {
707
     StringBuilder sb = new StringBuilder();
708

    
709
     for(int i=0; i<mNumVertices; i++)
710
       {
711
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
712
       sb.append(' ');
713
       }
714

    
715
     Log.d("mesh", sb.toString());
716
     }
717

    
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719
/**
720
 * Not part of public API, do not document (public only because has to be used from the main package)
721
 *
722
 * @y.exclude
723
 */
724
   public void printTex()
725
     {
726
     int vert=0;
727
     int num = getNumTexComponents();
728
     StringBuilder sb = new StringBuilder();
729
     sb.append("tex components: ").append(num);
730

    
731
     for(int i=0; i<num; i++)
732
       {
733
       int end = mTexComponent.get(i).mEndIndex;
734
       sb.append("\n");
735

    
736
       for( ; vert<=end; vert++)
737
         {
738
         sb.append(' ');
739
         sb.append('(');
740
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
741
         sb.append(',');
742
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
743
         sb.append(')');
744
         }
745
       }
746

    
747
     Log.d("mesh", sb.toString());
748
     }
749

    
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751
/**
752
 * Not part of public API, do not document (public only because has to be used from the main package)
753
 *
754
 * @y.exclude
755
 */
756
   public int getTFO()
757
     {
758
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
759
     }
760

    
761
///////////////////////////////////////////////////////////////////////////////////////////////////
762
/**
763
 * Not part of public API, do not document (public only because has to be used from the main package)
764
 *
765
 * @y.exclude
766
 */
767
   public int getNumVertices()
768
     {
769
     return mNumVertices;
770
     }
771

    
772
///////////////////////////////////////////////////////////////////////////////////////////////////
773
/**
774
 * Not part of public API, do not document (public only because has to be used from the main package)
775
 *
776
 * @y.exclude
777
 */
778
   public void send(int programH, int variant)
779
     {
780
     if( !mStrideCorrected )
781
       {
782
       mStrideCorrected = true;
783
       mUBA.correctStride(mStride);
784
       }
785

    
786
     int indexA = mUBA.getIndex();
787
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
788
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
789

    
790
     if( mUseCenters )
791
       {
792
       int indexC = mUBC.getIndex();
793
       GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
794
       GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
795
       }
796
     }
797

    
798
///////////////////////////////////////////////////////////////////////////////////////////////////
799
/**
800
 * Not part of public API, do not document (public only because has to be used from the main package)
801
 *
802
 * @y.exclude
803
 */
804
   public void bindVertexAttribs(DistortedProgram program)
805
     {
806
     if( mJobNode[0]!=null )
807
       {
808
       mJobNode[0].execute();  // this will set itself to null
809
       }
810

    
811
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
812
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
813

    
814
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
815
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
816
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
817
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
818
     GLES30.glVertexAttribPointer(program.mAttribute[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
819
     GLES30.glVertexAttribPointer(program.mAttribute[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
820
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
821
     }
822

    
823
///////////////////////////////////////////////////////////////////////////////////////////////////
824
/**
825
 * Not part of public API, do not document (public only because has to be used from the main package)
826
 *
827
 * @y.exclude
828
 */
829
   public void bindTransformAttribs(DistortedProgram program)
830
     {
831
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
832

    
833
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
834
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
835
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
836
     }
837

    
838
///////////////////////////////////////////////////////////////////////////////////////////////////
839
/**
840
 * Not part of public API, do not document (public only because has to be used from the main package)
841
 *
842
 * @y.exclude
843
 */
844
   public void setInflate(float inflate)
845
     {
846
     mInflate = inflate;
847
     }
848

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850
/**
851
 * Not part of public API, do not document (public only because has to be used from the main package)
852
 *
853
 * @y.exclude
854
 */
855
   public float getInflate()
856
     {
857
     return mInflate;
858
     }
859

    
860
///////////////////////////////////////////////////////////////////////////////////////////////////
861
// Return the number of bytes read.
862

    
863
   int read(DataInputStream stream)
864
     {
865
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
866
     int version, numEff, numTex;
867

    
868
     //////////////////////////////////////////////////////////////////////////////////////////
869
     // read version, number of vertices, number of tex components, number of effect components
870
     //////////////////////////////////////////////////////////////////////////////////////////
871

    
872
     try
873
       {
874
       stream.read(buffer);
875
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
876

    
877
       version = byteBuf.getInt(0);
878

    
879
       if( version==1 || version==2 )
880
         {
881
         mNumVertices= byteBuf.getInt(4);
882
         numTex      = byteBuf.getInt(8);
883
         numEff      = byteBuf.getInt(12);
884
         }
885
       else
886
         {
887
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
888
         return 0;
889
         }
890
       }
891
     catch(IOException e)
892
       {
893
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
894
       return 0;
895
       }
896

    
897
     //////////////////////////////////////////////////////////////////////////////////////////
898
     // read contents of all texture and effect components
899
     //////////////////////////////////////////////////////////////////////////////////////////
900

    
901
     if( mNumVertices>0 && numEff>0 && numTex>0 )
902
       {
903
       int size = numEff+TEX_COMP_SIZE*numTex;
904
       float[] tmp = new float[size];
905
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
906
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
907

    
908
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
909
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
910
       // ... and there was no center.
911
       int centerSize  = version==2 ? 3*numEff : 0;
912

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

    
915
       try
916
         {
917
         stream.read(buffer);
918
         }
919
       catch(IOException e)
920
         {
921
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
922
         return 0;
923
         }
924

    
925
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
926
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
927

    
928
       floatBuf.get(tmp,0,size);
929

    
930
       TexComponent tex;
931
       int index, texComp;
932
       float x, y, z, w;
933

    
934
       for(texComp=0; texComp<numTex; texComp++)
935
         {
936
         index = (int)tmp[TEX_COMP_SIZE*texComp];
937

    
938
         x= tmp[TEX_COMP_SIZE*texComp+1];
939
         y= tmp[TEX_COMP_SIZE*texComp+2];
940
         z= tmp[TEX_COMP_SIZE*texComp+3];
941
         w= tmp[TEX_COMP_SIZE*texComp+4];
942

    
943
         tex = new TexComponent(index);
944
         tex.setMap(new Static4D(x,y,z,w));
945

    
946
         mTexComponent.add(tex);
947
         }
948

    
949
       for(int effComp=0; effComp<numEff; effComp++)
950
         {
951
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
952
         mEffComponent.add(index);
953
         }
954

    
955
       //////////////////////////////////////////////////////////////////////////////////////////
956
       // read vert1 array, vert2 array and (if version>1) the component centers.
957
       //////////////////////////////////////////////////////////////////////////////////////////
958

    
959
       switch(version)
960
         {
961
         case 1 : index = floatBuf.position();
962

    
963
                  for(int vert=0; vert<mNumVertices; vert++)
964
                    {
965
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
966
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
967
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
968
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
969
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
970
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
971
                    index+=vert1InFile;
972
                    }
973
                  floatBuf.position(index);
974
                  break;
975
         case 2 : float[] centers = new float[3*numEff];
976
                  floatBuf.get(centers,0,3*numEff);
977

    
978
                  if( mUseCenters )
979
                    {
980
                    for(int eff=0; eff<numEff; eff++)
981
                      {
982
                      mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
983
                      }
984
                    }
985

    
986
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
987
                  break;
988
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
989
                  return 0;
990
         }
991

    
992
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
993
       }
994

    
995
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
996
     }
997

    
998
///////////////////////////////////////////////////////////////////////////////////////////////////
999
/**
1000
 * @y.exclude
1001
 */
1002
   public int getLastVertexEff(int effComponent)
1003
     {
1004
     if( effComponent>=0 && effComponent<mTexComponent.size() )
1005
       {
1006
       return mEffComponent.get(effComponent);
1007
       }
1008

    
1009
     return 0;
1010
     }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013
/**
1014
 * @y.exclude
1015
 */
1016
   public int getLastVertexTex(int texComponent)
1017
     {
1018
     if( texComponent>=0 && texComponent<mTexComponent.size() )
1019
       {
1020
       return mTexComponent.get(texComponent).mEndIndex;
1021
       }
1022

    
1023
     return 0;
1024
     }
1025

    
1026
///////////////////////////////////////////////////////////////////////////////////////////////////
1027
// PUBLIC API
1028
///////////////////////////////////////////////////////////////////////////////////////////////////
1029
/**
1030
 * Write the Mesh to a File.
1031
 */
1032
   public void write(DataOutputStream stream)
1033
     {
1034
     TexComponent tex;
1035

    
1036
     int numTex = mTexComponent.size();
1037
     int numEff = mEffComponent.size();
1038

    
1039
     try
1040
       {
1041
       stream.writeInt(2);  // version
1042
       stream.writeInt(mNumVertices);
1043
       stream.writeInt(numTex);
1044
       stream.writeInt(numEff);
1045

    
1046
       for(int i=0; i<numTex; i++)
1047
         {
1048
         tex = mTexComponent.get(i);
1049

    
1050
         stream.writeFloat((float)tex.mEndIndex);
1051
         stream.writeFloat(tex.mTextureMap.get0());
1052
         stream.writeFloat(tex.mTextureMap.get1());
1053
         stream.writeFloat(tex.mTextureMap.get2());
1054
         stream.writeFloat(tex.mTextureMap.get3());
1055
         }
1056

    
1057
       for(int i=0; i<numEff; i++)
1058
         {
1059
         stream.writeFloat((float)mEffComponent.get(i));
1060
         }
1061

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

    
1064
       for(int i=0; i<numEff; i++)
1065
         {
1066
         stream.writeFloat(centers[4*i  ]);
1067
         stream.writeFloat(centers[4*i+1]);
1068
         stream.writeFloat(centers[4*i+2]);
1069
         }
1070

    
1071
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
1072
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
1073
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
1074
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
1075

    
1076
       stream.write(vertBuf1.array());
1077
       stream.write(vertBuf2.array());
1078
       }
1079
     catch(IOException ex)
1080
       {
1081
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
1082
       }
1083
     }
1084

    
1085
///////////////////////////////////////////////////////////////////////////////////////////////////
1086
/**
1087
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1088
 * <p>
1089
 * Will work only on OpenGL ES >= 3.0 devices.
1090
 *
1091
 * @param show Controls if we render the Normal vectors or not.
1092
 */
1093
   public void setShowNormals(boolean show)
1094
     {
1095
     mShowNormals = show;
1096
     }
1097

    
1098
///////////////////////////////////////////////////////////////////////////////////////////////////
1099
/**
1100
 * When rendering this mesh, should we also draw the normal vectors?
1101
 *
1102
 * @return <i>true</i> if we do render normal vectors
1103
 */
1104
   public boolean getShowNormals()
1105
     {
1106
     return mShowNormals;
1107
     }
1108

    
1109
///////////////////////////////////////////////////////////////////////////////////////////////////
1110
/**
1111
 * Merge all texture components of this Mesh into a single one.
1112
 */
1113
   public void mergeTexComponents()
1114
     {
1115
     if( mJobNode[0]==null )
1116
       {
1117
       mergeTexComponentsNow();
1118
       }
1119
     else
1120
       {
1121
       mJobNode[0] = DeferredJobs.mergeTex(this);
1122
       }
1123
     }
1124

    
1125
///////////////////////////////////////////////////////////////////////////////////////////////////
1126
/**
1127
 * Merge all effect components of this Mesh into a single one.
1128
 */
1129
   public void mergeEffComponents()
1130
     {
1131
     if( mJobNode[0]==null )
1132
       {
1133
       mergeEffComponentsNow();
1134
       }
1135
     else
1136
       {
1137
       mJobNode[0] = DeferredJobs.mergeEff(this);
1138
       }
1139
     }
1140

    
1141
///////////////////////////////////////////////////////////////////////////////////////////////////
1142
/**
1143
 * Release all internal resources.
1144
 */
1145
   public void markForDeletion()
1146
     {
1147
     mVertAttribs1 = null;
1148
     mVertAttribs2 = null;
1149

    
1150
     mVBO1.markForDeletion();
1151
     mVBO2.markForDeletion();
1152
     mTFO.markForDeletion();
1153
     mUBA.markForDeletion();
1154

    
1155
     if( mUBC!=null )
1156
       {
1157
       mUBC.markForDeletion();
1158
       }
1159
     }
1160

    
1161
///////////////////////////////////////////////////////////////////////////////////////////////////
1162
/**
1163
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1164
 * <p>
1165
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1166
 * contains any Dynamics, they will be evaluated at 0.
1167
 *
1168
 * @param effect List of Matrix Effects to apply to the Mesh.
1169
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1170
 * @param equAssoc 'equality' association which defines which components will be affected.
1171
 */
1172
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1173
     {
1174
     if( mJobNode[0]==null )
1175
       {
1176
       applyMatrix(effect,andAssoc,equAssoc);
1177
       }
1178
     else
1179
       {
1180
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1181
       }
1182
     }
1183

    
1184
///////////////////////////////////////////////////////////////////////////////////////////////////
1185
/**
1186
 * Apply a Vertex Effect to the vertex mesh.
1187
 * <p>
1188
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1189
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1190
 *
1191
 * We would call this several times building up a list of Effects to do. This list of effects gets
1192
 * lazily executed only when the Mesh is used for rendering for the first time.
1193
 *
1194
 * @param effect Vertex Effect to apply to the Mesh.
1195
 */
1196
   public void apply(VertexEffect effect)
1197
     {
1198
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1199
     }
1200

    
1201
///////////////////////////////////////////////////////////////////////////////////////////////////
1202
/**
1203
 * Sets texture maps for (some of) the components of this mesh.
1204
 * <p>
1205
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1206
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1207
 * upper-left quadrant of the texture.
1208
 * <p>
1209
 * Probably the most common user case would be sending as many maps as there are components in this
1210
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1211
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1212
 * of the 2nd and 4rd one, call this with
1213
 * maps = new Static4D[4]
1214
 * maps[0] = null
1215
 * maps[1] = the map for the 2nd component
1216
 * maps[2] = null
1217
 * maps[3] = the map for the 4th component
1218
 *
1219
 * A map's width and height have to be non-zero (but can be negative!)
1220
 *
1221
 * @param maps            List of texture maps to apply to the texture's components.
1222
 * @param startComponent  the component the first of the maps refers to.
1223
 */
1224
   public void setTextureMap(Static4D[] maps, int startComponent)
1225
     {
1226
     if( mJobNode[0]==null )
1227
       {
1228
       textureMap(maps,startComponent);
1229
       }
1230
     else
1231
       {
1232
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1233
       }
1234
     }
1235

    
1236
///////////////////////////////////////////////////////////////////////////////////////////////////
1237
/**
1238
 * Return the texture map of one of the components.
1239
 *
1240
 * @param component The component number whose texture map we want to retrieve.
1241
 */
1242
   public Static4D getTextureMap(int component)
1243
     {
1244
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1245
     }
1246

    
1247
///////////////////////////////////////////////////////////////////////////////////////////////////
1248
/**
1249
 * Set Effect association.
1250
 *
1251
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1252
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1253
 * will only be active on vertices of Components such that
1254
 *
1255
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1256
 *
1257
 * (see main_vertex_shader)
1258
 *
1259
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1260
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1261
 */
1262
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1263
     {
1264
     if( component>=0 && component<mMaxComponents )
1265
       {
1266
       if( mJobNode[0]==null )
1267
         {
1268
         setEffectAssociationNow(component, andAssociation, equAssociation);
1269
         }
1270
       else
1271
         {
1272
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1273
         }
1274
       }
1275
     }
1276

    
1277
///////////////////////////////////////////////////////////////////////////////////////////////////
1278
/**
1279
 * Set center of a Component.
1280
 *
1281
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1282
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1283
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1284
 * add the 'halo' around an object.
1285
 *
1286
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1287
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1288
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1289
 */
1290
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1291
     {
1292
     if( component>=0 && component<mMaxComponents )
1293
       {
1294
       if( mJobNode[0]==null )
1295
         {
1296
         setComponentCenterNow(component, centerX, centerY, centerZ);
1297
         }
1298
       else
1299
         {
1300
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1301
         }
1302
       }
1303
     }
1304

    
1305
///////////////////////////////////////////////////////////////////////////////////////////////////
1306
/**
1307
 * Set the centers of a all components to the same value in one go.
1308
 */
1309
   public void setAllComponentCenters(float centerX, float centerY, float centerZ)
1310
     {
1311
     if( mJobNode[0]==null )
1312
       {
1313
       setComponentCenterNow(-1, centerX, centerY, centerZ);
1314
       }
1315
     else
1316
       {
1317
       mJobNode[0] = DeferredJobs.componentCenter(this,-1,centerX, centerY, centerZ);
1318
       }
1319
     }
1320

    
1321
///////////////////////////////////////////////////////////////////////////////////////////////////
1322
/**
1323
 * Adds an empty (no vertices) texture component to the end of the text component list.
1324
 *
1325
 * Sometimes we want to do this to have several Meshes with equal number of tex components each.
1326
 */
1327
   public void addEmptyTexComponent()
1328
     {
1329
      if( mJobNode[0]==null )
1330
       {
1331
       addEmptyTexComponentNow();
1332
       }
1333
     else
1334
       {
1335
       mJobNode[0] = DeferredJobs.addEmptyTex(this);
1336
       }
1337
     }
1338

    
1339
///////////////////////////////////////////////////////////////////////////////////////////////////
1340
/**
1341
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1342
 * which can be independently textured.
1343
 *
1344
 * @return The number of Texture Components of this Mesh.
1345
 */
1346
   public int getNumTexComponents()
1347
     {
1348
     return mTexComponent.size();
1349
     }
1350

    
1351
///////////////////////////////////////////////////////////////////////////////////////////////////
1352
/**
1353
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1354
 * to which a VertexEffect can be addressed, independently of other vertices.
1355
 *
1356
 * @return The number of Effect Components of this Mesh.
1357
 */
1358
   public int getNumEffComponents()
1359
     {
1360
     return mEffComponent.size();
1361
     }
1362

    
1363
///////////////////////////////////////////////////////////////////////////////////////////////////
1364
/**
1365
 * Copy the Mesh.
1366
 *
1367
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1368
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1369
 *             coordinates and effect associations, is always deep copied)
1370
 */
1371
   public abstract MeshBase copy(boolean deep);
1372
   }
(2-2/10)