Project

General

Profile

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

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

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 ASSOC_UBO_BINDING  = 3;
50
   private static final int CENTER_UBO_BINDING = 4;
51
           static final int MAX_EFFECT_COMPONENTS= 100;
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 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 TEX_ATTRIB   = 0;
62
   static final int COM_ATTRIB   = TEX_DATA_SIZE;
63

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

    
68
   private static final int BYTES_PER_FLOAT = 4;
69

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

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

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

    
88
   DeferredJobs.JobNode[] mJobNode;
89

    
90
   private static final int TEX_COMP_SIZE = 5; // 5 four-byte entities inside the component
91

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

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

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

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

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

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

    
131
     mJobNode = new DeferredJobs.JobNode[1];
132

    
133
     mUBA = new UniformBlockAssociation();
134
     mUBC = new UniformBlockCenter();
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
     mUBA = new UniformBlockAssociation(original.mUBA);
151
     mUBC = new UniformBlockCenter(original.mUBC);
152

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

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

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

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

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

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

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

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

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

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

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

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

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

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

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

    
264
     mVBO1.invalidate();
265
     }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

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

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

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

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

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

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

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

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
309
     {
310
     mUBA.setEffectAssociationNow(component, andAssociation, equAssociation);
311
     }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
   void setEffectCenterNow(int component, float x, float y, float z)
316
     {
317
     mUBC.setEffectCenterNow(component, x, y, z);
318
     }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321
// when a derived class is done computing its mesh, it has to call this method.
322

    
323
   void setAttribs(float[] vert1Attribs, float[] vert2Attribs)
324
     {
325
     mNumVertices = vert1Attribs.length/VERT1_ATTRIBS;
326
     mVertAttribs1= vert1Attribs;
327
     mVertAttribs2= vert2Attribs;
328

    
329
     mTexComponent.add(new TexComponent(mNumVertices-1));
330
     mEffComponent.add(mNumVertices-1);
331

    
332
     mVBO1.invalidate();
333
     mVBO2.invalidate();
334
     }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
   void joinAttribs(MeshBase[] meshes)
339
     {
340
     MeshBase mesh;
341
     TexComponent comp;
342
     int numMeshes = meshes.length;
343
     int numVertices,origVertices = mNumVertices;
344
     int origTexComponents,numTexComponents;
345
     int origEffComponents=0,numEffComponents;
346

    
347
     if( origVertices>0 )
348
       {
349
       origTexComponents = mTexComponent.size();
350
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
351
       mTexComponent.get(origTexComponents-1).mEndIndex = mNumVertices-1;
352
       origEffComponents = mEffComponent.size();
353
       mEffComponent.set(origEffComponents-1,mNumVertices-1);
354
       }
355

    
356
     for(int i=0; i<numMeshes; i++)
357
       {
358
       mesh = meshes[i];
359
       numTexComponents = mesh.mTexComponent.size();
360
       numEffComponents = mesh.mEffComponent.size();
361
       numVertices = mesh.mNumVertices;
362

    
363
       int extraVerticesBefore = mNumVertices==0 ? 0:1;
364
       int extraVerticesAfter  = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1);
365

    
366
       for(int j=0; j<numTexComponents; j++)
367
         {
368
         comp = new TexComponent(mesh.mTexComponent.get(j));
369
         comp.mEndIndex += (extraVerticesBefore+mNumVertices);
370
         if( j==numTexComponents-1) comp.mEndIndex += extraVerticesAfter;
371
         mTexComponent.add(comp);
372
         }
373

    
374
       for(int j=0; j<numEffComponents; j++)
375
         {
376
         int index = mesh.mEffComponent.get(j);
377
         index += (extraVerticesBefore+mNumVertices);
378
         if( j==numEffComponents-1 ) index += extraVerticesAfter;
379
         mEffComponent.add(index);
380

    
381
         if( origEffComponents<MAX_EFFECT_COMPONENTS )
382
           {
383
           mUBA.copy(origEffComponents, mesh.mUBA, j);
384
           mUBC.copy(origEffComponents, mesh.mUBC, j);
385
           origEffComponents++;
386
           }
387
         }
388

    
389
       mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
390
       }
391

    
392
     float[] newAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
