How can I open .obj files?
809845Nov 1 2010 — edited Nov 3 2010Hi!
I have a little problem,I write a code which can open and draw .obj file.But if the .obj file does't include enter in the end of the file,I get errors(com.sun.j3d.loaders.ParsingErrorException: Expected number on line 24) or the program can't do anything,but when I put the enter in the end of the file the program works correctly.
Can somebody help me how it would be necessary to modify the program to work correctly?
Thank a lot:)
Timmy
The soure code is:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class egy extends Applet {
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup szene = macheSzene();
szene.compile();
universe = new SimpleUniverse(canvas3D);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(szene);
}
public BranchGroup macheSzene() {
BranchGroup objWurzel = new BranchGroup();
Transform3D drehung = new Transform3D();
Transform3D drehung2 = new Transform3D();
drehung.rotX(Math.PI / 4.0d);
drehung2.rotY(Math.PI / 5.0d);
drehung.mul(drehung2);
TransformGroup objDreh = new TransformGroup(drehung);
ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
Scene scene = null;
try {
scene = file.load(ClassLoader.getSystemResource("cube.obj"));
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
objDreh.addChild(scene.getSceneGroup());
DirectionalLight d_Licht = new DirectionalLight(new Color3f(1.0f, 1.0f,
1.0f), new Vector3f(-1.0f, -1.0f, -1.0f));
d_Licht.setInfluencingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d,
0.0d), 100.0d));
objDreh.addChild(d_Licht);
objWurzel.addChild(objDreh);
return objWurzel;
}
/**
* gibt speicher frei
*/
public void destroy() {
universe.removeAllLocales();
}
public static void main(String[] args) {
frame = new MainFrame(new egy(), 500, 500);
frame.setTitle("Kép");
}
private SimpleUniverse universe;
private Canvas3D canvas3D;
private static Frame frame;
}
The cube.obj includes:
# cube.obj created by IVREAD.
# Original data in cube.iv
g Object001
v -0.500000 -0.500000 1.00000
v 0.500000 -0.500000 1.00000
v 0.500000 0.500000 1.00000
v -0.500000 0.500000 1.00000
v 0.500000 -0.500000 0.000000E+00
v -0.500000 -0.500000 0.000000E+00
v -0.500000 0.500000 0.000000E+00
v 0.500000 0.500000 0.000000E+00
f 1 2 3
f 1 3 4
f 5 6 7
f 5 7 8
f 6 1 4
f 6 4 7
f 2 5 8
f 2 8 3
f 6 5 2
f 6 2 1
f 4 3 8
f 4 8 7