Project

General

Profile

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

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

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.nio.ByteBuffer;
34
import java.nio.ByteOrder;
35
import java.nio.FloatBuffer;
36
import java.util.ArrayList;
37

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

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

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

    
63
   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)
64
   static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE;                  // number of attributes of a vertex (the 'preapply invariant' part)
65
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;  // number of attributes of a transform feedback vertex
66

    
67
   private static final int BYTES_PER_FLOAT = 4;
68

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

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

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

    
88
   DeferredJobs.JobNode[] mJobNode;
89

    
90
   private static int[] mEquAssociationH = new int[EffectQueue.MAIN_VARIANTS];
91
   private static int[] mAndAssociationH = new int[EffectQueue.MAIN_VARIANTS];
92

    
93
   private static class TexComponent
94
     {
95
     private int mEndIndex;
96
     private Static4D mTextureMap;
97

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

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

    
114
     void setMap(Static4D map)
115
       {
116
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
117
       }
118
     }
119

    
120
   private ArrayList<TexComponent> mTexComponent;
121
   private ArrayList<Integer> mEffComponent;
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
   MeshBase()
126
     {
127
     mShowNormals  = false;
128
     mInflate      = 0.0f;
129
     mTexComponent = new ArrayList<>();
130
     mEffComponent = new ArrayList<>();
131

    
132
     mEquAssociation= new int[MAX_EFFECT_COMPONENTS];
133
     mAndAssociation= new int[MAX_EFFECT_COMPONENTS];
134

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

    
137
     for(int i=0; i<MAX_EFFECT_COMPONENTS; i++)
138
       {
139
       mAndAssociation[i] = DEFAULT_ASSOCIATION;
140
       mEquAssociation[i] = i;
141
       }
142

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// copy constructor
150

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

    
157
     mAndAssociation= new int[MAX_EFFECT_COMPONENTS];
158
     System.arraycopy(original.mAndAssociation, 0, mAndAssociation, 0, MAX_EFFECT_COMPONENTS);
159
     mEquAssociation= new int[MAX_EFFECT_COMPONENTS];
160
     System.arraycopy(original.mEquAssociation, 0, mEquAssociation, 0, MAX_EFFECT_COMPONENTS);
161

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

    
176
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
177
     mTFO.invalidate();
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
     mVBO1.invalidate();
190
     }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

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

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

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

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
   void mergeTexComponentsNow()
217
     {
218
     int num = mTexComponent.size();
219

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

    
225
       mVBO2.invalidate();
226
       }
227
     }
228

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

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

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

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

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

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

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

    
257
     Matrix.setIdentityM(matrix,0);
258
     effect.compute(uniforms,0,0,0);
259
     effect.apply(matrix, uniforms, 0);
260

    
261
     for(int i=0; i<numComp; i++)
262
       {
263
       start = end+1;
264
       end   = mEffComponent.get(i);
265

    
266
       if( (andAssoc & mAndAssociation[i]) != 0 || (equAssoc == mEquAssociation[i]) )
267
         {
268
         applyMatrixToComponent(matrix,start,end);
269
         }
270
       }
271

    
272
     mVBO1.invalidate();
273
     }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
   private void applyMatrixToComponent(float[] matrix, int start, int end)
278
     {
279
     float x,y,z;
280

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

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

    
291
       x = mVertAttribs1[index+NOR_ATTRIB  ];
292
       y = mVertAttribs1[index+NOR_ATTRIB+1];
293
       z = mVertAttribs1[index+NOR_ATTRIB+2];
294

    
295
       mVertAttribs1[index+NOR_ATTRIB  ] = matrix[0]*x + matrix[4]*y + matrix[ 8]*z;
296
       mVertAttribs1[index+NOR_ATTRIB+1] = matrix[1]*x + matrix[5]*y + matrix[ 9]*z;
297
       mVertAttribs1[index+NOR_ATTRIB+2] = matrix[2]*x + matrix[6]*y + matrix[10]*z;
298

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

    
303
       mVertAttribs1[index+INF_ATTRIB  ] = matrix[0]*x + matrix[4]*y + matrix[ 8]*z;
304
       mVertAttribs1[index+INF_ATTRIB+1] = matrix[1]*x + matrix[5]*y + matrix[ 9]*z;
305
       mVertAttribs1[index+INF_ATTRIB+2] = matrix[2]*x + matrix[6]*y + matrix[10]*z;
306
       }
