Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 41b3ada0

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.main.InternalBuffer;
29
import org.distorted.library.program.DistortedProgram;
30
import org.distorted.library.type.Static4D;
31

    
32
import java.io.DataOutputStream;
33
import java.io.IOException;
34
import java.io.DataInputStream;
35
import java.nio.ByteBuffer;
36
import java.nio.ByteOrder;
37
import java.nio.FloatBuffer;
38
import java.util.ArrayList;
39

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

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

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

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

    
69
   private static final int BYTES_PER_FLOAT = 4;
70

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

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

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

    
89
   DeferredJobs.JobNode[] mJobNode;
90

    
91
   private static final int TEX_COMP_SIZE = 5; // 5 four-bytes entities inside the component
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
     mJobNode = new DeferredJobs.JobNode[1];
133

    
134
     mAUB = new AssociationUniformBlock();
135

    
136
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
137
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
138
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
139
     }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// copy constructor
143

    
144
   MeshBase(MeshBase original, boolean deep)
145
     {
146
     mShowNormals= original.mShowNormals;
147
     mInflate    = original.mInflate;
148
     mNumVertices= original.mNumVertices;
149

    
150
     mAUB = new AssociationUniformBlock(original.mAUB);
151

    
152
     if( deep )
153
       {
154
       mJobNode = new DeferredJobs.JobNode[1];
155
       if( original.mJobNode[0]==null ) copy(original);
156
       else mJobNode[0] = DeferredJobs.copy(this,original);
157
       }
158
     else
159
       {
160
       mJobNode      = original.mJobNode;
161
       mVBO1         = original.mVBO1;
162
       mVertAttribs1 = original.mVertAttribs1;
163
       shallowCopy(original);
164
       }
165

    
166
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
167
     }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
   void copy(MeshBase original)
