Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 6c3a8db8

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

    
20
package org.distorted.library.mesh;
21

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

    
26
import org.distorted.library.effect.MatrixEffect;
27
import org.distorted.library.effect.VertexEffect;
28
import org.distorted.library.effectqueue.EffectQueue;
29
import org.distorted.library.main.InternalBuffer;
30
import org.distorted.library.program.DistortedProgram;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.library.uniformblock.UniformBlockAssociation;
33
import org.distorted.library.uniformblock.UniformBlockCenter;
34

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

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

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

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

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

    
71
   private static final int BYTES_PER_FLOAT = 4;
72

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

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

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

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

    
87
   private static boolean mUseCenters;
88

    
89
   private boolean mShowNormals;              // when rendering this mesh, draw normal vectors?
90
   private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
91
   private int mNumVertices;
92
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ
93
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
94
   private float mInflate;
95
   private final UniformBlockAssociation mUBA;
96
   private UniformBlockCenter mUBC;
97

    
98
   DeferredJobs.JobNode[] mJobNode;
99

    
100
   private static class TexComponent
101
     {
102
     private int mEndIndex;
103
     private final Static4D mTextureMap;
104

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

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

    
121
     void setMap(Static4D map)
122
       {
123
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
124
       }
125
     }
126

    
127
   private ArrayList<TexComponent> mTexComponent;
128
   private ArrayList<Integer> mEffComponent;
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
   MeshBase()
133
     {
134
     mShowNormals  = false;
135
     mInflate      = 0.0f;
136
     mTexComponent = new ArrayList<>();
137
     mEffComponent = new ArrayList<>();
138

    
139
     mJobNode = new DeferredJobs.JobNode[1];
140

    
141
     mUBA = new UniformBlockAssociation();
142

    
143
     if( mUseCenters ) mUBC = new UniformBlockCenter();
144

    
145
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
146
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
147
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
148
     }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// copy constructor
152

    
153
   MeshBase(MeshBase original, boolean deep)
154
     {
155
     mShowNormals= original.mShowNormals;
156
     mInflate    = original.mInflate;
157
     mNumVertices= original.mNumVertices;
158

    
159
     mUBA = new UniformBlockAssociation(original.mUBA);
160

    
161
     if( mUseCenters ) mUBC = new UniformBlockCenter(original.mUBC);
162

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

    
177
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
178
     }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
   void copy(MeshBase original)
183
     {
184
     shallowCopy(original);
185

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
   private void shallowCopy(MeshBase original)
194
     {
195
     int texComSize = original.mTexComponent.size();
196
     mTexComponent = new ArrayList<>();
197

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

    
204
     mEffComponent = new ArrayList<>();
205
     mEffComponent.addAll(original.mEffComponent);
206

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
   void mergeTexComponentsNow()
215
     {
216
     int num = mTexComponent.size();
217

    
218
     if( num>1 )
219
       {
220
       mTexComponent.clear();
221
       mTexComponent.add(new TexComponent(mNumVertices-1));
222

    
223
       mVBO2.invalidate();
224
       }
225
     }
226

    
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
   void mergeEffComponentsNow()
238
     {
239
     int num = mEffComponent.size();
240

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

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

    
251
       mVBO2.invalidate();
252
       }
253
     }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
264
     Matrix.setIdentityM(matrixP,0);
265
     Matrix.setIdentityM(matrixV,0);
266
     effect.compute(uniforms,0,0,0);
267
     effect.apply(matrixP, matrixV, uniforms, 0);
268

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

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

    
280
     mVBO1.invalidate();
281
     }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

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

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

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

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

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

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

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

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

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

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

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

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

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

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

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482
// called from MeshJoined
483

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

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

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

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

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

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

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

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

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

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

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

    
545
     mVBO2.invalidate();
546
     }
547

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

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

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568

    
569
   public static int getMaxEffComponents()
570
     {
571
     return MAX_EFFECT_COMPONENTS;
572
     }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

    
576
   public static void getUniforms(int programH, int variant)
577
     {
578
     mCenterBlockIndex[variant]= GLES30.glGetUniformBlockIndex(programH, "componentCenter");
579
     mAssocBlockIndex[variant] = GLES30.glGetUniformBlockIndex(programH, "componentAssociation");
580
     }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
