Project

General

Profile

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

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

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.GLES31;
23
import android.opengl.Matrix;
24

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

    
30
import java.nio.ByteBuffer;
31
import java.nio.ByteOrder;
32
import java.nio.FloatBuffer;
33
import java.util.ArrayList;
34

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

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

    
57
   private static final int BYTES_PER_FLOAT = 4;
58

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

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

    
73
   private class Component
74
     {
75
     private int mEndIndex;
76
     private float[] mTextureMap;
77

    
78
     Component()
79
       {
80
       mTextureMap = new float[8];
81

    
82
       mTextureMap[ 0] = 0.0f;  // LD_X
83
       mTextureMap[ 1] = 0.0f;  // LD_Y
84
       mTextureMap[ 2] = 0.0f;  // LU_X
85
       mTextureMap[ 3] = 1.0f;  // LU_Y
86
       mTextureMap[ 4] = 1.0f;  // RU_X
87
       mTextureMap[ 5] = 1.0f;  // RU_Y
88
       mTextureMap[ 6] = 1.0f;  // RD_X
89
       mTextureMap[ 7] = 0.0f;  // RD_Y
90
       }
91
     Component(Component original)
92
       {
93
       mEndIndex = original.mEndIndex;
94
       mTextureMap = new float[8];
95
       System.arraycopy(original.mTextureMap,0,mTextureMap,0,8);
96
       }
97
     }
98

    
99
   private ArrayList<Component> mComponent;
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
   MeshBase(float sx, float sy, float sz)
104
     {
105
     mStretchX = sx;
106
     mStretchY = sy;
107
     mStretchZ = sz;
108

    
109
     mShowNormals = false;
110
     mInflate     = 0.0f;
111
     mComponent = new ArrayList<>();
112
     mComponent.add(new Component());
113

    
114
     mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
115
     mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
116
     }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
// copy constructor
120

    
121
   MeshBase(MeshBase original)
122
     {
123
     mStretchX = original.mStretchX;
124
     mStretchY = original.mStretchY;
125
     mStretchZ = original.mStretchZ;
126

    
127
     mShowNormals = original.mShowNormals;
128
     mInflate     = original.mInflate;
129

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

    
138
     mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
139
     mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
140

    
141
     System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS);
142
     setAttribs(mVertAttribs);
143
     }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// when a derived class is done computing its mesh, it has to call this method.
147

    
148
   void setAttribs(float[] vertexAttribs)
149
     {
150
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
151
     mVertAttribs = vertexAttribs;
152

    
153
     mComponent.get(0).mEndIndex = mNumVertices;
154

    
155
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
156
     attribs.put(vertexAttribs).position(0);
157

    
158
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
159
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
160
     }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
/**
164
 * Not part of public API, do not document (public only because has to be used from the main package)
165
 *
166
 * @y.exclude
167
 */
168
   public int getTFO()
169
     {
170
     return mTFO.mIndex[0];
171
     }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
/**
175
 * Not part of public API, do not document (public only because has to be used from the main package)
176
 *
177
 * @y.exclude
178
 */
179
   public int getNumVertices()
180
     {
181
     return mNumVertices;
182
     }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
/**
186
 * Sets the stretch parameters. Coordinates of all vertices will be first pre-multiplied by those.
187
 */
188
  public void setStretch(int sx, int sy, int sz)
