Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ f4c5a46e

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.util.Log;
24

    
25
import org.distorted.library.effect.VertexEffect;
26
import org.distorted.library.effectqueue.EffectQueue;
27
import org.distorted.library.main.InternalBuffer;
28
import org.distorted.library.program.DistortedProgram;
29
import org.distorted.library.type.Static4D;
30

    
31
import java.nio.ByteBuffer;
32
import java.nio.ByteOrder;
33
import java.nio.FloatBuffer;
34
import java.util.ArrayList;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
/**
38
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
39
 * <p>
40
 * If you want to render to a particular shape, extend from here, construct a float array
41
 * containing per-vertex attributes, and call back setAttribs().
42
 */
43
public abstract class MeshBase
44
   {
45
   private static final int MAX_COMPONENTS= 100;
46
   private static final int DEFAULT_ASSOCIATION = 0xffffffff;
47

    
48
   // sizes of attributes of an individual vertex.
49
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
50
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
51
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
52
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
53
   private static final int COM_DATA_SIZE= 1; // component number, a single float
54

    
55
   static final int POS_ATTRIB   = 0;
56
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
57
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
58
   static final int TEX_ATTRIB   = 0;
59
   static final int COM_ATTRIB   = TEX_DATA_SIZE;
60

    
61
   static final int VERT1_ATTRIBS= POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;  // number of attributes of a vertex (the part changed by preapply)
62
   static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE;                  // number of attributes of a vertex (the 'preapply invariant' part)
63
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;  // number of attributes of a transform feedback vertex
64

    
65
   private static final int BYTES_PER_FLOAT = 4;
66

    
67
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
68
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
69
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
70
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
71
   private static final int OFFSET_COM = COM_ATTRIB*BYTES_PER_FLOAT;
72

    
73
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
74
   private static final int VERT1_SIZE = VERT1_ATTRIBS*BYTES_PER_FLOAT;
75
   private static final int VERT2_SIZE = VERT2_ATTRIBS*BYTES_PER_FLOAT;
76

    
77
   private boolean mShowNormals;              // when rendering this mesh, draw normal vectors?
78
   private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer
79
   private int mNumVertices;
80
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ
81
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
82
   private float mInflate;
83
   private int[] mAssociation;
84

    
85
   DeferredJobs.JobNode[] mJobNode;
86

    
87
   private static int[] mComponentAssociationH = new int[EffectQueue.MAIN_VARIANTS];
88

    
89
   private static class Component
90
     {
91
     private int mEndIndex;
92
     private Static4D mTextureMap;
93

    
94
     Component(int end)
95
       {
96
       mEndIndex  = end;
97
       mTextureMap= new Static4D(0,0,1,1);
98
       }
99
     Component(Component original)
100
       {
101
       mEndIndex = original.mEndIndex;
102

    
103
       float x = original.mTextureMap.get0();
104
       float y = original.mTextureMap.get1();
105
       float z = original.mTextureMap.get2();
106
       float w = original.mTextureMap.get3();
107
       mTextureMap = new Static4D(x,y,z,w);
108
       }
109

    
110
     void setMap(Static4D map)
111
       {
112
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
113
       }
114
     }
115

    
116
   private ArrayList<Component> mComponent;
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
   MeshBase()
121
     {
122
     mShowNormals= false;
123
     mInflate    = 0.0f;
124
     mComponent  = new ArrayList<>();
125
     mAssociation= new int[MAX_COMPONENTS];
126

    
127
     mJobNode = new DeferredJobs.JobNode[1];
128

    
129
     for(int i=0; i<MAX_COMPONENTS; i++) mAssociation[i] = DEFAULT_ASSOCIATION;
130

    
131
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
132
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
133
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
134
     }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// copy constructor
138

    
139
   MeshBase(MeshBase original, boolean deep)
140
     {
141
     mShowNormals     = original.mShowNormals;
142
     mInflate         = original.mInflate;
143
     mNumVertices     = original.mNumVertices;
144

    
145
     int size = original.mComponent.size();
146
     mComponent = new ArrayList<>();
147

    
148
     for(int i=0; i<size; i++)
149
       {
150
       Component comp = new Component(original.mComponent.get(i));
151
       mComponent.add(comp);
152
       }
153

    
154
     mAssociation= new int[MAX_COMPONENTS];
155
     System.arraycopy(original.mAssociation, 0, mAssociation, 0, MAX_COMPONENTS);
156

    
157
     if( deep )
158
       {
159
       mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
160
       mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
161

    
162
       mJobNode = new DeferredJobs.JobNode[1];
163

    
164
       if( original.mJobNode[0]!=null )
165
         {
166
         mJobNode[0] = DeferredJobs.copy(this,original);
167
         }
168
       else
169
         {
170
         deepCopyAttribs1(original);
171
         }
172
       }
173
     else
174
       {
175
       mJobNode      = original.mJobNode;
176
       mVBO1         = original.mVBO1;
177
       mVertAttribs1 = original.mVertAttribs1;
178
       }
179

    
180
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
181
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
182
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
183
     mVBO2.invalidate();
184

    
185
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
186
     mTFO.invalidate();
187
     }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
   public static int getMaxComponents()
192
     {
193
     return MAX_COMPONENTS;
194
     }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
   public static void getUniforms(int mProgramH, int variant)
199
     {
200
     mComponentAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComponAssoc");
201
     }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
   void deepCopyAttribs1(MeshBase original)
206
     {
207
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
208
     mVBO1.invalidate();
209
     }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
   int numComponents()
214
     {
215
     return mComponent.size();
216
     }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
// when a derived class is done computing its mesh, it has to call this method.
220

    
221
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
222
     {
223
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
224
     mVertAttribs1= vert1Attribs;
225
     mVertAttribs2= vert2Attribs;
226

    
227
     mComponent.add(new Component(mNumVertices-1));
228

    
229
     mVBO1.invalidate();
230
     mVBO2.invalidate();
231
     }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// called from MeshJoined
235

    
236
   void join(MeshBase[] meshes)
237
     {
238
     MeshBase mesh;
239
     Component comp;
240
     int origComponents=0, numComponents, numVertices, numMeshes = meshes.length;
241
     int origVertices = mNumVertices;
242

    
243
     // compute new numVertices; take care of Components
244

    
245
     if( origVertices>0 )
246
       {
247
       origComponents = mComponent.size();
248
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
249
       mComponent.get(origComponents-1).mEndIndex = mNumVertices-1;
250
       }
251

    
252
     for(int i=0; i<numMeshes; i++)
253
       {
254
       mesh = meshes[i];
255
       numComponents = mesh.mComponent.size();
256

    
257
       numVertices = mesh.mNumVertices;
258

    
259
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
260
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
261

    
262
       for(int j=0; j<numComponents; j++)
263
         {
264
         comp = new Component(mesh.mComponent.get(j));
265
         comp.mEndIndex += (extraVerticesBefore+mNumVertices+extraVerticesAfter);
266
         mComponent.add(comp);
267

    
268
         if( origComponents<MAX_COMPONENTS )
269
           {
270
           mAssociation[origComponents] = mesh.mAssociation[j];
271
           origComponents++;
272
           }
273
         }
274

    
275
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
276
       }
277

    
278
     // allocate new attrib array
279
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
280
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
281
     numVertices = origVertices;
282

    
283
     if( origVertices>0 )
284
       {
285
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                         0, VERT1_ATTRIBS*numVertices);
286
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                         0, VERT2_ATTRIBS*numVertices);
287
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS    );
288
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS    );
289
       origVertices++;