393
     float[] newAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
394
     numVertices = origVertices;
395

    
396
     if( origVertices>0 )
397
       {
398
       System.arraycopy(mVertAttribs1,                              0, newAttribs1,                          0, VERT1_ATTRIBS*numVertices);
399
       System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS            );
400
       System.arraycopy(mVertAttribs2,                              0, newAttribs2,                          0, VERT2_ATTRIBS*numVertices);
401
       System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS            );
402
       origVertices++;
403

    
404
       if( numVertices%2==1 )
405
         {
406
         System.arraycopy(mVertAttribs1, VERT1_ATTRIBS*(origVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
407
         System.arraycopy(mVertAttribs2, VERT2_ATTRIBS*(origVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
408
         origVertices++;
409
         }
410
       }
411

    
412
     for(int i=0; i<numMeshes; i++)
413
       {
414
       mesh = meshes[i];
415
       numVertices = mesh.mNumVertices;
416

    
417
       if( origVertices>0 )
418
         {
419
         System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
420
         System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
421
         origVertices++;
422
         }
423
       System.arraycopy(mesh.mVertAttribs1, 0, newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS*numVertices);
424
       System.arraycopy(mesh.mVertAttribs2, 0, newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS*numVertices);
425
       origVertices+=numVertices;
426

    
427
       if( i<numMeshes-1 )
428
         {
429
         System.arraycopy(mesh.mVertAttribs1, VERT1_ATTRIBS*(numVertices-1), newAttribs1, VERT1_ATTRIBS*origVertices, VERT1_ATTRIBS);
430
         System.arraycopy(mesh.mVertAttribs2, VERT2_ATTRIBS*(numVertices-1), newAttribs2, VERT2_ATTRIBS*origVertices, VERT2_ATTRIBS);
431
         origVertices++;
432

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

    
442
     if( origVertices!=mNumVertices )
443
       {
444
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
445
       }
446

    
447
     int endIndex, index=0, numEffComp = mEffComponent.size();
448

    
449
     for(int component=0; component<numEffComp; component++)
450
       {
451
       endIndex = mEffComponent.get(component);
452

    
453
       for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component;
454
       }
455

    
456
     mVertAttribs1 = newAttribs1;
457
     mVertAttribs2 = newAttribs2;
458
     mVBO1.invalidate();
459
     mVBO2.invalidate();
460
     }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463
// called from MeshJoined
464

    
465
   void join(MeshBase[] meshes)
466
     {
467
     boolean immediateJoin = true;
468

    
469
     for (MeshBase mesh : meshes)
470
       {
471
       if (mesh.mJobNode[0] != null)
472
         {
473
         immediateJoin = false;
474
         break;
475
         }
476
       }
477

    
478
     if( immediateJoin ) joinAttribs(meshes);
479
     else                mJobNode[0] = DeferredJobs.join(this,meshes);
480
     }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
   void textureMap(Static4D[] maps, int startComponent)
485
     {
486
     int num_comp = mTexComponent.size();
487
     int num_maps = maps.length;
488
     int min = Math.min(num_comp-startComponent, num_maps);
489
     int vertex = startComponent>0 ? mTexComponent.get(startComponent-1).mEndIndex+1 : 0;
490
     Static4D newMap, oldMap;
491
     TexComponent comp;
492
     float newW, newH, ratW, ratH, movX, movY;
493

    
494
     for(int i=0; i<min; i++)
495
       {
496
       newMap = maps[i];
497
       comp = mTexComponent.get(i+startComponent);
498

    
499
       if( newMap!=null )
500
         {
501
         newW = newMap.get2();
502
         newH = newMap.get3();
503

    
504
         if( newW!=0.0f && newH!=0.0f )
505
           {
506
           oldMap = comp.mTextureMap;
507
           ratW = newW/oldMap.get2();
508
           ratH = newH/oldMap.get3();
509
           movX = newMap.get0() - ratW*oldMap.get0();
510
           movY = newMap.get1() - ratH*oldMap.get1();
511

    
512
           for( int index=vertex*VERT2_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT2_ATTRIBS)
513
             {
514
             mVertAttribs2[index  ] = ratW*mVertAttribs2[index  ] + movX;
515
             mVertAttribs2[index+1] = ratH*mVertAttribs2[index+1] + movY;
516
             }
517
           comp.setMap(newMap);
518
           }
519
         }
520

    
521
       vertex= comp.mEndIndex+1;
522
       }
523

    
524
     mVBO2.invalidate();
525
     }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

    
529
   public static int getMaxEffComponents()
530
     {
531
     return MAX_EFFECT_COMPONENTS;
532
     }
533

    
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
/**
536
 * Not part of public API, do not document (public only because has to be used from the main package)
537
 *
538
 * @y.exclude
539
 */
540
   public void copyTransformToVertex()
541
     {
542
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0,
543
                                                              TRAN_SIZE*mNumVertices, GLES30.GL_MAP_READ_BIT);
544
     if( buffer!=null )
545
       {
546
       FloatBuffer feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
547
       feedback.get(mVertAttribs1,0,VERT1_ATTRIBS*mNumVertices);
548
       mVBO1.updateFloat(mVertAttribs1);
549
       }
550
     else
551
       {
552
       int error = GLES30.glGetError();
553
       Log.e("mesh", "failed to map tf buffer, error="+error);
554
       }
555

    
556
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
557
     }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560
/**
561
 * Not part of public API, do not document (public only because has to be used from the main package)
562
 *
563
 * @y.exclude
564
 */
565
   public void debug()
566
     {
567
     if( mJobNode[0]!=null )
568
       {
569
       mJobNode[0].print(0);
570
       }
571
     else
572
       {
573
       android.util.Log.e("mesh", "JobNode null");
574
       }
575
     }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578
/**
579
 * Not part of public API, do not document (public only because has to be used from the main package)
580
 *
581
 * @y.exclude
582
 */
583
   public void printPos()
584
     {
585
     StringBuilder sb = new StringBuilder();
586

    
587
     for(int i=0; i<mNumVertices; i++)
588
       {
589
       sb.append('(');
590
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB  ]);
591
       sb.append(',');
592
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+1]);
593
       sb.append(',');
594
       sb.append(mVertAttribs1[VERT1_ATTRIBS*i+POS_ATTRIB+2]);
595
       sb.append(") ");
596
       }
