Project

General

Profile

« Previous | Next » 

Revision da681e7e

Added by Leszek Koltunski about 6 years ago

Some simplifications in Meshes. Hide stuff inside MeshBase.

View differences:

src/main/java/org/distorted/library/main/DistortedEffects.java
426 426
  private void displayNormals(MeshBase mesh)
427 427
    {
428 428
    int num = mesh.getNumVertices();
429
    int tfo = mesh.getTFO();
429 430

  
430
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.getTFO() );
431
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
431 432
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
432 433
    DistortedRenderState.switchOffDrawing();
433 434
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, num );
......
437 438

  
438 439
    mNormalProgram.useProgram();
439 440
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
440
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getTFO() );
441
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
442
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
441
    mesh.bindTransformAttribs(mNormalProgram);
443 442
    GLES31.glLineWidth(8.0f);
444 443
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*num);
445 444
    }
......
470 469
    GLES31.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight);
471 470
    GLES31.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
472 471

  
473
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
474
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET0);
475
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET1);
476
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[2], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET2);
477
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
472
    mesh.bindVertexAttribs(mMainOITProgram);
478 473

  
479 474
    mM.send(surface,halfW,halfH,halfZ,0,1);
480 475
    mV.send(1);
......
508 503
    mMainProgram.useProgram();
509 504
    GLES31.glUniform1i(mMainTextureH, 0);
510 505

  
511
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
512
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET0);
513
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET1);
514
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET2);
515
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
506
    mesh.bindVertexAttribs(mMainProgram);
516 507

  
517 508
    mM.send(surface,halfW,halfH,halfZ,0,0);
518 509
    mV.send(0);
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
171 171

  
172 172
      mPreProgram.useProgram();
173 173

  
174
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
175
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET0);
176
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET1);
177
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET2);
178
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
174
      mesh.bindVertexAttribs(mPreProgram);
179 175

  
180 176
      node.getEffects().send(halfW, halfH, halfZ, margin, buffer, 2);
181 177

  
src/main/java/org/distorted/library/mesh/MeshBase.java
23 23

  
24 24
import org.distorted.library.main.Distorted;
25 25
import org.distorted.library.main.DistortedBuffer;
26
import org.distorted.library.program.DistortedProgram;
26 27

  
28
import java.nio.ByteBuffer;
29
import java.nio.ByteOrder;
27 30
import java.nio.FloatBuffer;
28 31

  
29 32
///////////////////////////////////////////////////////////////////////////////////////////////////
......
31 34
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
32 35
 * 2) normals 3) texture coordinates.
33 36
 * <p>
34
 * If you want to render to a particular shape, extend from here, construct the attrib FloatBuffer
35
 * and provide correct numVertices.
37
 * If you want to render to a particular shape, extend from here, construct a float array
38
 * containing per-vertex attributes, and call back setData().
36 39
 */
