Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshBase.java @ 9099e567

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

    
100
   private ArrayList<Component> mComponent;
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// copy constructor
121

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
/**
383
 * Join a list of Meshes into this one.
384
 * <p>
385
 * Please note that calling this once with the complete list of Meshes will be much faster than
386
 * calling it repeatedly with one Mesh at a time, as we have to reallocate the array of vertices
387
 * each time.
388
 */
389
   public void join(MeshBase[] meshes)
390
     {
391

    
392
     }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
/**
396
 * Sets texture maps for all components of this mesh.
397
 * <p>
398
 * Please note that calling this once with the complete list of Maps will be much faster than
399
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
400
 * each time.
401
 */
402
   public void setTextureMap(float[][] maps)
403
     {
404
     int components = mComponent.size();
405

    
406
     for(int comp=0; comp<components; comp++)
407
       {
408

    
409
       }
410
     }
411
   }
412

    
413

    
414

    
(1-1/6)