Project

General

Profile

« Previous | Next » 

Revision fd527acd

Added by Leszek Koltunski 11 months ago

improve MeshPolygon. Now Multigon's meshes are close to perfect.

View differences:

src/main/java/org/distorted/library/mesh/MeshPolygon.java
52 52
  private int extraIndex, extraVertices;
53 53

  
54 54
  private float[] mCurveCache;
55
  private float[] mEdgeQuots;
55
  private float mVecX, mVecY;
56
  private int[] mEdgeShape;
56 57

  
57 58
///////////////////////////////////////////////////////////////////////////////////////////////////
58 59
// polygonVertices>=3 , polygonBands>=2
......
118 119
    mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands];
119 120
    }
120 121

  
121
///////////////////////////////////////////////////////////////////////////////////////////////////
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 122
///////////////////////////////////////////////////////////////////////////////////////////////////
150 123

  
151 124
  private float getSpecialQuot(int index)
......
225 198
    {
226 199
    if( x>=1.0f )
227 200
      {
228
      return mCurveCache[NUM_CACHE-1];
201
      return 0.0f;
229 202
      }
230 203
    else
231 204
      {
......
238 211

  
239 212
///////////////////////////////////////////////////////////////////////////////////////////////////
240 213

  
241
  private void doNormals(float[] attribs1, int index, float quot, int edgeShape,
242
                         float xEdge, float yEdge, float zEdge, int polyVertex, int polyBand)
214
  private void figureOutNormalVector2D(int edgeShape, int polyVertex, int polyEndVer, float quot, float xEdge, float yEdge )
243 215
    {
244
    if( ( quot<=0.5f && (edgeShape==SHAPE_UD || edgeShape==SHAPE_UU) ) ||
245
        ( quot> 0.5f && (edgeShape==SHAPE_DU || edgeShape==SHAPE_UU) )  )
216
    if( edgeShape==SHAPE_DD )
246 217
      {
247
      attribs1[index  ] = 0;
248
      attribs1[index+1] = 0;
249
      attribs1[index+2] = 1;
218
      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;
250 274
      }
251 275
    else
252 276
      {
253
      float d,x = 1-mPolygonBands[2*polyBand];
277
      float dx = mPolygonVertices[2*polyVertex  ] - mPolygonVertices[2*polyEndVer  ];
278
      float dy = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*polyEndVer+1];
254 279

  
255
      if( quot==0.0f || quot==1.0f || edgeShape==SHAPE_DD )
280
      if( quot<0.5 )
256 281
        {
257
        float t = mPolygonBands[2*mNumPolygonBands-1];
258
        d = ((t-zEdge)/t)*derivative(x);
282
        mVecX = dx;
283
        mVecY = dy;
259 284
        }
260 285
      else
261 286
        {
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);
287
        mVecX = -dx;
288
        mVecY = -dy;
269 289
        }
290
      }
291
    }
270 292

  
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);
293
///////////////////////////////////////////////////////////////////////////////////////////////////
275 294

  
276
      attribs1[index  ] = vx/len;
277
      attribs1[index+1] = vy/len;
278
      attribs1[index+2] = vz/len;
295
  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;
279 304
      }
305

  
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);
280 309
    }
281 310

  
282 311
///////////////////////////////////////////////////////////////////////////////////////////////////
283 312

  
284 313
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot,
285
                        int edgeShape, float[] attribs1, float[] attribs2)
314
                        float[] attribs1, float[] attribs2)
286 315
    {
287 316
    remainingVert--;
288 317

  
......
299 328
    float o = mPolygonBands[2*polyBand+1];
300 329
    float t = mPolygonBands[2*mNumPolygonBands-1];
301 330

  
302
    switch(edgeShape)
331
    int shape = mEdgeShape[polyVertex];
332

  
333
    switch(shape)
303 334
      {
304 335
      case SHAPE_DD : zEdge = 0.0f; break;
305 336
      case SHAPE_UU : zEdge = t;    break;
......
316 347
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
317 348
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
318 349

  
350
    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

  
319 358
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
320
    doNormals(attribs1,index, quot, edgeShape, xEdge, yEdge, zEdge, polyVertex, polyBand);
359
    attribs1[index  ] = vx/len;
360
    attribs1[index+1] = vy/len;
361
    attribs1[index+2] = vz/len;
321 362

  
322 363
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
323 364
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
......
327 368

  
328 369
///////////////////////////////////////////////////////////////////////////////////////////////////
329 370

  
330
  private int createBandStrip(int vertex, int polyBand, int polyVertex, int edgeShape, float[] attribs1, float[] attribs2)
371
  private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2)
331 372
    {
332 373
    if( polyVertex==0 )
333 374
      {
334
      vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
335
      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2);
375
      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
376
      if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
336 377
      }
337 378

  
338 379
    boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
......
356 397
        quot2 = getQuot(index+1,polyBand  , isExtra);
357 398
        }
358 399

  
359
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,edgeShape,attribs1,attribs2);
360
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,edgeShape,attribs1,attribs2);
400
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2);
401
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,quot2,attribs1,attribs2);
361 402
      }
362 403

  
363 404
    return vertex;
......
384 425
    {
385 426
    int vertex=0;
386 427

  
387
    int[] edgeShape = new int[mNumPolygonVertices];
428
    mEdgeShape = new int[mNumPolygonVertices];
388 429

  
389 430
    for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
390
      edgeShape[polyVertex] = computeEdgeShape(polyVertex);
431
      mEdgeShape[polyVertex] = computeEdgeShape(polyVertex);
391 432

  
392 433
    for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++)
393 434
      for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++)
394
        {
395
       // android.util.Log.e("D", "creating strip polyBand="+polyBand+" polyVertex="+polyVertex+" : "+vertex);
396
        vertex = createBandStrip(vertex,polyBand,polyVertex,edgeShape[polyVertex],attribs1,attribs2);
397
       // android.util.Log.e("D", "  "+vertex);
398
        }
435
        vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2);
399 436
    }
400 437

  
401 438
///////////////////////////////////////////////////////////////////////////////////////////////////
......
457 494

  
458 495
    computeNumberOfVertices();
459 496
    computeCache();
460
    computeQuots();
461 497

  
462 498
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
463 499
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];

Also available in: Unified diff