* This method is threadsafe and may be called from any thread.
* @param content an object containing a model
*/
public BranchGroup loadModel(Content content) throws IOException {
// Ensure we use a URLContent object
URLContent urlContent;
if (content instanceof URLContent) {
urlContent = (URLContent)content;
} else {
urlContent = TemporaryURLContent.copyToTemporaryURLContent(content);
}
Loader3DS loader3DSWithNoStackTraces = new Loader3DS() {
@Override
public Scene load(URL url) throws FileNotFoundException, IncorrectFormatException {
try {
// Check magic number 0x4D4D
InputStream in = url.openStream();
if (in.read() != 0x4D
&& in.read() != 0x4D) {
throw new IncorrectFormatException("Bad magic number");
}
in.close();
} catch (FileNotFoundException ex) {
throw ex;
} catch (IOException ex) {
throw new ParsingErrorException("Can't read url " + url);
}
PrintStream defaultSystemErrorStream = System.err;
try {
// Ignore stack traces on System.err during 3DS file loading
System.setErr(new PrintStream (new OutputStream() {
@Override
public void write(int b) throws IOException {
// Do nothing
}
}));
// Default load
return super.load(url);
} finally {
// Reset default err print stream
System.setErr(defaultSystemErrorStream);
}
}
};
Loader [] defaultLoaders = new Loader [] {new OBJLoader(),
new DAELoader(),
loader3DSWithNoStackTraces,
new Lw3dLoader()};
Loader [] loaders = new Loader [defaultLoaders.length + this.additionalLoaderClasses.length];
System.arraycopy(defaultLoaders, 0, loaders, 0, defaultLoaders.length);
for (int i = 0; i < this.additionalLoaderClasses.length; i++) {
try {
loaders [defaultLoaders.length + i] = this.additionalLoaderClasses [i].newInstance();
} catch (InstantiationException ex) {
// Can't happen: getLoaderClass checked this class is instantiable
throw new InternalError(ex.getMessage());
} catch (IllegalAccessException ex) {
// Can't happen: getLoaderClass checked this class is instantiable
throw new InternalError(ex.getMessage());
}
}
Exception lastException = null;
for (Loader loader : loaders) {
try {
// Ask loader to ignore lights, fogs...
loader.setFlags(loader.getFlags()
& ~(Loader.LOAD_LIGHT_NODES | Loader.LOAD_FOG_NODES
| Loader.LOAD_BACKGROUND_NODES | Loader.LOAD_VIEW_GROUPS));
// Return the first scene that can be loaded from model URL content
Scene scene = loader.load(urlContent.getURL());
BranchGroup modelNode = scene.getSceneGroup();
// If model doesn't have any child, consider the file as wrong
if (modelNode.numChildren() == 0) {
throw new IllegalArgumentException("Empty model");