Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 78ff6ea9

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

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

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

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

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

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

    
69
   private static final int BYTES_PER_FLOAT = 4;
70

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

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

    
80
   private boolean mShowNormals;              // when rendering this mesh, draw normal vectors?
81
   private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
82
   private int mNumVertices;
83
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ
84
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
85
   private float mInflate;
86
   private final UniformBlockAssociation mUBA;
87
   private final UniformBlockCenter mUBC;
88

    
89
   DeferredJobs.JobNode[] mJobNode;
90

    
91
   private static final int[] mCenterBlockIndex = new int[EffectQueue.MAIN_VARIANTS];
92
   private static final int[] mAssocBlockIndex  = new int[EffectQueue.MAIN_VARIANTS];
93

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

    
96
   private static class TexComponent
97
     {
98
     private int mEndIndex;
99
     private final Static4D mTextureMap;
100

    
101
     TexComponent(int end)
102
       {
103
       mEndIndex  = end;
104
       mTextureMap= new Static4D(0,0,1,1);
105
       }
106
     TexComponent(TexComponent original)
107
       {
108
       mEndIndex = original.mEndIndex;
109

    
110
       float x = original.mTextureMap.get0();
111
       float y = original.mTextureMap.get1();
112
       float z = original.mTextureMap.get2();
113
       float w = original.mTextureMap.get3();
114
       mTextureMap = new Static4D(x,y,z,w);
115
       }
116

    
117
     void setMap(Static4D map)
118
       {
119
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
120
       }
121
     }
122

    
123
   private ArrayList<TexComponent> mTexComponent;
124
   private ArrayList<Integer> mEffComponent;
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
   MeshBase()
129
     {
130
     mShowNormals  = false;
131
     mInflate      = 0.0f;
132
     mTexComponent = new ArrayList<>();
133
     mEffComponent = new ArrayList<>();
134

    
135
     mJobNode = new DeferredJobs.JobNode[1];
136

    
137
     mUBA = new UniformBlockAssociation();
138
     mUBC = new UniformBlockCenter();
139

    
140
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
141
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
142
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
143
     }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// copy constructor
147

    
148
   MeshBase(MeshBase original, boolean deep)
149
     {
150
     mShowNormals= original.mShowNormals;
151
     mInflate    = original.mInflate;
152
     mNumVertices= original.mNumVertices;
153

    
154
     mUBA = new UniformBlockAssociation(original.mUBA);
155
     mUBC = new UniformBlockCenter(original.mUBC);
156

    
157
     if( deep )
158
       {
159
       mJobNode = new DeferredJobs.JobNode[1];
160
       if( original.mJobNode[0]==null ) copy(original);
161
       else mJobNode[0] = DeferredJobs.copy(this,original);
162
       }
163
     else
164
       {
165
       mJobNode      = original.mJobNode;
166
       mVBO1         = original.mVBO1;
167
       mVertAttribs1 = original.mVertAttribs1;
168
       shallowCopy(original);
169
       }
170

    
171
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
172
     }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
   void copy(MeshBase original)
177
     {
178
     shallowCopy(original);
179

    
180
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
181
     mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
182
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
183
     }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
   private void shallowCopy(MeshBase original)
188
     {
189
     int texComSize = original.mTexComponent.size();
190
     mTexComponent = new ArrayList<>();
191

    
192
     for(int i=0; i<texComSize; i++)
193
       {
194
       TexComponent comp = new TexComponent(original.mTexComponent.get(i));
195
       mTexComponent.add(comp);
196
       }
197

    
198
     mEffComponent = new ArrayList<>();
199
     mEffComponent.addAll(original.mEffComponent);
200

    
201
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
202
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
203
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
204
     }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
   void mergeTexComponentsNow()
209
     {
210
     int num = mTexComponent.size();
211

    
212
     if( num>1 )
213
       {
214
       mTexComponent.clear();
215
       mTexComponent.add(new TexComponent(mNumVertices-1));
216

    
217
       mVBO2.invalidate();
218
       }
219
     }
220

    
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
   void addEmptyTexComponentNow()
225
     {
226
     mTexComponent.add(new TexComponent(mNumVertices-1));
227
     }
