Changing Orientation of basic Meshes

I’m not changing uvcoords, it was a variable that I haven’t change to reflect it correctly. Like I said it is a work in progress. I didn’t rename the variable, yet… It was still a test class to show concept.

public class MeshUtils {

   public enum ORIENTATION {
      LOWER_LEFT,
      LOWER_CENTER,
      CENTER
   }
   
   private static final int NUM_AXES = 3;
   
   
   public static Mesh translate(Mesh mesh, ORIENTATION orientation) {
      
      if (mesh.getBound() instanceof BoundingBox) {
         if (orientation == ORIENTATION.CENTER) {
            return centerMesh(mesh);
         } else {
            return alignBottonMesh(mesh, orientation);
         }
      } else {
         
      }
      
      return null;
   }

   private static Mesh alignBottonMesh(Mesh mesh, ORIENTATION orientation) {

      Vector3f leftPoint = null;
      BoundingVolume volume = mesh.getBound();
      leftPoint = ((BoundingBox)volume).getMin(leftPoint);
      
      Vector3f center = volume.getCenter();
      int numVertices = mesh.getVertexCount();
      VertexBuffer pb = mesh.getBuffer(Type.Position);
      if (pb != null ) {
         float[] positions = BufferUtils.getFloatArray((FloatBuffer) pb.getData());

         
         for(int x=0; x < numVertices;x++ ) {
            if (orientation == ORIENTATION.LOWER_CENTER) {
               positions[x * NUM_AXES] = positions[x * NUM_AXES + 0] - center.x;
            } else {
               positions[x * NUM_AXES] = positions[x * NUM_AXES + 0] - leftPoint.x;
            }
            positions[x * NUM_AXES + 1] = positions[x * NUM_AXES + 1] + leftPoint.y;
            positions[x * NUM_AXES + 2] = positions[x * NUM_AXES + 2] - center.z;
         }
         
         //apply new texture coordinates
         updateMesh(mesh, positions);

      }

      return mesh;
   }

   
   private static Mesh centerMesh(Mesh mesh) {

      BoundingVolume volume = mesh.getBound();
      Vector3f center = volume.getCenter();
      int numVertices = mesh.getVertexCount();
      VertexBuffer pb = mesh.getBuffer(Type.Position);
      if (pb != null ) {
         float[] positions = BufferUtils.getFloatArray((FloatBuffer) pb.getData());
         
         for(int x=0; x < numVertices;x++ ) {
            
            if (center.x != 0 ) {
               positions[x * NUM_AXES] = positions[x * NUM_AXES] - center.x;
            }
            if (center.y != 0) {
               positions[x * NUM_AXES + 1] = positions[x * NUM_AXES + 1] - center.y;
            }
            if (center.z != 0) {
               positions[x * NUM_AXES + 2] = positions[x * NUM_AXES + 2] - center.z;
            }
         }

         //apply new texture coordinates
         updateMesh(mesh, positions);
      }
      return mesh;
   }

   private static void updateMesh(Mesh mesh, float[] positions) {
      VertexBuffer positionBuffer = new VertexBuffer(Type.Position);
      positionBuffer.setupData(Usage.Static, 3, com.jme3.scene.VertexBuffer.Format.Float,
              BufferUtils.createFloatBuffer(positions));
      mesh.clearBuffer(Type.Position);
      mesh.setBuffer(positionBuffer);
      mesh.updateBound();
      
   }

}

It’d be nice if it handled animated meshes as well.

Also, this doesn’t change the orientation, it only translates the mesh.