Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 54ce4c6f

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

    
20
package org.distorted.library.mesh;
21

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

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

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

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

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

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

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

    
70
   private static final int BYTES_PER_FLOAT = 4;
71

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

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

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

    
91
   DeferredJobs.JobNode[] mJobNode;
92

    
93
   private static int[] mEquAssociationH = new int[EffectQueue.MAIN_VARIANTS];
94
   private static int[] mAndAssociationH = new int[EffectQueue.MAIN_VARIANTS];
95

    
96
   private static final int TEX_COMP_SIZE = 5; // 5 four-bytes entities inside the component
97

    
98
   private static class TexComponent
99
     {
100
     private int mEndIndex;
101
     private Static4D mTextureMap;
102

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

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

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

    
125
   private ArrayList<TexComponent> mTexComponent;
126
   private ArrayList<Integer> mEffComponent;
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

    
137
     mEquAssociation= new int[MAX_EFFECT_COMPONENTS];
138
     mAndAssociation= new int[MAX_EFFECT_COMPONENTS];
139

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

    
142
     for(int i=0; i<MAX_EFFECT_COMPONENTS; i++)
143
       {
144
       mAndAssociation[i] = DEFAULT_ASSOCIATION;
145
       mEquAssociation[i] = i;
146
       }
147

    
148
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
149
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
150
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
151
     }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// copy constructor
155

    
156
   MeshBase(MeshBase original, boolean deep)
157
     {
158
     mShowNormals= original.mShowNormals;
159
     mInflate    = original.mInflate;
160
     mNumVertices= original.mNumVertices;
161

    
162
     mAndAssociation= new int[MAX_EFFECT_COMPONENTS];
163
     System.arraycopy(original.mAndAssociation, 0, mAndAssociation, 0, MAX_EFFECT_COMPONENTS);
164
     mEquAssociation= new int[MAX_EFFECT_COMPONENTS];
165
     System.arraycopy(original.mEquAssociation, 0, mEquAssociation, 0, MAX_EFFECT_COMPONENTS);
166

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

    
181
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
182
     mTFO.invalidate();
183
     }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
   void copy(MeshBase original)
188
     {
189
     shallowCopy(original);
190

    
191
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
192
     mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
193
     System.arraycopy(original.mVertAttribs1,0,mVertAttribs1,0,mNumVertices*VERT1_ATTRIBS);
194
     mVBO1.invalidate();
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
   private void shallowCopy(MeshBase original)
200
     {
201
     int texComSize = original.mTexComponent.size();
202
     mTexComponent = new ArrayList<>();
203

    
204
     for(int i=0; i<texComSize; i++)
205
       {
206
       TexComponent comp = new TexComponent(original.mTexComponent.get(i));
207
       mTexComponent.add(comp);
208
       }
209

    
210
     mEffComponent = new ArrayList<>();
211
     mEffComponent.addAll(original.mEffComponent);
212

    
213
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
214
     mVertAttribs2= new float[mNumVertices*VERT2_ATTRIBS];
215
     System.arraycopy(original.mVertAttribs2,0,mVertAttribs2,0,mNumVertices*VERT2_ATTRIBS);
216
     mVBO2.invalidate();
217
     }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
   void mergeTexComponentsNow()
222
     {
223
     int num = mTexComponent.size();
224

    
225
     if( num>1 )
226
       {
227
       mTexComponent.clear();
228
       mTexComponent.add(new TexComponent(mNumVertices-1));
229

    
230
       mVBO2.invalidate();
231
       }
232
     }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

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

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

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

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

    
262
     Matrix.setIdentityM(matrix,0);
263
     effect.compute(uniforms,0,0,0);
264
     effect.apply(matrix, uniforms, 0);
265

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

    
271
       if( (andAssoc & mAndAssociation[i]) != 0 || (equAssoc == mEquAssociation[i]) )
272
         {
273
         applyMatrixToComponent(matrix,start,end);
274
         }
275
       }
276

    
277
     mVBO1.invalidate();
278
     }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

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

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

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

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

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

    
308
       mVertAttribs1[index+INF_ATTRIB  ] = matrix[0]*x + matrix[4]*y + matrix[ 8]*z;
309
       mVertAttribs1[index+INF_ATTRIB+1] = matrix[1]*x + matrix[5]*y + matrix[ 9]*z;
310
       mVertAttribs1[index+INF_ATTRIB+2] = matrix[2]*x + matrix[6]*y + matrix[10]*z;
311
       }
312
     }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
317
     {
318
     mAndAssociation[component] = andAssociation;
319
     mEquAssociation[component] = equAssociation;
320
     }
321

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

    
324
   int numTexComponents()
325
     {
326
     return mTexComponent.size();
327
     }
328

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

    
331
   int numEffComponents()
332
     {
333
     return mEffComponent.size();
334
     }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
