Project

General

Profile

« Previous | Next » 

Revision da681e7e

Added by Leszek Koltunski over 5 years ago

Some simplifications in Meshes. Hide stuff inside MeshBase.

View differences:

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
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff