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