Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.mesh;
22

    
23
import org.distorted.library.main.DistortedLibrary;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27
 * Create a polygon of any shape and varying elevations from the edges towards the center.
28
 * <p>
29
 * Specify a list of vertices. Any two adjacent vertices + the center (0,0,0) form a triangle. The
30
 * polygon is going to be split into such triangles, and each triangle is split into adjustable number
31
 * of 'bands' form the outer edge towards the center. Edges of each band can can at any elevation.
32
 */
33
public class MeshPolygon extends MeshBase
34
  {
35
  private static final int NUM_CACHE = 20;
36

    
37
  private float[] mPolygonVertices;
38
  private int mNumPolygonVertices;
39
  private float[] mPolygonBands;
40
  private int mNumPolygonBands;
41

    
42
  private int remainingVert;
43
  private int numVertices;
44
  private int extraBand, extraVertices;
45

    
46
  private float[] mCurveCache;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
// polygonVertices>=3 , polygonBands>=2
50

    
51
  private void computeNumberOfVertices()
52
     {
53
     if( mNumPolygonBands==2 && extraBand>0 )
54
       {
55
       numVertices = 1 + 2*mNumPolygonVertices*(1+extraBand+2*extraVertices);
56
       }
57
     else
58
       {
59
       numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1;
60
       numVertices+= 2*mNumPolygonVertices*(2*extraBand*extraVertices);
61
       }
62

    
63
     remainingVert = numVertices;
64
     }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  private void computeCache()
69
    {
70
    mCurveCache = new float[NUM_CACHE];
71
    float[] tmpD = new float[mNumPolygonBands+1];
72
    float[] tmpX = new float[mNumPolygonBands+1];
73

    
74
    for(int i=1; i<mNumPolygonBands; i++)
75
      {
76
      tmpD[i] = (mPolygonBands[2*i-1]-mPolygonBands[2*i+1]) / (mPolygonBands[2*i]-mPolygonBands[2*i-2]);
77
      tmpX[i] = 1.0f - (mPolygonBands[2*i]+mPolygonBands[2*i-2])/2;
78
      }
79

    
80
    tmpD[0] = tmpD[1];
81
    tmpD[mNumPolygonBands] = tmpD[mNumPolygonBands-1];
82
    tmpX[0] = 0.0f;
83
    tmpX[mNumPolygonBands] = 1.0f;
84

    
85
    int prev = 0;
86
    int next = 0;
87

    
88
    for(int i=0; i<NUM_CACHE-1; i++)
89
      {
90
      float x = i/(NUM_CACHE-1.0f);
91

    
92
      if( x>=tmpX[next] )
93
        {
94
        prev = next;
95
        while( next<=mNumPolygonBands && x>=tmpX[next] ) next++;
96
        }
97

    
98
      if( next>prev )
99
        {
100
        float t = (x-tmpX[prev]) / (tmpX[next]-tmpX[prev]);
101
        mCurveCache[i] = t*(tmpD[next]-tmpD[prev]) + tmpD[prev];
102
        }
103
      else
104
        {
105
        mCurveCache[i] = tmpD[next];
106
        }
107
      }
108

    
109
    mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands];
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  private float getSpecialQuot(int index)
115
    {
116
    int num = 1 + extraBand + 2*extraVertices;
117
    int change1 = extraVertices+1;
118
    int change2 = num-change1;
119
    float quot = 1.0f/(extraBand+1);
120

    
121
    if( index<change1 )      return index*quot/change1;
122
    else if( index>change2 ) return 1-quot + (index-change2)*quot/change1;
123
    else                     return (index-change1+1)*quot;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private float getQuot(int index, int band, boolean isExtra)
129
    {
130
    int num = mNumPolygonBands-1-band;
131

    
132
    if( num>0 )
133
      {
134
      if( isExtra )
135
        {
136
        int extra = extraBand-band+extraVertices;
137

    
138
        if( index < extra )
139
          {
140
          float quot = ((float)extraBand-band)/(extra*num);
141
          return index*quot;
142
          }
143
        else if( index > num+2*extraVertices-extra )
144
          {
145
          float quot = ((float)extraBand-band)/(extra*num);
146
          return (1.0f-((float)extraBand-band)/num) + (index-num-2*extraVertices+extra)*quot;
147
          }
148
        else
149
          {
150
          return ((float)(index-extraVertices))/num;
151
          }
152
        }
153

    
154
      return (float)index/num;
155
      }
156

    
157
    return 1.0f;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  private float derivative(float x)
163
    {
164
    if( x>=1.0f )
165
      {
166
      return 0.0f;
167
      }
168
    else
169
      {
170
      float tmp = x*(NUM_CACHE-1);
171
      int i1 = (int)tmp;
172
      int i2 = i1+1;
173

    
174
      // why 0.5? Arbitrarily; this way the cubit faces of Twisty Puzzles
175
      // [the main and only user of this class] look better.
176
      return 0.5f*((tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1]);
177
      }
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
183
                        float[] attribs1, float[] attribs2)
184
    {
185
    remainingVert--;
186

    
187
    float Xfirst= mPolygonVertices[2*polyVertex  ];
188
    float Yfirst= mPolygonVertices[2*polyVertex+1];
189
    float Xlast = mPolygonVertices[2*polyEndVer  ];
190
    float Ylast = mPolygonVertices[2*polyEndVer+1];
191

    
192
    float xEdge = Xfirst + quot*(Xlast-Xfirst);
193
    float yEdge = Yfirst + quot*(Ylast-Yfirst);
194

    
195
    float q = mPolygonBands[2*polyBand];
196
    float x = q*xEdge;
197
    float y = q*yEdge;
198
    float z = mPolygonBands[2*polyBand+1];
199

    
200
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
201
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
202
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
203

    
204
    float d = derivative(1-mPolygonBands[2*polyBand]);
205

    
206
    float vx = d*xEdge;
207
    float vy = d*yEdge;
208
    float vz = xEdge*xEdge + yEdge*yEdge;
209
    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
210

    
211
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
212
    attribs1[index  ] = vx/len;
213
    attribs1[index+1] = vy/len;
214
    attribs1[index+2] = vz/len;
215

    
216
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
217
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
218

    
219
    return vertex+1;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
225
    {
226
    if( polyVertex==0 )
227
      {
228
      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
229
      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
230
      }
231

    
232
    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraBand>0;
233
    boolean isExtra = polyBand<extraBand;
234
    int numPairs = specialCase ? extraBand+1 : mNumPolygonBands-1-polyBand;
235
    if( isExtra ) numPairs += 2*extraVertices;
236

    
237
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
238
    float quot1, quot2;
239

    
240
    for(int index=0; index<numPairs; index++)
241
      {
242
      if( specialCase )
243
        {
244
        quot1 = 1.0f;
245
        quot2 = getSpecialQuot(index+1);
246
        }
247
      else
248
        {
249
        quot1 = getQuot(index  ,polyBand+1, isExtra);
250
        quot2 = getQuot(index+1,polyBand  , isExtra);
251
        }
252

    
253
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
254
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
255
      }
256

    
257
    return vertex;
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private void buildGrid(float[] attribs1, float[] attribs2)
263
    {
264
    int vertex=0;
265

    
266
    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
267
      for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
268
        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// PUBLIC API
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
/**
275
 * Create a polygon of any shape and varying elevations from the edges towards the center.
276
 * Optionally make it more dense at the vertices.
277
 *
278
 * @param verticesXY 2N floats - packed description of polygon vertices. N pairs (x,y).
279
 *                   Vertices HAVE TO be specified in a COUNTERCLOCKWISE order (starting from any).
280
 * @param bands      2K floats; K pairs of two floats each describing a single band.
281
 *                   From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center,
282
 *                   its elevation). The polygon is split into such concentric bands.
283
 *                   Must be band[2*i] > band[2*(i+1)] !
284
 * @param exBand     This and the next parameter describe how to make the mesh denser at the
285
 *                   polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the
286
 *                   outermost band (and 2 triangles of the next band, and 1 triangle of the third
287
 *                   band) get denser - the 3 triangles become 3+2 = 5.
288
 * @param exVertices See above.
289
 * @param centerX    the X coordinate of the 'center' of the Polygon, i.e. point of the mesh
290
 *                   all bands go to.
291
 * @param centerY    Y coordinate of the center.
292
 */
293
  public MeshPolygon(float[] verticesXY, float[] bands, int exBand, int exVertices, float centerX, float centerY)
294
    {
295
    super();
296

    
297
    mPolygonVertices   = verticesXY;
298
    mPolygonBands      = bands;
299
    mNumPolygonVertices= mPolygonVertices.length /2;
300
    mNumPolygonBands   = mPolygonBands.length /2;
301
    extraBand          = exBand;
302
    extraVertices      = exVertices;
303

    
304
    if( centerX!=0.0f || centerY!=0.0f )
305
      {
306
      for(int v=0; v<mNumPolygonVertices; v++)
307
        {
308
        mPolygonVertices[2*v  ] -= centerX;
309
        mPolygonVertices[2*v+1] -= centerY;
310
        }
311
      }
312

    
313
    computeNumberOfVertices();
314
    computeCache();
315

    
316
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
317
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
318

    
319
    buildGrid(attribs1,attribs2);
320

    
321
    if( remainingVert!=0 )
322
      DistortedLibrary.logMessage("MeshPolygon: remainingVert " +remainingVert );
323

    
324
    if( centerX!=0.0f || centerY!=0.0f )
325
      {
326
      for(int v=0; v<numVertices; v++)
327
        {
328
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB  ] += centerX;
329
        attribs1[VERT1_ATTRIBS*v + POS_ATTRIB+1] += centerY;
330
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB  ] += centerX;
331
        attribs2[VERT2_ATTRIBS*v + TEX_ATTRIB+1] += centerY;
332
        }
333
      }
334

    
335
    setAttribs(attribs1,attribs2);
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices)
341
    {
342
    this(verticesXY,bands,exIndex,exVertices,0.0f,0.0f);
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
/**
347
 * Create a polygon of any shape and varying elevations from the edges towards the center.
348
 * Equivalent of the previous with exIndex=0 or exVertices=0.
349
 */
350
  public MeshPolygon(float[] verticesXY, float[] bands)
351
    {
352
    this(verticesXY,bands,0,0,0.0f,0.0f);
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
/**
357
 * Copy constructor.
358
 */
359
  public MeshPolygon(MeshPolygon mesh, boolean deep)
360
    {
361
    super(mesh,deep);
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
/**
366
 * Copy the Mesh.
367
 *
368
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
369
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
370
 *             coordinates and effect associations, is always deep copied)
371
 */
372
  public MeshPolygon copy(boolean deep)
373
    {
374
    return new MeshPolygon(this,deep);
375
    }
376
 }
(8-8/12)