Look at
http://java.sun.com/products/java-media/3D/collateral/

Here is the code. I used JAI for downloading .png images

Jacob Nikom

//=====beginning of the file SphereTextureApp =========
import javax.media.jai.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.image.*;

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

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;

import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;

// SphereTextureApp creates a single plane with texture mapping.
public class SphereTextureApp extends JPanel
{
  public SphereTextureApp (BufferedImage bufferedImage)
  {
    Canvas3D canvas = new Canvas3D(null);

    setLayout(new BorderLayout());
    add("Center", canvas);

    BranchGroup scene = createSceneGraph(bufferedImage);

    SimpleUniverse su = new SimpleUniverse(canvas);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    su.getViewingPlatform().setNominalViewingTransform();

    su.addBranchGraph(scene);
  }

  public BranchGroup createSceneGraph(BufferedImage bufferedImage)
  {
    BranchGroup objRoot = new BranchGroup();
    Transform3D transform = new Transform3D();
    Appearance appear = new Appearance();

    //  Create 3D transformation variables(internally a matrix)
    Transform3D revXY = new Transform3D();
    Transform3D revY  = new Transform3D();
    Transform3D movX  = new Transform3D();

    //  Set Transformations
    movX.set(new Vector3d(-0.5d, 0.0d, 0.0d));
    revXY.rotX(Math.PI/6.0);
    revY.rotY(Math.PI/6.0);

    // Put the final result into the same matrix movX
    revXY.mul(revY);
    movX.mul(revXY);

    //  Create a Transform Group object
//  TransformGroup objTrans = new TransformGroup(movX);
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    TextureLoader loader = new TextureLoader(bufferedImage);
    ImageComponent2D imageTexture = loader.getImage();

    // can't use parameterless constuctor
    Texture2D texture = new Texture2D(
                                      Texture.BASE_LEVEL,
                                      Texture.RGBA,
                                      imageTexture.getWidth(),
                                      imageTexture.getHeight()
                                     );

    texture.setImage(0, imageTexture);

    appear.setTexture(texture);

    appear.setTransparencyAttributes(
         new TransparencyAttributes(TransparencyAttributes.FASTEST,
0.1f));

    // Create Geometry
    Sphere sphere = new Sphere(1.0f, Primitive.GENERATE_TEXTURE_COORDS,
appear);
    QuadArray plane = createPlane();  // initialize QuadArray object

//  Shape3D planeObj = new Shape3D(plane, appear);
//  Shape3D sphereObj = new Shape3D(sphere, appear);

    // Key point - add the geometry not to the scene
    // but to the transformation object!
    objTrans.addChild(sphere);

    //  add Transform group object to the Branch group
    objRoot.addChild(objTrans);

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

    // Create the rotate behavior node
    MouseRotate behavior1 = new MouseRotate(objTrans);
    objTrans.addChild(behavior1);
    behavior1.setSchedulingBounds(bounds);

    // Create the zoom behavior node
    MouseZoom behavior2 = new MouseZoom(objTrans);
    objTrans.addChild(behavior2);
    behavior2.setSchedulingBounds(bounds);

    // Create the translate behavior node
    MouseTranslate behavior3 = new MouseTranslate(objTrans);
    objTrans.addChild(behavior3);
    behavior3.setSchedulingBounds(bounds);

    Background background = new Background();
    background.setColor(0.0f, 0.0f, 0.5f);
    background.setApplicationBounds(new BoundingSphere());
    objRoot.addChild(background);

    return objRoot;
  }

  public QuadArray createPlane()
  {
    QuadArray plane =
      new QuadArray(4, GeometryArray.COORDINATES |
GeometryArray.TEXTURE_COORDINATE_2);

    Point3f p = new Point3f(-1.0f,  1.0f,  0.0f);
    plane.setCoordinate(0, p);
    p.set(-1.0f, -1.0f,  0.0f);
    plane.setCoordinate(1, p);
    p.set(1.0f, -1.0f,  0.0f);
    plane.setCoordinate(2, p);
    p.set(1.0f,  1.0f,  0.0f);
    plane.setCoordinate(3, p);

    Point2f q = new Point2f( 0.0f,  1.0f);
    plane.setTextureCoordinate(0, q);
    q.set(0.0f, 0.0f);
    plane.setTextureCoordinate(1, q);
    q.set(1.0f, 0.0f);
    plane.setTextureCoordinate(2, q);
    q.set(1.0f, 1.0f);
    plane.setTextureCoordinate(3, q);

    return plane;
  }

  public static void main(String args[])
  {
    JFrame jFrame = new JFrame("Swing3D Primitive Texture");
    jFrame.setBounds(100, 100, 300, 300);

    jFrame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent event)
      {
        System.exit(0);
      }
    });

    Container jFrameContainer = jFrame.getContentPane();
    jFrameContainer.setLayout(null);
    jFrameContainer.setBounds(0, 0, 300, 300);
    jFrameContainer.setBackground(Color.pink);

    if (args.length != 1)
    {
      System.out.println ("usage: java SphereTextureApp
<image_file.ext>");
      System.exit (1);
    }

    PlanarImage planarImage = JAI.create("fileload", args[0]);
    BufferedImage buffImage = planarImage.getAsBufferedImage();

    SphereTextureApp sphereTextureApp = new SphereTextureApp(buffImage);
    sphereTextureApp.setBounds(0, 0, 256, 256);
    sphereTextureApp.setVisible(true);

    jFrameContainer.add(sphereTextureApp);
    jFrame.setVisible(true);
    jFrame.validate();

  }
}


> pieter wrote:
>
> hello all.
>
> I would like to make a Texture-object from the information in an Image
> object. I got the Image via the getImage() method in an AWT Component.
> Texture-classes (Texture2D in particular) seem to want an
> ImageComponent-object. And an ImageComponent-object seems to want a
> BufferedImage-object. And to use a BufferedImage-object, I need a kind
> of
> Raster.
> But I can't seem to convert the Image2D-object that I have to any of
> those
> required classes.
>
> So if anybody knows how to make a Texture from an Image2D, I'd be very
> gratefull if you could tell me!
>
> P.S. Are there any good books that address such issues (or that
> explain the
> exact working of the util-package classes) ?
>
> Thanx.
> Pieter.

===========================================================================
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".

Reply via email to