Project

General

Profile

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

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

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.util.Log;
24

    
25
import org.distorted.library.effect.VertexEffect;
26
import org.distorted.library.effectqueue.EffectQueueVertex;
27
import org.distorted.library.main.DistortedLibrary;
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.nio.ByteBuffer;
33
import java.nio.ByteOrder;
34
import java.nio.FloatBuffer;
35
import java.util.ArrayList;
36

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

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

    
59
   private static final int BYTES_PER_FLOAT = 4;
60

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

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

    
75
   private static class Component
76
     {
77
     private int mEndIndex;
78
     private Static4D mTextureMap;
79
     private EffectQueueVertex mQueue;
80

    
81
     Component(int end)
82
       {
83
       mEndIndex  = end;
84
       mTextureMap= new Static4D(0,0,1,1);
85
       mQueue     = new EffectQueueVertex();
86
       }
87
     Component(Component original)
88
       {
89
       mEndIndex = original.mEndIndex;
90

    
91
       float x = original.mTextureMap.get0();
92
       float y = original.mTextureMap.get1();
93
       float z = original.mTextureMap.get2();
94
       float w = original.mTextureMap.get3();
95
       mTextureMap = new Static4D(x,y,z,w);
96
       mQueue = new EffectQueueVertex(original.mQueue);
97
       }
98

    
99
     void setMap(Static4D map)
100
       {
101
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
102
       }
103
     }
104

    
105
   private ArrayList<Component> mComponent;
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
   MeshBase()
110
     {
111
     mShowNormals     = false;
112
     mInflate         = 0.0f;
113
     mNeedsAdjustment = false;
114
     mComponent       = new ArrayList<>();
115

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

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// copy constructor
122

    
123
   MeshBase(MeshBase original)
124
     {
125
     mShowNormals     = original.mShowNormals;
126
     mInflate         = original.mInflate;
127
     mNeedsAdjustment = original.mNeedsAdjustment;
128

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

    
137
     mVBO = 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
     mNumVertices = original.mNumVertices;
141
     mVertAttribs = new float[mNumVertices*VERT_ATTRIBS];
142
     System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,mNumVertices*VERT_ATTRIBS);
143

    
144
     mVBO.invalidate();
145
     mTFO.invalidate();
146
     }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
   int numComponents()
151
     {
152
     return mComponent.size();
153
     }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// when a derived class is done computing its mesh, it has to call this method.
157

    
158
   void setAttribs(float[] vertexAttribs)
159
     {
160
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
161
     mVertAttribs = vertexAttribs;
162

    
163
     mComponent.add(new Component(mNumVertices));
164

    
165
     mVBO.invalidate();
166
     mTFO.invalidate();
167
     }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
// called from MeshJoined
171

    
172
   void join(MeshBase[] meshes)
173
     {
174
     MeshBase mesh;
175
     Component comp;
176
     int numComponents, numVertices, numMeshes = meshes.length;
177
     int origVertices = mNumVertices;
178

    
179
     // compute new numVertices; take care of Components
180

    
181
     if( origVertices>0 )
182
       {
183
       numComponents = mComponent.size();
184
       mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
185
       mComponent.get(numComponents-1).mEndIndex = mNumVertices;
186
       }
187

    
188
     for(int i=0; i<numMeshes; i++)
189
       {
190
       mesh = meshes[i];
191
       numComponents = mesh.mComponent.size();
192

    
193
       for(int j=0; j<numComponents; j++)
194
         {
195
         comp = new Component(mesh.mComponent.get(j));
196
         comp.mEndIndex += mNumVertices;
197
         mComponent.add(comp);
198
         }
199

    
200
       numVertices = mesh.mNumVertices;
201

    
202
       if( mNumVertices==0 )
203
         {
204
         if( numMeshes>1 )
205
            mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1);
206
         else
207
            mNumVertices +=  numVertices;
208
         }
209
       else if( i==numMeshes-1 ) mNumVertices += (numVertices+1);
210
       else                      mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
211
       }
212

    
213
     // allocate new attrib array
214
     float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
215
     numVertices = origVertices;
216

    
217
     if( origVertices>0 )
218
       {
219
       System.arraycopy(mVertAttribs,                             0, newAttribs,                         0, VERT_ATTRIBS*numVertices);
220
       System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
221
       origVertices++;
222

    
223
       if( numVertices%2==1 )
224
         {
225
         System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
226
         origVertices++;
227
         }
228
       }