597
     Log.d("mesh", sb.toString());
598
     }
599

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

    
610
     for(int i=0; i<mNumVertices; i++)
611
       {
612
       sb.append(mVertAttribs2[VERT2_ATTRIBS*i+COM_ATTRIB]);
613
       sb.append(' ');
614
       }
615

    
616
     Log.d("mesh", sb.toString());
617
     }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620
/**
621
 * Not part of public API, do not document (public only because has to be used from the main package)
622
 *
623
 * @y.exclude
624
 */
625
   public void printTex()
626
     {
627
     int vert=0;
628
     int num = numTexComponents();
629
     StringBuilder sb = new StringBuilder();
630
     sb.append("tex components: ").append(num);
631

    
632
     for(int i=0; i<num; i++)
633
       {
634
       int end = mTexComponent.get(i).mEndIndex;
635
       sb.append("\n");
636

    
637
       for( ; vert<=end; vert++)
638
         {
639
         sb.append(' ');
640
         sb.append('(');
641
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB]);
642
         sb.append(',');
643
         sb.append(mVertAttribs2[VERT2_ATTRIBS*vert+TEX_ATTRIB+1]);
644
         sb.append(')');
645
         }
646
       }
647

    
648
     Log.d("mesh", sb.toString());
649
     }
650

    
651
///////////////////////////////////////////////////////////////////////////////////////////////////
652
/**
653
 * Not part of public API, do not document (public only because has to be used from the main package)
654
 *
655
 * @y.exclude
656
 */
657
   public int getTFO()
658
     {
659
     return mTFO.createImmediatelyFloat(mNumVertices*TRAN_SIZE, null);
660
     }
661

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

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674
/**
675
 * Not part of public API, do not document (public only because has to be used from the main package)
676
 *
677
 * @y.exclude
678
 */
679
   public void send(int programH)
680
     {
681
     int indexA = mUBA.getIndex();
682
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA);
683
     GLES30.glUniformBlockBinding(programH, ASSOC_UBO_BINDING, indexA);