// when a derived class is done computing its mesh, it has to call this method.
338

    
339
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
340
     {
341
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
342
     mVertAttribs1= vert1Attribs;
343
     mVertAttribs2= vert2Attribs;
344

    
345
     mTexComponent.add(new TexComponent(mNumVertices-1));
346
     mEffComponent.add(mNumVertices-1);
347

    
348
     mVBO1.invalidate();
349
     mVBO2.invalidate();
350
     }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

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

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

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

    
379
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
380
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
381

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

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

    
397
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
398
           {
399
           mAndAssociation[origEffComponents] = mesh.mAndAssociation[j];
400
           mEquAssociation[origEffComponents] = mesh.mEquAssociation[j];
401
           origEffComponents++;
402
           }
403
         }
404

    
405
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
406
       }
407

    
408
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
409
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
410
     numVertices = origVertices;
411

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

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

    
428
     for(int i=0; i<numMeshes; i++)
429
       {
430
       mesh = meshes[i];
431
       numVertices = mesh.mNumVertices;
432

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

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

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

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

    
463
     int endIndex, index=0, numEffComp = mEffComponent.size();
464

    
465
     for(int component=0; component<numEffComp; component++)
466
       {
467
       endIndex = mEffComponent.get(component);
468

    
469
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
470
       }
471

    
472
     mVertAttribs1 = newAttribs1;
473
     mVertAttribs2 = newAttribs2;
474
     mVBO1.invalidate();
475
     mVBO2.invalidate();
476
     }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479
// called from MeshJoined
480

    
481
   void join(MeshBase[] meshes)
482
     {
483
     boolean immediateJoin = true;
484

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

    
494
     if( immediateJoin ) joinAttribs(meshes);
495
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
496
     }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

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

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

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

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

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

    
537
       vertex= comp.mEndIndex+1;
538
       }
539

    
540
     mVBO2.invalidate();
541
     }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
   public static int getMaxEffComponents()
546
     {
547
     return MAX_EFFECT_COMPONENTS;
548
     }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
   public static void getUniforms(int mProgramH, int variant)
553
     {
554
     mEquAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComEquAssoc");
555
     mAndAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComAndAssoc");
556
     }
557

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

    
580
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
581
     }
582

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

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

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

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

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

    
640
     Log.d("mesh", sb.toString());
641
     }
642

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

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

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

    
672
     Log.d("mesh", sb.toString());
673
     }
674

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

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

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

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

    
735
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
736
     int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
737

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

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

    
758
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
759
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
760
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
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 void setInflate(float inflate)
770
     {
771
     mInflate = inflate;
772
     }
773

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

    
785
///////////////////////////////////////////////////////////////////////////////////////////////////
786
// Return the number of bytes read.
787

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

    
793
     try
794
       {
795
       stream.read(buffer);
796
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
797

    
798
       version = byteBuf.getInt(0);
799

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

    
818
     if( mNumVertices>0 && numEff>0 && numTex>0 )
819
       {
820
       int size = numEff+TEX_COMP_SIZE*numTex;
821
       float[] tmp = new float[size];
822
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
823
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
824

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

    
827
       try
828
         {
829
         stream.read(buffer);
830
         }
831
       catch(IOException e)
832
         {
833
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
834
         return 0;
835
         }
836

    
837
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
838
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
839

    
840
       floatBuf.get(tmp,0,size);
841
       floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
842
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
843

    
844
       TexComponent tex;
845
       int index, texComp;
846
       float x, y, z, w;
847

    
848
       for(texComp=0; texComp<numTex; texComp++)
849
         {
850
         index = (int)tmp[TEX_COMP_SIZE*texComp];
851

    
852
         x= tmp[TEX_COMP_SIZE*texComp+1];
853
         y= tmp[TEX_COMP_SIZE*texComp+2];
854
         z= tmp[TEX_COMP_SIZE*texComp+3];
855
         w= tmp[TEX_COMP_SIZE*texComp+4];
856

    
857
         tex = new TexComponent(index);
858
         tex.setMap(new Static4D(x,y,z,w));
859

    
860
         mTexComponent.add(tex);
861
         }
862

    
863
       for(int effComp=0; effComp<numEff; effComp++)
864
         {
865
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
866
         mEffComponent.add(index);
867
         }
868
       }
869

    
870
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
871
     }
872

    
873
///////////////////////////////////////////////////////////////////////////////////////////////////
874
// PUBLIC API
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876
/**
877
 * Write the Mesh to a File.
878
 */
879
   public void write(DataOutputStream stream)
880
     {
881
     TexComponent tex;
882

    
883
     int numTex = mTexComponent.size();
884
     int numEff = mEffComponent.size();
885

    
886
     try
887
       {
888
       stream.writeInt(1);  // version
889
       stream.writeInt(mNumVertices);
890
       stream.writeInt(numTex);
891
       stream.writeInt(numEff);
892

    
893
       for(int i=0; i<numTex; i++)
894
         {
895
         tex = mTexComponent.get(i);
896

    
897
         stream.writeFloat((float)tex.mEndIndex);
898
         stream.writeFloat(tex.mTextureMap.get0());
899
         stream.writeFloat(tex.mTextureMap.get1());
900
         stream.writeFloat(tex.mTextureMap.get2());
901
         stream.writeFloat(tex.mTextureMap.get3());
902
         }
903

    
904
       for(int i=0; i<numEff; i++)
905
         {
906
         stream.writeFloat((float)mEffComponent.get(i));
907
         }
908

    
909
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
910
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
911
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
912
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
913

    
914
       stream.write(vertBuf1.array());
915
       stream.write(vertBuf2.array());
916
       }
917
     catch(IOException ex)
918
       {
919
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
920
       }
921
     }
