Hi all,
I've been trying for ages now to texture a jpeg image onto an object
file (.obj) that I have loaded in to a 3D scene, but I still haven't
managed to do it. At the end of this text is the code I have for loading in
the object file (done using a frame to select the file you want).
I have tried various methods, that seem to make sense, but they didn't work.
Could somebody add in the extra bit needed that will actually texture a
jpeg image onto this object? I only have 1 weeks work left, so I really
need to get this done!
Thanks a million for any help you may give.
Here's the code that loads the object file:
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.io.*;
import com.sun.j3d.utils.behaviors.mouse.*;
public class ObjLoad2 extends Applet
{
private double creaseAngle = 60.0;
private String filename = null;
public BranchGroup createSceneGraph()
{
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.7);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
//Opens frame to select file to load
Frame frame = new Frame();
FileDialog fileDialog = new FileDialog(frame);
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();
String file = fileDialog.getFile();
if (file == null){}
String directory = fileDialog.getDirectory();
File filename = new File(directory, file);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(objTrans);
int flags = ObjectFile.RESIZE;
ObjectFile f = new ObjectFile(flags,(float)(creaseAngle * Math.PI / 180.0));
Scene s = null;
try
{
s = f.load(file);
}
catch (FileNotFoundException e)
{
System.err.println(e);
System.exit(1);
}
catch (ParsingErrorException e)
{
System.err.println(e);
System.exit(1);
}
catch (IncorrectFormatException e)
{
System.err.println(e);
System.exit(1);
}
objTrans.addChild(s.getSceneGroup());
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
// Create the rotate behavior node
MouseRotate behavior = new MouseRotate();
behavior.setTransformGroup(objTrans);
objTrans.addChild(behavior);
behavior.setSchedulingBounds(bounds);
// Create the zoom behavior node
MouseZoom behavior2 = new MouseZoom();
behavior2.setTransformGroup(objTrans);
objTrans.addChild(behavior2);
behavior2.setSchedulingBounds(bounds);
// Create the translate behavior node
MouseTranslate behavior3 = new MouseTranslate();
behavior3.setTransformGroup(objTrans);
objTrans.addChild(behavior3);
behavior3.setSchedulingBounds(bounds);
// Set up the background
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
Background bgNode = new Background(bgColor);
bgNode.setApplicationBounds(bounds);
objRoot.addChild(bgNode);
// Set up the ambient light
Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
// Set up the directional lights
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
DirectionalLight light2
= new DirectionalLight(light2Color, light2Direction);
light2.setInfluencingBounds(bounds);
objRoot.addChild(light2);
return objRoot;
}
public ObjLoad2()
{
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args)
{
new MainFrame(new ObjLoad2(), 700, 700);
}
}
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".