private void accumulateNormals ( final RandomElementReader vertices , final Element face ) throws IOException { int [ ] indices = face . getIntList ( "vertex_index" ) ; for ( int i = 0 ; i < indices . length ; i ++ ) { int pre ; int post ; if ( counterClockwise ) { pre = ( i + indices . length - 1 ) % indices . length ; post = ( i + 1 ) % indices . length ; } else { pre = ( i + 1 ) % indices . length ; post = ( i + indices . length - 1 ) % indices . length ; } Element centerVertex ; Element preVertex ; Element postVertex ; try { centerVertex = vertices . readElement ( indices [ i ] ) ; preVertex = vertices . readElement ( indices [ pre ] ) ; postVertex = vertices . readElement ( indices [ post ] ) ; accumulateNormal ( centerVertex , preVertex , postVertex ) ; } catch ( IndexOutOfBoundsException e ) { // we ignore defects in the normals. } } }
Calculate the face normal weight by angle and add them to the vertices .