Project

General

Profile

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

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

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

    
24
import org.distorted.library.effect.MatrixEffect;
25
import org.distorted.library.main.DistortedLibrary;
26
import org.distorted.library.main.InternalBuffer;
27
import org.distorted.library.program.DistortedProgram;
28

    
29
import java.nio.ByteBuffer;
30
import java.nio.ByteOrder;
31
import java.nio.FloatBuffer;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
36
 * <p>
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 setAttribs().
39
 */
40
public abstract class MeshBase
41
   {
42
   // sizes of attributes of an individual vertex.
43
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
44
   private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
45
   private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
46
   private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
47

    
48
   static final int POS_ATTRIB   = 0;
49
   static final int NOR_ATTRIB   = POS_DATA_SIZE;
50
   static final int INF_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE;
51
   static final int TEX_ATTRIB   = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
52
   static final int VERT_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE + TEX_DATA_SIZE;  // number of attributes of a 'normal' vertex
53
   static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE;                                  // number of attributes of a transform feedback vertex
54

    
55
   private static final int BYTES_PER_FLOAT = 4;
56

    
57
   private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
58
   private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
59
   private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
60
   private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
61
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
62
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
63

    
64
   private boolean mShowNormals;      // when rendering this mesh, draw normal vectors?
65
   private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
66
   private final float zFactor;       // strange workaround for the fact that we need to somehow store the 'depth'
67
                                      // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   MeshBase(float factor)
75
     {
76
     zFactor      = factor;
77
     mShowNormals = false;
78

    
79
     mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER             , GLES31.GL_STATIC_READ);
80
     mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ);
81
     }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// when a derived class is done computing its mesh, it has to call this method.
85

    
86
   void setAttribs(float[] vertexAttribs)
87
     {
88
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
89
     mVertAttribs = vertexAttribs;
90

    
91
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
92
     attribs.put(vertexAttribs).position(0);
93

    
94
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
95
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
96
     }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
/**
100
 * Not part of public API, do not document (public only because has to be used from the main package)
101
 *
102
 * @y.exclude
103
 */
104
   public int getTFO()
105
     {
106
     return mTFO.mIndex[0];
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
/**
111
 * Not part of public API, do not document (public only because has to be used from the main package)
112
 *
113
 * @y.exclude
114
 */
115
   public int getNumVertices()
116
     {
117
     return mNumVertices;
118
     }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
/**
122
 * Not part of public API, do not document (public only because has to be used from the main package)
123
 *
124
 * @y.exclude
125
 */
126
   public float getZFactor()
127
     {
128
     return zFactor;
129
     }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Not part of public API, do not document (public only because has to be used from the main package)
134
 *
135
 * @y.exclude
136
 */
137
   public void bindVertexAttribs(DistortedProgram program)
138
     {
139
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
140
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
141
     GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
142
     GLES31.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
143
     GLES31.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
144
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
145
     }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
/**
149
 * Not part of public API, do not document (public only because has to be used from the main package)
150
 *
151
 * @y.exclude
152
 */
153
   public void bindTransformAttribs(DistortedProgram program)
154
     {
155
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] );
156
     GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
157
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
158
     }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
/**
162
 * Not part of public API, do not document (public only because has to be used from the main package)
163
 *
164
 * @y.exclude
165
 */
166
   public void setInflate(float inflate)
167
     {
168
     mInflate = inflate;
169
     }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
/**
173
 * Not part of public API, do not document (public only because has to be used from the main package)
174
 *
175
 * @y.exclude
176
 */
177
   public float getInflate()
178
     {
179
     return mInflate;
180
     }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
// PUBLIC API
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
/**
186
 * When rendering this Mesh, do we want to render the Normal vectors as well?
187
 * <p>
188
 * Will work only on OpenGL ES >= 3.0 devices.
189
 *
190
 * @param show Controls if we render the Normal vectors or not.
191
 */
192
   public void setShowNormals(boolean show)
193
     {
194
     mShowNormals = (DistortedLibrary.GLSL >= 300 && show);
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
/**
199
 * When rendering this mesh, should we also draw the normal vectors?
200
 *
201
 * @return <i>true</i> if we do render normal vectors
202
 */
203
   public boolean getShowNormals()
204
     {
205
     return mShowNormals;
206
     }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
/**
210
 * Release all internal resources.
211
 */
212
   public void markForDeletion()
213
     {
214
     mVertAttribs = null;
215

    
216
     mVBO.markForDeletion();
217
     mTFO.markForDeletion();
218
     }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
/**
222
 * Multiply all coordinates, normal and inflate vectors by the matrix.
223
 * <p>
224
 * This is a static, permanent modification of the vertices contained in this Mesh. If the Effect
225
 * contains any Dynamics, they will be evaluated at 0.
226
 *
227
 * Please note that calling this once with the complete list of Effects will be much faster than
228
 * calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices
229
 * each time.
230
 */
231
   public void apply(MatrixEffect[] effects)
232
     {
233
     for(int v=0; v<mNumVertices; v++)
234
       {
235
       for(MatrixEffect eff: effects)
236
         {
237
         if( eff!=null ) eff.applyToVertex( mVertAttribs, v*VERT_ATTRIBS );
238
         }
239
       }
240

    
241
     setAttribs(mVertAttribs);
242
     }
243
   }
244

    
245

    
246

    
(1-1/5)