Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 62c869ad

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[] matrixP  = new float[16];
259
     float[] matrixV  = new float[16];
260
     float[] uniforms = new float[7];
261
     int start, end=-1, numComp = mEffComponent.size();
262

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

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

    
273
       if( (andAssoc & mAndAssociation[i]) != 0 || (equAssoc == mEquAssociation[i]) )
274
         {
275
         applyMatrixToComponent(matrixP, matrixV, start, end);
276
         }
277
       }
278

    
279
     mVBO1.invalidate();
280
     }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

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

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

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

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

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

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

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

    
319
       x = mVertAttribs1[index+INF_ATTRIB  ];
320
       y = mVertAttribs1[index+INF_ATTRIB+1];
321
       z = mVertAttribs1[index+INF_ATTRIB+2];
322

    
323
       mVertAttribs1[index+INF_ATTRIB  ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z;
324
       mVertAttribs1[index+INF_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z;
325
       mVertAttribs1[index+INF_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z;
326

    
327
       x = mVertAttribs1[index+INF_ATTRIB  ];
328
       y = mVertAttribs1[index+INF_ATTRIB+1];
329
       z = mVertAttribs1[index+INF_ATTRIB+2];
330

    
331
       float len2 = (float)Math.sqrt(x*x + y*y + z*z);
332

    
333
       if( len2>0.0f )
334
         {
335
         mVertAttribs1[index+INF_ATTRIB  ] /= len2;
336
         mVertAttribs1[index+INF_ATTRIB+1] /= len2;
337
         mVertAttribs1[index+INF_ATTRIB+2] /= len2;
338
         }
339
       }
340
     }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
345
     {
346
     mAndAssociation[component] = andAssociation;
347
     mEquAssociation[component] = equAssociation;
348
     }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
   int numTexComponents()
353
     {
354
     return mTexComponent.size();
355
     }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
   int numEffComponents()
360
     {
361
     return mEffComponent.size();
362
     }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
// when a derived class is done computing its mesh, it has to call this method.
366

    
367
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
368
     {
369
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
370
     mVertAttribs1= vert1Attribs;
371
     mVertAttribs2= vert2Attribs;
372

    
373
     mTexComponent.add(new TexComponent(mNumVertices-1));
374
     mEffComponent.add(mNumVertices-1);
375

    
376
     mVBO1.invalidate();
377
     mVBO2.invalidate();
378
     }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
   void joinAttribs(MeshBase[] meshes)
383
     {
384
     MeshBase mesh;
385
     TexComponent comp;
386
     int numMeshes = meshes.length;
387
     int numVertices,origVertices = mNumVertices;
388
     int origTexComponents,numTexComponents;
389
     int origEffComponents=0,numEffComponents;
390

    
391
     if( origVertices>0 )
392
       {
393
       origTexComponents = mTexComponent.size();
394
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
395
       mTexComponent.get(origTexComponents-1).mEndIndex = mNumVertices-1;
396
       origEffComponents = mEffComponent.size();
397
       mEffComponent.set(origEffComponents-1,mNumVertices-1);
398
       }
399

    
400
     for(int i=0; i<numMeshes; i++)
401
       {
402
       mesh = meshes[i];
403
       numTexComponents = mesh.mTexComponent.size();
404
       numEffComponents = mesh.mEffComponent.size();
405
       numVertices = mesh.mNumVertices;
406

    
407
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
408
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
409

    
410
       for(int j=0; j<numTexComponents; j++)
411
         {
412
         comp = new TexComponent(mesh.mTexComponent.get(j));
413
         comp.mEndIndex += (extraVerticesBefore+mNumVertices);
414
         if( j==numTexComponents-1) comp.mEndIndex += extraVerticesAfter;
415
         mTexComponent.add(comp);
416
         }
417

    
418
       for(int j=0; j<numEffComponents; j++)
419
         {
420
         int index = mesh.mEffComponent.get(j);
421
         index += (extraVerticesBefore+mNumVertices);
422
         if( j==numEffComponents-1 ) index += extraVerticesAfter;
423
         mEffComponent.add(index);
424

    
425
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
426
           {
427
           mAndAssociation[origEffComponents] = mesh.mAndAssociation[j];
428
           mEquAssociation[origEffComponents] = mesh.mEquAssociation[j];
429
           origEffComponents++;
430
           }
431
         }
432

    
433
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
434
       }
435

    
436
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
437
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
438
     numVertices = origVertices;
439

    
440
     if( origVertices>0 )
441
       {
442
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                          0, VERT1_ATTRIBS*numVertices);
443
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS            );
444
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                          0, VERT2_ATTRIBS*numVertices);
445
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS            );
446
       origVertices++;
