ByteArrayOutputStream BO=new ByteArrayOutputStream();
//logger.info("Starting to convert .obj to .jme");
converter.convert(objFile.openStream(),BO);
//logger.info("Done converting, now watch how fast it loads!");
long time=System.currentTimeMillis();
Spatial r = (Spatial)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
//logger.info("Finished loading time is "+(System.currentTimeMillis()-time));
r.setLocalScale(150f);
//r.setLocalScale(new Vector3f(0, 150, 400));
//rotate about the Y-Axis by approximately 1 pi
Vector3f axis = Vector3f.UNIT_Y; // this equals (0, 1, 0) and does not require to create a new object
float angle = 1.6f;
r.getLocalRotation().fromAngleAxis(angle, axis);
Vector3f axisZ = Vector3f.UNIT_Z; // this equals (0, 1, 0) and does not require to create a new object
float angleZ = 0.1f;
// r.getLocalRotation().fromAngleAxis(angleZ, axisZ);
objects.attachChild(r);
Apparently there is an mtl file which has to location to the image.
But when i start the programm i see the default image of jme and i get the following messagge:
The MTL file is the materials file that contains information about how the texture is mapped to the object. It indicates the file name for the texture as well.
I've tried both tga and png formats for the texture.
The obj file itself contains the vertex data, any normal data, and the faces and some other info as well as the mtl file name. This is all in plain text.
The textures are a different kind of resource, you will have to create a ResourceLocator yourself that finds the texture by the string given and returns the URL, then register it as a texture locator. Write this from my phone so I have no example right now, but I posted one not so long ago on this forum…
I've tried putting them just about everywhere. For instance.
public class ResourceLoader {
public static Spatial loadShip() {
URL modelDir = null;
URL objFile = null;
try {
modelDir = new File(".").toURI().toURL();
objFile = new File("./wing2.obj").toURI().toURL();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
FormatConverter converter = new ObjToJme();
converter.setProperty("mtllib", modelDir);
converter.setProperty("texdir", modelDir);
ByteArrayOutputStream output = new ByteArrayOutputStream();
Spatial s = null;
try {
converter.convert(objFile.openStream(), output);
byte[] ba = output.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
s = (Spatial) BinaryImporter.getInstance().load(bais);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return s;
}
The object loads fine, I can see it on screen. I know it reads the mtl file because it gives me a warning with the URL of the location of the texture.
Mar 22, 2010 2:40:49 AM com.jme.util.resource.ResourceLocatorTool locateResource
WARNING: Unable to locate: /C:/Users/angellus00/Documents/NetBeansProjects/Tyrian3d/wing1_color2.tga
This file is there. I've even tried sending it texurl with
public class ResourceLoader {
public static Spatial loadShip() {
URL modelDir = null;
URL objFile = null;
URL texURL = null;
try {
modelDir = new File(".").toURI().toURL();
objFile = new File("./wing2.obj").toURI().toURL();
texURL = new File("./wing1_color2.tga").toURI().toURL();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
FormatConverter converter = new ObjToJme();
converter.setProperty("mtllib", modelDir);
converter.setProperty("texdir", modelDir);
converter.setProperty("texurl", texURL);
ByteArrayOutputStream output = new ByteArrayOutputStream();
Spatial s = null;
try {
converter.convert(objFile.openStream(), output);
byte[] ba = output.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
s = (Spatial) BinaryImporter.getInstance().load(bais);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return s;
}
}