922

    
923
///////////////////////////////////////////////////////////////////////////////////////////////////
924
/**
925
 * When rendering this Mesh, do we want to render the Normal vectors as well?
926
 * <p>
927
 * Will work only on OpenGL ES >= 3.0 devices.
928
 *
929
 * @param show Controls if we render the Normal vectors or not.
930
 */
931
   public void setShowNormals(boolean show)
932
     {
933
     mShowNormals = show;
934
     }
935

    
936
///////////////////////////////////////////////////////////////////////////////////////////////////
937
/**
938
 * When rendering this mesh, should we also draw the normal vectors?
939
 *
940
 * @return <i>true</i> if we do render normal vectors
941
 */
942
   public boolean getShowNormals()
943
     {
944
     return mShowNormals;
945
     }
946

    
947
///////////////////////////////////////////////////////////////////////////////////////////////////
948
/**
949
 * Merge all texture components of this Mesh into a single one.
950
 */
951
   public void mergeTexComponents()
952
     {
953
     if( mJobNode[0]==null )
954
       {
955
       mergeTexComponentsNow();
956
       }
957
     else
958
       {
959
       mJobNode[0] = DeferredJobs.mergeTex(this);
960
       }
961
     }
962

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

    
979
///////////////////////////////////////////////////////////////////////////////////////////////////
980
/**
981
 * Release all internal resources.
982
 */
983
   public void markForDeletion()
984
     {
985
     mVertAttribs1 = null;
986
     mVertAttribs2 = null;
987

    
988
     mVBO1.markForDeletion();
989
     mVBO2.markForDeletion();
990
     mTFO.markForDeletion();
991
     }
992

    
993
///////////////////////////////////////////////////////////////////////////////////////////////////
994
/**
995
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
996
 * <p>
997
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
998
 * contains any Dynamics, they will be evaluated at 0.
999
 *
1000
 * @param effect List of Matrix Effects to apply to the Mesh.
1001
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1002
 * @param equAssoc 'equality' association which defines which components will be affected.
1003
 */
1004
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1005
     {
1006
     if( mJobNode[0]==null )
1007
       {
1008
       applyMatrix(effect,andAssoc,equAssoc);
1009
       }
1010
     else
1011
       {
1012
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1013
       }
1014
     }
1015

    
1016
///////////////////////////////////////////////////////////////////////////////////////////////////
1017
/**
1018
 * Apply a Vertex Effect to the vertex mesh.
1019
 * <p>
1020
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1021
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1022
 *
1023
 * We would call this several times building up a list of Effects to do. This list of effects gets
1024
 * lazily executed only when the Mesh is used for rendering for the first time.
1025
 *
1026
 * @param effect Vertex Effect to apply to the Mesh.
1027
 */
1028
   public void apply(VertexEffect effect)
1029
     {
1030
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1031
     }
1032

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

    
1068
///////////////////////////////////////////////////////////////////////////////////////////////////
1069
/**
1070
 * Return the texture map of one of the components.
1071
 *
1072
 * @param component The component number whose texture map we want to retrieve.
1073
 */
1074
   public Static4D getTextureMap(int component)
1075
     {
1076
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1077
     }
1078

    
1079
///////////////////////////////////////////////////////////////////////////////////////////////////
1080
/**
1081
 * Set Effect association.
1082
 *
1083
 * This creates an association between a Component of this Mesh and a Vertex Effect.
1084
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
1085
 * will only be active on vertices of Components such that
1086
 *
1087
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
1088
 *
1089
 * (see main_vertex_shader)
1090
 *
1091
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
1092
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
1093
 */
1094
   public void setEffectAssociation(int component, int andAssociation, int equAssociation)
1095
     {
1096
     if( component>=0 && component<MAX_EFFECT_COMPONENTS )
1097
       {
1098
       if( mJobNode[0]==null )
1099
         {
1100
         setEffectAssociationNow(component, andAssociation, equAssociation);
1101
         }
1102
       else
1103
         {
1104
         mJobNode[0] = DeferredJobs.effectAssoc(this,component,andAssociation,equAssociation);
1105
         }
1106
       }
1107
     }
1108

    
1109
///////////////////////////////////////////////////////////////////////////////////////////////////
1110
/**
1111
 * Copy the Mesh.
1112
 *
1113
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1114
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
1115
 *             coordinates and effect associations, is always deep copied)
1116
 */
1117
   public abstract MeshBase copy(boolean deep);
1118
   }
(2-2/9)