229

    
230
     for(int i=0; i<numMeshes; i++)
231
       {
232
       mesh = meshes[i];
233
       numVertices = mesh.mNumVertices;
234

    
235
       if( origVertices>0 )
236
         {
237
         System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS    );
238
         origVertices++;
239
         }
240
       System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*numVertices);
241
       origVertices+=numVertices;
242

    
243
       if( i<numMeshes-1 )
244
         {
245
         System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
246
         origVertices++;
247

    
248
         if( numVertices%2==1 )
249
           {
250
           System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
251
           origVertices++;
252
           }
253
         }
254
       }
255

    
256
     if( origVertices!=mNumVertices )
257
       {
258
       android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
259
       }
260

    
261
     mVertAttribs = newAttribs;
262

    
263
     mVBO.invalidate();
264
     }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
/**
268
 * Not part of public API, do not document (public only because has to be used from the main package)
269
 *
270
 * @y.exclude
271
 */
272
   public void copyTransformToVertex()
273
     {
274
     float posX, posY, posZ, norX, norY, norZ;
275
     FloatBuffer feedback=null;
276

    
277
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, TRAN_SIZE*mNumVertices,
278
                                                              GLES30.GL_MAP_READ_BIT);
279
     if( buffer!=null )
280
       {
281
       feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
282
       }
283
     else
284
       {
285
       int error = GLES30.glGetError();
286
       Log.e("mesh", "failed to map tf buffer, error="+error);
287
       }
288

    
289
     if( feedback!=null )
290
       {
291
       for(int vertex=0; vertex<mNumVertices; vertex++)
292
         {
293
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB     ] = feedback.get(6*vertex  );
294
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB + 1 ] = feedback.get(6*vertex+1);
295
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB + 2 ] = feedback.get(6*vertex+2);
296
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB     ] = feedback.get(6*vertex+3);
297
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB + 1 ] = feedback.get(6*vertex+4);
298
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB + 2 ] = feedback.get(6*vertex+5);
299
         }
300

    
301
       mVBO.update(mVertAttribs);
302
       }
303

    
304
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
305

    
306
     int numComp = mComponent.size();
307

    
308
     for(int i=0; i<numComp; i++)
309
       {
310
       mComponent.get(i).mQueue.removeAll(false);
311
       }
312
     }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
/**
316
 * Not part of public API, do not document (public only because has to be used from the main package)
317
 *
318
 * @y.exclude
319
 */
320
   public void computeQueue()
321
     {
322
     int numComp = mComponent.size();
323

    
324
     for(int i=0; i<numComp; i++)
325
       {
326
       mComponent.get(i).mQueue.compute(1);
327
       }
328
     }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
/**
332
 * Not part of public API, do not document (public only because has to be used from the main package)
333
 *
334
 * @y.exclude
335
 */
336
   public void sendQueue()
337
     {
338
     int numComp = mComponent.size();
339

    
340
     for(int i=0; i<numComp; i++)
341
       {
342
       mComponent.get(i).mQueue.send(0.0f,3);
343
       }
344
     }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347
/**
348
 * Not part of public API, do not document (public only because has to be used from the main package)
349
 *
350
 * @y.exclude
351
 */
352
   public int getTFO()
353
     {
354
     return mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
355
     }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
/**
359
 * Not part of public API, do not document (public only because has to be used from the main package)
360
 *
361
 * @y.exclude
362
 */
363
   public int getNumVertices()
364
     {
365
     return mNumVertices;
366
     }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369
/**
370
 * Not part of public API, do not document (public only because has to be used from the main package)
371
 *
372
 * @y.exclude
373
 */
374
   public void bindVertexAttribs(DistortedProgram program)
375
     {
376
     if( mNeedsAdjustment )
377
       {
378
       mNeedsAdjustment = false;
379
       DistortedLibrary.adjustVertices(this);
380
       }
381

    
382
     int index = mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs);
383

    
384
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
385
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
386
     GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
387
     GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
388
     GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
389
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
390
     }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
/**
394
 * Not part of public API, do not document (public only because has to be used from the main package)
395
 *
396
 * @y.exclude
397
 */
398
   public void bindTransformAttribs(DistortedProgram program)
399
     {
400
     int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
401

    
402
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
403
     GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
404
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
405
     }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
/**
409
 * Not part of public API, do not document (public only because has to be used from the main package)
410
 *
411
 * @y.exclude
412
 */
413
   public void setInflate(float inflate)
414
     {
415
     mInflate = inflate;
416
     }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419