228

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

    
231
   void mergeEffComponentsNow()
232
     {
233
     int num = mEffComponent.size();
234

    
235
     if( num>1 )
236
       {
237
       mEffComponent.clear();
238
       mEffComponent.add(mNumVertices-1);
239

    
240
       for(int index=0; index<mNumVertices; index++)
241
         {
242
         mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0;
243
         }
244

    
245
       mVBO2.invalidate();
246
       }
247
     }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
   void applyMatrix(MatrixEffect effect, int andAssoc, int equAssoc)
252
     {
253
     float[] matrixP  = new float[16];
254
     float[] matrixV  = new float[16];
255
     float[] uniforms = new float[7];
256
     int start, end=-1, numComp = mEffComponent.size();
257

    
258
     Matrix.setIdentityM(matrixP,0);
259
     Matrix.setIdentityM(matrixV,0);
260
     effect.compute(uniforms,0,0,0);
261
     effect.apply(matrixP, matrixV, uniforms, 0);
262

    
263
     for(int comp=0; comp<numComp; comp++)
264
       {
265
       start = end+1;
266
       end   = mEffComponent.get(comp);
267

    
268
       if( mUBA.matchesAssociation(comp, andAssoc, equAssoc) )
269
         {
270
         applyMatrixToComponent(matrixP, matrixV, start, end);
271
         }
272
       }
273

    
274
     mVBO1.invalidate();
275
     }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
   private void applyMatrixToComponent(float[] matrixP, float[] matrixV, int start, int end)
280
     {
281
     float x,y,z;
282

    
283
     for(int index=start*VERT1_ATTRIBS; index<=end*VERT1_ATTRIBS; index+=VERT1_ATTRIBS )
284
       {
285
       x = mVertAttribs1[index+POS_ATTRIB  ];
286
       y = mVertAttribs1[index+POS_ATTRIB+1];
287
       z = mVertAttribs1[index+POS_ATTRIB+2];
288

    
289
       mVertAttribs1[index+POS_ATTRIB  ] = matrixP[0]*x + matrixP[4]*y + matrixP[ 8]*z + matrixP[12];
290
       mVertAttribs1[index+POS_ATTRIB+1] = matrixP[1]*x + matrixP[5]*y + matrixP[ 9]*z + matrixP[13];
291
       mVertAttribs1[index+POS_ATTRIB+2] = matrixP[2]*x + matrixP[6]*y + matrixP[10]*z + matrixP[14];
292

    
293
       x = mVertAttribs1[index+NOR_ATTRIB  ];
294
       y = mVertAttribs1[index+NOR_ATTRIB+1];
295
       z = mVertAttribs1[index+NOR_ATTRIB+2];
296

    
297
       mVertAttribs1[index+NOR_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
298
       mVertAttribs1[index+NOR_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
299
       mVertAttribs1[index+NOR_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
300

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

    
305
       float len1 = (float)Math.sqrt(x*x + y*y + z*z);
306

    
307
       if( len1>0.0f )
308
         {
309
         mVertAttribs1[index+NOR_ATTRIB  ] /= len1;
310
         mVertAttribs1[index+NOR_ATTRIB+1] /= len1;
311
         mVertAttribs1[index+NOR_ATTRIB+2] /= len1;
312
         }
313
       }
314
     }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
319
     {
320
     mUBA.setEffectAssociationNow(component, andAssociation, equAssociation);
321
     }
322

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

    
325
   void setComponentCenterNow(int component, float x, float y, float z)
326
     {
327
     mUBC.setEffectCenterNow(component, x, y, z);
328
     }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// when a derived class is done computing its mesh, it has to call this method.
332

    
333
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
334
     {
335
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
336
     mVertAttribs1= vert1Attribs;
337
     mVertAttribs2= vert2Attribs;
338

    
339
     mTexComponent.add(new TexComponent(mNumVertices-1));
340
     mEffComponent.add(mNumVertices-1);
341

    
342
     mVBO1.invalidate();
343
     mVBO2.invalidate();
344
     }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
   void joinAttribs(MeshBase[] meshes)
349
     {
350
     MeshBase mesh;
351
     TexComponent comp;
352
     int numMeshes = meshes.length;
353
     int numVertices,origVertices = mNumVertices;
354
     int origTexComponents,numTexComponents;
355
     int origEffComponents=0,numEffComponents;
356

    
357
     if( origVertices>0 )
358
       {
359
       origTexComponents = mTexComponent.size();
360
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
361
       mTexComponent.get(origTexComponents-1).mEndIndex = mNumVertices-1;
362
       origEffComponents = mEffComponent.size();
363
       mEffComponent.set(origEffComponents-1,mNumVertices-1);
364
       }
365

    
366
     for(int i=0; i<numMeshes; i++)
367
       {
368
       mesh = meshes[i];
369
       numTexComponents = mesh.mTexComponent.size();
370
       numEffComponents = mesh.mEffComponent.size();
371
       numVertices = mesh.mNumVertices;
372

    
373
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
374
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
375

    
376
       for(int j=0; j<numTexComponents; j++)
377
         {
378
         comp = new TexComponent(mesh.mTexComponent.get(j));
379
         comp.mEndIndex += (extraVerticesBefore+mNumVertices);
380
         if( j==numTexComponents-1) comp.mEndIndex += extraVerticesAfter;
381
         mTexComponent.add(comp);
382
         }
383

    
384
       for(int j=0; j<numEffComponents; j++)
385
         {
386
         int index = mesh.mEffComponent.get(j);
387
         index += (extraVerticesBefore+mNumVertices);
388
         if( j==numEffComponents-1 ) index += extraVerticesAfter;
389
         mEffComponent.add(index);
390

    
391
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
392
           {
393
           mUBA.copy(origEffComponents, mesh.mUBA, j);
394
           mUBC.copy(origEffComponents, mesh.mUBC, j);
395
           origEffComponents++;
396
           }
397
         }
398

    
399
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
400
       }
401

    
402
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
403
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
404
     numVertices = origVertices;
405

    
406
     if( origVertices>0 )
407
       {
408
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                          0, VERT1_ATTRIBS*numVertices);
409
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS            );
410
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                          0, VERT2_ATTRIBS*numVertices);
411
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS            );
412
       origVertices++;
413

    
414
       if( numVertices%2==1 )
415
         {
416
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
417
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
418
         origVertices++;
419
         }
420
       }
