Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshPolygon.java @ fd527acd

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 fd527acd Leszek Koltunski
  private float mVecX, mVecY;
56
  private int[] mEdgeShape;
57 eeb5d115 Leszek Koltunski
58 808ef3aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// polygonVertices>=3 , polygonBands>=2
60
61 d456b075 Leszek Koltunski
  private void computeNumberOfVertices()
62 808ef3aa Leszek Koltunski
     {
63 fa640198 Leszek Koltunski
     if( mNumPolygonBands==2 && extraIndex>0 )
64
       {
65
       numVertices = 1 + 2*mNumPolygonVertices*(1+extraIndex+2*extraVertices);
66
       }
67
     else
68
       {
69
       numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
70
       numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
71
       }
72 d456b075 Leszek Koltunski
73 ac6a08e7 Leszek Koltunski
     remainingVert = numVertices;
74 808ef3aa Leszek Koltunski
     }
75 724f67ee Leszek Koltunski
76 808ef3aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78 eeb5d115 Leszek Koltunski
  private void computeCache()
79 808ef3aa Leszek Koltunski
    {
80 d3287c14 Leszek Koltunski
    mCurveCache = new float[NUM_CACHE];
81
    float[] tmpD = new float[mNumPolygonBands+1];
82
    float[] tmpX = new float[mNumPolygonBands+1];
83 ac6a08e7 Leszek Koltunski
84 d3287c14 Leszek Koltunski
    for(int i=1; i<mNumPolygonBands; i++)
85
      {
86
      tmpD[i] = (mPolygonBands[2*i-1]-mPolygonBands[2*i+1]) / (mPolygonBands[2*i]-mPolygonBands[2*i-2]);
87
      tmpX[i] = 1.0f - (mPolygonBands[2*i]+mPolygonBands[2*i-2])/2;
88
      }
89
90
    tmpD[0] = tmpD[1];
91
    tmpD[mNumPolygonBands] = tmpD[mNumPolygonBands-1];
92
    tmpX[0] = 0.0f;
93
    tmpX[mNumPolygonBands] = 1.0f;
94 ac6a08e7 Leszek Koltunski
95 d3287c14 Leszek Koltunski
    int prev = 0;
96
    int next = 0;
97
98
    for(int i=0; i<NUM_CACHE-1; i++)
99 eeb5d115 Leszek Koltunski
      {
100 d3287c14 Leszek Koltunski
      float x = i/(NUM_CACHE-1.0f);
101 ac6a08e7 Leszek Koltunski
102 d3287c14 Leszek Koltunski
      if( x>=tmpX[next] )
103
        {
104
        prev = next;
105
        while( next<=mNumPolygonBands && x>=tmpX[next] ) next++;
106
        }
107
108
      if( next>prev )
109
        {
110
        float t = (x-tmpX[prev]) / (tmpX[next]-tmpX[prev]);
111
        mCurveCache[i] = t*(tmpD[next]-tmpD[prev]) + tmpD[prev];
112
        }
113
      else
114
        {
115
        mCurveCache[i] = tmpD[next];
116
        }
117 eeb5d115 Leszek Koltunski
      }
118 d3287c14 Leszek Koltunski
119
    mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands];
120 eeb5d115 Leszek Koltunski
    }
121 724f67ee Leszek Koltunski
122 fa640198 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private float getSpecialQuot(int index)
125
    {
126
    int num = 1 + extraIndex + 2*extraVertices;
127
    int change1 = extraVertices+1;
128
    int change2 = num-change1;
129
    float quot = 1.0f/(extraIndex+1);
130
131
    if( index<change1 )      return index*quot/change1;
132
    else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
133
    else                     return (index-change1+1)*quot;
134
    }
135
136 eeb5d115 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137 808ef3aa Leszek Koltunski
138 d456b075 Leszek Koltunski
  private float getQuot(int index, int band, boolean isExtra)
