Project

General

Profile

« Previous | Next » 

Revision f046b159

Added by Leszek Koltunski almost 4 years ago

First attempt at the MeshBase.apply(VertexEffect) API.

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.java
20 20
package org.distorted.library.mesh;
21 21

  
22 22
import android.opengl.GLES30;
23
import android.opengl.Matrix;
23
import android.util.Log;
24 24

  
25
import org.distorted.library.effect.MatrixEffect;
25
import org.distorted.library.effect.VertexEffect;
26
import org.distorted.library.effectqueue.EffectQueueVertex;
27
import org.distorted.library.main.DistortedLibrary;
26 28
import org.distorted.library.main.InternalBuffer;
27 29
import org.distorted.library.program.DistortedProgram;
28 30
import org.distorted.library.type.Static4D;
29 31

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

  
32 37
///////////////////////////////////////////////////////////////////////////////////////////////////
......
65 70
   private int mNumVertices;
66 71
   private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
67 72
   private float mInflate;
68
   private float mStretchX, mStretchY, mStretchZ;
73
   private boolean mNeedsAdjustment;
69 74

  
70 75
   private static class Component
71 76
     {
72 77
     private int mEndIndex;
73 78
     private Static4D mTextureMap;
79
     private EffectQueueVertex mQueue;
74 80

  
75 81
     Component(int end)
76 82
       {
77 83
       mEndIndex  = end;
78 84
       mTextureMap= new Static4D(0,0,1,1);
85
       mQueue     = new EffectQueueVertex();
79 86
       }
80 87
     Component(Component original)
81 88
       {
......
86 93
       float z = original.mTextureMap.get2();
87 94
       float w = original.mTextureMap.get3();
88 95
       mTextureMap = new Static4D(x,y,z,w);
96
       mQueue = new EffectQueueVertex(original.mQueue);
89 97
       }
90 98

  
91 99
     void setMap(Static4D map)
......
100 108

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

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

  
111 116
     mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
112 117
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
......
117 122

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

  
124
     mShowNormals = original.mShowNormals;
125
     mInflate     = original.mInflate;
125
     mShowNormals     = original.mShowNormals;
126
     mInflate         = original.mInflate;
127
     mNeedsAdjustment = original.mNeedsAdjustment;
126 128

  
127 129
     int size = original.mComponent.size();
128 130
     mComponent = new ArrayList<>();
......
257 259
     mVBO.invalidate();
258 260
     }
259 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 void copyTransformToVertex()
269
     {
270
     float posX, posY, posZ, norX, norY, norZ;
271
     FloatBuffer feedback=null;
272
     ByteBuffer buffer = (ByteBuffer)GLES30.glMapBufferRange( GLES30.GL_TRANSFORM_FEEDBACK, 0, 6*4*mNumVertices,
273
                                                                GLES30.GL_MAP_READ_BIT);
274
     if( buffer!=null )
275
       {
276
       feedback = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
277
       }
278
     else
279
       {
280
       Log.e("mesh", "print: failed to map tf buffer");
281
       }
282

  
283
     if( feedback!=null )
284
       {
285
       for(int vertex=0; vertex<mNumVertices; vertex++)
286
         {
287
         posX = feedback.get(6*vertex  );
288
         posY = feedback.get(6*vertex+1);
289
         posZ = feedback.get(6*vertex+2);
290
         norX = feedback.get(6*vertex+3) - posX;
291
         norY = feedback.get(6*vertex+4) - posY;
292
         norZ = feedback.get(6*vertex+5) - posZ;
293

  
294
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB     ] = posX;
295
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB + 1 ] = posY;
296
         mVertAttribs[vertex*VERT_ATTRIBS + POS_ATTRIB + 2 ] = posZ;
297

  
298
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB     ] = norX;
299
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB + 1 ] = norY;
300
         mVertAttribs[vertex*VERT_ATTRIBS + NOR_ATTRIB + 2 ] = norZ;
301
         }
302
       }
303
     }
304

  
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
/**
307
 * Not part of public API, do not document (public only because has to be used from the main package)
308
 *
309
 * @y.exclude
310
 */
311
   public void computeQueue()
312
     {
313
     int numComp = mComponent.size();
314

  
315
     for(int i=0; i<numComp; i++)
316
       {
317
       mComponent.get(i).mQueue.compute(1);
318
       }
319
     }
320

  
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
/**
323
 * Not part of public API, do not document (public only because has to be used from the main package)
324
 *
325
 * @y.exclude
326
 */
327
   public void sendQueue()
328
     {
329
     int numComp = mComponent.size();
330

  
331
     for(int i=0; i<numComp; i++)
332
       {
333
       mComponent.get(i).mQueue.send(0.0f,4);
334
       }
335
     }
336

  
260 337
///////////////////////////////////////////////////////////////////////////////////////////////////
261 338
/**
262 339
 * Not part of public API, do not document (public only because has to be used from the main package)
......
287 364
 */
288 365
   public void bindVertexAttribs(DistortedProgram program)
289 366
     {
367
     if( mNeedsAdjustment )
368
       {
369
       mNeedsAdjustment = false;
370
       DistortedLibrary.adjustVertices(this);
371
       }
372

  
290 373
     int index = mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs);
291 374

  
292 375
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
......
374 457

  
375 458
///////////////////////////////////////////////////////////////////////////////////////////////////
376 459
/**
377
 * Apply all Effects to the vertex mesh. Overwrite the mesh in place.
460
 * Apply a Vertex Effect to the vertex mesh.
378 461
 * <p>
379 462
 * 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.
463
 * contain any Dynamics, the Dynamics will be evaluated at 0.
381 464
 *
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.
465
 * We would call this several times building up a list of Effects to do. This list of effects gets
466
 * lazily executed only when the Mesh is used for rendering for the first time.
385 467
 *
386
 * @param effects List of Matrix Effects to apply to the Mesh.
468
 * @param effect Vertex Effect to apply to the Mesh.
387 469
 */
388
   public void apply(MatrixEffect[] effects)
470
   public void apply(VertexEffect effect)
389 471
     {
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;
472
     int numComp = mComponent.size();
395 473

  
396
     for(MatrixEffect eff: effects)
474
     for(int i=0; i<numComp; i++)
397 475
       {
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
         }
476
       mComponent.get(i).mQueue.add(effect);
405 477
       }
406 478

  
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();
479
     mNeedsAdjustment = true;
440 480
     }
441 481

  
442 482
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff