Skip to main content

3dsloader problem

4 replies [Last post]
cooljprince
Offline
Joined: 2007-09-07

Hi everyone.
I'm trying to import a .3ds model using starfire 3ds loader but nothing displayed just a black screen :(
can anyone help me,
here is my code

import java.awt.*;
import java.applet.Applet;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.Scene;
import java.net.URL;
import java.net.MalformedURLException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import java.io.FileNotFoundException;
import com.mnstarfire.loaders3d.Loader3DS;

public class Test3d extends Applet
{
public Test3d()
{
SimpleUniverse u = new SimpleUniverse();
BranchGroup bg = add3DObject();
u.addBranchGraph(bg);
}

private BranchGroup add3DObject()
{
BranchGroup branch = new BranchGroup();
TransformGroup transform = loadAndScaleStarfie("models\\fence1.3DS");
Transform3D transform3d = new Transform3D();
transform3d.rotX(Math.PI/2);
transform3d.setTranslation( new Vector3d(-1.0,0.0,-8.0));
transform.setTransform(transform3d);
branch.addChild(transform);
branch.compile();
return branch;
}

public TransformGroup loadAndScaleStarfire(String filePath)
{
TransformGroup transform = null;
Loader3DS loader = new Loader3DS();
Scene scene = null;
try
{
URL loadUrl = new URL(filePath);
try
{
//load the scene
scene = loader.load(loadUrl);
}
catch (Exception e)
{
System.out.println("Exception loading URL:" + e);
}
}

catch (MalformedURLException badUrl)
{
//filePath may be a path name
try
{
//load the scene
scene = loader.load(filePath);
}
catch (Exception e)
{
System.out.println("Exception loading file from path:" + e);
}
}
if(scene == null)
{
BranchGroup model = scene.getSceneGroup();
transform = new TransformGroup();
transform.addChild(model);
}
return transform;
}

public static void main(String[] args)
{
Test3d test = new Test3d();
}
}

Thanks for any advice :)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cooljprince
Offline
Joined: 2007-09-07

import java.awt.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import java.applet.Applet;
import com.sun.j3d.utils.applet.MainFrame;

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.Scene;

import java.net.URL;
import java.net.MalformedURLException;

import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import java.io.FileNotFoundException;

import com.mnstarfire.loaders3d.Loader3DS;
//import com.microcrowd.loader.java3d.max3ds.Loader3DS;
import com.mnstarfire.loaders3d.Inspector3DS;