139 eeb5d115 Leszek Koltunski
    {
140 d456b075 Leszek Koltunski
    int num = mNumPolygonBands-1-band;
141 808ef3aa Leszek Koltunski
142 d456b075 Leszek Koltunski
    if( num>0 )
143
      {
144
      if( isExtra )
145
        {
146
        int extra = extraIndex-band+extraVertices;
147
148
        if( index < extra )
149
          {
150
          float quot = ((float)extraIndex-band)/(extra*num);
151
          return index*quot;
152
          }
153
        else if( index > num+2*extraVertices-extra )
154
          {
155
          float quot = ((float)extraIndex-band)/(extra*num);
156
          return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
157
          }
158
        else
159
          {
160
          return ((float)(index-extraVertices))/num;
161
          }
162
        }
163 808ef3aa Leszek Koltunski
164 d456b075 Leszek Koltunski
      return (float)index/num;
165
      }
166 808ef3aa Leszek Koltunski
167 d456b075 Leszek Koltunski
    return 1.0f;
168
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 b2913a86 Leszek Koltunski
  private float computeZEdge(float quot)
173
    {
174
    if( quot>=1.0f ) return 0.0f;
175
176
    for(int band=1; band<mNumPolygonBands; band++)
177
      {
178
      float curr = mPolygonBands[2*band];
179
180
      if( curr<=quot )
181
        {
182
        float prev = mPolygonBands[2*band-2];
183
        float prevH= mPolygonBands[2*band-1];
184
        float currH= mPolygonBands[2*band+1];
185
186
        float A = (prev-quot)/(prev-curr);
187
188
        return A*currH + (1-A)*prevH;
189
        }
190
      }
191
192
    return 0.0f;
193
    }
194
195 d3287c14 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
  private float derivative(float x)
198
    {
199
    if( x>=1.0f )
200
      {
201 fd527acd Leszek Koltunski
      return 0.0f;
202 d3287c14 Leszek Koltunski
      }
203
    else
204
      {
205
      float tmp = x*(NUM_CACHE-1);
206
      int i1 = (int)tmp;
207
      int i2 = i1+1;
208
      return (tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1];
209
      }
210
    }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214 fd527acd Leszek Koltunski
  private void figureOutNormalVector2D(int edgeShape, int polyVertex, int polyEndVer, float quot, float xEdge, float yEdge )
215 d3287c14 Leszek Koltunski
    {
216 fd527acd Leszek Koltunski
    if( edgeShape==SHAPE_DD )
217 d3287c14 Leszek Koltunski
      {
218 fd527acd Leszek Koltunski
      if( quot<0.5f )
219
        {
220
        int p = polyVertex>0 ? polyVertex-1 : mNumPolygonVertices-1;
221
        int prvShape = mEdgeShape[p];
222
223
        switch(prvShape)
224
          {
225
          case SHAPE_DD : mVecX = xEdge;
226
                          mVecY = yEdge;
227
                          break;
228
          case SHAPE_UD :
229
          case SHAPE_DUD: float a = 2*quot;
230
                          float xCurr= mPolygonVertices[2*polyVertex  ];
231
                          float yCurr= mPolygonVertices[2*polyVertex+1];
232
                          float xPrev= mPolygonVertices[2*p  ];
233
                          float yPrev= mPolygonVertices[2*p+1];
234
                          float xVec = xCurr-xPrev;
235
                          float yVec = yCurr-yPrev;
236
237
                          mVecX = a*xEdge + (1-a)*xVec;
238
                          mVecY = a*yEdge + (1-a)*yVec;
239
240
                          break;
241
          default: throw new RuntimeException("figureOutNormalVector2D: impossible1: "+prvShape);
242
          }
243
        }
244
      else
245
        {
246
        int n = polyEndVer==mNumPolygonVertices-1 ? 0 : polyEndVer+1;
247
        int nxtShape = mEdgeShape[polyEndVer];
248
249
        switch(nxtShape)
250
          {
251
          case SHAPE_DD : mVecX = xEdge;
252
                          mVecY = yEdge;
253
                          break;
254
          case SHAPE_DU :
255
          case SHAPE_DUD: float a = 2-2*quot;
256
                          float xCurr= mPolygonVertices[2*polyEndVer  ];
257
                          float yCurr= mPolygonVertices[2*polyEndVer+1];
258
                          float xNext= mPolygonVertices[2*n  ];
259
                          float yNext= mPolygonVertices[2*n+1];
260
                          float xVec = xCurr-xNext;
261
                          float yVec = yCurr-yNext;
262
263
                          mVecX = a*xEdge + (1-a)*xVec;
264
                          mVecY = a*yEdge + (1-a)*yVec;
265
                          break;
266
          default: throw new RuntimeException("figureOutNormalVector2D: impossible2: "+nxtShape);
267
          }
268
        }
269
      }
270
    else if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
271
      {
272
      mVecX = 1;
273
      mVecY = 0;
274 d3287c14 Leszek Koltunski
      }
275
    else
276
      {
277 fd527acd Leszek Koltunski
      float dx = mPolygonVertices[2*polyVertex  ] - mPolygonVertices[2*polyEndVer  ];
278
      float dy = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*polyEndVer+1];
279 d3287c14 Leszek Koltunski
280 fd527acd Leszek Koltunski
      if( quot<0.5 )
281 a60f7acf Leszek Koltunski
        {
282 fd527acd Leszek Koltunski
        mVecX = dx;
283
        mVecY = dy;
284 a60f7acf Leszek Koltunski
        }
285
      else
286
        {
287 fd527acd Leszek Koltunski
        mVecX = -dx;
288
        mVecY = -dy;
289 a60f7acf Leszek Koltunski
        }
290 fd527acd Leszek Koltunski
      }
291
    }