290

    
291
       if( numVertices%2==1 )
292
         {
293
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
294
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
295
         origVertices++;
296
         }
297
       }
298

    
299
     for(int i=0; i<numMeshes; i++)
300
       {
301
       mesh = meshes[i];
302
       numVertices = mesh.mNumVertices;
303

    
304
       if( origVertices>0 )
305
         {
306
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS    );
307
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS    );
308
         origVertices++;
309
         }
310
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
311
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
312
       origVertices+=numVertices;
313

    
314
       if( i<numMeshes-1 )
315
         {
316
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
317
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
318
         origVertices++;
319

    
320
         if( numVertices%2==1 )
321
           {
322
           System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
323
           System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
324
           origVertices++;
325
           }
326
         }
327
       }
328

    
329
     if( origVertices!=mNumVertices )
330
       {
331
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
332
       }
333

    
334
     int endIndex, index=0, numComp = mComponent.size();
335

    
336
     for(int component=0; component<numComp; component++)
337
       {
338
       endIndex = mComponent.get(component).mEndIndex;
339

    
340
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
341
       }
342

    
343
     mVertAttribs1 = newAttribs1;
344
     mVertAttribs2 = newAttribs2;
345

    
346
     mVBO1.invalidate();
347
     mVBO2.invalidate();
