Here is a snippet from another message:
Hi Pieter,
this is a short code fragment, that creates a texture object from an
image...
be aware, the image dimensions must be power of 2...
------------------------------------------------------------------------------------
//....
Image img = Toolkit.getDefaultToolkit().getImage(url);
PixelGrabber grabber = new PixelGrabber(img, 0, 0, -1, -1, true);
try {
grabber.grabPixels(timeout);
}catch(InterruptedException e){return null;}
if((grabber.getStatus() & ImageObserver.ALLBITS) != 0)
{
int w = grabber.getWidth();
int h = grabber.getHeight();
int[] pixels = (int[])grabber.getPixels();
BufferedImage bi = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
bi.setRGB(0, 0, w, h, pixels, 0, w);
Texture tx = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, w, h);
tx.setImage(0, new ImageComponent2D(ImageComponent.FORMAT_RGB,
bi));
tx.setMagFilter(Texture.BASE_LEVEL_LINEAR);
tx.setMinFilter(Texture.BASE_LEVEL_LINEAR);
tx.setBoundaryModeS(Texture.WRAP); //default
tx.setBoundaryModeT(Texture.WRAP); //default
tx.setEnable(true);
return tx;
}
//....
Jacob Nikom
PK wrote:
>
> 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".
===========================================================================
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".