292 d3287c14 Leszek Koltunski
293 fd527acd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
294 d3287c14 Leszek Koltunski
295 fd527acd Leszek Koltunski
  private float figureOutDerivative(int edgeShape, int polyBand, float quot)
296
    {
297
    if( edgeShape==SHAPE_DD )
298
      {
299
      return derivative(1-mPolygonBands[2*polyBand]);
300
      }
301
    if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) )
302
      {
303
      return 0;
304 d3287c14 Leszek Koltunski
      }
305 fd527acd Leszek Koltunski
306
    float x = 1-mPolygonBands[2*polyBand];
307
    float q = quot>=0.5f ? 2-2*quot : 2*quot;
308
    return derivative((1-x)*q+x);
309 d3287c14 Leszek Koltunski
    }
310
311 b2913a86 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
314 fd527acd Leszek Koltunski
                        float[] attribs1, float[] attribs2)
315 d456b075 Leszek Koltunski
    {
316
    remainingVert--;
317
318 eeb5d115 Leszek Koltunski
    float Xfirst= mPolygonVertices[2*polyVertex  ];
319
    float Yfirst= mPolygonVertices[2*polyVertex+1];
320
    float Xlast = mPolygonVertices[2*polyEndVer  ];
321
    float Ylast = mPolygonVertices[2*polyEndVer+1];
322 808ef3aa Leszek Koltunski
323 eeb5d115 Leszek Koltunski
    float xEdge = Xfirst + quot*(Xlast-Xfirst);
324
    float yEdge = Yfirst + quot*(Ylast-Yfirst);
325 b2913a86 Leszek Koltunski
    float zEdge;
326 808ef3aa Leszek Koltunski
327 d3287c14 Leszek Koltunski
    float q = mPolygonBands[2*polyBand];
328
    float o = mPolygonBands[2*polyBand+1];
329
    float t = mPolygonBands[2*mNumPolygonBands-1];
330
331 fd527acd Leszek Koltunski
    int shape = mEdgeShape[polyVertex];
332
333
    switch(shape)
334 ac6a08e7 Leszek Koltunski
      {
335 d3287c14 Leszek Koltunski
      case SHAPE_DD : zEdge = 0.0f; break;
336
      case SHAPE_UU : zEdge = t;    break;
337
      case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break;
338
      case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break;
339
      default       : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break;
340 eeb5d115 Leszek Koltunski
      }
341 ac6a08e7 Leszek Koltunski
342 b2913a86 Leszek Koltunski
    float x = q*xEdge;
343
    float y = q*yEdge;
344 d3287c14 Leszek Koltunski
    float z = o + (t-o)*(zEdge/t);
345 ac6a08e7 Leszek Koltunski
346 eeb5d115 Leszek Koltunski
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
347
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
348 b2913a86 Leszek Koltunski
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
349 eeb5d115 Leszek Koltunski
350 fd527acd Leszek Koltunski
    figureOutNormalVector2D(shape, polyVertex, polyEndVer, quot, xEdge, yEdge);
351
    float d = figureOutDerivative(shape,polyBand,quot);
352
353
    float vx = d*mVecX;
354
    float vy = d*mVecY;
355
    float vz = mVecX*mVecX + mVecY*mVecY;
356
    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
357
358 724f67ee Leszek Koltunski
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
359 fd527acd Leszek Koltunski
    attribs1[index  ] = vx/len;
360
    attribs1[index+1] = vy/len;
361
    attribs1[index+2] = vz/len;
362 724f67ee Leszek Koltunski
363 eeb5d115 Leszek Koltunski
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
364
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
365 808ef3aa Leszek Koltunski
366
    return vertex+1;
367
    }