421

    
422
     for(int i=0; i<numMeshes; i++)
423
       {
424
       mesh = meshes[i];
425
       numVertices = mesh.mNumVertices;
426

    
427
       if( origVertices>0 )
428
         {
429
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
430
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
431
         origVertices++;
432
         }
433
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
434
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
435
       origVertices+=numVertices;
436

    
437
       if( i<numMeshes-1 )
438
         {
439
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
440
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
441
         origVertices++;
442

    
443
         if( numVertices%2==1 )
444
           {
445
           System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
446
           System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
447
           origVertices++;
448
           }
449
         }
450
       }
451

    
452
     if( origVertices!=mNumVertices )
453
       {
454
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
455
       }
456

    
457
     int endIndex, index=0, numEffComp = mEffComponent.size();
458

    
459
     for(int component=0; component<numEffComp; component++)
460
       {
461
       endIndex = mEffComponent.get(component);
462

    
463
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
464
       }
465

    
466
     mVertAttribs1 = newAttribs1;
467
     mVertAttribs2 = newAttribs2;
468
     mVBO1.invalidate();
469
     mVBO2.invalidate();
470
     }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473
// called from MeshJoined
474

    
475
   void join(MeshBase[] meshes)
476
     {
477
     boolean immediateJoin = true;
478

    
479
     for (MeshBase mesh : meshes)
480
       {
481
       if (mesh.mJobNode[0] != null)
482
         {
483
         immediateJoin = false;
484
         break;
485
         }
486
       }
487

    
488
     if( immediateJoin ) joinAttribs(meshes);
489
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
490
     }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

    
494
   void textureMap(Static4D[] maps, int startComponent)