37 40
public abstract class MeshBase
38 41
   {
39
   private static final int BYTES_PER_FLOAT = 4;
42
   static final int POS_DATA_SIZE= 3;
43
   static final int NOR_DATA_SIZE= 3;
44
   static final int TEX_DATA_SIZE= 2;
40 45

  
41
   public static final int POS_DATA_SIZE= 3;
42
   public static final int NOR_DATA_SIZE= 3;
43
   public static final int TEX_DATA_SIZE= 2;
46
   private static final int BYTES_PER_FLOAT = 4;
44 47

  
45
   public static final int OFFSET0 =                                                           0;
46
   public static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
47
   public static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
48
   private static final int OFFSET_POS =                                                        0;
49
   private static final int OFFSET_NOR = (POS_DATA_SIZE                         )*BYTES_PER_FLOAT;
50
   private static final int OFFSET_TEX = (POS_DATA_SIZE+NOR_DATA_SIZE           )*BYTES_PER_FLOAT;
48 51

  
49
   public static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
50
   public static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
52
   private static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
53
   private static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
51 54

  
52 55
   private boolean mShowNormals;       // when rendering this mesh, draw normal vectors?
53 56
   private DistortedBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
54 57
   private final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
55 58
                                       // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
56

  
57
   ////////////////////////////////////////////////////////////////////////////////
58
   // derived classes need to compute and fill up those 2 variables with data
59
   // describing a particular shape. Having done so, call setData()
60
   int mNumVertices;
61
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
62
   ////////////////////////////////////////////////////////////////////////////////
59
   private int mNumVertices;
60
   private FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, TexS,TexT
63 61

  
64 62
///////////////////////////////////////////////////////////////////////////////////////////////////
65 63

  
......
75 73
///////////////////////////////////////////////////////////////////////////////////////////////////
76 74
// when a derived class is done computing its mesh, it has to call this method.
77 75

  
78
   void setData(int numVertices, FloatBuffer vertAttribs)
76
   void setData(int numVertices, float[] vertexAttribs)
79 77
     {
80
     mVBO.setData(numVertices*VERTSIZE, vertAttribs);
81
     mTFO.setData(numVertices*TFSIZE  , null       );
78
     mNumVertices = numVertices;
79

  
80
     mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
81
     mVertAttribs.put(vertexAttribs).position(0);
82

  
83
     mVBO.setData(numVertices*VERTSIZE, mVertAttribs);
84
     mTFO.setData(numVertices*TFSIZE  , null        );
82 85
     }
83 86

  
84 87
///////////////////////////////////////////////////////////////////////////////////////////////////
......
87 90
 *
88 91
 * @y.exclude
89 92
 */
90
   public int getVBO()
93
   public int getTFO()
91 94
     {
92
     return mVBO.mIndex[0];
95
     return mTFO.mIndex[0];
93 96
     }
94 97

  
95 98
///////////////////////////////////////////////////////////////////////////////////////////////////
......
98 101
 *
99 102
 * @y.exclude
100 103
 */
101
   public int getTFO()
104
   public int getNumVertices()
102 105
     {
103
     return mTFO.mIndex[0];
106
     return mNumVertices;
104 107
     }
105 108

  
106 109
///////////////////////////////////////////////////////////////////////////////////////////////////
......
109 112
 *
110 113
 * @y.exclude
111 114
 */
112
   public int getNumVertices()
115
   public float getZFactor()
113 116
     {
114
     return mNumVertices;
117
     return zFactor;
115 118
     }
116 119

  
117 120
///////////////////////////////////////////////////////////////////////////////////////////////////
......
120 123
 *
121 124
 * @y.exclude
122 125
 */
123
   public float getZFactor()
126
   public void bindVertexAttribs(DistortedProgram program)
124 127
     {
125
     return zFactor;
128
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
129
     GLES31.glVertexAttribPointer(program.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_POS);
130
     GLES31.glVertexAttribPointer(program.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_NOR);
131
     GLES31.glVertexAttribPointer(program.mAttribute[2], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET_TEX);
132
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
133
     }
134

  
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/**
137
 * Not part of public API, do not document (public only because has to be used from the main package)
138
 *
139
 * @y.exclude
140
 */
141
   public void bindTransformAttribs(DistortedProgram program)
142
     {
143
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
144
     GLES31.glVertexAttribPointer(program.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
145
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
126 146
     }
127 147

  
128 148
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/mesh/MeshCubes.java
19 19

  
20 20
package org.distorted.library.mesh;
21 21

  
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24 22
import java.util.ArrayList;
25 23

  
26 24
///////////////////////////////////////////////////////////////////////////////////////////////////
......
61 59
   private ArrayList<Edge> mEdges = new ArrayList<>();
62 60

  
63 61
   private int remainingVert;
62
   private int numVertices;
64 63
   private int mSideBends;
65 64
   private int mEdgeNum;
66 65
   private int mSideWalls;
......
183 182
     mRows       =0;
184 183
     mCols       =0;
185 184
     mSlices     =slices;
186
     mNumVertices=0;
185
     numVertices =0;
187 186
     
188 187
     if( cols>0 && desc.contains("1") )
189 188
       {
......
197 196
           mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
198 197

  
199 198
       markRegions();
200
       mNumVertices = computeDataLength();
201

  
202
       remainingVert = mNumVertices;
199
       numVertices = computeDataLength();
200
       remainingVert = numVertices;
203 201
       }
204 202
     }
205 203

  
......
211 209
     mRows        =rows;
212 210
     mCols        =cols;
213 211
     mSlices      =slices;
214
     mNumVertices =   0;
212
     numVertices  =0;
215 213

  
216 214
     if( cols>0 && rows>0 )
217 215
       {
......
222 220
           mCubes[i][j] = 1;
223 221

  
224 222
       markRegions();
225
       mNumVertices = computeDataLength();
226

  
227
       remainingVert = mNumVertices;
223
       numVertices = computeDataLength();
224
       remainingVert = numVertices;
228 225
       }
229 226
     }
230 227

  
......
691 688
  private void build()
692 689
     {
693 690
     int vertSoFar=0;
694
     float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*mNumVertices];
691
     float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
695 692

  
696 693
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
697 694

  
......
713 710
     if( remainingVert!=0 )
714 711
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
715 712

  
716
     mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
717
     mVertAttribs.put(attribs).position(0);
718

  
719
     setData(mNumVertices, mVertAttribs);
713
     setData(numVertices, attribs);
720 714
     }
721 715

  
722 716
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/mesh/MeshFlat.java
19 19

  
20 20
package org.distorted.library.mesh;
21 21

  
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24

  
25 22
///////////////////////////////////////////////////////////////////////////////////////////////////
26 23
/**
27 24
 * Create a flat, rectangular grid.
......
33 30
  {
34 31
  private int mCols, mRows;
35 32
  private int remainingVert;
33
  private int numVertices;
36 34

  
37 35
///////////////////////////////////////////////////////////////////////////////////////////////////
38 36
// Create a flat, full grid.
......
44 42

  
45 43
     if( cols==1 && rows==1 )
46 44
       {
47
       mNumVertices = 4;
45
       numVertices = 4;
48 46
       }
49 47
     else
50 48
       {
51
       mNumVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
49
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
52 50
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
53 51
       }
54 52

  
55 53
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
56 54

  
57
     remainingVert = mNumVertices;
55
     remainingVert = numVertices;
58 56
     }
59 57

  
60 58

  
......
178 176
    super(0.0f);
179 177
    computeNumberOfVertices(cols,rows);
180 178

  
181
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*mNumVertices];
179
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
182 180

  
183 181
    buildGrid(attribs);
184 182

  
......
188 186
    if( remainingVert!=0 )
189 187
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
190 188

  
191
    mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
192
    mVertAttribs.put(attribs).position(0);
193

  
194
    setData(mNumVertices, mVertAttribs);
189
    setData(numVertices, attribs);
195 190
    }
196 191
 }

Also available in: Unified diff