368
369 b2913a86 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
370
371 fd527acd Leszek Koltunski
  private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
372 808ef3aa Leszek Koltunski
    {
373
    if( polyVertex==0 )
374
      {
375 fd527acd Leszek Koltunski
      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
376
      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
377 808ef3aa Leszek Koltunski
      }
378
379 fa640198 Leszek Koltunski
    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
380 d456b075 Leszek Koltunski
    boolean isExtra = polyBand<extraIndex;
381 fa640198 Leszek Koltunski
    int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
382
    if( isExtra ) numPairs += 2*extraVertices;
383 d456b075 Leszek Koltunski
384 eeb5d115 Leszek Koltunski
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
385 d456b075 Leszek Koltunski
    float quot1, quot2;
386 808ef3aa Leszek Koltunski
387 eeb5d115 Leszek Koltunski
    for(int index=0; index<numPairs; index++)
388 808ef3aa Leszek Koltunski
      {
389 fa640198 Leszek Koltunski
      if( specialCase )
390
        {
391
        quot1 = 1.0f;
392
        quot2 = getSpecialQuot(index+1);
393
        }
394
      else
395
        {
396
        quot1 = getQuot(index  ,polyBand+1, isExtra);
397
        quot2 = getQuot(index+1,polyBand  , isExtra);
398
        }
399 d456b075 Leszek Koltunski
400 fd527acd Leszek Koltunski
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
401
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
402 b2913a86 Leszek Koltunski
      }
403
404 808ef3aa Leszek Koltunski
    return vertex;
405
    }
406
407 b2913a86 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
408
409
  private int computeEdgeShape(int curr)
410
    {
411
    if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD;
412
413
    int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1);
414
    int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1);
415
416 a60f7acf Leszek Koltunski
    boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] );
417
    boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] );
418 b2913a86 Leszek Koltunski
419 a60f7acf Leszek Koltunski
    return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU);
420 b2913a86 Leszek Koltunski
    }
421
422 808ef3aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
423
424
  private void buildGrid(float[] attribs1, float[] attribs2)
425
    {
426
    int vertex=0;
427
428 fd527acd Leszek Koltunski
    mEdgeShape = new int[mNumPolygonVertices];
429 b2913a86 Leszek Koltunski
430
    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
431 fd527acd Leszek Koltunski
      mEdgeShape[polyVertex] = computeEdgeShape(polyVertex);
432 b2913a86 Leszek Koltunski
433 ac6a08e7 Leszek Koltunski
    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
434 808ef3aa Leszek Koltunski
      for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
435 fd527acd Leszek Koltunski
        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
436 808ef3aa Leszek Koltunski
    }
437
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439
// PUBLIC API
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441
/**
442
 * Create a polygon of any shape and varying elevations from the edges towards the center.
443 d456b075 Leszek Koltunski
 * Optionally make it more dense at the vertices.
444 808ef3aa Leszek Koltunski
 *
445
 * @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
446 8b082b9f Leszek Koltunski
 *                   Vertices HAVE TO be specified in a COUNTERCLOCKWISE order (starting from any).
447 808ef3aa Leszek Koltunski
 * @param bands      2K floats; K pairs of two floats each describing a single band.
448
 *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
449
 *                   its elevation). The polygon is split into such concentric bands.
450 ac6a08e7 Leszek Koltunski
 *                   Must be band[2*i] > band[2*(i+1)] !
451 b2913a86 Leszek Koltunski
 * @param edgeUp     N booleans - one for each edge; the first connects vertices (0,1) and (2,3);
452
 *                   then just like 'verticesXY', i.e. counterclockwise.
453
 *                   If this is null, all edges are by default 'down'.
454
 *                   'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its
455
 *                   middle, is equal to the highest elevation in the middle of the mesh.
456
 *                   If the 'previous' edge is also up, then the Z is up horizontally from its middle
457
 *                   to the left vertex; else it goes down along the same function it goes along the bands.
458
 *                   Same with the right half of the edge.
459 a60f7acf Leszek Koltunski
 * @param vertUp     N booleans - one for each vertex. The first is about vertex 0, then vertex 1, etc.
460
 *                   If this is null or false, the vertex is down; otheerwise - it is 'up'.
461
 *                   This is taken into account only in one case: if both edges the vertex is the end of
462
 *                   are 'up', then the vertex can be 'up' or 'down'; otherwise - if at least one of
463
 *                   those edges is 'down' - then the vertex must be 'down' as well and this is ignored.
464 d456b075 Leszek Koltunski
 * @param exIndex    This and the next parameter describe how to make the mesh denser at the
465
 *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
466 f4a2d97e Leszek Koltunski
 *                   outermost band (and 2 triangles of the next band, and 1 triangle of the third
467 d456b075 Leszek Koltunski
 *                   band) get denser - the 3 triangles become 3+2 = 5.
468
 * @param exVertices See above.
469 0b732630 Leszek Koltunski
 * @param centerX    the X coordinate of the 'center' of the Polygon, i.e. point of the mesh
470
 *                   all bands go to.
471
 * @param centerY    Y coordinate of the center.
472 808ef3aa Leszek Koltunski
 */
