Loading 3ds model

Okay I'm trying to load a 3ds model through the HelloModelLoading.java class.

Heres my code:


/*
 * Copyright (c) 2003-2006 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingSphere;
import com.jme.scene.Node;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;

/**
 * Started Date: Jul 22, 2004<br><br>
 *
 * Demonstrates loading formats.
 *
 * @author Jack Lindamood
 */
public class HelloModelLoading extends SimpleGame {
    private static final Logger logger = Logger
            .getLogger(HelloModelLoading.class.getName());
   
    public static void main(String[] args) {
        HelloModelLoading app = new HelloModelLoading();
        app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG);
        // Turn the logger off so we can see the XML later on
        app.start();
    }

    protected void simpleInitGame() {
        // Point to a URL of my model
        URL model=HelloModelLoading.class.getClassLoader().getResource("jmetest/data/model/slugmonster.obj");

        // Create something to convert .obj format to .jme
        FormatConverter converter=new ObjToJme();
        // Point the converter to where it will find the .mtl file from
        converter.setProperty("mtllib",model);

        // This byte array will hold my .jme file
        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        try {
            // Use the format converter to convert .obj to .jme
            converter.convert(model.openStream(), BO);
            Node maggie=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            // shrink this baby down some
            maggie.setLocalScale(.1f);
            maggie.setModelBound(new BoundingSphere());
            maggie.updateModelBound();
            // Put her on the scene graph
            rootNode.attachChild(maggie);
        } catch (IOException e) {   // Just in case anything happens
            logger.logp(Level.SEVERE, this.getClass().toString(),
                    "simpleInitGame()", "Exception", e);
            System.exit(0);
        }
    }
}


Errors:

init:
deps-jar:
compile-single:
run-single:
Mar 1, 2008 3:53:26 PM com.jme.app.BaseGame start
INFO: Application started.
Mar 1, 2008 3:53:26 PM com.jme.system.PropertiesIO <init>
INFO: PropertiesIO created
Mar 1, 2008 3:53:26 PM com.jme.system.PropertiesIO load
INFO: Read properties
Mar 1, 2008 3:53:29 PM com.jme.input.joystick.DummyJoystickInput <init>
INFO: Joystick support is disabled
Mar 1, 2008 3:53:29 PM com.jme.system.lwjgl.LWJGLDisplaySystem <init>
INFO: LWJGL Display System created.
Mar 1, 2008 3:53:29 PM com.jme.system.PropertiesIO save
INFO: Saved properties
Mar 1, 2008 3:53:29 PM com.jme.app.BaseSimpleGame initSystem
INFO: jME version 1.0
Mar 1, 2008 3:53:29 PM com.jme.renderer.lwjgl.LWJGLRenderer <init>
INFO: LWJGLRenderer created. W:  640H: 480
Mar 1, 2008 3:53:29 PM com.jme.app.BaseSimpleGame initSystem
INFO: Running on: ati2dvag
Driver version: 6.14.10.6467
ATI Technologies Inc. - RADEON X300 x86/SSE2 - 1.5.4517 WinXP Release
Mar 1, 2008 3:53:29 PM com.jme.renderer.AbstractCamera <init>
INFO: Camera created.
Mar 1, 2008 3:53:29 PM com.jme.util.lwjgl.LWJGLTimer <init>
INFO: Timer resolution: 1000 ticks per second
Mar 1, 2008 3:53:29 PM com.jme.scene.Node <init>
INFO: Node created.
Mar 1, 2008 3:53:30 PM com.jme.scene.Node <init>
INFO: Node created.
Mar 1, 2008 3:53:30 PM com.jme.scene.Node attachChild
INFO: Child (FPS label) attached to this node (FPS node)
Mar 1, 2008 3:53:30 PM com.jme.scene.Node <init>
INFO: Node created.
Mar 1, 2008 3:53:30 PM com.jme.util.geom.GeometryTool minimizeVerts
INFO: batch: temp0: Batch 0 old: 2609 new: 2609
Mar 1, 2008 3:53:30 PM com.jme.scene.Node attachChild
INFO: Child (temp0) attached to this node (obj file)
Mar 1, 2008 3:53:30 PM class HelloModelLoading start()
SEVERE: Exception in game loop
java.lang.ClassCastException: com.jme.scene.TriMesh cannot be cast to com.jme.scene.Node
        at HelloModelLoading.simpleInitGame(HelloModelLoading.java:82)
        at com.jme.app.BaseSimpleGame.initGame(Unknown Source)
        at com.jme.app.BaseGame.start(Unknown Source)
        at HelloModelLoading.main(HelloModelLoading.java:65)
Mar 1, 2008 3:53:30 PM com.jme.app.BaseSimpleGame cleanup
INFO: Cleaning up resources.
Mar 1, 2008 3:53:30 PM com.jme.app.BaseGame start
INFO: Application ending.
BUILD SUCCESSFUL (total time: 4 seconds)



So I do not know what is wrong. How would I load it? Oh, and also, I have one of the converters so I can also have it in .jme files. How would I load it like that too?

Thanks, Kluskey

Replace Node maggie, with TriMesh maggie.

A loaded model can either be a TriMesh or Node, this is dependant on how the model was created in the first place.

Why not just use Spatial?

I made the same observation… and found the same solution.

But as far as I understand a TriMesh is a Geometry and is therefore a visible element of the object tree (like Box, …). A Node on the other hand is not visible in itself but holds information about the subtree and is for organizational purposes.

Now, why can an Importer import a Node???

And if so, how do I differentiate in my program? How can I detect which import results in a Node and which in  a Geometry/TriMesh?



I ran into that problem when I tried to write a generic method to import objects (obj,…). I tried to import three different 3ds-Files, two where TriMeshes and one was a Node. I genereated that one with Autodesk 3ds Max and exported it to obj (a generic tree from 3ds Max). I do not understand what I did in 3ds to make it a Node.

The suggested solution (make it a TriMesh) does not help for an generic import method.



With regards

Joa

Everything is a sub-class of Spatial.  Node extends it directly, TriMesh extends Geometry and Geometry extends Spatial.  Therefore import your model as a Spatial, so you are sure you can get it no matter what it is. Then, if you need a TriMesh or a Node specifically you can just use:


final Node nodeModel;
final TriMesh trimeshModel;
try {
  trimeshModel = (TriMesh) spatialModel;
} catch ( Exception ex ) {
  // must have more than one mesh
  nodeModel = (Node) spatialModel;
}



Almost all models more complex than a ball have multiple meshes.  Furthermore, Spatials can be passed around just as easily as Nodes.

Hope that helps :)

Hello

You can get a trimesh associated a node?

Or failing to obtain the vertices.

Thanks