Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 277eddbb

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

    
25
import org.distorted.library.effect.MatrixEffect;
26
import org.distorted.library.main.InternalBuffer;
27
import org.distorted.library.program.DistortedProgram;
28
import org.distorted.library.type.Static4D;
29

    
30
import java.util.ArrayList;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
35
 * <p>
36
 * If you want to render to a particular shape, extend from here, construct a float array
37
 * containing per-vertex attributes, and call back setAttribs().
38
 */
39
public abstract class MeshBase
40
   {
41
   // sizes of attributes of an individual vertex.
42
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
43
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
44
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
45
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
46

    
47
   static final int POS_ATTRIB   = 0;
48
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
49
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
50
   static final int TEX_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
51
   static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE;  // number of attributes of a 'normal' vertex
52
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE;                                  // number of attributes of a transform feedback vertex
53

    
54
   private static final int BYTES_PER_FLOAT = 4;
55

    
56
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
57
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
58
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
59
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
60
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
61
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
62

    
63
   private boolean mShowNormals;      // when rendering this mesh, draw normal vectors?
64
   private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
65
   private int mNumVertices;
66
   private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
67
   private float mInflate;
68
   private float mStretchX, mStretchY, mStretchZ;
69

    
70
   private static class Component
71
     {
72
     private int mEndIndex;
73
     private Static4D mTextureMap;
74

    
75
     Component(int end)
76
       {
77
       mEndIndex  = end;
78
       mTextureMap= new Static4D(0,0,1,1);
79
       }
80
     Component(Component original)
81
       {
82
       mEndIndex = original.mEndIndex;
83

    
84
       float x = original.mTextureMap.get0();
85
       float y = original.mTextureMap.get1();
86
       float z = original.mTextureMap.get2();
87
       float w = original.mTextureMap.get3();
88
       mTextureMap = new Static4D(x,y,z,w);
89
       }
90

    
91
     void setMap(Static4D map)
92
       {
93
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
94
       }
95
     }
96

    
97
   private ArrayList<Component> mComponent;
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
   MeshBase()
102
     {
103
     mStretchX = 1.0f;
104
     mStretchY = 1.0f;
105
     mStretchZ = 1.0f;
106

    
107
     mShowNormals = false;
108
     mInflate     = 0.0f;
109
     mComponent   = new ArrayList<>();
110

    
111
     mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
112
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
113
     }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// copy constructor
117

    
118
   MeshBase(MeshBase original)
119
     {
120
     mStretchX = original.mStretchX;
121
     mStretchY = original.mStretchY;
122
     mStretchZ = original.mStretchZ;
123

    
124
     mShowNormals = original.mShowNormals;
125
     mInflate     = original.mInflate;
126

    
127
     int size = original.mComponent.size();
128
     mComponent = new ArrayList<>();
129
     for(int i=0; i<size; i++)
130
       {
131
       Component comp = new Component(original.mComponent.get(i));
132
       mComponent.add(comp);
133
       }
134

    
135
     mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
136
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
137

    
138
     System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS);
139
     setAttribs(mVertAttribs);
140
     }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
   int numComponents()
145
     {
146
     return mComponent.size();
147
     }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// when a derived class is done computing its mesh, it has to call this method.
151

    
152
   void setAttribs(float[] vertexAttribs)
153
     {
154
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
155
     mVertAttribs = vertexAttribs;
156

    
157
     mComponent.add(new Component(mNumVertices));
158

    
159
     mVBO.invalidate();
160
     mTFO.invalidate();
161
     }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// called from MeshJoined
165

    
166
   void join(MeshBase[] meshes)
167
     {
168
     MeshBase mesh;
169
     Component comp;
170
     int numComponents, numVertices, numMeshes = meshes.length;
171
     int origVertices = mNumVertices;
172

    
173
     // compute new numVertices; take care of Components
174

    
175
     if( origVertices>0 )
176
       {
177
       numComponents = mComponent.size();
178
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
179
       mComponent.get(numComponents-1).mEndIndex = mNumVertices;
180
       }
181

    
182
     for(int i=0; i<numMeshes; i++)
183
       {
184
       mesh = meshes[i];
185
       numComponents = mesh.mComponent.size();
186

    
187
       for(int j=0; j<numComponents; j++)
188
         {
189
         comp = new Component(mesh.mComponent.get(j));
190
         comp.mEndIndex += mNumVertices;
191
         mComponent.add(comp);
192
         }
193

    
194
       numVertices = mesh.mNumVertices;
195

    
196
       if( mNumVertices==0 )
197
         {
198
         if( numMeshes>1 )
199
            mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1);
200
         else
201
            mNumVertices +=  numVertices;
202
         }
203
       else if( i==numMeshes-1 ) mNumVertices += (numVertices+1);
204
       else                      mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
205
       }
206

    
207
     // allocate new attrib array
208
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
209
     numVertices = origVertices;
210

    
211
     if( origVertices>0 )
212
       {
213
       System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*numVertices);
214
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
215
       origVertices++;
216

    
217
       if( numVertices%2==1 )
218
         {
219
         System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
220
         origVertices++;
221
         }
222
       }
223

    
224
     for(int i=0; i<numMeshes; i++)
225
       {
226
       mesh = meshes[i];
227
       numVertices = mesh.mNumVertices;
228

    
229
       if( origVertices>0 )
230
         {
231
         System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
232
         origVertices++;
233
         }
234
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*numVertices);
235
       origVertices+=numVertices;
236

    
237
       if( i<numMeshes-1 )
238
         {
239
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
240
         origVertices++;
241

    
242
         if( numVertices%2==1 )
243
           {
244
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
245
           origVertices++;
246
           }
247
         }
248
       }
249

    
250
     if( origVertices!=mNumVertices )
251
       {
252
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
253
       }
254

    
255
     mVertAttribs = newAttribs;
256

    
257
     mVBO.invalidate();
258
     }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
/**
262
 * Not part of public API, do not document (public only because has to be used from the main package)
263
 *
264
 * @y.exclude
265
 */
266
   public int getTFO()
267
     {
268
     return mTFO.getIndex();
269
     }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
/**
273
 * Not part of public API, do not document (public only because has to be used from the main package)
274
 *
275
 * @y.exclude
276
 */
277
   public int getNumVertices()
278
     {
279
     return mNumVertices;
280
     }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
/**
284
 * Not part of public API, do not document (public only because has to be used from the main package)
285
 *
286
 * @y.exclude
287
 */
288
   public void bindVertexAttribs(DistortedProgram program)
289
     {
290
     int index = mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs);
291

    
292
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
293
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
294
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
295
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
296
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
297
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
298
     }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
/**
302
 * Not part of public API, do not document (public only because has to be used from the main package)
303
 *
304
 * @y.exclude
305
 */
306
   public void bindTransformAttribs(DistortedProgram program)
307
     {
308
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
309

    
310
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
311
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
312
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
313
     }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
/**
317
 * Not part of public API, do not document (public only because has to be used from the main package)
318
 *
319
 * @y.exclude
320
 */
321
   public void setInflate(float inflate)
322
     {
323
     mInflate = inflate;
324
     }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
/**
328
 * Not part of public API, do not document (public only because has to be used from the main package)
329
 *
330
 * @y.exclude
331
 */
332
   public float getInflate()
333
     {
334
     return mInflate;
335
     }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
// PUBLIC API
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
/**
341
 * When rendering this Mesh, do we want to render the Normal vectors as well?
342
 * <p>
343
 * Will work only on OpenGL ES >= 3.0 devices.
344
 *
345
 * @param show Controls if we render the Normal vectors or not.
346
 */
347
   public void setShowNormals(boolean show)
348
     {
349
     mShowNormals = show;
350
     }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353
/**
354
 * When rendering this mesh, should we also draw the normal vectors?
355
 *
356
 * @return <i>true</i> if we do render normal vectors
357
 */
358
   public boolean getShowNormals()
359
     {
360
     return mShowNormals;
361
     }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364
/**
365
 * Release all internal resources.
366
 */
367
   public void markForDeletion()
368
     {
369
     mVertAttribs = null;
370

    
371
     mVBO.markForDeletion();
372
     mTFO.markForDeletion();
373
     }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
/**
377
 * Apply all Effects to the vertex mesh. Overwrite the mesh in place.
378
 * <p>
379
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
380
 * contain any Dynamics, they will be evaluated at 0.
381
 *
382
 * Please note that calling this once with the complete list of Effects will be much faster than
383
 * calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices
384
 * each time.
385
 *
386
 * @param effects List of Matrix Effects to apply to the Mesh.
387
 */
388
   public void apply(MatrixEffect[] effects)