/**
584
 * Not part of public API, do not document (public only because has to be used from the main package)
585
 *
586
 * @y.exclude
587
 */
588
   public void copyTransformToVertex()
589
     {
590
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
591
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
592
     if( buffer!=null )
593
       {
594
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
595
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
596
       mVBO1.updateFloat(mVertAttribs1);
597
       }
598
     else
599
       {
600
       int error = GLES30.glGetError();
601
       Log.e("mesh", "failed to map tf buffer, error="+error);
602
       }
603

    
604
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
605
     }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608
/**
609
 * Not part of public API, do not document (public only because has to be used from the main package)
610
 *
611
 * @y.exclude
612
 */
613
   public void debug()
614
     {
615
     if( mJobNode[0]!=null )
616
       {
617
       mJobNode[0].print(0);
618
       }
619
     else
620
       {
621
       android.util.Log.e("mesh", "JobNode null");
622
       }
623
     }
624

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

    
635
     for(int i=0; i<mNumVertices; i++)
636
       {
637
       sb.append('(');
638
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
639
       sb.append(',');
640
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
641
       sb.append(',');
642
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
643
       sb.append(") ");
644
       }
645
     Log.d("mesh", sb.toString());
646
     }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
/**
650
 * Not part of public API, do not document (public only because has to be used from the main package)
651
 *
652
 * @y.exclude
653
 */
654
   public void printCom()
655
     {
656
     StringBuilder sb = new StringBuilder();
657

    
658
     for(int i=0; i<mNumVertices; i++)
659
       {
660
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
661
       sb.append(' ');
662
       }
663

    
664
     Log.d("mesh", sb.toString());
665
     }
666

    
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668
/**
669
 * Not part of public API, do not document (public only because has to be used from the main package)
670
 *
671
 * @y.exclude
672
 */
673
   public void printTex()
674
     {
675
     int vert=0;
676
     int num = numTexComponents();
677
     StringBuilder sb = new StringBuilder();
678
     sb.append("tex components: ").append(num);
679

    
680
     for(int i=0; i<num; i++)
681
       {
682
       int end = mTexComponent.get(i).mEndIndex;
683
       sb.append("\n");
684

    
685
       for( ; vert<=end; vert++)
686
         {
687
         sb.append(' ');
688
         sb.append('(');
689
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
690
         sb.append(',');
691
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
692
         sb.append(')');
693
         }
694
       }
695

    
696
     Log.d("mesh", sb.toString());
697
     }
698

    
699
///////////////////////////////////////////////////////////////////////////////////////////////////
700
/**
701
 * Not part of public API, do not document (public only because has to be used from the main package)
702
 *
703
 * @y.exclude
704
 */
705
   public int getTFO()
706
     {
707
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
708
     }
709

    
710
///////////////////////////////////////////////////////////////////////////////////////////////////
711
/**
712
 * Not part of public API, do not document (public only because has to be used from the main package)
713
 *
714
 * @y.exclude
715
 */
716
   public int getNumVertices()
717
     {
718
     return mNumVertices;
719
     }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722
/**
723
 * Not part of public API, do not document (public only because has to be used from the main package)
724
 *
725
 * @y.exclude
726
 */
727
   public void send(int programH, int variant)
728
     {
729
     int indexA = mUBA.getIndex();
730
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
731
     GLES30.glUniformBlockBinding(programH, mAssocBlockIndex[variant], ASSOC_UBO_BINDING);
732

    
733
     if( mUseCenters )
734
       {
735
       int indexC = mUBC.getIndex();
736
       GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
737
       GLES30.glUniformBlockBinding(programH, mCenterBlockIndex[variant], CENTER_UBO_BINDING);
738
       }
739
     }
740

    
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742
/**
743
 * Not part of public API, do not document (public only because has to be used from the main package)
744
 *
745
 * @y.exclude
746
 */
747
   public void bindVertexAttribs(DistortedProgram program)