307
     }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
312
     {
313
     mAndAssociation[component] = andAssociation;
314
     mEquAssociation[component] = equAssociation;
315
     }
316

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

    
319
   int numTexComponents()
320
     {
321
     return mTexComponent.size();
322
     }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
   int numEffComponents()
327
     {
328
     return mEffComponent.size();
329
     }
330

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

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

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

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

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

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

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

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

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

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

    
392
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
393
           {
394
           mAndAssociation[origEffComponents] = mesh.mAndAssociation[j];
395
           mEquAssociation[origEffComponents] = mesh.mEquAssociation[j];
396
           origEffComponents++;
397
           }
398
         }
399

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

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

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

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

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

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

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

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

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

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

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

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

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

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474
// called from MeshJoined
475

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

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

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

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
   void textureMap(Static4D[] maps, int startComponent)
496
     {
497
     int num_comp = mTexComponent.size();
498
     int num_maps = maps.length;
499
     int min = Math.min(num_comp-startComponent, num_maps);
500
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
501
     Static4D newMap, oldMap;
502
     TexComponent comp;
503
     float newW, newH, ratW, ratH, movX, movY;
504

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

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

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

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

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

    
535
     mVBO2.invalidate();
536
     }
537

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

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

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
   public static void getUniforms(int mProgramH, int variant)
548
     {
549
     mEquAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComEquAssoc");
550
     mAndAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComAndAssoc");
551
     }
552

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

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

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

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

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

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

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

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

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

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

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

    
667
     Log.d("mesh", sb.toString());
668
     }
669

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

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

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
/**
694
 * Not part of public API, do not document (public only because has to be used from the main package)
695
 *
696
 * @y.exclude
697
 */
698
   public void send(int variant)
699
     {
700
     GLES30.glUniform1iv( mEquAssociationH[variant], MAX_EFFECT_COMPONENTS, mEquAssociation, 0);
701
     GLES30.glUniform1iv( mAndAssociationH[variant], MAX_EFFECT_COMPONENTS, mAndAssociation, 0);
702
     }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705
/**
706
 * Not part of public API, do not document (public only because has to be used from the main package)
707
 *
708
 * @y.exclude
709
 */
710
   public void bindVertexAttribs(DistortedProgram program)
711
     {
712
     if( mJobNode[0]!=null )
713
       {
714
       mJobNode[0].execute();  // this will set itself to null
715
       }
716

    
717
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
718
     int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
719

    
720
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
721
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
722
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
723
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
724
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
725
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
726
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
727
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
728
     }
729

    
730
///////////////////////////////////////////////////////////////////////////////////////////////////
731
/**
732
 * Not part of public API, do not document (public only because has to be used from the main package)
733
 *
734
 * @y.exclude
735
 */
736
   public void bindTransformAttribs(DistortedProgram program)
737
     {
738
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
739

    
740
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
741
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
742
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
743
     }
744

    
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746
/**
747
 * Not part of public API, do not document (public only because has to be used from the main package)
748
 *
749
 * @y.exclude
750
 */
751
   public void setInflate(float inflate)
752
     {
753
     mInflate = inflate;
754
     }
755

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

    
767
///////////////////////////////////////////////////////////////////////////////////////////////////
768
// PUBLIC API
769
///////////////////////////////////////////////////////////////////////////////////////////////////
770
/**
771
 * When rendering this Mesh, do we want to render the Normal vectors as well?
772
 * <p>
773
 * Will work only on OpenGL ES >= 3.0 devices.
774
 *
775
 * @param show Controls if we render the Normal vectors or not.
776
 */
777
   public void setShowNormals(boolean show)
778
     {
779
     mShowNormals = show;
780
     }
781

    
782
///////////////////////////////////////////////////////////////////////////////////////////////////
783
/**
784
 * When rendering this mesh, should we also draw the normal vectors?
785
 *
786
 * @return <i>true</i> if we do render normal vectors
787
 */
788
   public boolean getShowNormals()
789
     {
790
     return mShowNormals;
791
     }
792

    
793
///////////////////////////////////////////////////////////////////////////////////////////////////
794
/**
795
 * Merge all texture components of this Mesh into a single one.
796
 */
797
   public void mergeTexComponents()
798
     {
799
     if( mJobNode[0]==null )
800
       {
801
       mergeTexComponentsNow();
802
       }
803
     else
804
       {
805
       mJobNode[0] = DeferredJobs.mergeTex(this);
806
       }
807
     }