348
     }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351
/**
352
 * Not part of public API, do not document (public only because has to be used from the main package)
353
 *
354
 * @y.exclude
355
 */
356
   public void copyTransformToVertex()
357
     {
358
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
359
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
360
     if( buffer!=null )
361
       {
362
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
363
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
364
       mVBO1.update(mVertAttribs1);
365
       }
366
     else
367
       {
368
       int error = GLES30.glGetError();
369
       Log.e("mesh", "failed to map tf buffer, error="+error);
370
       }
371

    
372
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
373
     }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
/**
377
 * Not part of public API, do not document (public only because has to be used from the main package)
378
 *
379
 * @y.exclude
380
 */
381
   public int getTFO()
382
     {
383
     return mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
384
     }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387
/**
388
 * Not part of public API, do not document (public only because has to be used from the main package)
389
 *
390
 * @y.exclude
391
 */
392
   public int getNumVertices()
393
     {
394
     return mNumVertices;
395
     }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
/**
399
 * Not part of public API, do not document (public only because has to be used from the main package)
400
 *
401
 * @y.exclude
402
 */
403
   public void send(int variant)
404
     {
405
     GLES30.glUniform1iv( mComponentAssociationH[variant], MAX_COMPONENTS, mAssociation, 0);
406
     }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409
/**
410
 * Not part of public API, do not document (public only because has to be used from the main package)
411
 *
412
 * @y.exclude
413
 */
414
   public void bindVertexAttribs(DistortedProgram program)
415
     {
416
     if( mJobNode[0]!=null )
417
       {
418
       mJobNode[0].execute();  // this will set itself to null
419
       }
420

    
421
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
422
     int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
423

    
424
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
425
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
426
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
427
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
428
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
429
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
430
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
431
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
432
     }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
/**
436
 * Not part of public API, do not document (public only because has to be used from the main package)
437
 *
438
 * @y.exclude
439
 */
440
   public void bindTransformAttribs(DistortedProgram program)
441
     {
442
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
443

    
444
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
445
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
446
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
447
     }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450
/**
451
 * Not part of public API, do not document (public only because has to be used from the main package)
452
 *
453
 * @y.exclude
454
 */
455
   public void setInflate(float inflate)
456
     {
457
     mInflate = inflate;
458
     }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461
/**
462
 * Not part of public API, do not document (public only because has to be used from the main package)
463
 *
464
 * @y.exclude
465
 */
466
   public float getInflate()
467
     {
468
     return mInflate;
469
     }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472
// PUBLIC API
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474
/**
475
 * When rendering this Mesh, do we want to render the Normal vectors as well?
476
 * <p>
477
 * Will work only on OpenGL ES >= 3.0 devices.
478
 *
479
 * @param show Controls if we render the Normal vectors or not.
480
 */
481
   public void setShowNormals(boolean show)
482
     {
483
     mShowNormals = show;
484
     }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487
/**
488
 * When rendering this mesh, should we also draw the normal vectors?
489
 *
490
 * @return <i>true</i> if we do render normal vectors
491
 */
492
   public boolean getShowNormals()
493
     {
494
     return mShowNormals;
495
     }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498
/**
499
 * Release all internal resources.
500
 */
501
   public void markForDeletion()
502
     {
503
     mVertAttribs1 = null;
504
     mVertAttribs2 = null;
505

    
506
     mVBO1.markForDeletion();
507
     mVBO2.markForDeletion();
508
     mTFO.markForDeletion();
509
     }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512
/**
513
 * Apply a Vertex Effect to the vertex mesh.
514
 * <p>
515
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
516
 * contain any Dynamics, the Dynamics will be evaluated at 0.
517
 *
518
 * We would call this several times building up a list of Effects to do. This list of effects gets
519
 * lazily executed only when the Mesh is used for rendering for the first time.
520
 *
521
 * @param effect Vertex Effect to apply to the Mesh.
522
 */
523
   public void apply(VertexEffect effect)
524
     {
525
     mJobNode[0] = DeferredJobs.vertex(this,effect);
526
     }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Sets texture maps for (some of) the components of this mesh.
531
 * <p>
532
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
533
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
534
 * upper-left quadrant of the texture.
535
 * <p>
536
 * Probably the most common user case would be sending as many maps as there are components in this
537
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
538
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
539
 * of the 2nd and 4rd one, call this with
540
 * maps = new Static4D[4]
541
 * maps[0] = null
542
 * maps[1] = the map for the 2nd component
543
 * maps[2] = null
544
 * maps[3] = the map for the 4th component
545
 *
546
 * A map's width and height have to be non-zero (but can be negative!)
547
 *
548
 * @param maps List of texture maps to apply to the texture's components.
549
 */
550
   public void setTextureMap(Static4D[] maps)
551
     {
552
     int num_comp = mComponent.size();
553
     int num_maps = maps.length;
554
     int min = Math.min(num_comp, num_maps);
555
     int vertex = 0;
556
     Static4D newMap, oldMap;
557
     Component comp;
558
     float newW, newH, ratW, ratH, movX, movY;
559

    
560
     for(int i=0; i<min; i++)
561
       {
562
       newMap = maps[i];
563
       comp = mComponent.get(i);
564

    
565
       if( newMap!=null )
566
         {
567
         newW = newMap.get2();
568
         newH = newMap.get3();
569

    
570
         if( newW!=0.0f && newH!=0.0f )
571
           {
572
           oldMap = comp.mTextureMap;
573
           ratW = newW/oldMap.get2();
574
           ratH = newH/oldMap.get3();
575
           movX = newMap.get0() - ratW*oldMap.get0();
576
           movY = newMap.get1() - ratH*oldMap.get1();
577

    
578
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
579
             {
580
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
581
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
582
             }
583
           comp.setMap(newMap);
584
           }
585
         }
586

    
587
       vertex= comp.mEndIndex+1;
588
       }
589

    
590
     mVBO2.invalidate();
591
     }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594
/**
595
 * Return the texture map of one of the components.
596
 *
597
 * @param component The component number whose texture map we want to retrieve.
598
 */
599
   public Static4D getTextureMap(int component)
600
     {
601
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
602
     }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
/**
606
 * Set Effect association.
607
 *
608
 * This creates an association between a Component of this Mesh and a Vertex Effect.
609
 * One can set the association of an Effect and of a Component, and the Effect will only be active on
610
 * vertices of Components such that
611
 *
612
 * (effect assoc) & (component assoc) != 0 || (effect component) == (mesh component)
613
 *
614
 * (see main_vertex_shader)
615
 *
616
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
617
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
618
 */
619
  public void setEffectAssociation(int component, int association)
620
    {
621
    if( component>=0 && component<MAX_COMPONENTS )
622
      {
623
      mAssociation[component] = association;
624
      }
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628
/**
629
 * Copy the Mesh.
630
 *
631
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
632
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
633
 *             coordinates and effect associations, is always deep copied)
634
 */
635
   public abstract MeshBase copy(boolean deep);
636
   }
(2-2/8)