748
     {
749
     if( mJobNode[0]!=null )
750
       {
751
       mJobNode[0].execute();  // this will set itself to null
752
/*
753
       try
754
         {
755
         String name = "/sdcard/"+mNumVertices+".dmesh";
756
         DataOutputStream dos = new DataOutputStream(new java.io.FileOutputStream(name));
757
         write(dos);
758
         android.util.Log.e("mesh", "file written: "+name);
759
         }
760
       catch(java.io.FileNotFoundException ex)
761
         {
762
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
763
         }
764
 */
765
       }
766

    
767
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
768
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
769

    
770
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
771
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
772
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
773
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
774
     GLES30.glVertexAttribPointer(program.mAttribute[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
775
     GLES30.glVertexAttribPointer(program.mAttribute[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
776
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
777
     }
778

    
779
///////////////////////////////////////////////////////////////////////////////////////////////////
780
/**
781
 * Not part of public API, do not document (public only because has to be used from the main package)
782
 *
783
 * @y.exclude
784
 */
785
   public void bindTransformAttribs(DistortedProgram program)
786
     {
787
     int index = mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
788

    
789
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
790
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
791
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
792
     }
793

    
794
///////////////////////////////////////////////////////////////////////////////////////////////////
795
/**
796
 * Not part of public API, do not document (public only because has to be used from the main package)
797
 *
798
 * @y.exclude
799
 */
800
   public void setInflate(float inflate)
801
     {
802
     mInflate = inflate;
803
     }
804

    
805
///////////////////////////////////////////////////////////////////////////////////////////////////
806
/**
807
 * Not part of public API, do not document (public only because has to be used from the main package)
808
 *
809
 * @y.exclude
810
 */
811
   public float getInflate()
812
     {
813
     return mInflate;
814
     }
815

    
816
///////////////////////////////////////////////////////////////////////////////////////////////////
817
// Return the number of bytes read.
818

    
819
   int read(DataInputStream stream)
820
     {
821
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
822
     int version, numEff, numTex;
823

    
824
     //////////////////////////////////////////////////////////////////////////////////////////
825
     // read version, number of vertices, number of tex components, number of effect components
826
     //////////////////////////////////////////////////////////////////////////////////////////
827

    
828
     try
829
       {
830
       stream.read(buffer);
831
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
832

    
833
       version = byteBuf.getInt(0);
834

    
835
       if( version==1 || version==2 )
836
         {
837
         mNumVertices= byteBuf.getInt(4);
838
         numTex      = byteBuf.getInt(8);
839
         numEff      = byteBuf.getInt(12);
840
         }
841
       else
842
         {
843
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
844
         return 0;
845
         }
846
       }
847
     catch(IOException e)
848
       {
849
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
850
       return 0;
851
       }
852

    
853
     //////////////////////////////////////////////////////////////////////////////////////////
854
     // read contents of all texture and effect components
855
     //////////////////////////////////////////////////////////////////////////////////////////
856

    
857
     if( mNumVertices>0 && numEff>0 && numTex>0 )
858
       {
859
       int size = numEff+TEX_COMP_SIZE*numTex;
860
       float[] tmp = new float[size];
861
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
862
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
863

    
864
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
865
       int vert1InFile = version==2 ? VERT1_ATTRIBS : VERT1_ATTRIBS+3;
866
       // ... and there was no center.
867
       int centerSize  = version==2 ? 3*numEff : 0;
868

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

    
871
       try
872
         {
873
         stream.read(buffer);
874
         }
875
       catch(IOException e)
876
         {
877
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
878
         return 0;
879
         }
880

    
881
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
882
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
883

    
884
       floatBuf.get(tmp,0,size);
885

    
886
       TexComponent tex;
887
       int index, texComp;
888
       float x, y, z, w;
889

    
890
       for(texComp=0; texComp<numTex; texComp++)
891
         {
892
         index = (int)tmp[TEX_COMP_SIZE*texComp];
893

    
894
         x= tmp[TEX_COMP_SIZE*texComp+1];
895
         y= tmp[TEX_COMP_SIZE*texComp+2];
896
         z= tmp[TEX_COMP_SIZE*texComp+3];
897
         w= tmp[TEX_COMP_SIZE*texComp+4];
898

    
899
         tex = new TexComponent(index);
900
         tex.setMap(new Static4D(x,y,z,w));
901

    
902
         mTexComponent.add(tex);
903
         }
904

    
905
       for(int effComp=0; effComp<numEff; effComp++)
906
         {
907
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
908
         mEffComponent.add(index);
909
         }
910

    
911
       //////////////////////////////////////////////////////////////////////////////////////////
912
       // read vert1 array, vert2 array and (if version>1) the component centers.
913
       //////////////////////////////////////////////////////////////////////////////////////////
914

    
915
       switch(version)
916
         {
917
         case 1 : index = floatBuf.position();
918

    
919
                  for(int vert=0; vert<mNumVertices; vert++)
920
                    {
921
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
922
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
923
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
924
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
925
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
926
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
927
                    index+=vert1InFile;
928
                    }
929
                  floatBuf.position(index);
930
                  break;
931
         case 2 : float[] centers = new float[3*numEff];
932
                  floatBuf.get(centers,0,3*numEff);
933

    
934
                  if( mUseCenters )
935
                    {
936
                    for(int eff=0; eff<numEff; eff++)
937
                      {
938
                      mUBC.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2]);
939
                      }
940
                    }
941

    
942
                  floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
943
                  break;
944
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
945
                  return 0;
946
         }
947

    
948
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
949
       }
950

    
951
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
952
     }
953

    
954
///////////////////////////////////////////////////////////////////////////////////////////////////
955
/**
956
 * @y.exclude
957
 */
958
   public int getLastVertexEff(int effComponent)
959
     {
960
     if( effComponent>=0 && effComponent<mTexComponent.size() )
961
       {
962
       return mEffComponent.get(effComponent);
963
       }
964

    
965
     return 0;
966
     }
967

    
968
///////////////////////////////////////////////////////////////////////////////////////////////////
969
/**
970
 * @y.exclude
971
 */
972
   public int getLastVertexTex(int texComponent)
973
     {
974
     if( texComponent>=0 && texComponent<mTexComponent.size() )
975
       {
976
       return mTexComponent.get(texComponent).mEndIndex;
977
       }
978

    
979
     return 0;
980
     }
981

    
982
///////////////////////////////////////////////////////////////////////////////////////////////////
983
// PUBLIC API
984
///////////////////////////////////////////////////////////////////////////////////////////////////
985
/**
986
 * Write the Mesh to a File.
987
 */
988
   public void write(DataOutputStream stream)
989
     {
990
     TexComponent tex;
991

    
992
     int numTex = mTexComponent.size();
993
     int numEff = mEffComponent.size();
994

    
995
     try
996
       {
997
       stream.writeInt(2);  // version
998
       stream.writeInt(mNumVertices);
999
       stream.writeInt(numTex);
1000
       stream.writeInt(numEff);
1001

    
1002
       for(int i=0; i<numTex; i++)
1003
         {
1004
         tex = mTexComponent.get(i);
1005

    
1006
         stream.writeFloat((float)tex.mEndIndex);
1007
         stream.writeFloat(tex.mTextureMap.get0());
1008
         stream.writeFloat(tex.mTextureMap.get1());
1009
         stream.writeFloat(tex.mTextureMap.get2());
1010
         stream.writeFloat(tex.mTextureMap.get3());
1011
         }
1012

    
1013
       for(int i=0; i<numEff; i++)
1014
         {
1015
         stream.writeFloat((float)mEffComponent.get(i));
1016
         }
1017

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

    
1020
       for(int i=0; i<numEff; i++)
1021
         {
1022
         stream.writeFloat(centers[4*i  ]);
1023
         stream.writeFloat(centers[4*i+1]);
1024
         stream.writeFloat(centers[4*i+2]);
1025
         }
1026

    
1027
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
1028
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
1029
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
1030
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
1031

    
1032
       stream.write(vertBuf1.array());
1033
       stream.write(vertBuf2.array());
1034
       }
1035
     catch(IOException ex)
1036
       {
1037
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
1038
       }
1039
     }
1040

    
1041
///////////////////////////////////////////////////////////////////////////////////////////////////
1042
/**
1043
 * When rendering this Mesh, do we want to render the Normal vectors as well?
1044
 * <p>
1045
 * Will work only on OpenGL ES >= 3.0 devices.
1046
 *
1047
 * @param show Controls if we render the Normal vectors or not.
1048
 */
1049
   public void setShowNormals(boolean show)
1050
     {
1051
     mShowNormals = show;
1052
     }
1053

    
1054
///////////////////////////////////////////////////////////////////////////////////////////////////
1055
/**
1056
 * When rendering this mesh, should we also draw the normal vectors?
1057
 *
1058
 * @return <i>true</i> if we do render normal vectors
1059
 */
1060
   public boolean getShowNormals()
1061
     {
1062
     return mShowNormals;
1063
     }
1064

    
1065
///////////////////////////////////////////////////////////////////////////////////////////////////
1066
/**
1067
 * Merge all texture components of this Mesh into a single one.
1068
 */
1069
   public void mergeTexComponents()
1070
     {
1071
     if( mJobNode[0]==null )
1072
       {
1073
       mergeTexComponentsNow();
1074
       }
1075
     else
1076
       {
1077
       mJobNode[0] = DeferredJobs.mergeTex(this);
1078
       }
1079
     }
1080

    
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
/**
1083
 * Merge all effect components of this Mesh into a single one.
1084
 */
1085
   public void mergeEffComponents()
1086
     {
1087
     if( mJobNode[0]==null )
1088
       {
1089
       mergeEffComponentsNow();
1090
       }
1091
     else
1092
       {
1093
       mJobNode[0] = DeferredJobs.mergeEff(this);
1094
       }
1095
     }
1096

    
1097
///////////////////////////////////////////////////////////////////////////////////////////////////
1098
/**
1099
 * Release all internal resources.
1100
 */
1101
   public void markForDeletion()
1102
     {
1103
     mVertAttribs1 = null;
1104
     mVertAttribs2 = null;
1105

    
1106
     mVBO1.markForDeletion();
1107
     mVBO2.markForDeletion();
1108
     mTFO.markForDeletion();
1109
     mUBA.markForDeletion();
1110

    
1111
     if( mUBC!=null )
1112
       {
1113
       mUBC.markForDeletion();
1114
       }
1115
     }
1116

    
1117
///////////////////////////////////////////////////////////////////////////////////////////////////
1118
/**
1119
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1120
 * <p>
1121
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1122
 * contains any Dynamics, they will be evaluated at 0.
1123
 *
1124
 * @param effect List of Matrix Effects to apply to the Mesh.
1125
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1126
 * @param equAssoc 'equality' association which defines which components will be affected.
1127
 */
1128
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1129
     {
1130
     if( mJobNode[0]==null )
1131
       {
1132
       applyMatrix(effect,andAssoc,equAssoc);
1133
       }
1134
     else
1135
       {
1136
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1137
       }
1138
     }
1139

    
1140
///////////////////////////////////////////////////////////////////////////////////////////////////
1141
/**
1142
 * Apply a Vertex Effect to the vertex mesh.
1143
 * <p>
1144
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1145
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1146
 *
1147
 * We would call this several times building up a list of Effects to do. This list of effects gets
1148
 * lazily executed only when the Mesh is used for rendering for the first time.
1149
 *
1150
 * @param effect Vertex Effect to apply to the Mesh.
1151
 */
1152
   public void apply(VertexEffect effect)
1153
     {
1154
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1155
     }
1156

    
1157
///////////////////////////////////////////////////////////////////////////////////////////////////
1158
/**
1159
 * Sets texture maps for (some of) the components of this mesh.
1160
 * <p>
1161
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1162
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1163
 * upper-left quadrant of the texture.
1164
 * <p>
1165
 * Probably the most common user case would be sending as many maps as there are components in this
1166
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1167
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1168
 * of the 2nd and 4rd one, call this with
1169
 * maps = new Static4D[4]
1170
 * maps[0] = null
1171
 * maps[1] = the map for the 2nd component
1172
 * maps[2] = null
1173
 * maps[3] = the map for the 4th component
1174
 *
1175
 * A map's width and height have to be non-zero (but can be negative!)
1176
 *
1177
 * @param maps            List of texture maps to apply to the texture's components.
1178
 * @param startComponent  the component the first of the maps refers to.
1179
 */
1180
   public void setTextureMap(Static4D[] maps, int startComponent)
1181
     {
1182
     if( mJobNode[0]==null )
1183
       {
1184
       textureMap(maps,startComponent);
1185
       }
1186
     else
1187
       {
1188
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1189
       }
1190
     }
1191

    
1192
///////////////////////////////////////////////////////////////////////////////////////////////////
1193
/**
1194
 * Return the texture map of one of the components.
1195
 *
1196
 * @param component The component number whose texture map we want to retrieve.
1197
 */
1198
   public Static4D getTextureMap(int component)
1199
     {
1200
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1201
     }
1202

    
1203
///////////////////////////////////////////////////////////////////////////////////////////////////
1204
/**
1205
 * Set Effect association.
1206
 *
1207
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1208
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1209
 * will only be active on vertices of Components such that
1210
 *
1211
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1212
 *
1213
 * (see main_vertex_shader)
1214
 *
1215
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1216
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1217
 */
1218
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1219
     {
1220
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1221
       {
1222
       if( mJobNode[0]==null )
1223
         {
1224
         setEffectAssociationNow(component, andAssociation, equAssociation);
1225
         }
1226
       else
1227
         {
1228
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1229
         }
1230
       }
1231
     }
1232

    
1233
///////////////////////////////////////////////////////////////////////////////////////////////////
1234
/**
1235
 * Set center of a Component.
1236
 *
1237
 * A 'center' of a (effect) component is a 3D point in space. The array of centers gets sent to
1238
 * the vertex shader as a Uniform Buffer Object; in the vertex shader we can then use it to compute
1239
 * the 'Inflation' of the whole Mesh per-component. 'Inflation' is needed by postprocess effects to
1240
 * add the 'halo' around an object.
1241
 *
1242
 * This is all 'per-component' so that a user has a chance to make the halo look right in case of
1243
 * non-convex meshes: then we need to ensure that each component of such a mesh is a convex
1244
 * sub-mesh with its center being more-or-less the center of gravity of the sub-mesh.
1245
 */
1246
   public void setComponentCenter(int component, float centerX, float centerY, float centerZ)
1247
     {
1248
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1249
       {
1250
       if( mJobNode[0]==null )
1251
         {
1252
         setComponentCenterNow(component, centerX, centerY, centerZ);
1253
         }
1254
       else
1255
         {
1256
         mJobNode[0] = DeferredJobs.componentCenter(this,component,centerX, centerY, centerZ);
1257
         }
1258
       }
1259
     }
1260

    
1261
///////////////////////////////////////////////////////////////////////////////////////////////////
1262
/**
1263
 * Adds an empty (no vertices) texture component to the end of the text component list.
1264
 *
1265
 * Sometimes we want to do this to have several Meshes with equal number of tex components each.
1266
 */
1267
   public void addEmptyTexComponent()
1268
     {
1269
      if( mJobNode[0]==null )
1270
       {
1271
       addEmptyTexComponentNow();
1272
       }
1273
     else
1274
       {
1275
       mJobNode[0] = DeferredJobs.addEmptyTex(this);
1276
       }
1277
     }
1278

    
1279
///////////////////////////////////////////////////////////////////////////////////////////////////
1280
/**
1281
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1282
 * which can be independently textured.
1283
 *
1284
 * @return The number of Texture Components of this Mesh.
1285
 */
1286
   public int numTexComponents()
1287
     {
1288
     return mTexComponent.size();
1289
     }
1290

    
1291
///////////////////////////////////////////////////////////////////////////////////////////////////
1292
/**
1293
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1294
 * to which a VertexEffect can be addressed, independently of other vertices.
1295
 *
1296
 * @return The number of Effect Components of this Mesh.
1297
 */
1298
   public int numEffComponents()
1299
     {
1300
     return mEffComponent.size();
1301
     }
1302

    
1303
///////////////////////////////////////////////////////////////////////////////////////////////////
1304
/**
1305
 * Copy the Mesh.
1306
 *
1307
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1308
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1309
 *             coordinates and effect associations, is always deep copied)
1310
 */
1311
   public abstract MeshBase copy(boolean deep);
1312
   }
(2-2/10)