684

    
685
     int indexC = mUBC.getIndex();
686
     GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC);
687
     GLES30.glUniformBlockBinding(programH, CENTER_UBO_BINDING, indexC);
688
     }
689

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

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

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

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

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

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

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

    
765
///////////////////////////////////////////////////////////////////////////////////////////////////
766
// Return the number of bytes read.
767

    
768
   int read(DataInputStream stream)
769
     {
770
     byte[] buffer = new byte[BYTES_PER_FLOAT*4];
771
     int version, numEff, numTex;
772

    
773
     try
774
       {
775
       stream.read(buffer);
776
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
777

    
778
       version = byteBuf.getInt(0);
779

    
780
       if( version==1 || version==2 )
781
         {
782
         mNumVertices= byteBuf.getInt(4);
783
         numTex      = byteBuf.getInt(8);
784
         numEff      = byteBuf.getInt(12);
785
         }
786
       else
787
         {
788
         android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
789
         return 0;
790
         }
791
       }
792
     catch(IOException e)
793
       {
794
       android.util.Log.e("mesh", "IOException1 trying to read file: "+e.toString());
795
       return 0;
796
       }
797

    
798
     if( mNumVertices>0 && numEff>0 && numTex>0 )
799
       {
800
       int size = numEff+TEX_COMP_SIZE*numTex;
801
       float[] tmp = new float[size];
802
       mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices];
803
       mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices];
804

    
805
       // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
806
       int vert1InFile = version==1 ? VERT1_ATTRIBS+3 : VERT1_ATTRIBS;
807

    
808
       buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(vert1InFile+VERT2_ATTRIBS))];
809

    
810
       try
811
         {
812
         stream.read(buffer);
813
         }
814
       catch(IOException e)
815
         {
816
         android.util.Log.e("mesh", "IOException2 trying to read file: "+e.toString());
817
         return 0;
818
         }
819

    
820
       ByteBuffer byteBuf = ByteBuffer.wrap(buffer);
821
       FloatBuffer floatBuf = byteBuf.asFloatBuffer();
822

    
823
       floatBuf.get(tmp,0,size);
824

    
825
       switch(version)
826
         {
827
         case 1 : int index = floatBuf.position();
828

    
829
                  for(int vert=0; vert<mNumVertices; vert++)
830
                    {
831
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf.get(index+POS_ATTRIB  );
832
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1);
833
                    mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2);
834
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf.get(index+NOR_ATTRIB  );
835
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1);
836
                    mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2);
837
                    index+=vert1InFile;
838
                    }
839
                  floatBuf.position(index);
840
                  break;
841
         case 2 : floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices);
842
                  break;
843
         default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version));
844
                  return 0;
845
         }
846

    
847
       floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices);
848

    
849
       TexComponent tex;
850
       int index, texComp;
851
       float x, y, z, w;
852

    
853
       for(texComp=0; texComp<numTex; texComp++)
854
         {
855
         index = (int)tmp[TEX_COMP_SIZE*texComp];
856

    
857
         x= tmp[TEX_COMP_SIZE*texComp+1];
858
         y= tmp[TEX_COMP_SIZE*texComp+2];
859
         z= tmp[TEX_COMP_SIZE*texComp+3];
860
         w= tmp[TEX_COMP_SIZE*texComp+4];
861

    
862
         tex = new TexComponent(index);
863
         tex.setMap(new Static4D(x,y,z,w));
864

    
865
         mTexComponent.add(tex);
866
         }
867

    
868
       for(int effComp=0; effComp<numEff; effComp++)
869
         {
870
         index = (int)tmp[TEX_COMP_SIZE*texComp + effComp ];
871
         mEffComponent.add(index);
872
         }
873
       }
874

    
875
     return BYTES_PER_FLOAT*( 4 + numEff+TEX_COMP_SIZE*numTex + VERT1_ATTRIBS*mNumVertices + VERT2_ATTRIBS*mNumVertices );
876
     }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879
/**
880
 * @y.exclude
881
 */
882
   public int getLastVertexEff(int effComponent)
883
     {
884
     if( effComponent>=0 && effComponent<mTexComponent.size() )
885
       {
886
       return mEffComponent.get(effComponent);
887
       }
888

    
889
     return 0;
890
     }