447

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

    
456
     for(int i=0; i<numMeshes; i++)
457
       {
458
       mesh = meshes[i];
459
       numVertices = mesh.mNumVertices;
460

    
461
       if( origVertices>0 )
462
         {
463
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
464
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
465
         origVertices++;
466
         }
467
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
468
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
469
       origVertices+=numVertices;
470

    
471
       if( i<numMeshes-1 )
472
         {
473
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
474
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
475
         origVertices++;
476

    
477
         if( numVertices%2==1 )
478
           {
479
           System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
480
           System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
481
           origVertices++;
482
           }
483
         }
484
       }
485

    
486
     if( origVertices!=mNumVertices )
487
       {
488
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
489
       }
490

    
491
     int endIndex, index=0, numEffComp = mEffComponent.size();
492

    
493
     for(int component=0; component<numEffComp; component++)
494
       {
495
       endIndex = mEffComponent.get(component);
496

    
497
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
498
       }
499

    
500
     mVertAttribs1 = newAttribs1;
501
     mVertAttribs2 = newAttribs2;
502
     mVBO1.invalidate();
503
     mVBO2.invalidate();
504
     }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
// called from MeshJoined
508

    
509
   void join(MeshBase[] meshes)
510
     {
511
     boolean immediateJoin = true;
512

    
513
     for (MeshBase mesh : meshes)
514
       {
515
       if (mesh.mJobNode[0] != null)
516
         {
517
         immediateJoin = false;
518
         break;
519
         }
520
       }
521

    
522
     if( immediateJoin ) joinAttribs(meshes);
523
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
524
     }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
   void textureMap(Static4D[] maps, int startComponent)
529
     {
530
     int num_comp = mTexComponent.size();
531
     int num_maps = maps.length;
532
     int min = Math.min(num_comp-startComponent, num_maps);
533
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
534
     Static4D newMap, oldMap;
535
     TexComponent comp;
536
     float newW, newH, ratW, ratH, movX, movY;
537

    
538
     for(int i=0; i<min; i++)
539
       {
540
       newMap = maps[i];
541
       comp = mTexComponent.get(i+startComponent);
542

    
543
       if( newMap!=null )
544
         {
545
         newW = newMap.get2();
546
         newH = newMap.get3();
547

    
548
         if( newW!=0.0f && newH!=0.0f )
549
           {
550
           oldMap = comp.mTextureMap;
551
           ratW = newW/oldMap.get2();
552
           ratH = newH/oldMap.get3();
553
           movX = newMap.get0() - ratW*oldMap.get0();
554
           movY = newMap.get1() - ratH*oldMap.get1();
555

    
556
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
557
             {
558
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
559
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
560
             }
561
           comp.setMap(newMap);
562
           }
563
         }
564

    
565
       vertex= comp.mEndIndex+1;
566
       }
567

    
568
     mVBO2.invalidate();
569
     }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
   public static int getMaxEffComponents()
574
     {
575
     return MAX_EFFECT_COMPONENTS;
576
     }
577

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579

    
580
   public static void getUniforms(int mProgramH, int variant)
581
     {
582
     mEquAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComEquAssoc");
583
     mAndAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComAndAssoc");
584
     }
585

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

    
608
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
609
     }
610

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

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

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

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

    
662
     for(int i=0; i<mNumVertices; i++)
663
       {
664
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
665
       sb.append(' ');
666
       }
667

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

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

    
684
     for(int i=0; i<num; i++)
685
       {
686
       int end = mTexComponent.get(i).mEndIndex;
687
       sb.append("\n");
688

    
689
       for( ; vert<=end; vert++)
690
         {
691
         sb.append(' ');
692
         sb.append('(');
693
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
694
         sb.append(',');
695
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
696
         sb.append(')');
697
         }
698
       }
699

    
700
     Log.d("mesh", sb.toString());
701
     }
702

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

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

    
725
///////////////////////////////////////////////////////////////////////////////////////////////////
726
/**
727
 * Not part of public API, do not document (public only because has to be used from the main package)
728
 *
729
 * @y.exclude
730
 */
731
   public void send(int variant)