172
     {
173
     shallowCopy(original);
174

    
175
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
176
     mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
177
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
178
     mVBO1.invalidate();
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   private void shallowCopy(MeshBase original)
184
     {
185
     int texComSize = original.mTexComponent.size();
186
     mTexComponent = new ArrayList<>();
187

    
188
     for(int i=0; i<texComSize; i++)
189
       {
190
       TexComponent comp = new TexComponent(original.mTexComponent.get(i));
191
       mTexComponent.add(comp);
192
       }
193

    
194
     mEffComponent = new ArrayList<>();
195
     mEffComponent.addAll(original.mEffComponent);
196

    
197
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
198
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
199
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
200
     mVBO2.invalidate();
201
     }
202

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

    
205
   void mergeTexComponentsNow()
206
     {
207
     int num = mTexComponent.size();
208

    
209
     if( num>1 )
210
       {
211
       mTexComponent.clear();
212
       mTexComponent.add(new TexComponent(mNumVertices-1));
213

    
214
       mVBO2.invalidate();
215
       }
216
     }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
   void mergeEffComponentsNow()
221
     {
222
     int num = mEffComponent.size();
223

    
224
     if( num>1 )
225
       {
226
       mEffComponent.clear();
227
       mEffComponent.add(mNumVertices-1);
228

    
229
       for(int index=0; index<mNumVertices; index++)
230
         {
231
         mVertAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = 0;
232
         }
233

    
234
       mVBO2.invalidate();
235
       }
236
     }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
   void applyMatrix(MatrixEffect effect, int andAssoc, int equAssoc)
241
     {
242
     float[] matrixP  = new float[16];
243
     float[] matrixV  = new float[16];
244
     float[] uniforms = new float[7];
245
     int start, end=-1, numComp = mEffComponent.size();
246

    
247
     Matrix.setIdentityM(matrixP,0);
248
     Matrix.setIdentityM(matrixV,0);
249
     effect.compute(uniforms,0,0,0);
250
     effect.apply(matrixP, matrixV, uniforms, 0);
251

    
252
     for(int comp=0; comp<numComp; comp++)
253
       {
254
       start = end+1;
255
       end   = mEffComponent.get(comp);
256

    
257
       if( mAUB.matchesAssociation(comp, andAssoc, equAssoc) )
258
         {
259
         applyMatrixToComponent(matrixP, matrixV, start, end);
260
         }
261
       }
262

    
263
     mVBO1.invalidate();
264
     }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
   private void applyMatrixToComponent(float[] matrixP, float[] matrixV, int start, int end)
269
     {
270
     float x,y,z;
271

    
272
     for(int index=start*VERT1_ATTRIBS; index<=end*VERT1_ATTRIBS; index+=VERT1_ATTRIBS )
273
       {
274
       x = mVertAttribs1[index+POS_ATTRIB  ];
275
       y = mVertAttribs1[index+POS_ATTRIB+1];
276
       z = mVertAttribs1[index+POS_ATTRIB+2];
277

    
278
       mVertAttribs1[index+POS_ATTRIB  ] = matrixP[0]*x + matrixP[4]*y + matrixP[ 8]*z + matrixP[12];
279
       mVertAttribs1[index+POS_ATTRIB+1] = matrixP[1]*x + matrixP[5]*y + matrixP[ 9]*z + matrixP[13];
280
       mVertAttribs1[index+POS_ATTRIB+2] = matrixP[2]*x + matrixP[6]*y + matrixP[10]*z + matrixP[14];
281

    
282
       x = mVertAttribs1[index+NOR_ATTRIB  ];
283
       y = mVertAttribs1[index+NOR_ATTRIB+1];
284
       z = mVertAttribs1[index+NOR_ATTRIB+2];
285

    
286
       mVertAttribs1[index+NOR_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
287
       mVertAttribs1[index+NOR_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
288
       mVertAttribs1[index+NOR_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
289

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

    
294
       float len1 = (float)Math.sqrt(x*x + y*y + z*z);
295

    
296
       if( len1>0.0f )
297
         {
298
         mVertAttribs1[index+NOR_ATTRIB  ] /= len1;
299
         mVertAttribs1[index+NOR_ATTRIB+1] /= len1;
300
         mVertAttribs1[index+NOR_ATTRIB+2] /= len1;
301
         }
302

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

    
307
       mVertAttribs1[index+INF_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
308
       mVertAttribs1[index+INF_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
309
       mVertAttribs1[index+INF_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
310

    
311
       x = mVertAttribs1[index+INF_ATTRIB  ];
312
       y = mVertAttribs1[index+INF_ATTRIB+1];
313
       z = mVertAttribs1[index+INF_ATTRIB+2];
314

    
315
       float len2 = (float)Math.sqrt(x*x + y*y + z*z);
316

    
317
       if( len2>0.0f )
318
         {
319
         mVertAttribs1[index+INF_ATTRIB  ] /= len2;
320
         mVertAttribs1[index+INF_ATTRIB+1] /= len2;
321
         mVertAttribs1[index+INF_ATTRIB+2] /= len2;
322
         }
323
       }
324
     }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
329
     {
330
     mAUB.setEffectAssociationNow(component, andAssociation, equAssociation);
331
     }
332

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

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

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

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

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
536
     mVBO2.invalidate();
537
     }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
724
     int index1 = mVBO1.createImmediatelyFloat(mNumVertices*VERT1_SIZE, mVertAttribs1);
725
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
726

    
727
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
728
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
729
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
730
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
731
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
732
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
733
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
734
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
735
     }
736

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

    
747
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
748
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
749
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
750
     }
751

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

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

    
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775
// Return the number of bytes read.
776

    
777
   int read(DataInputStream stream)
778
     {
779
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
780
     int version, numEff, numTex;
781

    
782
     try
783
       {
784
       stream.read(buffer);
785
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
786

    
787
       version = byteBuf.getInt(0);
788

    
789
       if( version==1 )
790
         {
791
         mNumVertices= byteBuf.getInt(4);
792
         numTex      = byteBuf.getInt(8);
793
         numEff      = byteBuf.getInt(12);
794
         }
795
       else
796
         {
797
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
798
         return 0;
799
         }
800
       }
801
     catch(IOException e)
802
       {
803
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
804
       return 0;
805
       }
806

    
807
     if( mNumVertices>0 && numEff>0 && numTex>0 )
808
       {
809
       int size = numEff+TEX_COMP_SIZE*numTex;
810
       float[] tmp = new float[size];
811
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
812
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
813

    
814
       buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(VERT1_ATTRIBS+VERT2_ATTRIBS))];
815

    
816
       try
817
         {
818
         stream.read(buffer);
819
         }
820
       catch(IOException e)
821
         {
822
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
823
         return 0;
824
         }
825

    
826
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
827
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
828

    
829
       floatBuf.get(tmp,0,size);
830
       floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
831
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
832

    
833
       TexComponent tex;
834
       int index, texComp;
835
       float x, y, z, w;
836

    
837
       for(texComp=0; texComp<numTex; texComp++)
838
         {
839
         index = (int)tmp[TEX_COMP_SIZE*texComp];
840

    
841
         x= tmp[TEX_COMP_SIZE*texComp+1];
842
         y= tmp[TEX_COMP_SIZE*texComp+2];
843
         z= tmp[TEX_COMP_SIZE*texComp+3];
844
         w= tmp[TEX_COMP_SIZE*texComp+4];
845

    
846
         tex = new TexComponent(index);
847
         tex.setMap(new Static4D(x,y,z,w));
848

    
849
         mTexComponent.add(tex);
850
         }
851

    
852
       for(int effComp=0; effComp<numEff; effComp++)
853
         {
854
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
855
         mEffComponent.add(index);
856
         }
857
       }
858

    
859
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
860
     }
861

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863
/**
864
 * @y.exclude
865
 */
866
   public int getLastVertexEff(int effComponent)
867
     {
868
     if( effComponent>=0 && effComponent<mTexComponent.size() )
869
       {
870
       return mEffComponent.get(effComponent);
871
       }
872

    
873
     return 0;
874
     }
875

    
876
///////////////////////////////////////////////////////////////////////////////////////////////////
877
/**
878
 * @y.exclude
879
 */
880
   public int getLastVertexTex(int texComponent)
881
     {
882
     if( texComponent>=0 && texComponent<mTexComponent.size() )
883
       {
884
       return mTexComponent.get(texComponent).mEndIndex;
885
       }
886

    
887
     return 0;
888
     }
889

    
890
///////////////////////////////////////////////////////////////////////////////////////////////////
891
// PUBLIC API
892
///////////////////////////////////////////////////////////////////////////////////////////////////
893
/**
894
 * Write the Mesh to a File.
895
 */
896
   public void write(DataOutputStream stream)
897
     {
898
     TexComponent tex;
899

    
900
     int numTex = mTexComponent.size();
901
     int numEff = mEffComponent.size();
902

    
903
     try
904
       {
905
       stream.writeInt(1);  // version
906
       stream.writeInt(mNumVertices);
907
       stream.writeInt(numTex);
908
       stream.writeInt(numEff);
909

    
910
       for(int i=0; i<numTex; i++)
911
         {
912
         tex = mTexComponent.get(i);
913

    
914
         stream.writeFloat((float)tex.mEndIndex);
915
         stream.writeFloat(tex.mTextureMap.get0());
916
         stream.writeFloat(tex.mTextureMap.get1());
917
         stream.writeFloat(tex.mTextureMap.get2());
918
         stream.writeFloat(tex.mTextureMap.get3());
919
         }
920

    
921
       for(int i=0; i<numEff; i++)
922
         {
923
         stream.writeFloat((float)mEffComponent.get(i));
924
         }
925

    
926
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
927
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
928
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
929
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
930

    
931
       stream.write(vertBuf1.array());
932
       stream.write(vertBuf2.array());
933
       }
934
     catch(IOException ex)
935
       {
936
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
937
       }
938
     }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941
/**
942
 * When rendering this Mesh, do we want to render the Normal vectors as well?
943
 * <p>
944
 * Will work only on OpenGL ES >= 3.0 devices.
945
 *
946
 * @param show Controls if we render the Normal vectors or not.
947
 */
948
   public void setShowNormals(boolean show)
949
     {
950
     mShowNormals = show;
951
     }
952

    
953
///////////////////////////////////////////////////////////////////////////////////////////////////
954
/**
955
 * When rendering this mesh, should we also draw the normal vectors?
956
 *
957
 * @return <i>true</i> if we do render normal vectors
958
 */
959
   public boolean getShowNormals()
960
     {
961
     return mShowNormals;
962
     }
963

    
964
///////////////////////////////////////////////////////////////////////////////////////////////////
965
/**
966
 * Merge all texture components of this Mesh into a single one.
967
 */
968
   public void mergeTexComponents()
969
     {
970
     if( mJobNode[0]==null )
971
       {
972
       mergeTexComponentsNow();
973
       }
974
     else
975
       {
976
       mJobNode[0] = DeferredJobs.mergeTex(this);
977
       }
978
     }
979

    
980
///////////////////////////////////////////////////////////////////////////////////////////////////
981
/**
982
 * Merge all effect components of this Mesh into a single one.
983
 */
984
   public void mergeEffComponents()
985
     {
986
     if( mJobNode[0]==null )
987
       {
988
       mergeEffComponentsNow();
989
       }
990
     else
991
       {
992
       mJobNode[0] = DeferredJobs.mergeEff(this);
993
       }
994
     }
995

    
996
///////////////////////////////////////////////////////////////////////////////////////////////////
997
/**
998
 * Release all internal resources.
999
 */
1000
   public void markForDeletion()
1001
     {
1002
     mVertAttribs1 = null;
1003
     mVertAttribs2 = null;
1004

    
1005
     mVBO1.markForDeletion();
1006
     mVBO2.markForDeletion();
1007
     mTFO.markForDeletion();
1008
     }
1009

    
1010
///////////////////////////////////////////////////////////////////////////////////////////////////
1011
/**
1012
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1013
 * <p>
1014
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1015
 * contains any Dynamics, they will be evaluated at 0.
1016
 *
1017
 * @param effect List of Matrix Effects to apply to the Mesh.
1018
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1019
 * @param equAssoc 'equality' association which defines which components will be affected.
1020
 */
1021
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1022
     {
1023
     if( mJobNode[0]==null )
1024
       {
1025
       applyMatrix(effect,andAssoc,equAssoc);
1026
       }
1027
     else
1028
       {
1029
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1030
       }
1031
     }
1032

    
1033
///////////////////////////////////////////////////////////////////////////////////////////////////
1034
/**
1035
 * Apply a Vertex Effect to the vertex mesh.
1036
 * <p>
1037
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1038
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1039
 *
1040
 * We would call this several times building up a list of Effects to do. This list of effects gets
1041
 * lazily executed only when the Mesh is used for rendering for the first time.
1042
 *
1043
 * @param effect Vertex Effect to apply to the Mesh.
1044
 */
1045
   public void apply(VertexEffect effect)
1046
     {
1047
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1048
     }
1049

    
1050
///////////////////////////////////////////////////////////////////////////////////////////////////
1051
/**
1052
 * Sets texture maps for (some of) the components of this mesh.
1053
 * <p>
1054
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
1055
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
1056
 * upper-left quadrant of the texture.
1057
 * <p>
1058
 * Probably the most common user case would be sending as many maps as there are components in this
1059
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
1060
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
1061
 * of the 2nd and 4rd one, call this with
1062
 * maps = new Static4D[4]
1063
 * maps[0] = null
1064
 * maps[1] = the map for the 2nd component
1065
 * maps[2] = null
1066
 * maps[3] = the map for the 4th component
1067
 *
1068
 * A map's width and height have to be non-zero (but can be negative!)
1069
 *
1070
 * @param maps            List of texture maps to apply to the texture's components.
1071
 * @param startComponent  the component the first of the maps refers to.
1072
 */
1073
   public void setTextureMap(Static4D[] maps, int startComponent)
1074
     {
1075
     if( mJobNode[0]==null )
1076
       {
1077
       textureMap(maps,startComponent);
1078
       }
1079
     else
1080
       {
1081
       mJobNode[0] = DeferredJobs.textureMap(this,maps,startComponent);
1082
       }
1083
     }
1084

    
1085
///////////////////////////////////////////////////////////////////////////////////////////////////
1086
/**
1087
 * Return the texture map of one of the components.
1088
 *
1089
 * @param component The component number whose texture map we want to retrieve.
1090
 */
1091
   public Static4D getTextureMap(int component)
1092
     {
1093
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1094
     }
1095

    
1096
///////////////////////////////////////////////////////////////////////////////////////////////////
1097
/**
1098
 * Set Effect association.
1099
 *
1100
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1101
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1102
 * will only be active on vertices of Components such that
1103
 *
1104
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1105
 *
1106
 * (see main_vertex_shader)
1107
 *
1108
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1109
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1110
 */
1111
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1112
     {
1113
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1114
       {
1115
       if( mJobNode[0]==null )
1116
         {
1117
         setEffectAssociationNow(component, andAssociation, equAssociation);
1118
         }
1119
       else
1120
         {
1121
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1122
         }
1123
       }
1124
     }
1125

    
1126
///////////////////////////////////////////////////////////////////////////////////////////////////
1127
/**
1128
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1129
 * which can be independently textured.
1130
 *
1131
 * @return The number of Texture Components of this Mesh.
1132
 */
1133
   public int numTexComponents()
1134
     {
1135
     return mTexComponent.size();
1136
     }
1137

    
1138
///////////////////////////////////////////////////////////////////////////////////////////////////
1139
/**
1140
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1141
 * to which a VertexEffect can be addressed, independently of other vertices.
1142
 *
1143
 * @return The number of Effect Components of this Mesh.
1144
 */
1145
   public int numEffComponents()
1146
     {
1147
     return mEffComponent.size();
1148
     }
1149

    
1150
///////////////////////////////////////////////////////////////////////////////////////////////////
1151
/**
1152
 * Copy the Mesh.
1153
 *
1154
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1155
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
1156
 *             coordinates and effect associations, is always deep copied)
1157
 */
1158
   public abstract MeshBase copy(boolean deep);
1159
   }
(3-3/10)