808

    
809
///////////////////////////////////////////////////////////////////////////////////////////////////
810
/**
811
 * Merge all effect components of this Mesh into a single one.
812
 */
813
   public void mergeEffComponents()
814
     {
815
     if( mJobNode[0]==null )
816
       {
817
       mergeEffComponentsNow();
818
       }
819
     else
820
       {
821
       mJobNode[0] = DeferredJobs.mergeEff(this);
822
       }
823
     }
824

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826
/**
827
 * Release all internal resources.
828
 */
829
   public void markForDeletion()
830
     {
831
     mVertAttribs1 = null;
832
     mVertAttribs2 = null;
833

    
834
     mVBO1.markForDeletion();
835
     mVBO2.markForDeletion();
836
     mTFO.markForDeletion();
837
     }
838

    
839
///////////////////////////////////////////////////////////////////////////////////////////////////
840
/**
841
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
842
 * <p>
843
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
844
 * contains any Dynamics, they will be evaluated at 0.
845
 *
846
 * @param effect List of Matrix Effects to apply to the Mesh.
847
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
848
 * @param equAssoc 'equality' association which defines which components will be affected.
849
 */
850
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
851
     {
852
     if( mJobNode[0]==null )
853
       {
854
       applyMatrix(effect,andAssoc,equAssoc);
855
       }
856
     else
857
       {
858
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
859
       }
860
     }
861

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863
/**
864
 * Apply a Vertex Effect to the vertex mesh.
865
 * <p>
866
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
867
 * contain any Dynamics, the Dynamics will be evaluated at 0.
868
 *
869
 * We would call this several times building up a list of Effects to do. This list of effects gets
870
 * lazily executed only when the Mesh is used for rendering for the first time.
871
 *
872
 * @param effect Vertex Effect to apply to the Mesh.
873
 */
874
   public void apply(VertexEffect effect)
875
     {
876
     mJobNode[0] = DeferredJobs.vertex(this,effect);
877
     }
878

    
879
///////////////////////////////////////////////////////////////////////////////////////////////////
880
/**
881
 * Sets texture maps for (some of) the components of this mesh.
882
 * <p>
883
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
884
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
885
 * upper-left quadrant of the texture.
886
 * <p>
887
 * Probably the most common user case would be sending as many maps as there are components in this
888
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
889
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
890
 * of the 2nd and 4rd one, call this with
891
 * maps = new Static4D[4]
892
 * maps[0] = null
893
 * maps[1] = the map for the 2nd component
894
 * maps[2] = null
895
 * maps[3] = the map for the 4th component
896
 *
897
 * A map's width and height have to be non-zero (but can be negative!)
898
 *
899
 * @param maps            List of texture maps to apply to the texture's components.
900
 * @param startComponent  the component the first of the maps refers to.
901
 */
902
   public void setTextureMap(Static4D[] maps, int startComponent)
903
     {
904
     if( mJobNode[0]==null )
905
       {
906
       textureMap(maps,startComponent);
907
       }
908
     else
909
       {
910
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
911
       }
912
     }
913

    
914
///////////////////////////////////////////////////////////////////////////////////////////////////
915
/**
916
 * Return the texture map of one of the components.
917
 *
918
 * @param component The component number whose texture map we want to retrieve.
919
 */
920
   public Static4D getTextureMap(int component)
921
     {
922
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
923
     }
924

    
925
///////////////////////////////////////////////////////////////////////////////////////////////////
926
/**
927
 * Set Effect association.
928
 *
929
 * This creates an association between a Component of this Mesh and a Vertex Effect.
930
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
931
 * will only be active on vertices of Components such that
932
 *
933
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
934
 *
935
 * (see main_vertex_shader)
936
 *
937
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
938
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
939
 */
940
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
941
     {
942
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
943
       {
944
       if( mJobNode[0]==null )
945
         {
946
         setEffectAssociationNow(component, andAssociation, equAssociation);
947
         }
948
       else
949
         {
950
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
951
         }
952
       }
953
     }
954

    
955
///////////////////////////////////////////////////////////////////////////////////////////////////
956
/**
957
 * Copy the Mesh.
958
 *
959
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
960
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
961
 *             coordinates and effect associations, is always deep copied)
962
 */
963
   public abstract MeshBase copy(boolean deep);
964
   }
(2-2/8)