1 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
c4d06f90
|
Leszek Koltunski
|
// Copyright 2020 Leszek Koltunski leszek@koltunski.pl //
|
3 |
808ef3aa
|
Leszek Koltunski
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
c4d06f90
|
Leszek Koltunski
|
// This library is free software; you can redistribute it and/or //
|
7 |
|
|
// modify it under the terms of the GNU Lesser General Public //
|
8 |
|
|
// License as published by the Free Software Foundation; either //
|
9 |
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
10 |
808ef3aa
|
Leszek Koltunski
|
// //
|
11 |
c4d06f90
|
Leszek Koltunski
|
// This library is distributed in the hope that it will be useful, //
|
12 |
808ef3aa
|
Leszek Koltunski
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
c4d06f90
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
808ef3aa
|
Leszek Koltunski
|
// //
|
16 |
c4d06f90
|
Leszek Koltunski
|
// You should have received a copy of the GNU Lesser General Public //
|
17 |
|
|
// License along with this library; if not, write to the Free Software //
|
18 |
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
|
|
package org.distorted.library.mesh;
|
22 |
|
|
|
23 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
24 |
8c57d77b
|
Leszek Koltunski
|
|
25 |
|
|
import org.distorted.library.main.DistortedLibrary;
|
26 |
|
|
|
27 |
808ef3aa
|
Leszek Koltunski
|
/**
|
28 |
|
|
* Create a polygon of any shape and varying elevations from the edges towards the center.
|
29 |
|
|
* <p>
|
30 |
|
|
* Specify a list of vertices. Any two adjacent vertices + the center (0,0,0) form a triangle. The
|
31 |
2d732361
|
Leszek Koltunski
|
* polygon is going to be split into such triangles, and each triangle is split into adjustable number
|
32 |
808ef3aa
|
Leszek Koltunski
|
* of 'bands' form the outer edge towards the center. Edges of each band can can at any elevation.
|
33 |
|
|
*/
|
34 |
|
|
public class MeshPolygon extends MeshBase
|
35 |
|
|
{
|
36 |
d3287c14
|
Leszek Koltunski
|
private static final int NUM_CACHE = 20;
|
37 |
b2913a86
|
Leszek Koltunski
|
private static final int SHAPE_DD = 0;
|
38 |
|
|
private static final int SHAPE_DU = 1;
|
39 |
|
|
private static final int SHAPE_UD = 2;
|
40 |
|
|
private static final int SHAPE_UU = 3;
|
41 |
|
|
private static final int SHAPE_DUD= 4;
|
42 |
|
|
|
43 |
808ef3aa
|
Leszek Koltunski
|
private float[] mPolygonVertices;
|
44 |
|
|
private int mNumPolygonVertices;
|
45 |
|
|
private float[] mPolygonBands;
|
46 |
|
|
private int mNumPolygonBands;
|
47 |
b2913a86
|
Leszek Koltunski
|
private boolean[] mEdgeUp;
|
48 |
a60f7acf
|
Leszek Koltunski
|
private boolean[] mVertUp;
|
49 |
808ef3aa
|
Leszek Koltunski
|
|
50 |
|
|
private int remainingVert;
|
51 |
|
|
private int numVertices;
|
52 |
d456b075
|
Leszek Koltunski
|
private int extraIndex, extraVertices;
|
53 |
808ef3aa
|
Leszek Koltunski
|
|
54 |
d3287c14
|
Leszek Koltunski
|
private float[] mCurveCache;
|
55 |
a60f7acf
|
Leszek Koltunski
|
private float[] mEdgeQuots;
|
56 |
eeb5d115
|
Leszek Koltunski
|
|
57 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58 |
|
|
// polygonVertices>=3 , polygonBands>=2
|
59 |
|
|
|
60 |
d456b075
|
Leszek Koltunski
|
private void computeNumberOfVertices()
|
61 |
808ef3aa
|
Leszek Koltunski
|
{
|
62 |
fa640198
|
Leszek Koltunski
|
if( mNumPolygonBands==2 && extraIndex>0 )
|
63 |
|
|
{
|
64 |
|
|
numVertices = 1 + 2*mNumPolygonVertices*(1+extraIndex+2*extraVertices);
|
65 |
|
|
}
|
66 |
|
|
else
|
67 |
|
|
{
|
68 |
|
|
numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
|
69 |
|
|
numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
|
70 |
|
|
}
|
71 |
d456b075
|
Leszek Koltunski
|
|
72 |
ac6a08e7
|
Leszek Koltunski
|
remainingVert = numVertices;
|
73 |
808ef3aa
|
Leszek Koltunski
|
}
|
74 |
724f67ee
|
Leszek Koltunski
|
|
75 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
|
77 |
eeb5d115
|
Leszek Koltunski
|
private void computeCache()
|
78 |
808ef3aa
|
Leszek Koltunski
|
{
|
79 |
d3287c14
|
Leszek Koltunski
|
mCurveCache = new float[NUM_CACHE];
|
80 |
|
|
float[] tmpD = new float[mNumPolygonBands+1];
|
81 |
|
|
float[] tmpX = new float[mNumPolygonBands+1];
|
82 |
ac6a08e7
|
Leszek Koltunski
|
|
83 |
d3287c14
|
Leszek Koltunski
|
for(int i=1; i<mNumPolygonBands; i++)
|
84 |
|
|
{
|
85 |
|
|
tmpD[i] = (mPolygonBands[2*i-1]-mPolygonBands[2*i+1]) / (mPolygonBands[2*i]-mPolygonBands[2*i-2]);
|
86 |
|
|
tmpX[i] = 1.0f - (mPolygonBands[2*i]+mPolygonBands[2*i-2])/2;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
tmpD[0] = tmpD[1];
|
90 |
|
|
tmpD[mNumPolygonBands] = tmpD[mNumPolygonBands-1];
|
91 |
|
|
tmpX[0] = 0.0f;
|
92 |
|
|
tmpX[mNumPolygonBands] = 1.0f;
|
93 |
ac6a08e7
|
Leszek Koltunski
|
|
94 |
d3287c14
|
Leszek Koltunski
|
int prev = 0;
|
95 |
|
|
int next = 0;
|
96 |
|
|
|
97 |
|
|
for(int i=0; i<NUM_CACHE-1; i++)
|
98 |
eeb5d115
|
Leszek Koltunski
|
{
|
99 |
d3287c14
|
Leszek Koltunski
|
float x = i/(NUM_CACHE-1.0f);
|
100 |
ac6a08e7
|
Leszek Koltunski
|
|
101 |
d3287c14
|
Leszek Koltunski
|
if( x>=tmpX[next] )
|
102 |
|
|
{
|
103 |
|
|
prev = next;
|
104 |
|
|
while( next<=mNumPolygonBands && x>=tmpX[next] ) next++;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
if( next>prev )
|
108 |
|
|
{
|
109 |
|
|
float t = (x-tmpX[prev]) / (tmpX[next]-tmpX[prev]);
|
110 |
|
|
mCurveCache[i] = t*(tmpD[next]-tmpD[prev]) + tmpD[prev];
|
111 |
|
|
}
|
112 |
|
|
else
|
113 |
|
|
{
|
114 |
|
|
mCurveCache[i] = tmpD[next];
|
115 |
|
|
}
|
116 |
eeb5d115
|
Leszek Koltunski
|
}
|
117 |
d3287c14
|
Leszek Koltunski
|
|
118 |
|
|
mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands];
|
119 |
eeb5d115
|
Leszek Koltunski
|
}
|
120 |
724f67ee
|
Leszek Koltunski
|
|
121 |
a60f7acf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122 |
|
|
|
123 |
|
|
private void computeQuots()
|
124 |
|
|
{
|
125 |
|
|
mEdgeQuots = new float[mNumPolygonVertices];
|
126 |
|
|
|
127 |
|
|
for(int i=0; i<mNumPolygonVertices; i++)
|
128 |
|
|
{
|
129 |
|
|
int n = (i==mNumPolygonVertices-1 ? 0:i+1);
|
130 |
|
|
|
131 |
|
|
float x1 = mPolygonVertices[2*i];
|
132 |
|
|
float y1 = mPolygonVertices[2*i+1];
|
133 |
|
|
float x2 = mPolygonVertices[2*n];
|
134 |
|
|
float y2 = mPolygonVertices[2*n+1];
|
135 |
|
|
float A = x1-x2;
|
136 |
|
|
float B = y1-y2;
|
137 |
|
|
float C = A*A+B*B;
|
138 |
|
|
|
139 |
|
|
float x = (B*B*x1-A*B*y1)/C;
|
140 |
|
|
float y = (A*A*y1-A*B*x1)/C;
|
141 |
|
|
|
142 |
|
|
float dx = x1-x;
|
143 |
|
|
float dy = y1-y;
|
144 |
|
|
|
145 |
|
|
mEdgeQuots[i] = (float)Math.sqrt((dx*dx+dy*dy)/C);
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
|
149 |
fa640198
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150 |
|
|
|
151 |
|
|
private float getSpecialQuot(int index)
|
152 |
|
|
{
|
153 |
|
|
int num = 1 + extraIndex + 2*extraVertices;
|
154 |
|
|
int change1 = extraVertices+1;
|
155 |
|
|
int change2 = num-change1;
|
156 |
|
|
float quot = 1.0f/(extraIndex+1);
|
157 |
|
|
|
158 |
|
|
if( index<change1 ) return index*quot/change1;
|
159 |
|
|
else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
|
160 |
|
|
else return (index-change1+1)*quot;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
eeb5d115
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164 |
808ef3aa
|
Leszek Koltunski
|
|
165 |
d456b075
|
Leszek Koltunski
|
private float getQuot(int index, int band, boolean isExtra)
|
166 |
eeb5d115
|
Leszek Koltunski
|
{
|
167 |
d456b075
|
Leszek Koltunski
|
int num = mNumPolygonBands-1-band;
|
168 |
808ef3aa
|
Leszek Koltunski
|
|
169 |
d456b075
|
Leszek Koltunski
|
if( num>0 )
|
170 |
|
|
{
|
171 |
|
|
if( isExtra )
|
172 |
|
|
{
|
173 |
|
|
int extra = extraIndex-band+extraVertices;
|
174 |
|
|
|
175 |
|
|
if( index < extra )
|
176 |
|
|
{
|
177 |
|
|
float quot = ((float)extraIndex-band)/(extra*num);
|
178 |
|
|
return index*quot;
|
179 |
|
|
}
|
180 |
|
|
else if( index > num+2*extraVertices-extra )
|
181 |
|
|
{
|
182 |
|
|
float quot = ((float)extraIndex-band)/(extra*num);
|
183 |
|
|
return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
|
184 |
|
|
}
|
185 |
|
|
else
|
186 |
|
|
{
|
187 |
|
|
return ((float)(index-extraVertices))/num;
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
808ef3aa
|
Leszek Koltunski
|
|
191 |
d456b075
|
Leszek Koltunski
|
return (float)index/num;
|
192 |
|
|
}
|
193 |
808ef3aa
|
Leszek Koltunski
|
|
194 |
d456b075
|
Leszek Koltunski
|
return 1.0f;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198 |
|
|
|
199 |
b2913a86
|
Leszek Koltunski
|
private float computeZEdge(float quot)
|
200 |
|
|
{
|
201 |
|
|
if( quot>=1.0f ) return 0.0f;
|
202 |
|
|
|
203 |
|
|
for(int band=1; band<mNumPolygonBands; band++)
|
204 |
|
|
{
|
205 |
|
|
float curr = mPolygonBands[2*band];
|
206 |
|
|
|
207 |
|
|
if( curr<=quot )
|
208 |
|
|
{
|
209 |
|
|
float prev = mPolygonBands[2*band-2];
|
210 |
|
|
float prevH= mPolygonBands[2*band-1];
|
211 |
|
|
float currH= mPolygonBands[2*band+1];
|
212 |
|
|
|
213 |
|
|
float A = (prev-quot)/(prev-curr);
|
214 |
|
|
|
215 |
|
|
return A*currH + (1-A)*prevH;
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
return 0.0f;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
d3287c14
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223 |
|
|
|
224 |
|
|
private float derivative(float x)
|
225 |
|
|
{
|
226 |
|
|
if( x>=1.0f )
|
227 |
|
|
{
|
228 |
|
|
return mCurveCache[NUM_CACHE-1];
|
229 |
|
|
}
|
230 |
|
|
else
|
231 |
|
|
{
|
232 |
|
|
float tmp = x*(NUM_CACHE-1);
|
233 |
|
|
int i1 = (int)tmp;
|
234 |
|
|
int i2 = i1+1;
|
235 |
|
|
return (tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1];
|
236 |
|
|
}
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240 |
|
|
|
241 |
|
|
private void doNormals(float[] attribs1, int index, float quot, int edgeShape,
|
242 |
a60f7acf
|
Leszek Koltunski
|
float xEdge, float yEdge, float zEdge, int polyVertex, int polyBand)
|
243 |
d3287c14
|
Leszek Koltunski
|
{
|
244 |
|
|
if( ( quot<=0.5f && (edgeShape==SHAPE_UD || edgeShape==SHAPE_UU) ) ||
|
245 |
|
|
( quot> 0.5f && (edgeShape==SHAPE_DU || edgeShape==SHAPE_UU) ) )
|
246 |
|
|
{
|
247 |
|
|
attribs1[index ] = 0;
|
248 |
|
|
attribs1[index+1] = 0;
|
249 |
|
|
attribs1[index+2] = 1;
|
250 |
|
|
}
|
251 |
|
|
else
|
252 |
|
|
{
|
253 |
a60f7acf
|
Leszek Koltunski
|
float d,x = 1-mPolygonBands[2*polyBand];
|
254 |
d3287c14
|
Leszek Koltunski
|
|
255 |
d45273cd
|
Leszek Koltunski
|
if( quot==0.0f || quot==1.0f || edgeShape==SHAPE_DD )
|
256 |
a60f7acf
|
Leszek Koltunski
|
{
|
257 |
|
|
float t = mPolygonBands[2*mNumPolygonBands-1];
|
258 |
|
|
d = ((t-zEdge)/t)*derivative(x);
|
259 |
|
|
}
|
260 |
|
|
else
|
261 |
|
|
{
|
262 |
|
|
float q = quot + x*(mEdgeQuots[polyVertex]-quot);
|
263 |
|
|
d = (q<0.5f ? derivative(2*q) : -derivative(2-2*q));
|
264 |
|
|
int nextVertex = polyVertex==mNumPolygonVertices-1 ? 0:polyVertex+1;
|
265 |
|
|
xEdge = mPolygonVertices[2*polyVertex ] - mPolygonVertices[2*nextVertex ];
|
266 |
|
|
yEdge = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*nextVertex+1];
|
267 |
|
|
|
268 |
|
|
//android.util.Log.e("D", "normals: edgeQuot="+mEdgeQuots[polyVertex]+" vertex="+polyVertex+" d="+d+" xEdge="+xEdge+" yEdge="+yEdge+" q="+q+" quot="+quot);
|
269 |
|
|
}
|
270 |
d3287c14
|
Leszek Koltunski
|
|
271 |
|
|
float vx = d*xEdge;
|
272 |
|
|
float vy = d*yEdge;
|
273 |
|
|
float vz = xEdge*xEdge + yEdge*yEdge;
|
274 |
|
|
float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
|
275 |
|
|
|
276 |
|
|
attribs1[index ] = vx/len;
|
277 |
|
|
attribs1[index+1] = vy/len;
|
278 |
|
|
attribs1[index+2] = vz/len;
|
279 |
|
|
}
|
280 |
|
|
}
|
281 |
|
|
|
282 |
b2913a86
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283 |
|
|
|
284 |
|
|
private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
|
285 |
|
|
int edgeShape, float[] attribs1, float[] attribs2)
|
286 |
d456b075
|
Leszek Koltunski
|
{
|
287 |
|
|
remainingVert--;
|
288 |
|
|
|
289 |
eeb5d115
|
Leszek Koltunski
|
float Xfirst= mPolygonVertices[2*polyVertex ];
|
290 |
|
|
float Yfirst= mPolygonVertices[2*polyVertex+1];
|
291 |
|
|
float Xlast = mPolygonVertices[2*polyEndVer ];
|
292 |
|
|
float Ylast = mPolygonVertices[2*polyEndVer+1];
|
293 |
808ef3aa
|
Leszek Koltunski
|
|
294 |
eeb5d115
|
Leszek Koltunski
|
float xEdge = Xfirst + quot*(Xlast-Xfirst);
|
295 |
|
|
float yEdge = Yfirst + quot*(Ylast-Yfirst);
|
296 |
b2913a86
|
Leszek Koltunski
|
float zEdge;
|
297 |
808ef3aa
|
Leszek Koltunski
|
|
298 |
d3287c14
|
Leszek Koltunski
|
float q = mPolygonBands[2*polyBand];
|
299 |
|
|
float o = mPolygonBands[2*polyBand+1];
|
300 |
|
|
float t = mPolygonBands[2*mNumPolygonBands-1];
|
301 |
|
|
|
302 |
b2913a86
|
Leszek Koltunski
|
switch(edgeShape)
|
303 |
ac6a08e7
|
Leszek Koltunski
|
{
|
304 |
d3287c14
|
Leszek Koltunski
|
case SHAPE_DD : zEdge = 0.0f; break;
|
305 |
|
|
case SHAPE_UU : zEdge = t; break;
|
306 |
|
|
case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break;
|
307 |
|
|
case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break;
|
308 |
|
|
default : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break;
|
309 |
eeb5d115
|
Leszek Koltunski
|
}
|
310 |
ac6a08e7
|
Leszek Koltunski
|
|
311 |
b2913a86
|
Leszek Koltunski
|
float x = q*xEdge;
|
312 |
|
|
float y = q*yEdge;
|
313 |
d3287c14
|
Leszek Koltunski
|
float z = o + (t-o)*(zEdge/t);
|
314 |
ac6a08e7
|
Leszek Koltunski
|
|
315 |
eeb5d115
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x;
|
316 |
|
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
|
317 |
b2913a86
|
Leszek Koltunski
|
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
|
318 |
eeb5d115
|
Leszek Koltunski
|
|
319 |
724f67ee
|
Leszek Koltunski
|
int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
|
320 |
a60f7acf
|
Leszek Koltunski
|
doNormals(attribs1,index, quot, edgeShape, xEdge, yEdge, zEdge, polyVertex, polyBand);
|
321 |
724f67ee
|
Leszek Koltunski
|
|
322 |
eeb5d115
|
Leszek Koltunski
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f;
|
323 |
|
|
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
|
324 |
808ef3aa
|
Leszek Koltunski
|
|
325 |
|
|
return vertex+1;
|
326 |
|
|
}
|
327 |
|
|
|
328 |
b2913a86
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
329 |
|
|
|
330 |
|
|
private int createBandStrip(int vertex, int polyBand, int polyVertex, int edgeShape, float[] attribs1, float[] attribs2)
|
331 |
808ef3aa
|
Leszek Koltunski
|
{
|
332 |
|
|
if( polyVertex==0 )
|
333 |
|
|
{
|
334 |
b2913a86
|
Leszek Koltunski
|
vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
|
335 |
|
|
if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
|
336 |
808ef3aa
|
Leszek Koltunski
|
}
|
337 |
|
|
|
338 |
fa640198
|
Leszek Koltunski
|
boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
|
339 |
d456b075
|
Leszek Koltunski
|
boolean isExtra = polyBand<extraIndex;
|
340 |
fa640198
|
Leszek Koltunski
|
int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
|
341 |
|
|
if( isExtra ) numPairs += 2*extraVertices;
|
342 |
d456b075
|
Leszek Koltunski
|
|
343 |
eeb5d115
|
Leszek Koltunski
|
int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
|
344 |
d456b075
|
Leszek Koltunski
|
float quot1, quot2;
|
345 |
808ef3aa
|
Leszek Koltunski
|
|
346 |
eeb5d115
|
Leszek Koltunski
|
for(int index=0; index<numPairs; index++)
|
347 |
808ef3aa
|
Leszek Koltunski
|
{
|
348 |
fa640198
|
Leszek Koltunski
|
if( specialCase )
|
349 |
|
|
{
|
350 |
|
|
quot1 = 1.0f;
|
351 |
|
|
quot2 = getSpecialQuot(index+1);
|
352 |
|
|
}
|
353 |
|
|
else
|
354 |
|
|
{
|
355 |
|
|
quot1 = getQuot(index ,polyBand+1, isExtra);
|
356 |
|
|
quot2 = getQuot(index+1,polyBand , isExtra);
|
357 |
|
|
}
|
358 |
d456b075
|
Leszek Koltunski
|
|
359 |
b2913a86
|
Leszek Koltunski
|
vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,edgeShape,attribs1,attribs2);
|
360 |
|
|
vertex = addVertex(vertex,polyBand ,polyVertex,polyEndVer,quot2,edgeShape,attribs1,attribs2);
|
361 |
|
|
}
|
362 |
|
|
|
363 |
808ef3aa
|
Leszek Koltunski
|
return vertex;
|
364 |
|
|
}
|
365 |
|
|
|
366 |
b2913a86
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
367 |
|
|
|
368 |
|
|
private int computeEdgeShape(int curr)
|
369 |
|
|
{
|
370 |
|
|
if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
|
371 |
|
|
|
372 |
|
|
int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
|
373 |
|
|
int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
|
374 |
|
|
|
375 |
a60f7acf
|
Leszek Koltunski
|
boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] );
|
376 |
|
|
boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] );
|
377 |
b2913a86
|
Leszek Koltunski
|
|
378 |
a60f7acf
|
Leszek Koltunski
|
return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU);
|
379 |
b2913a86
|
Leszek Koltunski
|
}
|
380 |
|
|
|
381 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
382 |
|
|
|
383 |
|
|
private void buildGrid(float[] attribs1, float[] attribs2)
|
384 |
|
|
{
|
385 |
|
|
int vertex=0;
|
386 |
|
|
|
387 |
b2913a86
|
Leszek Koltunski
|
int[] edgeShape = new int[mNumPolygonVertices];
|
388 |
|
|
|
389 |
|
|
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
|
390 |
|
|
edgeShape[polyVertex] = computeEdgeShape(polyVertex);
|
391 |
|
|
|
392 |
ac6a08e7
|
Leszek Koltunski
|
for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
|
393 |
808ef3aa
|
Leszek Koltunski
|
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
|
394 |
|
|
{
|
395 |
82b62e04
|
Leszek Koltunski
|
// android.util.Log.e("D", "creating strip polyBand="+polyBand+" polyVertex="+polyVertex+" : "+vertex);
|
396 |
b2913a86
|
Leszek Koltunski
|
vertex = createBandStrip(vertex,polyBand,polyVertex,edgeShape[polyVertex],attribs1,attribs2);
|
397 |
82b62e04
|
Leszek Koltunski
|
// android.util.Log.e("D", " "+vertex);
|
398 |
808ef3aa
|
Leszek Koltunski
|
}
|
399 |
|
|
}
|
400 |
|
|
|
401 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402 |
|
|
// PUBLIC API
|
403 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
404 |
|
|
/**
|
405 |
|
|
* Create a polygon of any shape and varying elevations from the edges towards the center.
|
406 |
d456b075
|
Leszek Koltunski
|
* Optionally make it more dense at the vertices.
|
407 |
808ef3aa
|
Leszek Koltunski
|
*
|
408 |
|
|
* @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
|
409 |
8b082b9f
|
Leszek Koltunski
|
* Vertices HAVE TO be specified in a COUNTERCLOCKWISE order (starting from any).
|
410 |
808ef3aa
|
Leszek Koltunski
|
* @param bands 2K floats; K pairs of two floats each describing a single band.
|
411 |
|
|
* From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
|
412 |
|
|
* its elevation). The polygon is split into such concentric bands.
|
413 |
ac6a08e7
|
Leszek Koltunski
|
* Must be band[2*i] > band[2*(i+1)] !
|
414 |
b2913a86
|
Leszek Koltunski
|
* @param edgeUp N booleans - one for each edge; the first connects vertices (0,1) and (2,3);
|
415 |
|
|
* then just like 'verticesXY', i.e. counterclockwise.
|
416 |
|
|
* If this is null, all edges are by default 'down'.
|
417 |
|
|
* 'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its
|
418 |
|
|
* middle, is equal to the highest elevation in the middle of the mesh.
|
419 |
|
|
* If the 'previous' edge is also up, then the Z is up horizontally from its middle
|
420 |
|
|
* to the left vertex; else it goes down along the same function it goes along the bands.
|
421 |
|
|
* Same with the right half of the edge.
|
422 |
a60f7acf
|
Leszek Koltunski
|
* @param vertUp N booleans - one for each vertex. The first is about vertex 0, then vertex 1, etc.
|
423 |
|
|
* If this is null or false, the vertex is down; otheerwise - it is 'up'.
|
424 |
|
|
* This is taken into account only in one case: if both edges the vertex is the end of
|
425 |
|
|
* are 'up', then the vertex can be 'up' or 'down'; otherwise - if at least one of
|
426 |
|
|
* those edges is 'down' - then the vertex must be 'down' as well and this is ignored.
|
427 |
d456b075
|
Leszek Koltunski
|
* @param exIndex This and the next parameter describe how to make the mesh denser at the
|
428 |
|
|
* polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
|
429 |
f4a2d97e
|
Leszek Koltunski
|
* outermost band (and 2 triangles of the next band, and 1 triangle of the third
|
430 |
d456b075
|
Leszek Koltunski
|
* band) get denser - the 3 triangles become 3+2 = 5.
|
431 |
|
|
* @param exVertices See above.
|
432 |
0b732630
|
Leszek Koltunski
|
* @param centerX the X coordinate of the 'center' of the Polygon, i.e. point of the mesh
|
433 |
|
|
* all bands go to.
|
434 |
|
|
* @param centerY Y coordinate of the center.
|
435 |
808ef3aa
|
Leszek Koltunski
|
*/
|
436 |
a60f7acf
|
Leszek Koltunski
|
public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, boolean[] vertUp, int exIndex, int exVertices, float centerX, float centerY)
|
437 |
808ef3aa
|
Leszek Koltunski
|
{
|
438 |
|
|
super();
|
439 |
|
|
|
440 |
d456b075
|
Leszek Koltunski
|
mPolygonVertices = verticesXY;
|
441 |
|
|
mPolygonBands = bands;
|
442 |
|
|
mNumPolygonVertices= mPolygonVertices.length /2;
|
443 |
|
|
mNumPolygonBands = mPolygonBands.length /2;
|
444 |
b2913a86
|
Leszek Koltunski
|
mEdgeUp = edgeUp;
|
445 |
a60f7acf
|
Leszek Koltunski
|
mVertUp = vertUp;
|
446 |
d456b075
|
Leszek Koltunski
|
extraIndex = exIndex;
|
447 |
|
|
extraVertices = exVertices;
|
448 |
808ef3aa
|
Leszek Koltunski
|
|
449 |
0b732630
|
Leszek Koltunski
|
if( centerX!=0.0f || centerY!=0.0f )
|
450 |
|
|
{
|
451 |
|
|
for(int v=0; v<mNumPolygonVertices; v++)
|
452 |
|
|
{
|
453 |
|
|
mPolygonVertices[2*v ] -= centerX;
|
454 |
|
|
mPolygonVertices[2*v+1] -= centerY;
|
455 |
|
|
}
|
456 |
|
|
}
|
457 |
|
|
|
458 |
d456b075
|
Leszek Koltunski
|
computeNumberOfVertices();
|
459 |
724f67ee
|
Leszek Koltunski
|
computeCache();
|
460 |
a60f7acf
|
Leszek Koltunski
|
computeQuots();
|
461 |
808ef3aa
|
Leszek Koltunski
|
|
462 |
|
|
float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
|
463 |
|
|
float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
|
464 |
|
|
|
465 |
|
|
buildGrid(attribs1,attribs2);
|
466 |
|
|
|
467 |
|
|
if( remainingVert!=0 )
|
468 |
8c57d77b
|
Leszek Koltunski
|
DistortedLibrary.logMessage("MeshPolygon: remainingVert " +remainingVert );
|
469 |
808ef3aa
|
Leszek Koltunski
|
|
470 |
0b732630
|
Leszek Koltunski
|
if( centerX!=0.0f || centerY!=0.0f )
|
471 |
|
|
{
|
472 |
|
|
for(int v=0; v<numVertices; v++)
|
473 |
|
|
{
|
474 |
|
|
attribs1[VERT1_ATTRIBS*v + POS_ATTRIB ] += centerX;
|
475 |
|
|
attribs1[VERT1_ATTRIBS*v + POS_ATTRIB+1] += centerY;
|
476 |
|
|
attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB ] += centerX;
|
477 |
|
|
attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB+1] += centerY;
|
478 |
|
|
}
|
479 |
|
|
}
|
480 |
|
|
|
481 |
808ef3aa
|
Leszek Koltunski
|
setAttribs(attribs1,attribs2);
|
482 |
|
|
}
|
483 |
|
|
|
484 |
0b732630
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
485 |
|
|
|
486 |
|
|
public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
|
487 |
|
|
{
|
488 |
a60f7acf
|
Leszek Koltunski
|
this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
|
489 |
0b732630
|
Leszek Koltunski
|
}
|
490 |
|
|
|
491 |
d456b075
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
492 |
|
|
/**
|
493 |
|
|
* Create a polygon of any shape and varying elevations from the edges towards the center.
|
494 |
|
|
* Equivalent of the previous with exIndex=0 or exVertices=0.
|
495 |
|
|
*/
|
496 |
|
|
public MeshPolygon(float[] verticesXY, float[] bands)
|
497 |
|
|
{
|
498 |
a60f7acf
|
Leszek Koltunski
|
this(verticesXY,bands,null,null,0,0,0.0f,0.0f);
|
499 |
d456b075
|
Leszek Koltunski
|
}
|
500 |
|
|
|
501 |
808ef3aa
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
502 |
|
|
/**
|
503 |
|
|
* Copy constructor.
|
504 |
|
|
*/
|
505 |
|
|
public MeshPolygon(MeshPolygon mesh, boolean deep)
|
506 |
|
|
{
|
507 |
|
|
super(mesh,deep);
|
508 |
|
|
}
|
509 |
|
|
|
510 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
511 |
|
|
/**
|
512 |
|
|
* Copy the Mesh.
|
513 |
|
|
*
|
514 |
|
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
515 |
|
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
516 |
|
|
* coordinates and effect associations, is always deep copied)
|
517 |
|
|
*/
|
518 |
|
|
public MeshPolygon copy(boolean deep)
|
519 |
|
|
{
|
520 |
|
|
return new MeshPolygon(this,deep);
|
521 |
|
|
}
|
522 |
|
|
}
|