891

    
892
///////////////////////////////////////////////////////////////////////////////////////////////////
893
/**
894
 * @y.exclude
895
 */
896
   public int getLastVertexTex(int texComponent)
897
     {
898
     if( texComponent>=0 && texComponent<mTexComponent.size() )
899
       {
900
       return mTexComponent.get(texComponent).mEndIndex;
901
       }
902

    
903
     return 0;
904
     }
905

    
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907
// PUBLIC API
908
///////////////////////////////////////////////////////////////////////////////////////////////////
909
/**
910
 * Write the Mesh to a File.
911
 */
912
   public void write(DataOutputStream stream)
913
     {
914
     TexComponent tex;
915

    
916
     int numTex = mTexComponent.size();
917
     int numEff = mEffComponent.size();
918

    
919
     try
920
       {
921
       stream.writeInt(2);  // version
922
       stream.writeInt(mNumVertices);
923
       stream.writeInt(numTex);
924
       stream.writeInt(numEff);
925

    
926
       for(int i=0; i<numTex; i++)
927
         {
928
         tex = mTexComponent.get(i);
929

    
930
         stream.writeFloat((float)tex.mEndIndex);
931
         stream.writeFloat(tex.mTextureMap.get0());
932
         stream.writeFloat(tex.mTextureMap.get1());
933
         stream.writeFloat(tex.mTextureMap.get2());
934
         stream.writeFloat(tex.mTextureMap.get3());
935
         }
936

    
937
       for(int i=0; i<numEff; i++)
938
         {
939
         stream.writeFloat((float)mEffComponent.get(i));
940
         }
941

    
942
       ByteBuffer vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*mNumVertices);
943
       vertBuf1.asFloatBuffer().put(mVertAttribs1);
944
       ByteBuffer vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*mNumVertices);
945
       vertBuf2.asFloatBuffer().put(mVertAttribs2);
946

    
947
       stream.write(vertBuf1.array());
948
       stream.write(vertBuf2.array());
949
       }
950
     catch(IOException ex)
951
       {
952
       android.util.Log.e("mesh", "IOException trying to write a mesh: "+ex.toString());
953
       }
954
     }
955

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

    
969
///////////////////////////////////////////////////////////////////////////////////////////////////
970
/**
971
 * When rendering this mesh, should we also draw the normal vectors?
972
 *
973
 * @return <i>true</i> if we do render normal vectors
974
 */
975
   public boolean getShowNormals()
976
     {
977
     return mShowNormals;
978
     }
979

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

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

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013
/**
1014
 * Release all internal resources.
1015
 */
1016
   public void markForDeletion()
1017
     {
1018
     mVertAttribs1 = null;
1019
     mVertAttribs2 = null;
1020

    
1021
     mVBO1.markForDeletion();
1022
     mVBO2.markForDeletion();
1023
     mTFO.markForDeletion();
1024
     }
1025

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

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

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

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

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

    
1142
///////////////////////////////////////////////////////////////////////////////////////////////////
1143
/**
1144
 * Return the number of texture components, i.e. individual subsets of the whole set of vertices
1145
 * which can be independently textured.
1146
 *
1147
 * @return The number of Texture Components of this Mesh.
1148
 */
1149
   public int numTexComponents()
1150
     {
1151
     return mTexComponent.size();
1152
     }
1153

    
1154
///////////////////////////////////////////////////////////////////////////////////////////////////
1155
/**
1156
 * Return the number of 'effect' components, i.e. individual subsets of the whole set of vertices
1157
 * to which a VertexEffect can be addressed, independently of other vertices.
1158
 *
1159
 * @return The number of Effect Components of this Mesh.
1160
 */
1161
   public int numEffComponents()
1162
     {
1163
     return mEffComponent.size();
1164
     }
1165

    
1166
///////////////////////////////////////////////////////////////////////////////////////////////////
1167
/**
1168
 * Copy the Mesh.
1169
 *
1170
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
1171
 *             and normals (the rest, in particular the mVertAttribs2 containing texture
1172
 *             coordinates and effect associations, is always deep copied)
1173
 */
1174
   public abstract MeshBase copy(boolean deep);
1175
   }
(2-2/11)