495
     {
496
     int num_comp = mTexComponent.size();
497
     if( startComponent>num_comp ) return;
498

    
499
     int num_maps = maps.length;
500
     int min = Math.min(num_comp-startComponent, num_maps);
501
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
502
     Static4D newMap, oldMap;
503
     TexComponent comp;
504
     float newW, newH, ratW, ratH, movX, movY;
505

    
506
     for(int i=0; i<min; i++)
507
       {
508
       newMap = maps[i];
509
       comp = mTexComponent.get(i+startComponent);
510

    
511
       if( newMap!=null )
512
         {
513
         newW = newMap.get2();
514
         newH = newMap.get3();
515

    
516
         if( newW!=0.0f && newH!=0.0f )
517
           {
518
           oldMap = comp.mTextureMap;
519
           ratW = newW/oldMap.get2();
520
           ratH = newH/oldMap.get3();
521
           movX = newMap.get0() - ratW*oldMap.get0();
522
           movY = newMap.get1() - ratH*oldMap.get1();
523

    
524
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
525
             {
526
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
527
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
528
             }
529
           comp.setMap(newMap);
530
           }
531
         }
532

    
533
       vertex= comp.mEndIndex+1;
534
       }
535

    
536
     mVBO2.invalidate();
537
     }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
   public static int getMaxEffComponents()
542
     {
543
     return MAX_EFFECT_COMPONENTS;
544
     }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547

    
548
   public static void getUniforms(int programH, int variant)
549
     {
550
     mCenterBlockIndex[variant]= GLES30.glGetUniformBlockIndex(programH, "componentCenter");
551
     mAssocBlockIndex[variant] = GLES30.glGetUniformBlockIndex(programH, "componentAssociation");
552
     }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555
/**
556
 * Not part of public API, do not document (public only because has to be used from the main package)
557
 *
558
 * @y.exclude
559
 */
560
   public void copyTransformToVertex()
561
     {
562
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
563
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
564
     if( buffer!=null )
565
       {
566
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
567
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
568
       mVBO1.updateFloat(mVertAttribs1);
569
       }
570
     else
571
       {
572
       int error = GLES30.glGetError();
573
       Log.e("mesh", "failed to map tf buffer, error="+error);
574
       }
575

    
576
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
577
     }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
/**
581
 * Not part of public API, do not document (public only because has to be used from the main package)
582
 *
583
 * @y.exclude
584
 */
585
   public void debug()
586
     {
587
     if( mJobNode[0]!=null )
588
       {
589
       mJobNode[0].print(0);
590
       }
591
     else
592
       {
593
       android.util.Log.e("mesh", "JobNode null");
594
       }
595
     }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
/**
599
 * Not part of public API, do not document (public only because has to be used from the main package)
600
 *
601
 * @y.exclude
602
 */
603
   public void printPos()
604
     {
605
     StringBuilder sb = new StringBuilder();
606

    
607
     for(int i=0; i<mNumVertices; i++)
608
       {
609
       sb.append('(');
610
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
611
       sb.append(',');
612
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
613
       sb.append(',');
614
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
615
       sb.append(") ");
616
       }
617
     Log.d("mesh", sb.toString());
618
     }
619

    
620
///////////////////////////////////////////////////////////////////////////////////////////////////
621
/**
622
 * Not part of public API, do not document (public only because has to be used from the main package)
623
 *
624
 * @y.exclude
625
 */
626
   public void printCom()
627
     {
628
     StringBuilder sb = new StringBuilder();
629

    
630
     for(int i=0; i<mNumVertices; i++)
631
       {
632
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
633
       sb.append(' ');
634
       }
635

    
636
     Log.d("mesh", sb.toString());
637
     }
638

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640
/**
641
 * Not part of public API, do not document (public only because has to be used from the main package)
642
 *
643
 * @y.exclude
644
 */
645
   public void printTex()
646
     {
647
     int vert=0;
648
     int num = numTexComponents();
649
     StringBuilder sb = new StringBuilder();
650
     sb.append("tex components: ").append(num);
651

    
652
     for(int i=0; i<num; i++)
653
       {
654
       int end = mTexComponent.get(i).mEndIndex;
655
       sb.append("\n");
656

    
657
       for( ; vert<=end; vert++)
658
         {
659
         sb.append(' ');
660
         sb.append('(');
661
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
662
         sb.append(',');
663
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
664
         sb.append(')');
665
         }
666
       }
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 int getTFO()
678
     {
679
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
680
     }
681

    
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683
/**
684
 * Not part of public API, do not document (public only because has to be used from the main package)
685
 *
686
 * @y.exclude
687
 */
688
   public int getNumVertices()
689
     {
690
     return mNumVertices;
691
     }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694
/**
695
 * Not part of public API, do not document (public only because has to be used from the main package)
696
 *
697
 * @y.exclude
698
 */
699
   public void send(int programH, int variant)
700
     {
701
     int indexA = mUBA.getIndex();
702
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
703
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
704

    
705
     int indexC = mUBC.getIndex();
706
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
707
     GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
708
     }
709

    
710
///////////////////////////////////////////////////////////////////////////////////////////////////
711
/**
712
 * Not part of public API, do not document (public only because has to be used from the main package)
713
 *
714
 * @y.exclude
715
 */
716
   public void bindVertexAttribs(DistortedProgram program)
717
     {
718
     if( mJobNode[0]!=null )
719
       {
720
       mJobNode[0].execute();  // this will set itself to null
721
/*
722
       try
723
         {
724
         String name = "/sdcard/"+mNumVertices+".dmesh";
725
         DataOutputStream dos = new DataOutputStream(new java.io.FileOutputStream(name));
726
         write(dos);
727
         android.util.Log.e("mesh", "file written: "+name);
728
         }
729
       catch(java.io.FileNotFoundException ex)
730
         {
731
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
732
         }
733
 */
734
       }
735

    
736
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
737
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
738

    
739
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
740
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
741
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
742
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
743
     GLES30.glVertexAttribPointer(program.mAttribute[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
744
     GLES30.glVertexAttribPointer(program.mAttribute[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
745
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
746
     }
747

    
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749
/**
750
 * Not part of public API, do not document (public only because has to be used from the main package)
751
 *
752
 * @y.exclude
753
 */
754
   public void bindTransformAttribs(DistortedProgram program)
755
     {
756
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
757

    
758
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
759
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
760
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
761
     }
762

    
763
///////////////////////////////////////////////////////////////////////////////////////////////////
764
/**
765
 * Not part of public API, do not document (public only because has to be used from the main package)
766
 *
767
 * @y.exclude
768
 */
769
   public void setInflate(float inflate)
770
     {
771
     mInflate = inflate;
772
     }
773

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

    
785
///////////////////////////////////////////////////////////////////////////////////////////////////
786
// Return the number of bytes read.
787

    
788
   int read(DataInputStream stream)
789
     {
790
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
791
     int version, numEff, numTex;
792

    
793
     //////////////////////////////////////////////////////////////////////////////////////////
794
     // read version, number of vertices, number of tex components, number of effect components
795
     //////////////////////////////////////////////////////////////////////////////////////////
796

    
797
     try
798
       {
799
       stream.read(buffer);
800
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
801

    
802
       version = byteBuf.getInt(0);
803

    
804
       if( version==1 || version==2 )
805
         {
806
         mNumVertices= byteBuf.getInt(4);
807
         numTex      = byteBuf.getInt(8);
808
         numEff      = byteBuf.getInt(12);
809
         }
810
       else
811
         {
812
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
813
         return 0;
814
         }
815
       }
816
     catch(IOException e)
817
       {
818
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
819
       return 0;
820
       }
821

    
822
     //////////////////////////////////////////////////////////////////////////////////////////
823
     // read contents of all texture and effect components
824
     //////////////////////////////////////////////////////////////////////////////////////////
825

    
826
     if( mNumVertices>0 && numEff>0 && numTex>0 )
827
       {
828
       int size = numEff+TEX_COMP_SIZE*numTex;
829
       float[] tmp = new float[size];
830
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
831
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
832

    
833
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
834
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
835
       // ... and there was no center.
836
       int centerSize  = version==2 ? 3*numEff : 0;
837

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

    
840
       try
841
         {
842
         stream.read(buffer);
843
         }
844
       catch(IOException e)
845
         {
846
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
847
         return 0;
848
         }
849

    
850
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
851
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
852

    
853
       floatBuf.get(tmp,0,size);
854

    
855
       TexComponent tex;
856
       int index, texComp;
857
       float x, y, z, w;
858

    
859
       for(texComp=0; texComp<numTex; texComp++)
860
         {
861
         index = (int)tmp[TEX_COMP_SIZE*texComp];
862

    
863
         x= tmp[TEX_COMP_SIZE*texComp+1];
864
         y= tmp[TEX_COMP_SIZE*texComp+2];
865
         z= tmp[TEX_COMP_SIZE*texComp+3];
866
         w= tmp[TEX_COMP_SIZE*texComp+4];
867

    
868
         tex = new TexComponent(index);
869
         tex.setMap(new Static4D(x,y,z,w));
870

    
871
         mTexComponent.add(tex);
872
         }
873

    
874
       for(int effComp=0; effComp<numEff; effComp++)
875
         {
876
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
877
         mEffComponent.add(index);
878
         }
879

    
880
       //////////////////////////////////////////////////////////////////////////////////////////
881
       // read vert1 array, vert2 array and (if version>1) the component centers.
882
       //////////////////////////////////////////////////////////////////////////////////////////
883

    
884
       switch(version)
885
         {
886
         case 1 : index = floatBuf.position();
887

    
888
                  for(int vert=0; vert<mNumVertices; vert++)
889
                    {
890
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
891
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
892
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
893
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
894
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
895
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
896
                    index+=vert1InFile;
897
                    }
898
                  floatBuf.position(index);
899
                  break;
900
         case 2 : float[] centers = new float[3*numEff];
901
                  floatBuf.get(centers,0,3*numEff);
902

    
903
                  for(int eff=0; eff<numEff; eff++)
904
                    {
905
                    mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
906
                    }
907

    
908
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
909
                  break;
910
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
911
                  return 0;
912
         }
913

    
914
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
915
       }
916

    
917
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
918
     }
919

    
920
///////////////////////////////////////////////////////////////////////////////////////////////////
921
/**
922
 * @y.exclude
923
 */
924
   public int getLastVertexEff(int effComponent)
925
     {
926
     if( effComponent>=0 && effComponent<mTexComponent.size() )
927
       {
928
       return mEffComponent.get(effComponent);
929
       }
930

    
931
     return 0;
932
     }
933

    
934
///////////////////////////////////////////////////////////////////////////////////////////////////
935
/**
936
 * @y.exclude
937
 */
938
   public int getLastVertexTex(int texComponent)
939
     {
940
     if( texComponent>=0 && texComponent<mTexComponent.size() )
941
       {
942
       return mTexComponent.get(texComponent).mEndIndex;
943
       }
944

    
945
     return 0;
946
     }
947

    
948
///////////////////////////////////////////////////////////////////////////////////////////////////
949
// PUBLIC API
950
///////////////////////////////////////////////////////////////////////////////////////////////////
951
/**
952
 * Write the Mesh to a File.
953
 */
954
   public void write(DataOutputStream stream)
955
     {
956
     TexComponent tex;
957

    
958
     int numTex = mTexComponent.size();
959
     int numEff = mEffComponent.size();
960

    
961
     try
962
       {
963
       stream.writeInt(2);  // version
964
       stream.writeInt(mNumVertices);
965
       stream.writeInt(numTex);
966
       stream.writeInt(numEff);
967

    
968
       for(int i=0; i<numTex; i++)
969
         {
970
         tex = mTexComponent.get(i);
971

    
972
         stream.writeFloat((float)tex.mEndIndex);
973
         stream.writeFloat(tex.mTextureMap.get0());
974
         stream.writeFloat(tex.mTextureMap.get1());
975
         stream.writeFloat(tex.mTextureMap.get2());
976
         stream.writeFloat(tex.mTextureMap.get3());
977
         }
978

    
979
       for(int i=0; i<numEff; i++)
980
         {
981
         stream.writeFloat((float)mEffComponent.get(i));
982
         }
983

    
984
       float[] centers = mUBC.getBackingArray();
985

    
986
       for(int i=0; i<numEff; i++)
987
         {
988
         stream.writeFloat(centers[4*i  ]);
989
         stream.writeFloat(centers[4*i+1]);
990
         stream.writeFloat(centers[4*i+2]);
991
         }
992

    
993
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
994
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
995
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
996
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
997

    
998
       stream.write(vertBuf1.array());
999
       stream.write(vertBuf2.array());
1000
       }
1001
     catch(IOException ex)
1002
       {
1003
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
1004
       }
1005
     }
1006

    
1007
///////////////////////////////////////////////////////////////////////////////////////////////////
1008
/**
1009
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1010
 * <p>
1011
 * Will work only on OpenGL ES >= 3.0 devices.
1012
 *
1013
 * @param show Controls if we render the Normal vectors or not.
1014
 */
1015
   public void setShowNormals(boolean show)
1016
     {
1017
     mShowNormals = show;
1018
     }
1019

    
1020
///////////////////////////////////////////////////////////////////////////////////////////////////
1021
/**
1022
 * When rendering this mesh, should we also draw the normal vectors?
1023
 *
1024
 * @return <i>true</i> if we do render normal vectors
1025
 */
1026
   public boolean getShowNormals()
1027
     {
1028
     return mShowNormals;
1029
     }
1030

    
1031
///////////////////////////////////////////////////////////////////////////////////////////////////
1032
/**
1033
 * Merge all texture components of this Mesh into a single one.
1034
 */
1035
   public void mergeTexComponents()
1036
     {
1037
     if( mJobNode[0]==null )
1038
       {
1039
       mergeTexComponentsNow();
1040
       }
1041
     else
1042
       {
1043
       mJobNode[0] = DeferredJobs.mergeTex(this);
1044
       }
1045
     }
1046

    
1047
///////////////////////////////////////////////////////////////////////////////////////////////////
1048
/**
1049
 * Merge all effect components of this Mesh into a single one.
1050
 */
1051
   public void mergeEffComponents()
1052
     {
1053
     if( mJobNode[0]==null )
1054
       {
1055
       mergeEffComponentsNow();
1056
       }
1057
     else
1058
       {
1059
       mJobNode[0] = DeferredJobs.mergeEff(this);
1060
       }
1061
     }
1062

    
1063
///////////////////////////////////////////////////////////////////////////////////////////////////
1064
/**
1065
 * Release all internal resources.
1066
 */
1067
   public void markForDeletion()
1068
     {
1069
     mVertAttribs1 = null;
1070
     mVertAttribs2 = null;
1071

    
1072
     mVBO1.markForDeletion();
1073
     mVBO2.markForDeletion();
1074
     mTFO.markForDeletion();
1075
     }
1076

    
1077
///////////////////////////////////////////////////////////////////////////////////////////////////
1078
/**
1079
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1080
 * <p>
1081
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1082
 * contains any Dynamics, they will be evaluated at 0.
1083
 *
1084
 * @param effect List of Matrix Effects to apply to the Mesh.
1085
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1086
 * @param equAssoc 'equality' association which defines which components will be affected.
1087
 */
1088
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1089
     {
1090
     if( mJobNode[0]==null )
1091
       {
1092
       applyMatrix(effect,andAssoc,equAssoc);
1093
       }
1094
     else
1095
       {
1096
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1097
       }
1098
     }
1099

    
1100
///////////////////////////////////////////////////////////////////////////////////////////////////
1101
/**
1102
 * Apply a Vertex Effect to the vertex mesh.
1103
 * <p>
1104
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1105
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1106
 *
1107
 * We would call this several times building up a list of Effects to do. This list of effects gets
1108
 * lazily executed only when the Mesh is used for rendering for the first time.
1109
 *
1110
 * @param effect Vertex Effect to apply to the Mesh.
1111
 */
1112
   public void apply(VertexEffect effect)
1113
     {
1114
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1115
     }
1116

    
1117
///////////////////////////////////////////////////////////////////////////////////////////////////
1118
/**
1119
 * Sets texture maps for (some of) the components of this mesh.
1120
 * <p>
1121
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1122
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1123
 * upper-left quadrant of the texture.
1124
 * <p>
1125
 * Probably the most common user case would be sending as many maps as there are components in this
1126
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1127
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1128
 * of the 2nd and 4rd one, call this with
1129
 * maps = new Static4D[4]
1130
 * maps[0] = null
1131
 * maps[1] = the map for the 2nd component
1132
 * maps[2] = null
1133
 * maps[3] = the map for the 4th component
1134
 *
1135
 * A map's width and height have to be non-zero (but can be negative!)
1136
 *
1137
 * @param maps            List of texture maps to apply to the texture's components.
1138
 * @param startComponent  the component the first of the maps refers to.
1139
 */
1140
   public void setTextureMap(Static4D[] maps, int startComponent)
1141
     {
1142
     if( mJobNode[0]==null )
1143
       {
1144
       textureMap(maps,startComponent);
1145
       }
1146
     else
1147
       {
1148
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1149
       }
1150
     }
1151

    
1152
///////////////////////////////////////////////////////////////////////////////////////////////////
1153
/**
1154
 * Return the texture map of one of the components.
1155
 *
1156
 * @param component The component number whose texture map we want to retrieve.
1157
 */
1158
   public Static4D getTextureMap(int component)
1159
     {
1160
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1161
     }
1162

    
1163
///////////////////////////////////////////////////////////////////////////////////////////////////
1164
/**
1165
 * Set Effect association.
1166
 *
1167
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1168
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1169
 * will only be active on vertices of Components such that
1170
 *
1171
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1172
 *
1173
 * (see main_vertex_shader)
1174
 *
1175
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1176
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1177
 */
1178
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1179
     {
1180
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1181
       {
1182
       if( mJobNode[0]==null )
1183
         {
1184
         setEffectAssociationNow(component, andAssociation, equAssociation);
1185
         }
1186
       else
1187
         {
1188
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1189
         }
1190
       }
1191
     }
1192

    
1193
///////////////////////////////////////////////////////////////////////////////////////////////////
1194
/**
1195
 * Set center of a Component.
1196
 *
1197
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1198
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1199
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1200
 * add the 'halo' around an object.
1201
 *
1202
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1203
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1204
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1205
 */
1206
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1207
     {
1208
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1209
       {
1210
       if( mJobNode[0]==null )
1211
         {
1212
         setComponentCenterNow(component, centerX, centerY, centerZ);
1213
         }
1214
       else
1215
         {
1216
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1217
         }
1218
       }
1219
     }
1220

    
1221
///////////////////////////////////////////////////////////////////////////////////////////////////
1222
/**
1223
 * Adds an empty (no vertices) texture component to the end of the text component list.
1224
 *
1225
 * Sometimes we want to do this to have several Meshes with equal number of tex components each.
1226
 */
1227
   public void addEmptyTexComponent()
1228
     {
1229
      if( mJobNode[0]==null )
1230
       {
1231
       addEmptyTexComponentNow();
1232
       }
1233
     else
1234
       {
1235
       mJobNode[0] = DeferredJobs.addEmptyTex(this);
1236
       }
1237
     }
1238

    
1239
///////////////////////////////////////////////////////////////////////////////////////////////////
1240
/**
1241
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1242
 * which can be independently textured.
1243
 *
1244
 * @return The number of Texture Components of this Mesh.
1245
 */
1246
   public int numTexComponents()
1247
     {
1248
     return mTexComponent.size();
1249
     }
1250

    
1251
///////////////////////////////////////////////////////////////////////////////////////////////////
1252
/**
1253
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1254
 * to which a VertexEffect can be addressed, independently of other vertices.
1255
 *
1256
 * @return The number of Effect Components of this Mesh.
1257
 */
1258
   public int numEffComponents()
1259
     {
1260
     return mEffComponent.size();
1261
     }
1262

    
1263
///////////////////////////////////////////////////////////////////////////////////////////////////
1264
/**
1265
 * Copy the Mesh.
1266
 *
1267
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1268
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1269
 *             coordinates and effect associations, is always deep copied)
1270
 */
1271
   public abstract MeshBase copy(boolean deep);
1272
   }
(2-2/12)