Flip Polygon Winding Function / Reverse indexArray

Hi,



I had trouble finding a function for flipping the winding of a loaded model, so I thought I'd share what I came up with. In this case, I'm loading .obj models from Blender, which by default are the wrong way around for jMonkeyEngine.



It may be that this is built into jMonkeyEngine somewhere, I but I couldn't find it. Perhaps this functionality could be added to the object loading class?




   public static void reverseWinding(TriMesh object){
   IntBuffer indexBuffer = object.getIndexBuffer();
   int bufferCapacity = indexBuffer.capacity();
   IntBuffer reversedIndexBuffer = BufferUtils.createIntBuffer(bufferCapacity);
   for (int j = 0; j < bufferCapacity ; j++){
       reversedIndexBuffer.put( j, indexBuffer.get( bufferCapacity-1-j ) );
   }
   object.setIndexBuffer(reversedIndexBuffer);
    }



I also tried just flipping the normals, but this has no effect as winding is what determines the outside of the face.

 
  public static void flipNormals(Geometry object){
   FloatBuffer normals = object.getNormalBuffer();
   int i;
   for(i=0; i<normals.capacity(); i++){
       normals.put(i, -normals.get(i) );
   }
   object.setNormalBuffer(normals);
    }

I think with the Cullstate you can define the polygon winding

You should definitely read through this tutorial: http://www.jmonkeyengine.com/wiki/doku.php/blenderjme_basics_tutorial?s[]=blend&s[]=jme as it'll give you animation features etc. as well (:


  • Mikkel