Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 5413c8cc

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
   public static int MAX_EFFECT_COMPONENTS= 100;
56

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

    
63
   static final int POS_ATTRIB   = 0;
64
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
65
   static final int TEX_ATTRIB   = 0;
66
   static final int COM_ATTRIB   = TEX_DATA_SIZE;
67

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

    
72
   private static final int BYTES_PER_FLOAT = 4;
73

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

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

    
83
   private static final int[] mCenterBlockIndex = new int[EffectQueue.MAIN_VARIANTS];
84
   private static final int[] mAssocBlockIndex  = new int[EffectQueue.MAIN_VARIANTS];
85

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

    
88
   private static boolean mUseCenters;
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
   private static int mStride;
100

    
101
   DeferredJobs.JobNode[] mJobNode;
102

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

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

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

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

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

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

    
144
     mUBA = new UniformBlockAssociation();
145

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

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// copy constructor
155

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

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

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

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

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

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

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

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

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

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

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

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

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

    
266
     Matrix.setIdentityM(matrixP,0);
267
     Matrix.setIdentityM(matrixV,0);
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<MAX_EFFECT_COMPONENTS )
403
           {
404
           mUBA.copy(origEffComponents, mesh.mUBA, j);
405
           if( mUseCenters ) mUBC.copy(origEffComponents, mesh.mUBC, j);
406
           origEffComponents++;
407
           }
408
         }
409

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

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

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

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

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

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

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

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

    
463
     if( origVertices!=mNumVertices )
464
       {
465
       android.util.Log.e("mesh", "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 MAX_EFFECT_COMPONENTS;
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
     MAX_EFFECT_COMPONENTS = 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
       Log.e("mesh", "failed to map tf buffer, error="+error);
625
       }
626

    
627
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
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 debug()
637
     {
638
     if( mJobNode[0]!=null )
639
       {
640
       mJobNode[0].print(0);
641
       }
642
     else
643
       {
644
       android.util.Log.e("mesh", "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 printPos()
655
     {
656
     StringBuilder sb = new StringBuilder();
657

    
658
     for(int i=0; i<mNumVertices; i++)
659
       {
660
       sb.append('(');
661
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
662
       sb.append(',');
663
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
664
       sb.append(',');
665
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
666
       sb.append(") ");
667
       }
668
     Log.d("mesh", sb.toString());
669
     }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672
/**
673
 * Not part of public API, do not document (public only because has to be used from the main package)
674
 *
675
 * @y.exclude
676
 */
677
   public void printCom()
678
     {
679
     StringBuilder sb = new StringBuilder();
680

    
681
     for(int i=0; i<mNumVertices; i++)
682
       {
683
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
684
       sb.append(' ');
685
       }
686

    
687
     Log.d("mesh", sb.toString());
688
     }
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691
/**
692
 * Not part of public API, do not document (public only because has to be used from the main package)
693
 *
694
 * @y.exclude
695
 */
696
   public void printTex()
697
     {
698
     int vert=0;
699
     int num = getNumTexComponents();
700
     StringBuilder sb = new StringBuilder();
701
     sb.append("tex components: ").append(num);
702

    
703
     for(int i=0; i<num; i++)
704
       {
705
       int end = mTexComponent.get(i).mEndIndex;
706
       sb.append("\n");
707

    
708
       for( ; vert<=end; vert++)
709
         {
710
         sb.append(' ');
711
         sb.append('(');
712
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
713
         sb.append(',');
714
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
715
         sb.append(')');
716
         }
717
       }
718

    
719
     Log.d("mesh", sb.toString());
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 int getTFO()
729
     {
730
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
731
     }
732

    
733
///////////////////////////////////////////////////////////////////////////////////////////////////
734
/**
735
 * Not part of public API, do not document (public only because has to be used from the main package)
736
 *
737
 * @y.exclude
738
 */
739
   public int getNumVertices()
740
     {
741
     return mNumVertices;
742
     }
743

    
744
///////////////////////////////////////////////////////////////////////////////////////////////////
745
/**
746
 * Not part of public API, do not document (public only because has to be used from the main package)
747
 *
748
 * @y.exclude
749
 */
750
   public void send(int programH, int variant)
751
     {
752
     if( !mStrideCorrected )
753
       {
754
       mStrideCorrected = true;
755
       mUBA.correctStride(mStride);
756
       }
757

    
758
     int indexA = mUBA.getIndex();
759
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
760
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
761

    
762
     if( mUseCenters )
763
       {
764
       int indexC = mUBC.getIndex();
765
       GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
766
       GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
767
       }
768
     }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771
/**
772
 * Not part of public API, do not document (public only because has to be used from the main package)
773
 *
774
 * @y.exclude
775
 */
776
   public void bindVertexAttribs(DistortedProgram program)
777
     {
778
     if( mJobNode[0]!=null )
779
       {
780
       mJobNode[0].execute();  // this will set itself to null
781
/*
782
       try
783
         {
784
         String name = "/sdcard/"+mNumVertices+".dmesh";
785
         DataOutputStream dos = new DataOutputStream(new java.io.FileOutputStream(name));
786
         write(dos);
787
         android.util.Log.e("mesh", "file written: "+name);
788
         }
789
       catch(java.io.FileNotFoundException ex)
790
         {
791
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
792
         }
793
 */
794
       }
795

    
796
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
797
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
798

    
799
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
800
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
801
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
802
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
803
     GLES30.glVertexAttribPointer(program.mAttribute[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
804
     GLES30.glVertexAttribPointer(program.mAttribute[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
805
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
806
     }
807

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810
 * Not part of public API, do not document (public only because has to be used from the main package)
811
 *
812
 * @y.exclude
813
 */
814
   public void bindTransformAttribs(DistortedProgram program)
815
     {
816
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
817

    
818
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
819
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
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 setInflate(float inflate)
830
     {
831
     mInflate = inflate;
832
     }
833

    
834
///////////////////////////////////////////////////////////////////////////////////////////////////
835
/**
836
 * Not part of public API, do not document (public only because has to be used from the main package)
837
 *
838
 * @y.exclude
839
 */
840
   public float getInflate()
841
     {
842
     return mInflate;
843
     }
844

    
845
///////////////////////////////////////////////////////////////////////////////////////////////////
846
// Return the number of bytes read.
847

    
848
   int read(DataInputStream stream)
849
     {
850
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
851
     int version, numEff, numTex;
852

    
853
     //////////////////////////////////////////////////////////////////////////////////////////
854
     // read version, number of vertices, number of tex components, number of effect components
855
     //////////////////////////////////////////////////////////////////////////////////////////
856

    
857
     try
858
       {
859
       stream.read(buffer);
860
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
861

    
862
       version = byteBuf.getInt(0);
863

    
864
       if( version==1 || version==2 )
865
         {
866
         mNumVertices= byteBuf.getInt(4);
867
         numTex      = byteBuf.getInt(8);
868
         numEff      = byteBuf.getInt(12);
869
         }
870
       else
871
         {
872
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
873
         return 0;
874
         }
875
       }
876
     catch(IOException e)
877
       {
878
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
879
       return 0;
880
       }
881

    
882
     //////////////////////////////////////////////////////////////////////////////////////////
883
     // read contents of all texture and effect components
884
     //////////////////////////////////////////////////////////////////////////////////////////
885

    
886
     if( mNumVertices>0 && numEff>0 && numTex>0 )
887
       {
888
       int size = numEff+TEX_COMP_SIZE*numTex;
889
       float[] tmp = new float[size];
890
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
891
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
892

    
893
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
894
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
895
       // ... and there was no center.
896
       int centerSize  = version==2 ? 3*numEff : 0;
897

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

    
900
       try
901
         {
902
         stream.read(buffer);
903
         }
904
       catch(IOException e)
905
         {
906
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
907
         return 0;
908
         }
909

    
910
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
911
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
912

    
913
       floatBuf.get(tmp,0,size);
914

    
915
       TexComponent tex;
916
       int index, texComp;
917
       float x, y, z, w;
918

    
919
       for(texComp=0; texComp<numTex; texComp++)
920
         {
921
         index = (int)tmp[TEX_COMP_SIZE*texComp];
922

    
923
         x= tmp[TEX_COMP_SIZE*texComp+1];
924
         y= tmp[TEX_COMP_SIZE*texComp+2];
925
         z= tmp[TEX_COMP_SIZE*texComp+3];
926
         w= tmp[TEX_COMP_SIZE*texComp+4];
927

    
928
         tex = new TexComponent(index);
929
         tex.setMap(new Static4D(x,y,z,w));
930

    
931
         mTexComponent.add(tex);
932
         }
933

    
934
       for(int effComp=0; effComp<numEff; effComp++)
935
         {
936
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
937
         mEffComponent.add(index);
938
         }
939

    
940
       //////////////////////////////////////////////////////////////////////////////////////////
941
       // read vert1 array, vert2 array and (if version>1) the component centers.
942
       //////////////////////////////////////////////////////////////////////////////////////////
943

    
944
       switch(version)
945
         {
946
         case 1 : index = floatBuf.position();
947

    
948
                  for(int vert=0; vert<mNumVertices; vert++)
949
                    {
950
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
951
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
952
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
953
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
954
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
955
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
956
                    index+=vert1InFile;
957
                    }
958
                  floatBuf.position(index);
959
                  break;
960
         case 2 : float[] centers = new float[3*numEff];
961
                  floatBuf.get(centers,0,3*numEff);
962

    
963
                  if( mUseCenters )
964
                    {
965
                    for(int eff=0; eff<numEff; eff++)
966
                      {
967
                      mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
968
                      }
969
                    }
970

    
971
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
972
                  break;
973
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
974
                  return 0;
975
         }
976

    
977
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
978
       }
979

    
980
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
981
     }
982

    
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984
/**
985
 * @y.exclude
986
 */
987
   public int getLastVertexEff(int effComponent)
988
     {
989
     if( effComponent>=0 && effComponent<mTexComponent.size() )
990
       {
991
       return mEffComponent.get(effComponent);
992
       }
993

    
994
     return 0;
995
     }
996

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

    
1008
     return 0;
1009
     }
1010

    
1011
///////////////////////////////////////////////////////////////////////////////////////////////////
1012
// PUBLIC API
1013
///////////////////////////////////////////////////////////////////////////////////////////////////
1014
/**
1015
 * Write the Mesh to a File.
1016
 */
1017
   public void write(DataOutputStream stream)
1018
     {
1019
     TexComponent tex;
1020

    
1021
     int numTex = mTexComponent.size();
1022
     int numEff = mEffComponent.size();
1023

    
1024
     try
1025
       {
1026
       stream.writeInt(2);  // version
1027
       stream.writeInt(mNumVertices);
1028
       stream.writeInt(numTex);
1029
       stream.writeInt(numEff);
1030

    
1031
       for(int i=0; i<numTex; i++)
1032
         {
1033
         tex = mTexComponent.get(i);
1034

    
1035
         stream.writeFloat((float)tex.mEndIndex);
1036
         stream.writeFloat(tex.mTextureMap.get0());
1037
         stream.writeFloat(tex.mTextureMap.get1());
1038
         stream.writeFloat(tex.mTextureMap.get2());
1039
         stream.writeFloat(tex.mTextureMap.get3());
1040
         }
1041

    
1042
       for(int i=0; i<numEff; i++)
1043
         {
1044
         stream.writeFloat((float)mEffComponent.get(i));
1045
         }
1046

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

    
1049
       for(int i=0; i<numEff; i++)
1050
         {
1051
         stream.writeFloat(centers[4*i  ]);
1052
         stream.writeFloat(centers[4*i+1]);
1053
         stream.writeFloat(centers[4*i+2]);
1054
         }
1055

    
1056
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
1057
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
1058
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
1059
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
1060

    
1061
       stream.write(vertBuf1.array());
1062
       stream.write(vertBuf2.array());
1063
       }
1064
     catch(IOException ex)
1065
       {
1066
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
1067
       }
1068
     }
1069

    
1070
///////////////////////////////////////////////////////////////////////////////////////////////////
1071
/**
1072
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1073
 * <p>
1074
 * Will work only on OpenGL ES >= 3.0 devices.
1075
 *
1076
 * @param show Controls if we render the Normal vectors or not.
1077
 */
1078
   public void setShowNormals(boolean show)
1079
     {
1080
     mShowNormals = show;
1081
     }
1082

    
1083
///////////////////////////////////////////////////////////////////////////////////////////////////
1084
/**
1085
 * When rendering this mesh, should we also draw the normal vectors?
1086
 *
1087
 * @return <i>true</i> if we do render normal vectors
1088
 */
1089
   public boolean getShowNormals()
1090
     {
1091
     return mShowNormals;
1092
     }
1093

    
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095
/**
1096
 * Merge all texture components of this Mesh into a single one.
1097
 */
1098
   public void mergeTexComponents()
1099
     {
1100
     if( mJobNode[0]==null )
1101
       {
1102
       mergeTexComponentsNow();
1103
       }
1104
     else
1105
       {
1106
       mJobNode[0] = DeferredJobs.mergeTex(this);
1107
       }
1108
     }
1109

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

    
1126
///////////////////////////////////////////////////////////////////////////////////////////////////
1127
/**
1128
 * Release all internal resources.
1129
 */
1130
   public void markForDeletion()
1131
     {
1132
     mVertAttribs1 = null;
1133
     mVertAttribs2 = null;
1134

    
1135
     mVBO1.markForDeletion();
1136
     mVBO2.markForDeletion();
1137
     mTFO.markForDeletion();
1138
     mUBA.markForDeletion();
1139

    
1140
     if( mUBC!=null )
1141
       {
1142
       mUBC.markForDeletion();
1143
       }
1144
     }
1145

    
1146
///////////////////////////////////////////////////////////////////////////////////////////////////
1147
/**
1148
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1149
 * <p>
1150
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1151
 * contains any Dynamics, they will be evaluated at 0.
1152
 *
1153
 * @param effect List of Matrix Effects to apply to the Mesh.
1154
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1155
 * @param equAssoc 'equality' association which defines which components will be affected.
1156
 */
1157
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1158
     {
1159
     if( mJobNode[0]==null )
1160
       {
1161
       applyMatrix(effect,andAssoc,equAssoc);
1162
       }
1163
     else
1164
       {
1165
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1166
       }
1167
     }
1168

    
1169
///////////////////////////////////////////////////////////////////////////////////////////////////
1170
/**
1171
 * Apply a Vertex Effect to the vertex mesh.
1172
 * <p>
1173
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1174
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1175
 *
1176
 * We would call this several times building up a list of Effects to do. This list of effects gets
1177
 * lazily executed only when the Mesh is used for rendering for the first time.
1178
 *
1179
 * @param effect Vertex Effect to apply to the Mesh.
1180
 */
1181
   public void apply(VertexEffect effect)
1182
     {
1183
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1184
     }
1185

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

    
1221
///////////////////////////////////////////////////////////////////////////////////////////////////
1222
/**
1223
 * Return the texture map of one of the components.
1224
 *
1225
 * @param component The component number whose texture map we want to retrieve.
1226
 */
1227
   public Static4D getTextureMap(int component)
1228
     {
1229
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1230
     }
1231

    
1232
///////////////////////////////////////////////////////////////////////////////////////////////////
1233
/**
1234
 * Set Effect association.
1235
 *
1236
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1237
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1238
 * will only be active on vertices of Components such that
1239
 *
1240
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1241
 *
1242
 * (see main_vertex_shader)
1243
 *
1244
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1245
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1246
 */
1247
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1248
     {
1249
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1250
       {
1251
       if( mJobNode[0]==null )
1252
         {
1253
         setEffectAssociationNow(component, andAssociation, equAssociation);
1254
         }
1255
       else
1256
         {
1257
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1258
         }
1259
       }
1260
     }
1261

    
1262
///////////////////////////////////////////////////////////////////////////////////////////////////
1263
/**
1264
 * Set center of a Component.
1265
 *
1266
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1267
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1268
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1269
 * add the 'halo' around an object.
1270
 *
1271
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1272
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1273
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1274
 */
1275
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1276
     {
1277
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1278
       {
1279
       if( mJobNode[0]==null )
1280
         {
1281
         setComponentCenterNow(component, centerX, centerY, centerZ);
1282
         }
1283
       else
1284
         {
1285
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1286
         }
1287
       }
1288
     }
1289

    
1290
///////////////////////////////////////////////////////////////////////////////////////////////////
1291
/**
1292
 * Adds an empty (no vertices) texture component to the end of the text component list.
1293
 *
1294
 * Sometimes we want to do this to have several Meshes with equal number of tex components each.
1295
 */
1296
   public void addEmptyTexComponent()
1297
     {
1298
      if( mJobNode[0]==null )
1299
       {
1300
       addEmptyTexComponentNow();
1301
       }
1302
     else
1303
       {
1304
       mJobNode[0] = DeferredJobs.addEmptyTex(this);
1305
       }
1306
     }
1307

    
1308
///////////////////////////////////////////////////////////////////////////////////////////////////
1309
/**
1310
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1311
 * which can be independently textured.
1312
 *
1313
 * @return The number of Texture Components of this Mesh.
1314
 */
1315
   public int getNumTexComponents()
1316
     {
1317
     return mTexComponent.size();
1318
     }
1319

    
1320
///////////////////////////////////////////////////////////////////////////////////////////////////
1321
/**
1322
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1323
 * to which a VertexEffect can be addressed, independently of other vertices.
1324
 *
1325
 * @return The number of Effect Components of this Mesh.
1326
 */
1327
   public int getNumEffComponents()
1328
     {
1329
     return mEffComponent.size();
1330
     }
1331

    
1332
///////////////////////////////////////////////////////////////////////////////////////////////////
1333
/**
1334
 * Copy the Mesh.
1335
 *
1336
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1337
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1338
 *             coordinates and effect associations, is always deep copied)
1339
 */
1340
   public abstract MeshBase copy(boolean deep);
1341
   }
(2-2/10)