389
     {
390
     float[][] matrix = new float[effects.length][16];
391
     float[] tmp;
392
     float[] array = new float[7];
393
     float x,y,z;
394
     int numEffects = 0;
395

    
396
     for(MatrixEffect eff: effects)
397
       {
398
       if( eff!=null )
399
         {
400
         Matrix.setIdentityM(matrix[numEffects],0);
401
         eff.compute(array,0,0,0);
402
         eff.apply(matrix[numEffects], array, 0);
403
         numEffects++;
404
         }
405
       }
406

    
407
     for(int index=0; index<mNumVertices*VERT_ATTRIBS; index+=VERT_ATTRIBS )
408
       {
409
       for(int mat=0; mat<numEffects; mat++)
410
         {
411
         tmp = matrix[mat];
412

    
413
         x = mVertAttribs[index+POS_ATTRIB  ];
414
         y = mVertAttribs[index+POS_ATTRIB+1];
415
         z = mVertAttribs[index+POS_ATTRIB+2];
416

    
417
         mVertAttribs[index+POS_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z + tmp[12];
418
         mVertAttribs[index+POS_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z + tmp[13];
419
         mVertAttribs[index+POS_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z + tmp[14];
420

    
421
         x = mVertAttribs[index+NOR_ATTRIB  ];
422
         y = mVertAttribs[index+NOR_ATTRIB+1];
423
         z = mVertAttribs[index+NOR_ATTRIB+2];
424

    
425
         mVertAttribs[index+NOR_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
426
         mVertAttribs[index+NOR_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
427
         mVertAttribs[index+NOR_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
428

    
429
         x = mVertAttribs[index+INF_ATTRIB  ];
430
         y = mVertAttribs[index+INF_ATTRIB+1];
431
         z = mVertAttribs[index+INF_ATTRIB+2];
432

    
433
         mVertAttribs[index+INF_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
434
         mVertAttribs[index+INF_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
435
         mVertAttribs[index+INF_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
436
         }
437
       }
438

    
439
     mVBO.invalidate();
440
     }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
/**
444
 * Sets texture maps for (some of) the components of this mesh.
445
 * <p>
446
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
447
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
448
 * upper-left quadrant of the texture.
449
 * <p>
450
 * Probably the most common user case would be sending as many maps as there are components in this
451
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
452
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
453
 * of the 2nd and 4rd one, call this with
454
 * maps = new Static4D[4]
455
 * maps[0] = null
456
 * maps[1] = the map for the 2nd component
457
 * maps[2] = null
458
 * maps[3] = the map for the 4th component
459
 *
460
 * A map's width and height have to be non-zero (but can be negative!)
461
 *
462
 * @param maps List of texture maps to apply to the texture's components.
463
 */
464
   public void setTextureMap(Static4D[] maps)
465
     {
466
     int num_comp = mComponent.size();
467
     int num_maps = maps.length;
468
     int min = Math.min(num_comp, num_maps);
469
     int vertex = 0;
470
     Static4D newMap, oldMap;
471
     Component comp;
472
     float newW, newH, ratW, ratH, movX, movY;
473

    
474
     for(int i=0; i<min; i++)
475
       {
476
       newMap = maps[i];
477
       comp = mComponent.get(i);
478

    
479
       if( newMap!=null )
480
         {
481
         newW = newMap.get2();
482
         newH = newMap.get3();
483

    
484
         if( newW!=0.0f && newH!=0.0f )
485
           {
486
           oldMap = comp.mTextureMap;
487
           ratW = newW/oldMap.get2();
488
           ratH = newH/oldMap.get3();
489
           movX = newMap.get0() - ratW*oldMap.get0();
490
           movY = newMap.get1() - ratH*oldMap.get1();
491

    
492
           for( int index=vertex*VERT_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
493
             {
494
             mVertAttribs[index  ] = ratW*mVertAttribs[index  ] + movX;
495
             mVertAttribs[index+1] = ratH*mVertAttribs[index+1] + movY;
496
             }
497
           comp.setMap(newMap);
498
           }
499
         }
500

    
501
       vertex= comp.mEndIndex+1;
502
       }
503

    
504
     mVBO.invalidate();
505
     }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
/**
509
 * Return the texture map of one of the components.
510
 *
511
 * @param component The component number whose texture map we want to retrieve.
512
 */
513
   public Static4D getTextureMap(int component)
514
     {
515
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
516
     }
517
   }
518

    
519

    
520

    
(1-1/7)