732
     {
733
     GLES30.glUniform1iv( mEquAssociationH[variant], MAX_EFFECT_COMPONENTS, mEquAssociation, 0);
734
     GLES30.glUniform1iv( mAndAssociationH[variant], MAX_EFFECT_COMPONENTS, mAndAssociation, 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 bindVertexAttribs(DistortedProgram program)
744
     {
745
     if( mJobNode[0]!=null )
746
       {
747
       mJobNode[0].execute();  // this will set itself to null
748
/*
749
       try
750
         {
751
         String name = "/sdcard/"+mNumVertices+".dmesh";
752
         DataOutputStream dos = new DataOutputStream(new FileOutputStream(name));
753
         write(dos);
754
         android.util.Log.e("mesh", "file written: "+name);
755
         }
756
       catch(FileNotFoundException ex)
757
         {
758
         android.util.Log.e("mesh", "file not found exception: "+ex.toString());
759
         }
760
 */
761
       }
762

    
763
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
764
     int index2 = mVBO2.createImmediately(mNumVertices*VERT2_SIZE, mVertAttribs2);
765

    
766
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
767
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
768
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
769
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF);
770
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
771
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
772
     GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
773
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
774
     }
775

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

    
786
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
787
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
788
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
789
     }
790

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

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

    
813
///////////////////////////////////////////////////////////////////////////////////////////////////
814
// Return the number of bytes read.
815

    
816
   int read(DataInputStream stream)
817
     {
818
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
819
     int version, numEff, numTex;
820

    
821
     try
822
       {
823
       stream.read(buffer);
824
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
825

    
826
       version = byteBuf.getInt(0);
827

    
828
       if( version==1 )
829
         {
830
         mNumVertices= byteBuf.getInt(4);
831
         numTex      = byteBuf.getInt(8);
832
         numEff      = byteBuf.getInt(12);
833
         }
834
       else
835
         {
836
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
837
         return 0;
838
         }
839
       }
840
     catch(IOException e)
841
       {
842
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
843
       return 0;
844
       }
845

    
846
     if( mNumVertices>0 && numEff>0 && numTex>0 )
847
       {
848
       int size = numEff+TEX_COMP_SIZE*numTex;
849
       float[] tmp = new float[size];
850
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
851
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
852

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

    
855
       try
856
         {
857
         stream.read(buffer);
858
         }
859
       catch(IOException e)
860
         {
861
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
862
         return 0;
863
         }
864

    
865
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
866
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
867

    
868
       floatBuf.get(tmp,0,size);
869
       floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
870
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
871

    
872
       TexComponent tex;
873
       int index, texComp;
874
       float x, y, z, w;
875

    
876
       for(texComp=0; texComp<numTex; texComp++)
877
         {
878
         index = (int)tmp[TEX_COMP_SIZE*texComp];
879

    
880
         x= tmp[TEX_COMP_SIZE*texComp+1];
881
         y= tmp[TEX_COMP_SIZE*texComp+2];
882
         z= tmp[TEX_COMP_SIZE*texComp+3];
883
         w= tmp[TEX_COMP_SIZE*texComp+4];
884

    
885
         tex = new TexComponent(index);
886
         tex.setMap(new Static4D(x,y,z,w));
887

    
888
         mTexComponent.add(tex);
889
         }
890

    
891
       for(int effComp=0; effComp<numEff; effComp++)
892
         {
893
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
894
         mEffComponent.add(index);
895
         }
896
       }
897

    
898
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
899
     }
900

    
901
///////////////////////////////////////////////////////////////////////////////////////////////////
902
// PUBLIC API
903
///////////////////////////////////////////////////////////////////////////////////////////////////
904
/**
905
 * Write the Mesh to a File.
906
 */
907
   public void write(DataOutputStream stream)
908
     {
909
     TexComponent tex;
910

    
911
     int numTex = mTexComponent.size();
912
     int numEff = mEffComponent.size();
913

    
914
     try
915
       {
916
       stream.writeInt(1);  // version
917
       stream.writeInt(mNumVertices);
918
       stream.writeInt(numTex);
919
       stream.writeInt(numEff);
920

    
921
       for(int i=0; i<numTex; i++)
922
         {
923
         tex = mTexComponent.get(i);
924

    
925
         stream.writeFloat((float)tex.mEndIndex);
926
         stream.writeFloat(tex.mTextureMap.get0());
927
         stream.writeFloat(tex.mTextureMap.get1());
928
         stream.writeFloat(tex.mTextureMap.get2());
929
         stream.writeFloat(tex.mTextureMap.get3());
930
         }
931

    
932
       for(int i=0; i<numEff; i++)
933
         {
934
         stream.writeFloat((float)mEffComponent.get(i));
935
         }
936

    
937
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
938
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
939
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
940
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
941

    
942
       stream.write(vertBuf1.array());
943
       stream.write(vertBuf2.array());
944
       }
945
     catch(IOException ex)
946
       {
947
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
948
       }
949
     }
