Project

General

Profile

« Previous | Next » 

Revision 724f67ee

Added by Leszek Koltunski 11 months ago

MeshPolygon: fix the normals.

View differences:

src/main/java/org/distorted/library/mesh/MeshPolygon.java
33 33
 */
34 34
public class MeshPolygon extends MeshBase
35 35
  {
36
  private static final float NOT_DONE_YET = -1000;
36 37
  private static final int SHAPE_DD = 0;
37 38
  private static final int SHAPE_DU = 1;
38 39
  private static final int SHAPE_UD = 2;
......
49 50
  private int numVertices;
50 51
  private int extraIndex, extraVertices;
51 52

  
52
  //private float[] mBandQuot;
53
  private float[] mBandQuot;
53 54

  
54 55
///////////////////////////////////////////////////////////////////////////////////////////////////
55 56
// polygonVertices>=3 , polygonBands>=2
......
68 69

  
69 70
     remainingVert = numVertices;
70 71
     }
71
/*
72

  
72 73
///////////////////////////////////////////////////////////////////////////////////////////////////
73 74

  
74 75
  private void computeCache()
......
85 86
      mBandQuot[band] = (mPolygonBands[2*prev+1]-mPolygonBands[2*next+1]) / (mPolygonBands[2*next]-mPolygonBands[2*prev]);
86 87
      }
87 88
    }
88
*/
89

  
89 90
///////////////////////////////////////////////////////////////////////////////////////////////////
90 91

  
91 92
  private float getSpecialQuot(int index)
......
199 200
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
200 201
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z;
201 202

  
203
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
204

  
205
    if( quot==0.0f || quot==1.0f )
206
      {
207
      float vx = mBandQuot[polyBand]*xEdge;
208
      float vy = mBandQuot[polyBand]*yEdge;
209
      float vz = xEdge*xEdge + yEdge*yEdge;
210
      float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
211

  
212
      attribs1[index  ] = vx/len;
213
      attribs1[index+1] = vy/len;
214
      attribs1[index+2] = vz/len;
215
      }
216
    else
217
      {
218
      attribs1[index  ] = NOT_DONE_YET;
219
      }
220

  
202 221
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
203 222
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
204 223

  
......
209 228

  
210 229
  private void addVertexNormal(int vertex, int end1, int end2, float[] attribs1)
211 230
    {
212
    android.util.Log.e("D", "vertex="+vertex+" end1="+end1+" end2="+end2);
231
    // android.util.Log.e("D", "vertex="+vertex+" end1="+end1+" end2="+end2);
213 232

  
214
    int iv = VERT1_ATTRIBS*vertex + POS_ATTRIB;
215
    int i1 = VERT1_ATTRIBS*end1   + POS_ATTRIB;
216
    int i2 = VERT1_ATTRIBS*end2   + POS_ATTRIB;
233
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
217 234

  
218
    float vx = attribs1[iv  ];
219
    float vy = attribs1[iv+1];
220
    float vz = attribs1[iv+2];
235
    if( attribs1[index] == NOT_DONE_YET)
236
      {
237
      int iv = VERT1_ATTRIBS*vertex + POS_ATTRIB;
238
      int i1 = VERT1_ATTRIBS*end1   + POS_ATTRIB;
239
      int i2 = VERT1_ATTRIBS*end2   + POS_ATTRIB;
221 240

  
222
    float x1 = attribs1[i1  ];
223
    float y1 = attribs1[i1+1];
224
    float z1 = attribs1[i1+2];
241
      float vx = attribs1[iv  ];
242
      float vy = attribs1[iv+1];
243
      float vz = attribs1[iv+2];
225 244

  
226
    float x2 = attribs1[i2  ];
227
    float y2 = attribs1[i2+1];
228
    float z2 = attribs1[i2+2];
245
      float x1 = attribs1[i1  ];
246
      float y1 = attribs1[i1+1];
247
      float z1 = attribs1[i1+2];
229 248

  
230
    float dx1 = vx-x1;
231
    float dy1 = vy-y1;
232
    float dz1 = vz-z1;
249
      float x2 = attribs1[i2  ];
250
      float y2 = attribs1[i2+1];
251
      float z2 = attribs1[i2+2];
233 252

  
234
    float dx2 = vx-x2;
235
    float dy2 = vy-y2;
236
    float dz2 = vz-z2;
253
      float dx1 = vx-x1;
254
      float dy1 = vy-y1;
255
      float dz1 = vz-z1;
237 256

  
238
    float cx = dy1*dz2 - dz1*dy2;
239
    float cy = dz1*dx2 - dx1*dz2;
240
    float cz = dx1*dy2 - dy1*dx2;
257
      float dx2 = vx-x2;
258
      float dy2 = vy-y2;
259
      float dz2 = vz-z2;
241 260

  
242
    float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz);
261
      float cx = dy1*dz2 - dz1*dy2;
262
      float cy = dz1*dx2 - dx1*dz2;
263
      float cz = dx1*dy2 - dy1*dx2;
243 264

  
244
    int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB;
265
      float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz);
245 266

  
246
    attribs1[index  ] = cx/len;
247
    attribs1[index+1] = cy/len;
248
    attribs1[index+2] = cz/len;
267
      attribs1[index  ] = cx/len;
268
      attribs1[index+1] = cy/len;
269
      attribs1[index+2] = cz/len;
270
      }
249 271
    }
250 272

  
251 273
///////////////////////////////////////////////////////////////////////////////////////////////////
......
395 417
      }
396 418

  
397 419
    computeNumberOfVertices();
398
    //computeCache();
420
    computeCache();
399 421

  
400 422
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
401 423
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];

Also available in: Unified diff