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 |
b7074bc6
|
Leszek Koltunski
|
import android.opengl.GLES30;
|
23 |
c90aca24
|
Leszek Koltunski
|
import android.opengl.Matrix;
|
24 |
6c00149d
|
Leszek Koltunski
|
|
25 |
fa8bc998
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffect;
|
26 |
7602a827
|
Leszek Koltunski
|
import org.distorted.library.main.InternalBuffer;
|
27 |
da681e7e
|
Leszek Koltunski
|
import org.distorted.library.program.DistortedProgram;
|
28 |
a3a05347
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
29 |
6c00149d
|
Leszek Koltunski
|
|
30 |
c90aca24
|
Leszek Koltunski
|
import java.util.ArrayList;
|
31 |
6a06a912
|
Leszek Koltunski
|
|
32 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33 |
e0b6c593
|
Leszek Koltunski
|
/**
|
34 |
e5ba319d
|
Leszek Koltunski
|
* Abstract class which represents a Mesh, ie an array of vertices (rendered as a TRIANGLE_STRIP).
|
35 |
e0b6c593
|
Leszek Koltunski
|
* <p>
|
36 |
da681e7e
|
Leszek Koltunski
|
* If you want to render to a particular shape, extend from here, construct a float array
|
37 |
7a5e538a
|
Leszek Koltunski
|
* containing per-vertex attributes, and call back setAttribs().
|
38 |
e0b6c593
|
Leszek Koltunski
|
*/
|
39 |
715e7726
|
Leszek Koltunski
|
public abstract class MeshBase
|
40 |
6a06a912
|
Leszek Koltunski
|
{
|
41 |
227b9bca
|
Leszek Koltunski
|
// sizes of attributes of an individual vertex.
|
42 |
|
|
private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
|
43 |
|
|
private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z
|
44 |
|
|
private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z
|
45 |
7a5e538a
|
Leszek Koltunski
|
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t
|
46 |
6f2d931d
|
Leszek Koltunski
|
|
47 |
|
|
static final int POS_ATTRIB = 0;
|
48 |
|
|
static final int NOR_ATTRIB = POS_DATA_SIZE;
|
49 |
|
|
static final int INF_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE;
|
50 |
|
|
static final int TEX_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE;
|
51 |
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
|
52 |
|
|
static final int TRAN_ATTRIBS = POS_DATA_SIZE + POS_DATA_SIZE; // number of attributes of a transform feedback vertex
|
53 |
6a06a912
|
Leszek Koltunski
|
|
54 |
da681e7e
|
Leszek Koltunski
|
private static final int BYTES_PER_FLOAT = 4;
|
55 |
3ef3364d
|
Leszek Koltunski
|
|
56 |
6f2d931d
|
Leszek Koltunski
|
private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT;
|
57 |
|
|
private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT;
|
58 |
|
|
private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT;
|
59 |
|
|
private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT;
|
60 |
227b9bca
|
Leszek Koltunski
|
private static final int TRAN_SIZE = TRAN_ATTRIBS*BYTES_PER_FLOAT;
|
61 |
|
|
private static final int VERT_SIZE = VERT_ATTRIBS*BYTES_PER_FLOAT;
|
62 |
a51fe521
|
Leszek Koltunski
|
|
63 |
fa8bc998
|
Leszek Koltunski
|
private boolean mShowNormals; // when rendering this mesh, draw normal vectors?
|
64 |
|
|
private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
|
65 |
da681e7e
|
Leszek Koltunski
|
private int mNumVertices;
|
66 |
fa8bc998
|
Leszek Koltunski
|
private float[] mVertAttribs; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
|
67 |
7a5e538a
|
Leszek Koltunski
|
private float mInflate;
|
68 |
23b733db
|
Leszek Koltunski
|
private float mStretchX, mStretchY, mStretchZ;
|
69 |
3ef3364d
|
Leszek Koltunski
|
|
70 |
7e53a35f
|
Leszek Koltunski
|
private static class Component
|
71 |
c90aca24
|
Leszek Koltunski
|
{
|
72 |
|
|
private int mEndIndex;
|
73 |
a3a05347
|
Leszek Koltunski
|
private Static4D mTextureMap;
|
74 |
c90aca24
|
Leszek Koltunski
|
|
75 |
0d4aae88
|
Leszek Koltunski
|
Component(int end)
|
76 |
c90aca24
|
Leszek Koltunski
|
{
|
77 |
a3a05347
|
Leszek Koltunski
|
mEndIndex = end;
|
78 |
|
|
mTextureMap= new Static4D(0,0,1,1);
|
79 |
c90aca24
|
Leszek Koltunski
|
}
|
80 |
|
|
Component(Component original)
|
81 |
|
|
{
|
82 |
|
|
mEndIndex = original.mEndIndex;
|
83 |
a3a05347
|
Leszek Koltunski
|
|
84 |
|
|
float x = original.mTextureMap.get0();
|
85 |
|
|
float y = original.mTextureMap.get1();
|
86 |
|
|
float z = original.mTextureMap.get2();
|
87 |
|
|
float w = original.mTextureMap.get3();
|
88 |
|
|
mTextureMap = new Static4D(x,y,z,w);
|
89 |
0d4aae88
|
Leszek Koltunski
|
}
|
90 |
|
|
|
91 |
a3a05347
|
Leszek Koltunski
|
void setMap(Static4D map)
|
92 |
0d4aae88
|
Leszek Koltunski
|
{
|
93 |
a3a05347
|
Leszek Koltunski
|
mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
|
94 |
c90aca24
|
Leszek Koltunski
|
}
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
private ArrayList<Component> mComponent;
|
98 |
|
|
|
99 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100 |
3ef3364d
|
Leszek Koltunski
|
|
101 |
0f10a0b6
|
Leszek Koltunski
|
MeshBase()
|
102 |
3ef3364d
|
Leszek Koltunski
|
{
|
103 |
23b733db
|
Leszek Koltunski
|
mStretchX = 1.0f;
|
104 |
|
|
mStretchY = 1.0f;
|
105 |
|
|
mStretchZ = 1.0f;
|
106 |
|
|
|
107 |
3fc9327a
|
Leszek Koltunski
|
mShowNormals = false;
|
108 |
c90aca24
|
Leszek Koltunski
|
mInflate = 0.0f;
|
109 |
0d4aae88
|
Leszek Koltunski
|
mComponent = new ArrayList<>();
|
110 |
c90aca24
|
Leszek Koltunski
|
|
111 |
b7074bc6
|
Leszek Koltunski
|
mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ);
|
112 |
|
|
mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
|
113 |
c90aca24
|
Leszek Koltunski
|
}
|
114 |
|
|
|
115 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116 |
|
|
// copy constructor
|
117 |
|
|
|
118 |
|
|
MeshBase(MeshBase original)
|
119 |
|
|
{
|
120 |
23b733db
|
Leszek Koltunski
|
mStretchX = original.mStretchX;
|
121 |
|
|
mStretchY = original.mStretchY;
|
122 |
|
|
mStretchZ = original.mStretchZ;
|
123 |
|
|
|
124 |
c90aca24
|
Leszek Koltunski
|
mShowNormals = original.mShowNormals;
|
125 |
|
|
mInflate = original.mInflate;
|
126 |
|
|
|
127 |
|
|
int size = original.mComponent.size();
|
128 |
|
|
mComponent = new ArrayList<>();
|
129 |
|
|
for(int i=0; i<size; i++)
|
130 |
|
|
{
|
131 |
|
|
Component comp = new Component(original.mComponent.get(i));
|
132 |
|
|
mComponent.add(comp);
|
133 |
|
|
}
|
134 |
42571056
|
Leszek Koltunski
|
|
135 |
b7074bc6
|
Leszek Koltunski
|
mVBO = new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ);
|
136 |
|
|
mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
|
137 |
c90aca24
|
Leszek Koltunski
|
|
138 |
|
|
System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS);
|
139 |
|
|
setAttribs(mVertAttribs);
|
140 |
42571056
|
Leszek Koltunski
|
}
|
141 |
|
|
|
142 |
0d4aae88
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143 |
|
|
|
144 |
|
|
int numComponents()
|
145 |
|
|
{
|
146 |
|
|
return mComponent.size();
|
147 |
|
|
}
|
148 |
|
|
|
149 |
42571056
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150 |
6c00149d
|
Leszek Koltunski
|
// when a derived class is done computing its mesh, it has to call this method.
|
151 |
42571056
|
Leszek Koltunski
|
|
152 |
227b9bca
|
Leszek Koltunski
|
void setAttribs(float[] vertexAttribs)
|
153 |
42571056
|
Leszek Koltunski
|
{
|
154 |
227b9bca
|
Leszek Koltunski
|
mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
|
155 |
fa8bc998
|
Leszek Koltunski
|
mVertAttribs = vertexAttribs;
|
156 |
da681e7e
|
Leszek Koltunski
|
|
157 |
0d4aae88
|
Leszek Koltunski
|
mComponent.add(new Component(mNumVertices));
|
158 |
c90aca24
|
Leszek Koltunski
|
|
159 |
22e60fba
|
Leszek Koltunski
|
mVBO.invalidate();
|
160 |
|
|
mTFO.invalidate();
|
161 |
42571056
|
Leszek Koltunski
|
}
|
162 |
|
|
|
163 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164 |
0d4aae88
|
Leszek Koltunski
|
// called from MeshJoined
|
165 |
42571056
|
Leszek Koltunski
|
|
166 |
0d4aae88
|
Leszek Koltunski
|
void join(MeshBase[] meshes)
|
167 |
226144d0
|
leszek
|
{
|
168 |
0d4aae88
|
Leszek Koltunski
|
MeshBase mesh;
|
169 |
|
|
Component comp;
|
170 |
e7f85322
|
Leszek Koltunski
|
int numComponents, numVertices, numMeshes = meshes.length;
|
171 |
0d4aae88
|
Leszek Koltunski
|
int origVertices = mNumVertices;
|
172 |
42571056
|
Leszek Koltunski
|
|
173 |
0d4aae88
|
Leszek Koltunski
|
// compute new numVertices; take care of Components
|
174 |
044b5494
|
Leszek Koltunski
|
|
175 |
0d4aae88
|
Leszek Koltunski
|
if( origVertices>0 )
|
176 |
|
|
{
|
177 |
e7f85322
|
Leszek Koltunski
|
numComponents = mComponent.size();
|
178 |
0d4aae88
|
Leszek Koltunski
|
mNumVertices+= ( mNumVertices%2==1 ? 2:1 );
|
179 |
e7f85322
|
Leszek Koltunski
|
mComponent.get(numComponents-1).mEndIndex = mNumVertices;
|
180 |
0d4aae88
|
Leszek Koltunski
|
}
|
181 |
044b5494
|
Leszek Koltunski
|
|
182 |
e7f85322
|
Leszek Koltunski
|
for(int i=0; i<numMeshes; i++)
|
183 |
0d4aae88
|
Leszek Koltunski
|
{
|
184 |
|
|
mesh = meshes[i];
|
185 |
e7f85322
|
Leszek Koltunski
|
numComponents = mesh.mComponent.size();
|
186 |
044b5494
|
Leszek Koltunski
|
|
187 |
e7f85322
|
Leszek Koltunski
|
for(int j=0; j<numComponents; j++)
|
188 |
0d4aae88
|
Leszek Koltunski
|
{
|
189 |
|
|
comp = new Component(mesh.mComponent.get(j));
|
190 |
|
|
comp.mEndIndex += mNumVertices;
|
191 |
|
|
mComponent.add(comp);
|
192 |
|
|
}
|
193 |
23b733db
|
Leszek Koltunski
|
|
194 |
e7f85322
|
Leszek Koltunski
|
numVertices = mesh.mNumVertices;
|
195 |
22e60fba
|
Leszek Koltunski
|
|
196 |
e7f85322
|
Leszek Koltunski
|
if( mNumVertices==0 )
|
197 |
|
|
{
|
198 |
|
|
if( numMeshes>1 )
|
199 |
|
|
mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1);
|
200 |
|
|
else
|
201 |
|
|
mNumVertices += numVertices;
|
202 |
|
|
}
|
203 |
|
|
else if( i==numMeshes-1 ) mNumVertices += (numVertices+1);
|
204 |
|
|
else mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
|
205 |
0d4aae88
|
Leszek Koltunski
|
}
|
206 |
|
|
|
207 |
|
|
// allocate new attrib array
|
208 |
|
|
float[] newAttribs = new float[VERT_ATTRIBS*mNumVertices];
|
209 |
e7f85322
|
Leszek Koltunski
|
numVertices = origVertices;
|
210 |
0d4aae88
|
Leszek Koltunski
|
|
211 |
|
|
if( origVertices>0 )
|
212 |
|
|
{
|
213 |
e7f85322
|
Leszek Koltunski
|
System.arraycopy(mVertAttribs, 0, newAttribs, 0, VERT_ATTRIBS*numVertices);
|
214 |
0d4aae88
|
Leszek Koltunski
|
System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS );
|
215 |
|
|
origVertices++;
|
216 |
|
|
|
217 |
e7f85322
|
Leszek Koltunski
|
if( numVertices%2==1 )
|
218 |
0d4aae88
|
Leszek Koltunski
|
{
|
219 |
|
|
System.arraycopy(mVertAttribs, VERT_ATTRIBS*(origVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
|
220 |
|
|
origVertices++;
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
|
224 |
e7f85322
|
Leszek Koltunski
|
for(int i=0; i<numMeshes; i++)
|
225 |
0d4aae88
|
Leszek Koltunski
|
{
|
226 |
|
|
mesh = meshes[i];
|
227 |
e7f85322
|
Leszek Koltunski
|
numVertices = mesh.mNumVertices;
|
228 |
0d4aae88
|
Leszek Koltunski
|
|
229 |
22e60fba
|
Leszek Koltunski
|
if( origVertices>0 )
|
230 |
|
|
{
|
231 |
|
|
System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS );
|
232 |
|
|
origVertices++;
|
233 |
|
|
}
|
234 |
e7f85322
|
Leszek Koltunski
|
System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*numVertices);
|
235 |
|
|
origVertices+=numVertices;
|
236 |
0d4aae88
|
Leszek Koltunski
|
|
237 |
e7f85322
|
Leszek Koltunski
|
if( i<numMeshes-1 )
|
238 |
0d4aae88
|
Leszek Koltunski
|
{
|
239 |
e7f85322
|
Leszek Koltunski
|
System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
|
240 |
0d4aae88
|
Leszek Koltunski
|
origVertices++;
|
241 |
|
|
|
242 |
e7f85322
|
Leszek Koltunski
|
if( numVertices%2==1 )
|
243 |
0d4aae88
|
Leszek Koltunski
|
{
|
244 |
e7f85322
|
Leszek Koltunski
|
System.arraycopy(mesh.mVertAttribs, VERT_ATTRIBS*(numVertices-1), newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS);
|
245 |
0d4aae88
|
Leszek Koltunski
|
origVertices++;
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
if( origVertices!=mNumVertices )
|
251 |
|
|
{
|
252 |
|
|
android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices);
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
mVertAttribs = newAttribs;
|
256 |
|
|
|
257 |
22e60fba
|
Leszek Koltunski
|
mVBO.invalidate();
|
258 |
23b733db
|
Leszek Koltunski
|
}
|
259 |
|
|
|
260 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261 |
|
|
/**
|
262 |
0d4aae88
|
Leszek Koltunski
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
263 |
|
|
*
|
264 |
|
|
* @y.exclude
|
265 |
23b733db
|
Leszek Koltunski
|
*/
|
266 |
0d4aae88
|
Leszek Koltunski
|
public int getTFO()
|
267 |
23b733db
|
Leszek Koltunski
|
{
|
268 |
7e53a35f
|
Leszek Koltunski
|
return mTFO.getIndex();
|
269 |
23b733db
|
Leszek Koltunski
|
}
|
270 |
|
|
|
271 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272 |
|
|
/**
|
273 |
0d4aae88
|
Leszek Koltunski
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
274 |
|
|
*
|
275 |
|
|
* @y.exclude
|
276 |
23b733db
|
Leszek Koltunski
|
*/
|
277 |
0d4aae88
|
Leszek Koltunski
|
public int getNumVertices()
|
278 |
23b733db
|
Leszek Koltunski
|
{
|
279 |
0d4aae88
|
Leszek Koltunski
|
return mNumVertices;
|
280 |
23b733db
|
Leszek Koltunski
|
}
|
281 |
|
|
|
282 |
044b5494
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283 |
6c00149d
|
Leszek Koltunski
|
/**
|
284 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
285 |
|
|
*
|
286 |
|
|
* @y.exclude
|
287 |
|
|
*/
|
288 |
da681e7e
|
Leszek Koltunski
|
public void bindVertexAttribs(DistortedProgram program)
|
289 |
6c00149d
|
Leszek Koltunski
|
{
|
290 |
7e53a35f
|
Leszek Koltunski
|
int index = mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs);
|
291 |
22e60fba
|
Leszek Koltunski
|
|
292 |
b7074bc6
|
Leszek Koltunski
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
|
293 |
|
|
GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_POS);
|
294 |
|
|
GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR);
|
295 |
|
|
GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_INF);
|
296 |
|
|
GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT_SIZE, OFFSET_TEX);
|
297 |
|
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
298 |
da681e7e
|
Leszek Koltunski
|
}
|
299 |
|
|
|
300 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
301 |
|
|
/**
|
302 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
303 |
|
|
*
|
304 |
|
|
* @y.exclude
|
305 |
|
|
*/
|
306 |
|
|
public void bindTransformAttribs(DistortedProgram program)
|
307 |
|
|
{
|
308 |
7e53a35f
|
Leszek Koltunski
|
int index = mTFO.createImmediately(mNumVertices*TRAN_SIZE, null);
|
309 |
22e60fba
|
Leszek Koltunski
|
|
310 |
b7074bc6
|
Leszek Koltunski
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index );
|
311 |
|
|
GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
|
312 |
|
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
313 |
6c00149d
|
Leszek Koltunski
|
}
|
314 |
|
|
|
315 |
7a5e538a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316 |
|
|
/**
|
317 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
318 |
|
|
*
|
319 |
|
|
* @y.exclude
|
320 |
|
|
*/
|
321 |
|
|
public void setInflate(float inflate)
|
322 |
|
|
{
|
323 |
|
|
mInflate = inflate;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
327 |
|
|
/**
|
328 |
|
|
* Not part of public API, do not document (public only because has to be used from the main package)
|
329 |
|
|
*
|
330 |
|
|
* @y.exclude
|
331 |
|
|
*/
|
332 |
|
|
public float getInflate()
|
333 |
|
|
{
|
334 |
|
|
return mInflate;
|
335 |
|
|
}
|
336 |
|
|
|
337 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
338 |
|
|
// PUBLIC API
|
339 |
3fc9327a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
340 |
|
|
/**
|
341 |
|
|
* When rendering this Mesh, do we want to render the Normal vectors as well?
|
342 |
420836fc
|
leszek
|
* <p>
|
343 |
|
|
* Will work only on OpenGL ES >= 3.0 devices.
|
344 |
3fc9327a
|
Leszek Koltunski
|
*
|
345 |
|
|
* @param show Controls if we render the Normal vectors or not.
|
346 |
|
|
*/
|
347 |
|
|
public void setShowNormals(boolean show)
|
348 |
|
|
{
|
349 |
b7074bc6
|
Leszek Koltunski
|
mShowNormals = show;
|
350 |
3fc9327a
|
Leszek Koltunski
|
}
|
351 |
466450b5
|
Leszek Koltunski
|
|
352 |
6c00149d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
353 |
|
|
/**
|
354 |
|
|
* When rendering this mesh, should we also draw the normal vectors?
|
355 |
|
|
*
|
356 |
|
|
* @return <i>true</i> if we do render normal vectors
|
357 |
|
|
*/
|
358 |
|
|
public boolean getShowNormals()
|
359 |
|
|
{
|
360 |
|
|
return mShowNormals;
|
361 |
|
|
}
|
362 |
|
|
|
363 |
466450b5
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
364 |
|
|
/**
|
365 |
|
|
* Release all internal resources.
|
366 |
|
|
*/
|
367 |
|
|
public void markForDeletion()
|
368 |
|
|
{
|
369 |
fa8bc998
|
Leszek Koltunski
|
mVertAttribs = null;
|
370 |
466450b5
|
Leszek Koltunski
|
|
371 |
|
|
mVBO.markForDeletion();
|
372 |
|
|
mTFO.markForDeletion();
|
373 |
|
|
}
|
374 |
fa8bc998
|
Leszek Koltunski
|
|
375 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
376 |
|
|
/**
|
377 |
c90aca24
|
Leszek Koltunski
|
* Apply all Effects to the vertex mesh. Overwrite the mesh in place.
|
378 |
fa8bc998
|
Leszek Koltunski
|
* <p>
|
379 |
c90aca24
|
Leszek Koltunski
|
* 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.
|
381 |
fa8bc998
|
Leszek Koltunski
|
*
|
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.
|
385 |
e172985c
|
Leszek Koltunski
|
*
|
386 |
|
|
* @param effects List of Matrix Effects to apply to the Mesh.
|
387 |
fa8bc998
|
Leszek Koltunski
|
*/
|
388 |
|
|
public void apply(MatrixEffect[] effects)
|
389 |
|
|
{
|
390 |
c90aca24
|
Leszek Koltunski
|
float[][] matrix = new float[effects.length][16];
|
391 |
|
|
float[] tmp;
|
392 |
0d4aae88
|
Leszek Koltunski
|
float[] array = new float[7];
|
393 |
c90aca24
|
Leszek Koltunski
|
float x,y,z;
|
394 |
|
|
int numEffects = 0;
|
395 |
|
|
|
396 |
|
|
for(MatrixEffect eff: effects)
|
397 |
fa8bc998
|
Leszek Koltunski
|
{
|
398 |
c90aca24
|
Leszek Koltunski
|
if( eff!=null )
|
399 |
fa8bc998
|
Leszek Koltunski
|
{
|
400 |
c90aca24
|
Leszek Koltunski
|
Matrix.setIdentityM(matrix[numEffects],0);
|
401 |
|
|
eff.compute(array,0,0,0);
|
402 |
|
|
eff.apply(matrix[numEffects], array, 0);
|
403 |
|
|
numEffects++;
|
404 |
fa8bc998
|
Leszek Koltunski
|
}
|
405 |
|
|
}
|
406 |
|
|
|
407 |
7594a5d2
|
Leszek Koltunski
|
for(int index=0; index<mNumVertices*VERT_ATTRIBS; index+=VERT_ATTRIBS )
|
408 |
c90aca24
|
Leszek Koltunski
|
{
|
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 |
7e53a35f
|
Leszek Koltunski
|
mVBO.invalidate();
|
440 |
c90aca24
|
Leszek Koltunski
|
}
|
441 |
|
|
|
442 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
443 |
|
|
/**
|
444 |
a3a05347
|
Leszek Koltunski
|
* Sets texture maps for (some of) the components of this mesh.
|
445 |
c90aca24
|
Leszek Koltunski
|
* <p>
|
446 |
a3a05347
|
Leszek Koltunski
|
* Format: ( x of lower-left corner, y of lower-left corner, width, height ).
|
447 |
|
|
* For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
|
448 |
0d4aae88
|
Leszek Koltunski
|
* upper-left quadrant of the texture.
|
449 |
a3a05347
|
Leszek Koltunski
|
* <p>
|
450 |
|
|
* Probably the most common user case would be sending as many maps as there are components in this
|
451 |
|
|
* Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
|
452 |
|
|
* to null (those will be ignored as well). So if there are 5 components, and we want to set the map
|
453 |
|
|
* of the 2nd and 4rd one, call this with
|
454 |
|
|
* maps = new Static4D[4]
|
455 |
|
|
* maps[0] = null
|
456 |
|
|
* maps[1] = the map for the 2nd component
|
457 |
|
|
* maps[2] = null
|
458 |
|
|
* maps[3] = the map for the 4th component
|
459 |
|
|
*
|
460 |
|
|
* A map's width and height have to be non-zero (but can be negative!)
|
461 |
e172985c
|
Leszek Koltunski
|
*
|
462 |
|
|
* @param maps List of texture maps to apply to the texture's components.
|
463 |
c90aca24
|
Leszek Koltunski
|
*/
|
464 |
a3a05347
|
Leszek Koltunski
|
public void setTextureMap(Static4D[] maps)
|
465 |
c90aca24
|
Leszek Koltunski
|
{
|
466 |
0d4aae88
|
Leszek Koltunski
|
int num_comp = mComponent.size();
|
467 |
|
|
int num_maps = maps.length;
|
468 |
d917f059
|
Leszek Koltunski
|
int min = Math.min(num_comp, num_maps);
|
469 |
de53cf3e
|
Leszek Koltunski
|
int vertex = 0;
|
470 |
a3a05347
|
Leszek Koltunski
|
Static4D newMap, oldMap;
|
471 |
de53cf3e
|
Leszek Koltunski
|
Component comp;
|
472 |
a3a05347
|
Leszek Koltunski
|
float newW, newH, ratW, ratH, movX, movY;
|
473 |
ea88d502
|
Leszek Koltunski
|
|
474 |
0d4aae88
|
Leszek Koltunski
|
for(int i=0; i<min; i++)
|
475 |
ea88d502
|
Leszek Koltunski
|
{
|
476 |
a3a05347
|
Leszek Koltunski
|
newMap = maps[i];
|
477 |
7e53a35f
|
Leszek Koltunski
|
comp = mComponent.get(i);
|
478 |
a3a05347
|
Leszek Koltunski
|
|
479 |
|
|
if( newMap!=null )
|
480 |
ea88d502
|
Leszek Koltunski
|
{
|
481 |
a3a05347
|
Leszek Koltunski
|
newW = newMap.get2();
|
482 |
|
|
newH = newMap.get3();
|
483 |
de53cf3e
|
Leszek Koltunski
|
|
484 |
a3a05347
|
Leszek Koltunski
|
if( newW!=0.0f && newH!=0.0f )
|
485 |
de53cf3e
|
Leszek Koltunski
|
{
|
486 |
a3a05347
|
Leszek Koltunski
|
oldMap = comp.mTextureMap;
|
487 |
|
|
ratW = newW/oldMap.get2();
|
488 |
|
|
ratH = newH/oldMap.get3();
|
489 |
|
|
movX = newMap.get0() - ratW*oldMap.get0();
|
490 |
|
|
movY = newMap.get1() - ratH*oldMap.get1();
|
491 |
|
|
|
492 |
7e53a35f
|
Leszek Koltunski
|
for( int index=vertex*VERT_ATTRIBS+TEX_ATTRIB ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
|
493 |
a3a05347
|
Leszek Koltunski
|
{
|
494 |
|
|
mVertAttribs[index ] = ratW*mVertAttribs[index ] + movX;
|
495 |
|
|
mVertAttribs[index+1] = ratH*mVertAttribs[index+1] + movY;
|
496 |
|
|
}
|
497 |
|
|
comp.setMap(newMap);
|
498 |
de53cf3e
|
Leszek Koltunski
|
}
|
499 |
ea88d502
|
Leszek Koltunski
|
}
|
500 |
7e53a35f
|
Leszek Koltunski
|
|
501 |
|
|
vertex= comp.mEndIndex+1;
|
502 |
ea88d502
|
Leszek Koltunski
|
}
|
503 |
c90aca24
|
Leszek Koltunski
|
|
504 |
7e53a35f
|
Leszek Koltunski
|
mVBO.invalidate();
|
505 |
fa8bc998
|
Leszek Koltunski
|
}
|
506 |
9099e567
|
Leszek Koltunski
|
|
507 |
e172985c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
508 |
|
|
/**
|
509 |
|
|
* Return the texture map of one of the components.
|
510 |
|
|
*
|
511 |
|
|
* @param component The component number whose texture map we want to retrieve.
|
512 |
|
|
*/
|
513 |
|
|
public Static4D getTextureMap(int component)
|
514 |
|
|
{
|
515 |
|
|
return (component>=0 && component<mComponent.size()) ? mComponent.get(component).mTextureMap : null;
|
516 |
|
|
}
|
517 |
|
|
|
518 |
0d4aae88
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
519 |
|
|
/**
|
520 |
|
|
* Sometimes we want to display a Mesh on a rectangular screen. Then we need to stretch it by
|
521 |
|
|
* different factors in x and y (or z) directions. If we also wanted do display some vertex effects
|
522 |
|
|
* done on this mesh, let's say a bulge done by a Distort effect, and wanted the bulge to be round,
|
523 |
|
|
* (i.e the same in x and y directions) then doing so without this method would be impossible.
|
524 |
|
|
*
|
525 |
|
|
* This sets 'stretch' factors in each 3 dimensions. All vertices of this Mesh will be premultiplied
|
526 |
|
|
* by those factors in the very first line of the Vertex Shader, before any Effects are done on it.
|
527 |
|
|
* Using this we can thus pre-stretch the mesh to aspect ratio equal to the surface we eventually
|
528 |
|
|
* want to display the Mesh on, and this way we can achieve a round Distort bulge!
|
529 |
|
|
*
|
530 |
|
|
* This could also be used to pre-stretch a Rectangles Mesh to a size equal (in pixels) to the bitmap
|
531 |
|
|
* this mesh is textured with - and this lets us work with all Effects in natural, pixel units.
|
532 |
|
|
*
|
533 |
|
|
* @param sx stretch factor in x.
|
534 |
|
|
* @param sy stretch factor in y.
|
535 |
|
|
* @param sz stretch factor in z.
|
536 |
|
|
*/
|
537 |
|
|
public void setStretch(float sx, float sy, float sz)
|
538 |
9099e567
|
Leszek Koltunski
|
{
|
539 |
0d4aae88
|
Leszek Koltunski
|
mStretchX = sx;
|
540 |
|
|
mStretchY = sy;
|
541 |
|
|
mStretchZ = sz;
|
542 |
|
|
}
|
543 |
9099e567
|
Leszek Koltunski
|
|
544 |
0d4aae88
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
545 |
|
|
/**
|
546 |
|
|
* Returns the x-factor set by setStretch().
|
547 |
|
|
*/
|
548 |
|
|
public float getStretchX()
|
549 |
|
|
{
|
550 |
|
|
return mStretchX;
|
551 |
|
|
}
|
552 |
9099e567
|
Leszek Koltunski
|
|
553 |
0d4aae88
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
554 |
|
|
/**
|
555 |
|
|
* Returns the y-factor set by setStretch().
|
556 |
|
|
*/
|
557 |
|
|
public float getStretchY()
|
558 |
|
|
{
|
559 |
|
|
return mStretchY;
|
560 |
|
|
}
|
561 |
|
|
|
562 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
563 |
|
|
/**
|
564 |
|
|
* Returns the z-factor set by setStretch().
|
565 |
|
|
*/
|
566 |
|
|
public float getStretchZ()
|
567 |
|
|
{
|
568 |
|
|
return mStretchZ;
|
569 |
9099e567
|
Leszek Koltunski
|
}
|
570 |
6a06a912
|
Leszek Koltunski
|
}
|
571 |
226144d0
|
leszek
|
|
572 |
3fc9327a
|
Leszek Koltunski
|
|