Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 87a48d8e

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 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

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
   void mergeEffComponentsNow()
234
     {
235
     int num = mEffComponent.size();
236

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

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

    
247
       mVBO2.invalidate();
248
       }
249
     }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

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

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

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

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

    
276
     mVBO1.invalidate();
277
     }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

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

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

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

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

    
303
       x = mVertAttribs1[index+NOR_ATTRIB  ];
304
       y = mVertAttribs1[index+NOR_ATTRIB+1];
305
       z = mVertAttribs1[index+NOR_ATTRIB+2];
306

    
307
       float len1 = (float)Math.sqrt(x*x + y*y + z*z);
308

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

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

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

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

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

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

    
341
     mTexComponent.add(new TexComponent(mNumVertices-1));
342
     mEffComponent.add(mNumVertices-1);
343

    
344
     mVBO1.invalidate();
345
     mVBO2.invalidate();
346
     }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

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

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

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

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

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

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

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

    
401
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
402
       }
403

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

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

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

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

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

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

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

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

    
459
     int endIndex, index=0, numEffComp = mEffComponent.size();
460

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

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

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

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
// called from MeshJoined
476

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

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

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

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

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

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

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

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

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

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

    
535
       vertex= comp.mEndIndex+1;
536
       }
537

    
538
     mVBO2.invalidate();
539
     }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

    
543
   public static int getMaxEffComponents()
544
     {
545
     return MAX_EFFECT_COMPONENTS;
546
     }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

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

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

    
578
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
579
     }
580

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

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

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

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

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

    
638
     Log.d("mesh", sb.toString());
639
     }
640

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

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

    
659
       for( ; vert<=end; vert++)
660
         {
661
         sb.append(' ');
662
         sb.append('(');
663
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
664
         sb.append(',');
665
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
666
         sb.append(')');
667
         }
668
       }
669

    
670
     Log.d("mesh", sb.toString());
671
     }
672

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

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

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

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

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

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

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

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

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

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

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

    
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788
// Return the number of bytes read.
789

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

    
795
     //////////////////////////////////////////////////////////////////////////////////////////
796
     // read version, number of vertices, number of tex components, number of effect components
797
     //////////////////////////////////////////////////////////////////////////////////////////
798

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

    
804
       version = byteBuf.getInt(0);
805

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

    
824
     //////////////////////////////////////////////////////////////////////////////////////////
825
     // read contents of all texture and effect components
826
     //////////////////////////////////////////////////////////////////////////////////////////
827

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

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

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

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

    
852
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
853
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
854

    
855
       floatBuf.get(tmp,0,size);
856

    
857
       TexComponent tex;
858
       int index, texComp;
859
       float x, y, z, w;
860

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

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

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

    
873
         mTexComponent.add(tex);
874
         }
875

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

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

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

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

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

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

    
916
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
917
       }
918

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

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

    
933
     return 0;
934
     }
935

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

    
947
     return 0;
948
     }
949

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

    
960
     int numTex = mTexComponent.size();
961
     int numEff = mEffComponent.size();
962

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

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

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

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

    
986
       float[] centers = mUBC.getBackingArray();
987

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

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

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

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

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

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

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

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

    
1074
     mVBO1.markForDeletion();
1075
     mVBO2.markForDeletion();
1076
     mTFO.markForDeletion();
1077
     }
1078

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

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

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

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

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

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

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

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

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

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