um… ähm I’ve noticed a big error in my code. In odejava it is not possible to add different Geoms to one GeomTransform (ofcourse not, but all these guys comparing GeomTransform with TransformGroups irritated me
) So now I can’t simply return a PlaceableGeom, but a Vector of such Geoms, because such a BranchGroup can contain different TriangleArrays - so also different GeomTriMesh.
Here is the new code (I’ve changed it also to use org.odejava.xith3d.Xith3DToOdejava - I didn’t before [yes I know now why], because I was to lazy to download the whole “[odejava] / odejava-xith3d / src” from cvs):
import org.odejava.*;
import org.odejava.xith3d.Xith3DToOdejava;
import com.xith3d.scenegraph.*;
import org.xith3d.loader3ds.Loader3DS;
import org.xith3d.loaders.obj.OBJLoader;
import com.xith3d.loaders.ase.AseFile;
import javax.vecmath.*;
import java.util.*;
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);
}
private void addToGeom(Vector<PlaceableGeom> gVec, 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(gVec,(Group) n,newTrans);
}
else if(n instanceof Shape3D) {
Shape3D s = (Shape3D) n;
Geometry g = s.getGeometry();
GeomTriMesh gtm = null;
if(g instanceof TriangleArray) {
gtm = Xith3DToOdejava.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);
gVec.add(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 Vector<PlaceableGeom> convert(Group g) {
XithToOde xto = new XithToOde();
Vector<PlaceableGeom> gVec = new Vector<PlaceableGeom>();
Matrix4f m = new Matrix4f();
m.setIdentity();
xto.addToGeom(gVec,g,m);
return(gVec);
}
/**
* 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 Vector<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;
}
}
}
Have fun with it !!