189
    {
190
    mStretchX = sx;
191
    mStretchY = sy;
192
    mStretchZ = sz;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
/**
197
 * X coordinates of all vertices will be first pre-multiplied by this parameter.
198
 */
199
  public float getStretchX()
200
    {
201
    return mStretchX;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
/**
206
 * Y coordinates of all vertices will be first pre-multiplied by this parameter.
207
 */
208
  public float getStretchY()
209
    {
210
    return mStretchY;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
/**
215
 * Z coordinates of all vertices will be first pre-multiplied by this parameter.
216
 */
217
  public float getStretchZ()
218
    {
219
    return mStretchZ;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
/**
224
 * Not part of public API, do not document (public only because has to be used from the main package)
225
 *
226
 * @y.exclude
227
 */
228
   public void bindVertexAttribs(DistortedProgram program)
229
     {
230
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
231
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
232
     GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
233
     GLES31.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
234
     GLES31.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
235
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
236
     }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Not part of public API, do not document (public only because has to be used from the main package)
241
 *
242
 * @y.exclude
243
 */
244
   public void bindTransformAttribs(DistortedProgram program)
245
     {
246
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
247
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
248
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
249
     }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
/**
253
 * Not part of public API, do not document (public only because has to be used from the main package)
254
 *
255
 * @y.exclude
256
 */
257
   public void setInflate(float inflate)
258
     {
259
     mInflate = inflate;
260
     }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
/**
264
 * Not part of public API, do not document (public only because has to be used from the main package)
265
 *
266
 * @y.exclude
267
 */
268
   public float getInflate()
269
     {
270
     return mInflate;
271
     }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
// PUBLIC API
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
/**
277
 * When rendering this Mesh, do we want to render the Normal vectors as well?
278
 * <p>
279
 * Will work only on OpenGL ES >= 3.0 devices.
280
 *
281
 * @param show Controls if we render the Normal vectors or not.
282
 */
283
   public void setShowNormals(boolean show)
284
     {
285
     mShowNormals = (DistortedLibrary.GLSL >= 300 && show);
286
     }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
/**
290
 * When rendering this mesh, should we also draw the normal vectors?
291
 *
292
 * @return <i>true</i> if we do render normal vectors
293
 */
294
   public boolean getShowNormals()
295
     {
296
     return mShowNormals;
297
     }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
/**
301
 * Release all internal resources.
302
 */
303
   public void markForDeletion()
304
     {
305
     mVertAttribs = null;
306

    
307
     mVBO.markForDeletion();
308
     mTFO.markForDeletion();
309
     }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
/**
313
 * Apply all Effects to the vertex mesh. Overwrite the mesh in place.
314
 * <p>
315
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
316
 * contain any Dynamics, they will be evaluated at 0.
317
 *
318
 * Please note that calling this once with the complete list of Effects will be much faster than
319
 * calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices
320
 * each time.
321
 */
322
   public void apply(MatrixEffect[] effects)
323
     {
324
     float[][] matrix = new float[effects.length][16];
325
     float[] tmp;
326
     float[] array = new float[4];
327
     float x,y,z;
328
     int numEffects = 0;
329

    
330
     for(MatrixEffect eff: effects)
331
       {
332
       if( eff!=null )
333
         {
334
         Matrix.setIdentityM(matrix[numEffects],0);
335
         eff.compute(array,0,0,0);
336
         eff.apply(matrix[numEffects], array, 0);
337
         numEffects++;
338
         }
339
       }
340

    
341
     for(int index=0; index<mNumVertices; index+=VERT_ATTRIBS )
342
       {
343
       for(int mat=0; mat<numEffects; mat++)
344
         {
345
         tmp = matrix[mat];
346

    
347
         x = mVertAttribs[index+POS_ATTRIB  ];
348
         y = mVertAttribs[index+POS_ATTRIB+1];
349
         z = mVertAttribs[index+POS_ATTRIB+2];
350

    
351
         mVertAttribs[index+POS_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z + tmp[12];
352
         mVertAttribs[index+POS_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z + tmp[13];
353
         mVertAttribs[index+POS_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z + tmp[14];
354

    
355
         x = mVertAttribs[index+NOR_ATTRIB  ];
356
         y = mVertAttribs[index+NOR_ATTRIB+1];
357
         z = mVertAttribs[index+NOR_ATTRIB+2];
358

    
359
         mVertAttribs[index+NOR_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
360
         mVertAttribs[index+NOR_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
361
         mVertAttribs[index+NOR_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
362

    
363
         x = mVertAttribs[index+INF_ATTRIB  ];
364
         y = mVertAttribs[index+INF_ATTRIB+1];
365
         z = mVertAttribs[index+INF_ATTRIB+2];
366

    
367
         mVertAttribs[index+INF_ATTRIB  ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z;
368
         mVertAttribs[index+INF_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z;
369
         mVertAttribs[index+INF_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z;
370
         }
371
       }
372

    
373
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
374
     attribs.put(mVertAttribs).position(0);
375

    
376
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
377
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
378
     }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
/**
382
 * Join a list of Meshes into this one.
383
 * <p>
384
 * Please note that calling this once with the complete list of Meshes will be much faster than
385
 * calling it repeatedly with one Mesh at a time, as we have to reallocate the array of vertices
386
 * each time.
387
 */
388
   public void join(MeshBase[] meshes)
389
     {
390
     MeshBase mesh;
391
     Component comp;
392
     int com, num, len = meshes.length;
393
     int origVertices = mNumVertices;
394

    
395
     // compute new numVertices; take care of Components
396
     mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
397
     com = mComponent.size();
398
     mComponent.get(com-1).mEndIndex = mNumVertices;
399

    
400
     for(int i=0; i<len; i++)
401
       {
402
       mesh = meshes[i];
403
       com = mesh.mComponent.size();
404

    
405
       for(int j=0; j<com; j++)
406
         {
407
         comp = new Component(mesh.mComponent.get(j));
408
         comp.mEndIndex += mNumVertices;
409
         mComponent.add(comp);
410
         }
411

    
412
       num = mesh.mNumVertices;
413
       mNumVertices+= (i<len-1 ? ( num%2==1 ? num+2 : num+1 ) : num);
414
       }
415

    
416
     // allocate new attrib array
417
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
418
     num = origVertices;
419

    
420
     System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*num);
421
     System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
422
     origVertices++;
423

    
424
     if( num%2==1 )
425
       {
426
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
427
       origVertices++;
428
       }
429

    
430
     for(int i=0; i<len; i++)
431
       {
432
       mesh = meshes[i];
433
       num = mesh.mNumVertices;
434

    
435
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
436
       origVertices++;
437
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*num);
438
       origVertices+=num;
439

    
440
       if( i<len-1 )
441
         {
442
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
443
         origVertices++;
444

    
445
         if( num%2==1 )
446
           {
447
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(num-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
448
           origVertices++;
449
           }
450
         }
451
       }
452

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

    
458
     mVertAttribs = newAttribs;
459

    
460
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
461
     attribs.put(mVertAttribs).position(0);
462

    
463
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
464
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
465
     }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468
/**
469
 * Sets texture maps for all components of this mesh.
470
 * <p>
471
 * Please note that calling this once with the complete list of Maps will be much faster than
472
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
473
 * each time.
474
 */
475
   public void setTextureMap(float[][] maps)
476
     {
477
     int components = mComponent.size();
478

    
479
     for(int comp=0; comp<components; comp++)
480
       {
481

    
482
       }
483
     }
484
   }
485

    
486

    
487

    
(1-1/6)