public class load3d extends Applet
{

private Canvas3D c;

public load3d()
{
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse();
BranchGroup scene = createSceneBranchGroup();
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}

protected BranchGroup createSceneBranchGroup()
{
BranchGroup objRoot = new BranchGroup();

TransformGroup objTrans1 = new TransformGroup();
Transform3D tr = new Transform3D();
objTrans1.getTransform( tr );
// tr.rotX(90.0 * Math.PI / 180.0);
// tr.setTranslation(new Vector3d(0.5,0.0,-0.2));
tr.setScale( 0.7 );
objTrans1.setTransform( tr );
TransformGroup objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

BoundingSphere bounds = new BoundingSphere(
new Point3d(0.0,0.0,0.0), 100.0);

//Set up the global lights
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Vector3f lDir1 = new Vector3f(1.0f, 1.0f, 1.0f);
Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
objRoot.addChild(aLgt);
objRoot.addChild(lgt1);

Loader3DS loader = new Loader3DS();
loader.setFlags(Loader3DS.LOAD_ALL);

Scene scene = null;

try{
scene = loader.load("3Dimka_Pink_Panther_ready.3DS");
}
catch (Exception e)
{
scene = null;
System.err.println(e);
}
if( scene == null )
System.exit(1);

//connect the scenegraph
objTrans2.addChild( scene.getSceneGroup() );

objTrans1.addChild( objTrans2 );
objRoot.addChild( objTrans1 );

return objRoot;
}

public static void main(String[] args)
{
load3d t = new load3d();
}

How do I do to get the model on screen, is it something wrong with the model if so please show me where I can get a model that works(just for the test).

cooljprince
Offline
Joined: 2007-09-07

tmilard, thank you :)
but still have the same problem :(
I thought that the problem was that I didn't set up light and appearance
now I did but I can't see my 3ds object :(
here is my code

import java.awt.*;
import java.applet.Applet;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.Scene;
import java.net.URL;
import java.net.MalformedURLException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import java.io.FileNotFoundException;
import com.mnstarfire.loaders3d.Loader3DS;
import com.sun.j3d.utils.applet.MainFrame;

public class Test3d extends Applet
{

private Canvas3D c;

public Test3d()
{
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse();
BranchGroup scene = createSceneBranchGroup();
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}

protected BranchGroup createSceneBranchGroup()
{
BranchGroup objRoot = new BranchGroup();
//create a TransformGroup to flip the hand onto its end
//and enlarge it.
TransformGroup objTrans1 = new TransformGroup();
Transform3D tr = new Transform3D();
objTrans1.getTransform( tr );
tr.rotX(90.0 * Math.PI / 180.0);
tr.setScale( 10.0 );
objTrans1.setTransform( tr );
//create a TransformGroup to rotate the hand
TransformGroup objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BoundingSphere bounds = new BoundingSphere(
new Point3d(0.0,0.0,0.0), 100.0);
//create a RotationInterpolator behavior to rotate the hand
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator = new RotationInterpolator(
rotationAlpha, objTrans2, yAxis, 0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds); objTrans2.addChild(rotator);
//Set up the global lights
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Vector3f lDir1 = new Vector3f(1.0f, 1.0f, 1.0f);
Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
objRoot.addChild(aLgt);
objRoot.addChild(lgt1);
//load the object file
Scene scene = null;
Shape3D shape = null;
//read in the geometry information from the data file
Loader3DS loader = new Loader3DS();
try
{
scene = loader.load( new URL("D:\\Test\\models\\fence1.3DS"));
}
catch (Exception e)
{
scene = null;
System.err.println(e);
}
if( scene == null )
System.exit(1);

//retrieve the Shape3D object from the scene
BranchGroup branchGroup = scene.getSceneGroup();
shape = (Shape3D) branchGroup.getChild(0);
//create an Appearance and Material
Appearance app = new Appearance();
Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));
//assign the appearance to the Shape
shape.setAppearance( app );
//connect the scenegraph
objTrans2.addChild( scene.getSceneGroup() );
objTrans1.addChild( objTrans2 );
objRoot.addChild( objTrans1 );
return objRoot;
}

public static void main(String[] args)
{
new MainFrame( new Test3d(), 400, 400 );
}
}

tmilard
Offline
Joined: 2004-03-25

Sorry I do not have too much time now...
I tested your code rapidly. I see nothing exept white screen.
I twinkled a bit but with no success....

Please do modify your code with :
- first display a NON 3DS external object. Like in exemple "GrandeOurse3DJava3D
".
- Then if you see the 3D object then change or add your 3DS import code to the scene.

At least this way you are sure:
- The camera is really pointing at your objet (do not forget a camera sees only a very small portion of the 3D world)
- All the java3D "stuffs" (lights, camera front culling, back culling....) are OK

Voila that's all I can do now.

I know it seems easy to say when you sweet for hours in front of your screen. but ... well I am not yet a 3D expert : I also spend days face to face with such problems.
Thierry

tmilard
Offline
Joined: 2004-03-25

Hello cooliprince.
It is always difficult to start from scratch in 3D.
Because your applet displays nothing ... it does not mean it come always from 3D API.

First I advise to start form this simple Applet that does display balls and line.
Take this one for exeple : GrandeOurse3DJava3D
Here is the URL with source :http://raphaello.univ-fcomte.fr/ig/Java3D/Exemples/GrandeOurse3DJava3D/GrandeOurse3DJava3D.htm

After it works on your machine (and only then) do plug-in add your code that import a 3Ds object.

Please let me know if You see your 3Ds object now.

Thierry