Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 9f9924f8

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= 100;
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 UniformBlockAssociation mUBA;
87
   private UniformBlockCenter mUBC;
88

    
89
   DeferredJobs.JobNode[] mJobNode;
90

    
91
   private static int[] mCenterBlockIndex = new int[EffectQueue.MAIN_VARIANTS];
92
   private static 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 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
     mVBO1.invalidate();
184
     }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

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

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

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
   void mergeTexComponentsNow()
211
     {
212
     int num = mTexComponent.size();
213

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

    
219
       mVBO2.invalidate();
220
       }
221
     }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
   void mergeEffComponentsNow()
226
     {
227
     int num = mEffComponent.size();
228

    
229
     if( num>1 )
230
       {
231
       mEffComponent.clear();
232
       mEffComponent.add(mNumVertices-1);
233

    
234
       for(int index=0; index<mNumVertices; index++)
235
         {
236
         mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0;
237
         }
238

    
239
       mVBO2.invalidate();
240
       }
241
     }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
   void applyMatrix(MatrixEffect effect, int andAssoc, int equAssoc)
246
     {
247
     float[] matrixP  = new float[16];
248
     float[] matrixV  = new float[16];
249
     float[] uniforms = new float[7];
250
     int start, end=-1, numComp = mEffComponent.size();
251

    
252
     Matrix.setIdentityM(matrixP,0);
253
     Matrix.setIdentityM(matrixV,0);
254
     effect.compute(uniforms,0,0,0);
255
     effect.apply(matrixP, matrixV, uniforms, 0);
256

    
257
     for(int comp=0; comp<numComp; comp++)
258
       {
259
       start = end+1;
260
       end   = mEffComponent.get(comp);
261

    
262
       if( mUBA.matchesAssociation(comp, andAssoc, equAssoc) )
263
         {
264
         applyMatrixToComponent(matrixP, matrixV, start, end);
265
         }
266
       }
267

    
268
     mVBO1.invalidate();
269
     }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
   private void applyMatrixToComponent(float[] matrixP, float[] matrixV, int start, int end)