950

    
951
///////////////////////////////////////////////////////////////////////////////////////////////////
952
/**
953
 * When rendering this Mesh, do we want to render the Normal vectors as well?
954
 * <p>
955
 * Will work only on OpenGL ES >= 3.0 devices.
956
 *
957
 * @param show Controls if we render the Normal vectors or not.
958
 */
959
   public void setShowNormals(boolean show)
960
     {
961
     mShowNormals = show;
962
     }
963

    
964
///////////////////////////////////////////////////////////////////////////////////////////////////
965
/**
966
 * When rendering this mesh, should we also draw the normal vectors?
967
 *
968
 * @return <i>true</i> if we do render normal vectors
969
 */
970
   public boolean getShowNormals()
971
     {
972
     return mShowNormals;
973
     }
974

    
975
///////////////////////////////////////////////////////////////////////////////////////////////////
976
/**
977
 * Merge all texture components of this Mesh into a single one.
978
 */
979
   public void mergeTexComponents()
980
     {
981
     if( mJobNode[0]==null )
982
       {
983
       mergeTexComponentsNow();
984
       }
985
     else
986
       {
987
       mJobNode[0] = DeferredJobs.mergeTex(this);
988
       }
989
     }
990

    
991
///////////////////////////////////////////////////////////////////////////////////////////////////
992
/**
993
 * Merge all effect components of this Mesh into a single one.
994
 */
995
   public void mergeEffComponents()
996
     {
997
     if( mJobNode[0]==null )
998
       {
999
       mergeEffComponentsNow();
1000
       }
1001
     else
1002
       {
1003
       mJobNode[0] = DeferredJobs.mergeEff(this);
1004
       }
1005
     }
1006

    
1007
///////////////////////////////////////////////////////////////////////////////////////////////////
1008
/**
1009
 * Release all internal resources.
1010
 */
1011
   public void markForDeletion()
1012
     {
1013
     mVertAttribs1 = null;
1014
     mVertAttribs2 = null;
1015

    
1016
     mVBO1.markForDeletion();
1017
     mVBO2.markForDeletion();
1018
     mTFO.markForDeletion();
1019
     }
1020

    
1021
///////////////////////////////////////////////////////////////////////////////////////////////////
1022
/**
1023
 * Apply a Matrix Effect to the components which match the (addAssoc,equAssoc) association.
1024
 * <p>
1025
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effect
1026
 * contains any Dynamics, they will be evaluated at 0.
1027
 *
1028
 * @param effect List of Matrix Effects to apply to the Mesh.
1029
 * @param andAssoc 'Logical AND' association which defines which components will be affected.
1030
 * @param equAssoc 'equality' association which defines which components will be affected.
1031
 */
1032
   public void apply(MatrixEffect effect, int andAssoc, int equAssoc)
1033
     {
1034
     if( mJobNode[0]==null )
1035
       {
1036
       applyMatrix(effect,andAssoc,equAssoc);
1037
       }
1038
     else
1039
       {
1040
       mJobNode[0] = DeferredJobs.matrix(this,effect,andAssoc,equAssoc);
1041
       }
1042
     }
1043

    
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045
/**
1046
 * Apply a Vertex Effect to the vertex mesh.
1047
 * <p>
1048
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
1049
 * contain any Dynamics, the Dynamics will be evaluated at 0.
1050
 *
1051
 * We would call this several times building up a list of Effects to do. This list of effects gets
1052
 * lazily executed only when the Mesh is used for rendering for the first time.
1053
 *
1054
 * @param effect Vertex Effect to apply to the Mesh.
1055
 */
1056
   public void apply(VertexEffect effect)
1057
     {
1058
     mJobNode[0] = DeferredJobs.vertex(this,effect);
1059
     }
1060

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

    
1096
///////////////////////////////////////////////////////////////////////////////////////////////////
1097
/**
1098
 * Return the texture map of one of the components.
1099
 *
1100
 * @param component The component number whose texture map we want to retrieve.
1101
 */
1102
   public Static4D getTextureMap(int component)
1103
     {
1104
     return (component>=0 && component<mTexComponent.size()) ? mTexComponent.get(component).mTextureMap : null;
1105
     }
1106

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

    
1137
///////////////////////////////////////////////////////////////////////////////////////////////////
1138
/**
1139
 * Copy the Mesh.
1140
 *
1141
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1142
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
1143
 *             coordinates and effect associations, is always deep copied)
1144
 */
1145
   public abstract MeshBase copy(boolean deep);
1146
   }
(2-2/9)