Ok here it is:
import org.odejava.*;
import com.xith3d.scenegraph.*;
import org.xith3d.loader3ds.Loader3DS;
import org.xith3d.loaders.obj.OBJLoader;
import com.xith3d.loaders.ase.AseFile;
import javax.vecmath.*;
public class XithToOde {
private static int num = 0;
private Vector3f tempPos;
private Quat4f tempRot;
private XithToOde() {
tempPos = new Vector3f();
tempRot = new Quat4f();
}
private static String newName() {
num++;
return("GeneratedName"+num);
}
//this is from the old xith to ode converter that was located in org.odejava.xith3d.* or something like that.
public GeomTriMesh createTriMesh(TriangleArray ta, String name) {
float[] vertices = new float[ta.getVertexCount() * 3];
int[] index = new int[ta.getVertexCount()];
Point3f pos = new Point3f();
for (int i = 0; i < ta.getVertexCount(); i++) {
ta.getVertex(i, pos);
vertices[i * 3 + 0] = pos.x;
vertices[i * 3 + 1] = pos.y;
vertices[i * 3 + 2] = pos.z;
index[i] = i;
}
return new GeomTriMesh(name, vertices, index);
}
private void addToGeom(GeomTransform geom, Group group, Matrix4f transform) {
Matrix4f newTrans;
if(group instanceof TransformGroup) {
TransformGroup tgroup = (TransformGroup) group;
newTrans = new Matrix4f(transform);
newTrans.mul(tgroup.getTransform().getMatrix4f());
}
else newTrans = transform;
for(int i = 0; i < group.numChildren(); i++) {
Node n = (Node) group.getChild(i);
if(n instanceof Group) {
addToGeom(geom,(Group) n,newTrans);
}
else if(n instanceof Shape3D) {
Shape3D s = (Shape3D) n;
Geometry g = s.getGeometry();
GeomTriMesh gtm = null;
if(g instanceof TriangleArray) {
gtm = createTriMesh((TriangleArray) g, newName());
}
else {
System.out.println("not able to parse "+g.getClass());
}
if(gtm != null) {
newTrans.get(tempPos);
newTrans.get(tempRot);
gtm.setPosition(tempPos);
gtm.setQuaternion(tempRot);
geom.setEncapsulatedGeom(gtm);
}
}
}
}
/**
* creates a Geom that represents the Xith-Group.
* This function does NOT create Joints!!
*
Note: Only com.xith3d.scenegraph.TriangleArray is supported as Geometry.
* @param g the Group to convert
* @return a PlaceableGeom that represents the Group
*/
public static PlaceableGeom convert(Group g) {
XithToOde xto = new XithToOde();
GeomTransform gt = new GeomTransform("main_"+newName());
Matrix4f m = new Matrix4f();
m.setIdentity();
xto.addToGeom(gt,g,m);
return(gt);
}
/**
* This function uses convert(Group) to directly load Models to Odejava.
*
Supported file-formats:
*
ASE
*
OBJ
*
3DS
* @param filename the file to be loaded
* @return a PlaceableGeom that represents the Model
*/
public static PlaceableGeom load(String filename) {
try {
if(filename.endsWith(".ase") || filename.endsWith(".ASE")) {
//load ASE
return(convert(AseFile.getModel(filename)));
}
else if(filename.endsWith(".obj") || filename.endsWith(".OBJ")) {
//load OBJ
return(convert(new OBJLoader().load(filename)));
}
else if(filename.endsWith(".3ds") || filename.endsWith(".3DS")) {
//load 3DS
return(convert(new Loader3DS().load(filename).getSceneGroup()));
}
else return null;
} catch(java.io.IOException ex) {
System.err.println("not able to load Model!! - loading aborted");
return null;
}
}
}