1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
d333eb6b
|
Leszek Koltunski
|
// //
|
6 |
46b572b5
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
d333eb6b
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
d333eb6b
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
6c00149d
|
Leszek Koltunski
|
package org.distorted.library.mesh;
|
21 |
6a06a912
|
Leszek Koltunski
|
|
22 |
e6519ac8
|
Leszek Koltunski
|
import android.opengl.GLES31;
|
23 |
6c00149d
|
Leszek Koltunski
|
|
24 |
fa8bc998
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffect;
|
25 |
7602a827
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
26 |
|
|
import org.distorted.library.main.InternalBuffer;
|
27 |
da681e7e
|
Leszek Koltunski
|
import org.distorted.library.program.DistortedProgram;
|
28 |
6c00149d
|
Leszek Koltunski
|
|
29 |
da681e7e
|
Leszek Koltunski
|
import java.nio.ByteBuffer;
|
30 |
|
|
import java.nio.ByteOrder;
|
31 |
6a06a912
|
Leszek Koltunski
|
import java.nio.FloatBuffer;
|
32 |
|
|
|
33 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34 |
e0b6c593
|
Leszek Koltunski
|
/**
|
35 |
e5ba319d
|
Leszek Koltunski
|
* Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
|
36 |
e0b6c593
|
Leszek Koltunski
|
* <p>
|
37 |
da681e7e
|
Leszek Koltunski
|
* If you want to render to a particular shape, extend from here, construct a float array
|
38 |
7a5e538a
|
Leszek Koltunski
|
* containing per-vertex attributes, and call back setAttribs().
|
39 |
e0b6c593
|
Leszek Koltunski
|
*/
|
40 |
715e7726
|
Leszek Koltunski
|
public abstract class MeshBase
|
41 |
6a06a912
|
Leszek Koltunski
|
{
|
42 |
227b9bca
|
Leszek Koltunski
|
// 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 |
7a5e538a
|
Leszek Koltunski
|
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
|
47 |
6f2d931d
|
Leszek Koltunski
|
|
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 |
227b9bca
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
|
55 |
da681e7e
|
Leszek Koltunski
|
private static final int BYTES_PER_FLOAT = 4;
|
56 |
3ef3364d
|
Leszek Koltunski
|
|
57 |
6f2d931d
|
Leszek Koltunski
|
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 |
227b9bca
|
Leszek Koltunski
|
private static final int TRAN_SIZE = TRAN_ATTRIBS*BYTES_PER_FLOAT;
|
62 |
|
|
private static final int VERT_SIZE = VERT_ATTRIBS*BYTES_PER_FLOAT;
|
63 |
a51fe521
|
Leszek Koltunski
|
|
64 |
fa8bc998
|
Leszek Koltunski
|
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 |
da681e7e
|
Leszek Koltunski
|
private int mNumVertices;
|
69 |
fa8bc998
|
Leszek Koltunski
|
private float[] mVertAttribs; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
|
70 |
7a5e538a
|
Leszek Koltunski
|
private float mInflate;
|
71 |
3ef3364d
|
Leszek Koltunski
|
|
72 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73 |
3ef3364d
|
Leszek Koltunski
|
|
74 |
715e7726
|
Leszek Koltunski
|
MeshBase(float factor)
|
75 |
3ef3364d
|
Leszek Koltunski
|
{
|
76 |
466450b5
|
Leszek Koltunski
|
zFactor = factor;
|
77 |
3fc9327a
|
Leszek Koltunski
|
mShowNormals = false;
|
78 |
42571056
|
Leszek Koltunski
|
|
79 |
7602a827
|
Leszek Koltunski
|
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 |
42571056
|
Leszek Koltunski
|
}
|
82 |
|
|
|
83 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84 |
6c00149d
|
Leszek Koltunski
|
// when a derived class is done computing its mesh, it has to call this method.
|
85 |
42571056
|
Leszek Koltunski
|
|
86 |
227b9bca
|
Leszek Koltunski
|
void setAttribs(float[] vertexAttribs)
|
87 |
42571056
|
Leszek Koltunski
|
{
|
88 |
227b9bca
|
Leszek Koltunski
|
mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
|
89 |
fa8bc998
|
Leszek Koltunski
|
mVertAttribs = vertexAttribs;
|
90 |
da681e7e
|
Leszek Koltunski
|
|
91 |
fa8bc998
|
Leszek Koltunski
|
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
92 |
|
|
attribs.put(vertexAttribs).position(0);
|
93 |
da681e7e
|
Leszek Koltunski
|
|
94 |
fa8bc998
|
Leszek Koltunski
|
mVBO.setData(mNumVertices*VERT_SIZE, attribs);
|
95 |
|
|
mTFO.setData(mNumVertices*TRAN_SIZE, null );
|
96 |
42571056
|
Leszek Koltunski
|
}
|
97 |
|
|
|
98 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
6c00149d
|
Leszek Koltunski
|
/**
|
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 |
da681e7e
|
Leszek Koltunski
|
public int getTFO()
|
105 |
42571056
|
Leszek Koltunski
|
{
|
106 |
da681e7e
|
Leszek Koltunski
|
return mTFO.mIndex[0];
|
107 |
42571056
|
Leszek Koltunski
|
}
|
108 |
|
|
|
109 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110 |
6c00149d
|
Leszek Koltunski
|
/**
|
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 |
da681e7e
|
Leszek Koltunski
|
public int getNumVertices()
|
116 |
226144d0
|
leszek
|
{
|
117 |
da681e7e
|
Leszek Koltunski
|
return mNumVertices;
|
118 |
226144d0
|
leszek
|
}
|
119 |
42571056
|
Leszek Koltunski
|
|
120 |
6c00149d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
da681e7e
|
Leszek Koltunski
|
public float getZFactor()
|
127 |
6c00149d
|
Leszek Koltunski
|
{
|
128 |
da681e7e
|
Leszek Koltunski
|
return zFactor;
|
129 |
6c00149d
|
Leszek Koltunski
|
}
|
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 |
da681e7e
|
Leszek Koltunski
|
public void bindVertexAttribs(DistortedProgram program)
|
138 |
6c00149d
|
Leszek Koltunski
|
{
|
139 |
da681e7e
|
Leszek Koltunski
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] );
|
140 |
227b9bca
|
Leszek Koltunski
|
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 |
da681e7e
|
Leszek Koltunski
|
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 |
227b9bca
|
Leszek Koltunski
|
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
|
157 |
da681e7e
|
Leszek Koltunski
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
158 |
6c00149d
|
Leszek Koltunski
|
}
|
159 |
|
|
|
160 |
7a5e538a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183 |
|
|
// PUBLIC API
|
184 |
3fc9327a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185 |
|
|
/**
|
186 |
|
|
* When rendering this Mesh, do we want to render the Normal vectors as well?
|
187 |
420836fc
|
leszek
|
* <p>
|
188 |
|
|
* Will work only on OpenGL ES >= 3.0 devices.
|
189 |
3fc9327a
|
Leszek Koltunski
|
*
|
190 |
|
|
* @param show Controls if we render the Normal vectors or not.
|
191 |
|
|
*/
|
192 |
|
|
public void setShowNormals(boolean show)
|
193 |
|
|
{
|
194 |
7602a827
|
Leszek Koltunski
|
mShowNormals = (DistortedLibrary.GLSL >= 300 && show);
|
195 |
3fc9327a
|
Leszek Koltunski
|
}
|
196 |
466450b5
|
Leszek Koltunski
|
|
197 |
6c00149d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209 |
|
|
/**
|
210 |
|
|
* Release all internal resources.
|
211 |
|
|
*/
|
212 |
|
|
public void markForDeletion()
|
213 |
|
|
{
|
214 |
fa8bc998
|
Leszek Koltunski
|
mVertAttribs = null;
|
215 |
466450b5
|
Leszek Koltunski
|
|
216 |
|
|
mVBO.markForDeletion();
|
217 |
|
|
mTFO.markForDeletion();
|
218 |
|
|
}
|
219 |
fa8bc998
|
Leszek Koltunski
|
|
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 |
6a06a912
|
Leszek Koltunski
|
}
|
244 |
226144d0
|
leszek
|
|
245 |
3fc9327a
|
Leszek Koltunski
|
|