274
     {
275
     float x,y,z;
276

    
277
     for(int index=start*VERT1_ATTRIBS; index<=end*VERT1_ATTRIBS; index+=VERT1_ATTRIBS )
278
       {
279
       x = mVertAttribs1[index+POS_ATTRIB  ];
280
       y = mVertAttribs1[index+POS_ATTRIB+1];
281
       z = mVertAttribs1[index+POS_ATTRIB+2];
282

    
283
       mVertAttribs1[index+POS_ATTRIB  ] = matrixP[0]*x + matrixP[4]*y + matrixP[ 8]*z + matrixP[12];
284
       mVertAttribs1[index+POS_ATTRIB+1] = matrixP[1]*x + matrixP[5]*y + matrixP[ 9]*z + matrixP[13];
285
       mVertAttribs1[index+POS_ATTRIB+2] = matrixP[2]*x + matrixP[6]*y + matrixP[10]*z + matrixP[14];
286

    
287
       x = mVertAttribs1[index+NOR_ATTRIB  ];
288
       y = mVertAttribs1[index+NOR_ATTRIB+1];
289
       z = mVertAttribs1[index+NOR_ATTRIB+2];
290

    
291
       mVertAttribs1[index+NOR_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
292
       mVertAttribs1[index+NOR_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
293
       mVertAttribs1[index+NOR_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
294

    
295
       x = mVertAttribs1[index+NOR_ATTRIB  ];
296
       y = mVertAttribs1[index+NOR_ATTRIB+1];
297
       z = mVertAttribs1[index+NOR_ATTRIB+2];
298

    
299
       float len1 = (float)Math.sqrt(x*x + y*y + z*z);
300

    
301
       if( len1>0.0f )
302
         {
303
         mVertAttribs1[index+NOR_ATTRIB  ] /= len1;
304
         mVertAttribs1[index+NOR_ATTRIB+1] /= len1;
305
         mVertAttribs1[index+NOR_ATTRIB+2] /= len1;
306
         }
307
       }
308
     }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
313
     {
314
     mUBA.setEffectAssociationNow(component, andAssociation, equAssociation);
315
     }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
   void setComponentCenterNow(int component, float x, float y, float z)
320
     {
321
     mUBC.setEffectCenterNow(component, x, y, z);
322
     }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
// when a derived class is done computing its mesh, it has to call this method.
326

    
327
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
328
     {
329
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
330
     mVertAttribs1= vert1Attribs;
331
     mVertAttribs2= vert2Attribs;
332

    
333
     mTexComponent.add(new TexComponent(mNumVertices-1));
334
     mEffComponent.add(mNumVertices-1);
335

    
336
     mVBO1.invalidate();
337
     mVBO2.invalidate();
338
     }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
   void joinAttribs(MeshBase[] meshes)
343
     {
344
     MeshBase mesh;
345
     TexComponent comp;
346
     int numMeshes = meshes.length;
347
     int numVertices,origVertices = mNumVertices;
348
     int origTexComponents,numTexComponents;
349
     int origEffComponents=0,numEffComponents;
350

    
351
     if( origVertices>0 )
352
       {
353
       origTexComponents = mTexComponent.size();
354
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
355
       mTexComponent.get(origTexComponents-1).mEndIndex = mNumVertices-1;
356
       origEffComponents = mEffComponent.size();
357
       mEffComponent.set(origEffComponents-1,mNumVertices-1);
358
       }
359

    
360
     for(int i=0; i<numMeshes; i++)
361
       {
362
       mesh = meshes[i];
363
       numTexComponents = mesh.mTexComponent.size();
364
       numEffComponents = mesh.mEffComponent.size();
365
       numVertices = mesh.mNumVertices;
366

    
367
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
368
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
369

    
370
       for(int j=0; j<numTexComponents; j++)
371
         {
372
         comp = new TexComponent(mesh.mTexComponent.get(j));
373
         comp.mEndIndex += (extraVerticesBefore+mNumVertices);
374
         if( j==numTexComponents-1) comp.mEndIndex += extraVerticesAfter;
375
         mTexComponent.add(comp);
376
         }
377

    
378
       for(int j=0; j<numEffComponents; j++)
379
         {
380
         int index = mesh.mEffComponent.get(j);
381
         index += (extraVerticesBefore+mNumVertices);
382
         if( j==numEffComponents-1 ) index += extraVerticesAfter;
383
         mEffComponent.add(index);
384

    
385
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
386
           {
387
           mUBA.copy(origEffComponents, mesh.mUBA, j);
388
           mUBC.copy(origEffComponents, mesh.mUBC, j);
389
           origEffComponents++;
390
           }
391
         }
392

    
393
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
394
       }
395

    
396
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
397
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
398
     numVertices = origVertices;
399

    
400
     if( origVertices>0 )
401
       {
402
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                          0, VERT1_ATTRIBS*numVertices);
403
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS            );
404
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                          0, VERT2_ATTRIBS*numVertices);
405
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS            );
406
       origVertices++;
407

    
408
       if( numVertices%2==1 )
409
         {
410
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
411
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
412
         origVertices++;
413
         }
414
       }
415

    
416
     for(int i=0; i<numMeshes; i++)
417
       {
418
       mesh = meshes[i];
419
       numVertices = mesh.mNumVertices;
420

    
421
       if( origVertices>0 )
422
         {
423
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
424
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
425
         origVertices++;
426
         }
427
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
428
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
429
       origVertices+=numVertices;
430

    
431
       if( i<numMeshes-1 )
432
         {
433
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
434
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
435
         origVertices++;
436

    
437
         if( numVertices%2==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
         }
444
       }
445

    
446
     if( origVertices!=mNumVertices )
447
       {
448
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
449
       }
450

    
451
     int endIndex, index=0, numEffComp = mEffComponent.size();
452

    
453
     for(int component=0; component<numEffComp; component++)
454
       {
455
       endIndex = mEffComponent.get(component);
456

    
457
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
458
       }
459

    
460
     mVertAttribs1 = newAttribs1;
461
     mVertAttribs2 = newAttribs2;
462
     mVBO1.invalidate();
463
     mVBO2.invalidate();
464
     }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467
// called from MeshJoined
468

    
469
   void join(MeshBase[] meshes)
470
     {
471
     boolean immediateJoin = true;
472

    
473
     for (MeshBase mesh : meshes)
474
       {
475
       if (mesh.mJobNode[0] != null)
476
         {
477
         immediateJoin = false;
478
         break;
479
         }
480
       }
481

    
482
     if( immediateJoin ) joinAttribs(meshes);
483
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
484
     }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
   void textureMap(Static4D[] maps, int startComponent)
489
     {
490
     int num_comp = mTexComponent.size();
491
     int num_maps = maps.length;
492
     int min = Math.min(num_comp-startComponent, num_maps);
493
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
494
     Static4D newMap, oldMap;
495
     TexComponent comp;
496
     float newW, newH, ratW, ratH, movX, movY;
497

    
498
     for(int i=0; i<min; i++)
499
       {
500
       newMap = maps[i];
501
       comp = mTexComponent.get(i+startComponent);
502

    
503
       if( newMap!=null )
504
         {
505
         newW = newMap.get2();
506
         newH = newMap.get3();
507

    
508
         if( newW!=0.0f && newH!=0.0f )
509
           {
510
           oldMap = comp.mTextureMap;
511
           ratW = newW/oldMap.get2();
512
           ratH = newH/oldMap.get3();
513
           movX = newMap.get0() - ratW*oldMap.get0();
514
           movY = newMap.get1() - ratH*oldMap.get1();
515

    
516
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
517
             {
518
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
519
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
520
             }
521
           comp.setMap(newMap);
522
           }
523
         }
524

    
525
       vertex= comp.mEndIndex+1;
526
       }
527

    
528
     mVBO2.invalidate();
529
     }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
   public static int getMaxEffComponents()
534
     {
535
     return MAX_EFFECT_COMPONENTS;
536
     }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
   public static void getUniforms(int programH, int variant)
541
     {
542
     mCenterBlockIndex[variant]= GLES30.glGetUniformBlockIndex(programH, "componentCenter");
543
     mAssocBlockIndex[variant] = GLES30.glGetUniformBlockIndex(programH, "componentAssociation");
544
     }
545

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

    
568
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
569
     }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
/**
573
 * Not part of public API, do not document (public only because has to be used from the main package)
574
 *
575
 * @y.exclude
576
 */
577
   public void debug()
578
     {
579
     if( mJobNode[0]!=null )
580
       {
581
       mJobNode[0].print(0);
582
       }
583
     else
584
       {
585
       android.util.Log.e("mesh", "JobNode null");
586
       }
587
     }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590
/**
591
 * Not part of public API, do not document (public only because has to be used from the main package)
592
 *
593
 * @y.exclude
594
 */
595
   public void printPos()
596
     {
597
     StringBuilder sb = new StringBuilder();
598

    
599
     for(int i=0; i<mNumVertices; i++)
600
       {
601
       sb.append('(');
602
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
603
       sb.append(',');
604
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
605
       sb.append(',');
606
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
607
       sb.append(") ");
608
       }
609
     Log.d("mesh", sb.toString());
610
     }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
/**
614
 * Not part of public API, do not document (public only because has to be used from the main package)
615
 *
616
 * @y.exclude
617
 */
618
   public void printCom()
619
     {
620
     StringBuilder sb = new StringBuilder();
621

    
622
     for(int i=0; i<mNumVertices; i++)
623
       {
624
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
625
       sb.append(' ');
626
       }
627

    
628
     Log.d("mesh", sb.toString());
629
     }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
/**
633
 * Not part of public API, do not document (public only because has to be used from the main package)
634
 *
635
 * @y.exclude
636
 */
637
   public void printTex()