473 a60f7acf Leszek Koltunski
  public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, boolean[] vertUp, int exIndex, int exVertices, float centerX, float centerY)
474 808ef3aa Leszek Koltunski
    {
475
    super();
476
477 d456b075 Leszek Koltunski
    mPolygonVertices   = verticesXY;
478
    mPolygonBands      = bands;
479
    mNumPolygonVertices= mPolygonVertices.length /2;
480
    mNumPolygonBands   = mPolygonBands.length /2;
481 b2913a86 Leszek Koltunski
    mEdgeUp            = edgeUp;
482 a60f7acf Leszek Koltunski
    mVertUp            = vertUp;
483 d456b075 Leszek Koltunski
    extraIndex         = exIndex;
484
    extraVertices      = exVertices;
485 808ef3aa Leszek Koltunski
486 0b732630 Leszek Koltunski
    if( centerX!=0.0f || centerY!=0.0f )
487
      {
488
      for(int v=0; v<mNumPolygonVertices; v++)
489
        {
490
        mPolygonVertices[2*v  ] -= centerX;
491
        mPolygonVertices[2*v+1] -= centerY;
492
        }
493
      }
494
495 d456b075 Leszek Koltunski
    computeNumberOfVertices();
496 724f67ee Leszek Koltunski
    computeCache();
497 808ef3aa Leszek Koltunski
498
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
499
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
500
501
    buildGrid(attribs1,attribs2);
502
503
    if( remainingVert!=0 )
504 8c57d77b Leszek Koltunski
      DistortedLibrary.logMessage("MeshPolygon: remainingVert " +remainingVert );
505 808ef3aa Leszek Koltunski
506 0b732630 Leszek Koltunski
    if( centerX!=0.0f || centerY!=0.0f )
507
      {
508
      for(int v=0; v<numVertices; v++)
509
        {
510
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB  ] += centerX;
511
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB+1] += centerY;
512
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB  ] += centerX;
513
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB+1] += centerY;
514
        }
515
      }
516
517 808ef3aa Leszek Koltunski
    setAttribs(attribs1,attribs2);
518
    }
519
520 0b732630 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
521
522
  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
523
    {
524 a60f7acf Leszek Koltunski
    this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
525 0b732630 Leszek Koltunski
    }
526
527 d456b075 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
528
/**
529
 * Create a polygon of any shape and varying elevations from the edges towards the center.
530
 * Equivalent of the previous with exIndex=0 or exVertices=0.
531
 */
532
  public MeshPolygon(float[] verticesXY, float[] bands)
533
    {
534 a60f7acf Leszek Koltunski
    this(verticesXY,bands,null,null,0,0,0.0f,0.0f);
535 d456b075 Leszek Koltunski
    }
536
537 808ef3aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539
 * Copy constructor.
540
 */
541
  public MeshPolygon(MeshPolygon mesh, boolean deep)
542
    {
543
    super(mesh,deep);
544
    }
545
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547
/**
548
 * Copy the Mesh.
549
 *
550
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
551
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
552
 *             coordinates and effect associations, is always deep copied)
553
 */
554
  public MeshPolygon copy(boolean deep)
555
    {
556
    return new MeshPolygon(this,deep);
557
    }
558
 }