/**
420
 * Not part of public API, do not document (public only because has to be used from the main package)
421
 *
422
 * @y.exclude
423
 */
424
   public float getInflate()
425
     {
426
     return mInflate;
427
     }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430
// PUBLIC API
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432
/**
433
 * When rendering this Mesh, do we want to render the Normal vectors as well?
434
 * <p>
435
 * Will work only on OpenGL ES >= 3.0 devices.
436
 *
437
 * @param show Controls if we render the Normal vectors or not.
438
 */
439
   public void setShowNormals(boolean show)
440
     {
441
     mShowNormals = show;
442
     }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445
/**
446
 * When rendering this mesh, should we also draw the normal vectors?
447
 *
448
 * @return <i>true</i> if we do render normal vectors
449
 */
450
   public boolean getShowNormals()
451
     {
452
     return mShowNormals;
453
     }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456
/**
457
 * Release all internal resources.
458
 */
459
   public void markForDeletion()
460
     {
461
     mVertAttribs = null;
462

    
463
     mVBO.markForDeletion();
464
     mTFO.markForDeletion();
465
     }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468
/**
469
 * Apply a Vertex Effect to the vertex mesh.
470
 * <p>
471
 * This is a static, permanent modification of the vertices contained in this Mesh. If the effects
472
 * contain any Dynamics, the Dynamics will be evaluated at 0.
473
 *
474
 * We would call this several times building up a list of Effects to do. This list of effects gets
475
 * lazily executed only when the Mesh is used for rendering for the first time.
476
 *
477
 * @param effect Vertex Effect to apply to the Mesh.
478
 */
479
   public void apply(VertexEffect effect)
480
     {
481
     int numComp = mComponent.size();
482

    
483
     for(int i=0; i<numComp; i++)
484
       {
485
       mComponent.get(i).mQueue.add(effect);
486
       }
487

    
488
     mNeedsAdjustment = true;
489
     }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
/**
493
 * Sets texture maps for (some of) the components of this mesh.
494
 * <p>
495
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
496
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
497
 * upper-left quadrant of the texture.
498
 * <p>
499
 * Probably the most common user case would be sending as many maps as there are components in this
500
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
501
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
502
 * of the 2nd and 4rd one, call this with
503
 * maps = new Static4D[4]
504
 * maps[0] = null
505
 * maps[1] = the map for the 2nd component
506
 * maps[2] = null
507
 * maps[3] = the map for the 4th component
508
 *
509
 * A map's width and height have to be non-zero (but can be negative!)
510
 *
511
 * @param maps List of texture maps to apply to the texture's components.
512
 */
513
   public void setTextureMap(Static4D[] maps)
514
     {
515
     int num_comp = mComponent.size();
516
     int num_maps = maps.length;
517
     int min = Math.min(num_comp, num_maps);
518
     int vertex = 0;
519
     Static4D newMap, oldMap;
520
     Component comp;
521
     float newW, newH, ratW, ratH, movX, movY;
522

    
523
     for(int i=0; i<min; i++)
524
       {
525
       newMap = maps[i];
526
       comp = mComponent.get(i);
527

    
528
       if( newMap!=null )
529
         {
530
         newW = newMap.get2();
531
         newH = newMap.get3();
532

    
533
         if( newW!=0.0f && newH!=0.0f )
534
           {
535
           oldMap = comp.mTextureMap;
536
           ratW = newW/oldMap.get2();
537
           ratH = newH/oldMap.get3();
538
           movX = newMap.get0() - ratW*oldMap.get0();
539
           movY = newMap.get1() - ratH*oldMap.get1();
540

    
541
           for( int index=vertex*VERT_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
542
             {
543
             mVertAttribs[index  ] = ratW*mVertAttribs[index  ] + movX;
544
             mVertAttribs[index+1] = ratH*mVertAttribs[index+1] + movY;
545
             }
546
           comp.setMap(newMap);
547
           }
548
         }
549

    
550
       vertex= comp.mEndIndex+1;
551
       }
552

    
553
     mVBO.invalidate();
554
     }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
/**
558
 * Return the texture map of one of the components.
559
 *
560
 * @param component The component number whose texture map we want to retrieve.
561
 */
562
   public Static4D getTextureMap(int component)
563
     {
564
     return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
565
     }
566

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568
/**
569
 * Deep copy
570
 */
571
   public abstract MeshBase deepCopy();
572
   }
573

    
574

    
575

    
(1-1/7)