638
     {
639
     int vert=0;
640
     int num = numTexComponents();
641
     StringBuilder sb = new StringBuilder();
642
     sb.append("tex components: ").append(num);
643

    
644
     for(int i=0; i<num; i++)
645
       {
646
       int end = mTexComponent.get(i).mEndIndex;
647
       sb.append("\n");
648

    
649
       for( ; vert<=end; vert++)
650
         {
651
         sb.append(' ');
652
         sb.append('(');
653
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
654
         sb.append(',');
655
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
656
         sb.append(')');
657
         }
658
       }
659

    
660
     Log.d("mesh", sb.toString());
661
     }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664
/**
665
 * Not part of public API, do not document (public only because has to be used from the main package)
666
 *
667
 * @y.exclude
668
 */
669
   public int getTFO()
670
     {
671
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
672
     }
673

    
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
/**
676
 * Not part of public API, do not document (public only because has to be used from the main package)
677
 *
678
 * @y.exclude
679
 */
680
   public int getNumVertices()
681
     {
682
     return mNumVertices;
683
     }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686
/**
687
 * Not part of public API, do not document (public only because has to be used from the main package)
688
 *
689
 * @y.exclude
690
 */
691
   public void send(int programH, int variant)
692
     {
693
     int indexA = mUBA.getIndex();
694
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
695
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
696

    
697
     int indexC = mUBC.getIndex();
698
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
699
     GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
700
     }
701

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

    
728
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
729
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
730

    
731
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
732
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
733
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
734
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
735
     GLES30.glVertexAttribPointer(program.mAttribute[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
736
     GLES30.glVertexAttribPointer(program.mAttribute[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
737
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
738
     }
739

    
740
///////////////////////////////////////////////////////////////////////////////////////////////////
741
/**
742
 * Not part of public API, do not document (public only because has to be used from the main package)
743
 *
744
 * @y.exclude
745
 */
746
   public void bindTransformAttribs(DistortedProgram program)
747
     {
748
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
749

    
750
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
751
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
752
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
753
     }
754

    
755
///////////////////////////////////////////////////////////////////////////////////////////////////
756
/**
757
 * Not part of public API, do not document (public only because has to be used from the main package)
758
 *
759
 * @y.exclude
760
 */
761
   public void setInflate(float inflate)
762
     {
763
     mInflate = inflate;
764
     }
765

    
766
///////////////////////////////////////////////////////////////////////////////////////////////////
767
/**
768
 * Not part of public API, do not document (public only because has to be used from the main package)
769
 *
770
 * @y.exclude
771
 */
772
   public float getInflate()
773
     {
774
     return mInflate;
775
     }
776

    
777
///////////////////////////////////////////////////////////////////////////////////////////////////
778
// Return the number of bytes read.
779

    
780
   int read(DataInputStream stream)
781
     {
782
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
783
     int version, numEff, numTex;
784

    
785
     //////////////////////////////////////////////////////////////////////////////////////////
786
     // read version, number of vertices, number of tex components, number of effect components
787
     //////////////////////////////////////////////////////////////////////////////////////////
788

    
789
     try
790
       {
791
       stream.read(buffer);
792
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
793

    
794
       version = byteBuf.getInt(0);
795

    
796
       if( version==1 || version==2 )
797
         {
798
         mNumVertices= byteBuf.getInt(4);
799
         numTex      = byteBuf.getInt(8);
800
         numEff      = byteBuf.getInt(12);
801
         }
802
       else
803
         {
804
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
805
         return 0;
806
         }
807
       }
808
     catch(IOException e)
809
       {
810
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
811
       return 0;
812
       }
813

    
814
     //////////////////////////////////////////////////////////////////////////////////////////
815
     // read contents of all texture and effect components
816
     //////////////////////////////////////////////////////////////////////////////////////////
817

    
818
     if( mNumVertices>0 && numEff>0 && numTex>0 )
819
       {
820
       int size = numEff+TEX_COMP_SIZE*numTex;
821
       float[] tmp = new float[size];
822
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
823
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
824

    
825
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
826
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
827
       // ... and there was no center.
828
       int centerSize  = version==2 ? 3*numEff : 0;
829

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

    
832
       try
833
         {
834
         stream.read(buffer);
835
         }
836
       catch(IOException e)
837
         {
838
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
839
         return 0;
840
         }
841

    
842
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
843
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
844

    
845
       floatBuf.get(tmp,0,size);
846

    
847
       TexComponent tex;
848
       int index, texComp;
849
       float x, y, z, w;
850

    
851
       for(texComp=0; texComp<numTex; texComp++)
852
         {
853
         index = (int)tmp[TEX_COMP_SIZE*texComp];
854

    
855
         x= tmp[TEX_COMP_SIZE*texComp+1];
856
         y= tmp[TEX_COMP_SIZE*texComp+2];
857
         z= tmp[TEX_COMP_SIZE*texComp+3];
858
         w= tmp[TEX_COMP_SIZE*texComp+4];
859

    
860
         tex = new TexComponent(index);
861
         tex.setMap(new Static4D(x,y,z,w));
862

    
863
         mTexComponent.add(tex);
864
         }
865

    
866
       for(int effComp=0; effComp<numEff; effComp++)
867
         {
868
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
869
         mEffComponent.add(index);
870
         }
871

    
872
       //////////////////////////////////////////////////////////////////////////////////////////
873
       // read vert1 array, vert2 array and (if version>1) the component centers.
874
       //////////////////////////////////////////////////////////////////////////////////////////
875

    
876
       switch(version)
877
         {
878
         case 1 : index = floatBuf.position();
879

    
880
                  for(int vert=0; vert<mNumVertices; vert++)
881
                    {
882
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
883
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
884
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
885
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
886
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
887
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
888
                    index+=vert1InFile;
889
                    }
890
                  floatBuf.position(index);
891
                  break;
892
         case 2 : float[] centers = new float[3*numEff];
893
                  floatBuf.get(centers,0,3*numEff);
894

    
895
                  for(int eff=0; eff<numEff; eff++)
896
                    {
897
                    mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
898
                    }
899

    
900
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
901
                  break;
902
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
903
                  return 0;
904
         }
905

    
906
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
907
       }
908

    
909
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
910
     }
911

    
912
///////////////////////////////////////////////////////////////////////////////////////////////////
913
/**
914
 * @y.exclude
915
 */
916
   public int getLastVertexEff(int effComponent)
917
     {
918
     if( effComponent>=0 && effComponent<mTexComponent.size() )
919
       {
920
       return mEffComponent.get(effComponent);
921
       }
922

    
923
     return 0;
924
     }
925

    
926
///////////////////////////////////////////////////////////////////////////////////////////////////
927
/**
928
 * @y.exclude
929
 */
930
   public int getLastVertexTex(int texComponent)
931
     {
932
     if( texComponent>=0 && texComponent<mTexComponent.size() )
933
       {
934
       return mTexComponent.get(texComponent).mEndIndex;
935
       }
936

    
937
     return 0;
938
     }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941
// PUBLIC API
942
///////////////////////////////////////////////////////////////////////////////////////////////////
943
/**
944
 * Write the Mesh to a File.
945
 */
946
   public void write(DataOutputStream stream)
947
     {
948
     TexComponent tex;
949

    
950
     int numTex = mTexComponent.size();
951
     int numEff = mEffComponent.size();
952

    
953
     try
954
       {
955
       stream.writeInt(2);  // version
956
       stream.writeInt(mNumVertices);
957
       stream.writeInt(numTex);
958
       stream.writeInt(numEff);
959

    
960
       for(int i=0; i<numTex; i++)
961
         {
962
         tex = mTexComponent.get(i);
963

    
964
         stream.writeFloat((float)tex.mEndIndex);
965
         stream.writeFloat(tex.mTextureMap.get0());
966
         stream.writeFloat(tex.mTextureMap.get1());
967
         stream.writeFloat(tex.mTextureMap.get2());
968
         stream.writeFloat(tex.mTextureMap.get3());
969
         }
970

    
971
       for(int i=0; i<numEff; i++)
972
         {
973
         stream.writeFloat((float)mEffComponent.get(i));
974
         }
975

    
976
       float[] centers = mUBC.getBackingArray();
977

    
978
       for(int i=0; i<numEff; i++)
979
         {
980
         stream.writeFloat(centers[4*i  ]);
981
         stream.writeFloat(centers[4*i+1]);
982
         stream.writeFloat(centers[4*i+2]);
983
         }
984

    
985
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
986
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
987
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
988
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
989

    
990
       stream.write(vertBuf1.array());
991
       stream.write(vertBuf2.array());
992
       }
993
     catch(IOException ex)
994
       {
995
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
996
       }
997
     }
998

    
999
///////////////////////////////////////////////////////////////////////////////////////////////////
1000
/**
1001
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1002
 * <p>
1003
 * Will work only on OpenGL ES >= 3.0 devices.
1004
 *
1005
 * @param show Controls if we render the Normal vectors or not.
1006
 */
1007
   public void setShowNormals(boolean show)
1008
     {
1009
     mShowNormals = show;
1010
     }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013
/**
1014
 * When rendering this mesh, should we also draw the normal vectors?
1015
 *
1016
 * @return <i>true</i> if we do render normal vectors
1017
 */
1018
   public boolean getShowNormals()
1019
     {
1020
     return mShowNormals;
1021
     }
1022

    
1023
///////////////////////////////////////////////////////////////////////////////////////////////////
1024
/**
1025
 * Merge all texture components of this Mesh into a single one.
1026
 */
1027
   public void mergeTexComponents()
1028
     {
1029
     if( mJobNode[0]==null )
1030
       {
1031
       mergeTexComponentsNow();
1032
       }
1033
     else
1034
       {
1035
       mJobNode[0] = DeferredJobs.mergeTex(this);
1036
       }
1037
     }
1038

    
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040
/**
1041
 * Merge all effect components of this Mesh into a single one.
1042
 */
1043
   public void mergeEffComponents()
1044
     {
1045
     if( mJobNode[0]==null )
1046
       {
1047
       mergeEffComponentsNow();
1048
       }
1049
     else
1050
       {
1051
       mJobNode[0] = DeferredJobs.mergeEff(this);
1052
       }
1053
     }
1054

    
1055
///////////////////////////////////////////////////////////////////////////////////////////////////
1056
/**
1057
 * Release all internal resources.
1058
 */
1059
   public void markForDeletion()
1060
     {
1061
     mVertAttribs1 = null;
1062
     mVertAttribs2 = null;
1063

    
1064
     mVBO1.markForDeletion();
1065
     mVBO2.markForDeletion();
1066
     mTFO.markForDeletion();
1067
     }
1068

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

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

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

    
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145
/**
1146
 * Return the texture map of one of the components.
1147
 *
1148
 * @param component The component number whose texture map we want to retrieve.
1149
 */
1150
   public Static4D getTextureMap(int component)
1151
     {
1152
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1153
     }
1154

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

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

    
1213
///////////////////////////////////////////////////////////////////////////////////////////////////
1214
/**
1215
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1216
 * which can be independently textured.
1217
 *
1218
 * @return The number of Texture Components of this Mesh.
1219
 */
1220
   public int numTexComponents()
1221
     {
1222
     return mTexComponent.size();
1223
     }
1224

    
1225
///////////////////////////////////////////////////////////////////////////////////////////////////
1226
/**
1227
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1228
 * to which a VertexEffect can be addressed, independently of other vertices.
1229
 *
1230
 * @return The number of Effect Components of this Mesh.
1231
 */
1232
   public int numEffComponents()
1233
     {
1234
     return mEffComponent.size();
1235
     }
1236

    
1237
///////////////////////////////////////////////////////////////////////////////////////////////////
1238
/**
1239
 * Copy the Mesh.
1240
 *
1241
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1242
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1243
 *             coordinates and effect associations, is always deep copied)
1244
 */
1245
   public abstract MeshBase copy(boolean deep);
1246
   }
(2-2/11)