Example usage for javax.media.j3d Shape3D Shape3D

List of usage examples for javax.media.j3d Shape3D Shape3D

Introduction

In this page you can find the example usage for javax.media.j3d Shape3D Shape3D.

Prototype

public Shape3D(Geometry geometry, Appearance appearance) 

Source Link

Document

Constructs and initializes a Shape3D node with the specified geometry and appearance components.

Usage

From source file:ExText.java

public Group buildScene() {
    // Get the current font attributes
    Font font = (Font) fonts[currentFont].value;
    String textString = (String) fonts[currentFont].name;

    // Turn on the example headlight
    setHeadlightEnable(true);/*  w ww.jav a  2  s .c o m*/

    // Build the scene group
    scene = new Group();
    scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    scene.setCapability(Group.ALLOW_CHILDREN_WRITE);

    // Build a branch group to hold the text shape
    // (this allows us to remove the text shape later,
    // change it, then put it back, all under menu control)
    textGroup = new BranchGroup();
    textGroup.setCapability(BranchGroup.ALLOW_DETACH);
    scene.addChild(textGroup);

    // BEGIN EXAMPLE TOPIC
    // Create a font extrusion with a default extrusion shape
    extrusion = new FontExtrusion();

    // Define a 3D font with a default extrusion path
    Font3D font3d = new Font3D(font, extrusion);

    // Build 3D text geometry using the 3D font
    Text3D tex = new Text3D();
    tex.setFont3D(font3d);
    tex.setString(textString);
    tex.setAlignment(Text3D.ALIGN_CENTER);

    // Define a generic shaded appearance
    Appearance app = new Appearance();
    Material mat = new Material();
    mat.setLightingEnable(true);
    app.setMaterial(mat);

    // Assemble geometry and appearance into a shape
    // and add it to the scene
    shape = new Shape3D(tex, app);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    textGroup.addChild(shape);
    // END EXAMPLE TOPIC

    return scene;
}

From source file:ExRaster.java

public Group buildScene() {
    // Turn on the headlight
    setHeadlightEnable(true);//from  ww  w .  j  a  va 2  s  .  c om

    // Default to examine navigation
    setNavigationType(Examine);

    // Build the scene root
    Group scene = new Group();

    if (debug)
        System.err.println("  rasters...");

    // BEGIN EXAMPLE TOPIC
    // Create three raster geometry shapes, each with a
    // different annotation text image

    // Load the texture images
    TextureLoader texLoader = new TextureLoader("one.jpg", this);
    ImageComponent2D oneImage = texLoader.getImage();
    if (oneImage == null) {
        System.err.println("Cannot load 'one.jpg'");
    }

    texLoader = new TextureLoader("two.jpg", this);
    ImageComponent2D twoImage = texLoader.getImage();
    if (twoImage == null) {
        System.err.println("Cannot load 'two.jpg'");
    }

    texLoader = new TextureLoader("three.jpg", this);
    ImageComponent2D threeImage = texLoader.getImage();
    if (threeImage == null) {
        System.err.println("Cannot load 'three.jpg'");
    }

    // Create raster geometries and shapes
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    // Left
    Raster raster = new Raster();
    raster.setPosition(new Point3f(-2.0f, 0.75f, 0.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(oneImage);
    Shape3D sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

    // Middle-back
    raster = new Raster();
    raster.setPosition(new Point3f(0.0f, 0.75f, -2.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(twoImage);
    sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

    // Right
    raster = new Raster();
    raster.setPosition(new Point3f(2.0f, 0.75f, 0.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(threeImage);
    sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);
    // END EXAMPLE TOPIC

    // Build foreground geometry including a floor and
    // cones on which the raster images sit
    if (debug)
        System.err.println("  cones...");
    Appearance app0 = new Appearance();
    Material mat0 = new Material();
    mat0.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat0.setDiffuseColor(1.0f, 0.0f, 0.0f);
    mat0.setSpecularColor(0.7f, 0.7f, 0.7f);
    app0.setMaterial(mat0);

    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(-2.0f, 0.0f, 0.0f));
    TransformGroup tg0 = new TransformGroup(t3d);
    Cone cone0 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app0); // appearance
    tg0.addChild(cone0);
    scene.addChild(tg0);

    Appearance app1 = new Appearance();
    Material mat1 = new Material();
    mat1.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat1.setDiffuseColor(0.0f, 1.0f, 0.0f);
    mat1.setSpecularColor(0.7f, 0.7f, 0.7f);
    app1.setMaterial(mat1);

    t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(0.0f, 0.0f, -2.0f));
    TransformGroup tg1 = new TransformGroup(t3d);
    Cone cone1 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app1); // appearance
    tg1.addChild(cone1);
    scene.addChild(tg1);

    Appearance app2 = new Appearance();
    Material mat2 = new Material();
    mat2.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat2.setDiffuseColor(0.0f, 0.6f, 1.0f);
    mat2.setSpecularColor(0.7f, 0.7f, 0.7f);
    app2.setMaterial(mat2);

    t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(2.0f, 0.0f, 0.0f));
    TransformGroup tg2 = new TransformGroup(t3d);
    Cone cone2 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app2); // appearance
    tg2.addChild(cone2);
    scene.addChild(tg2);

    return scene;
}

From source file:LightsNPlanesApp.java

public LightsNPlanesApp() {
    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(null);
    add("Center", c);

    BoundingSphere bounds = new BoundingSphere(new Point3d(), 0.1);

    Vector3f direction = new Vector3f(0.0f, -1.0f, 0.0f);
    Point3f position = new Point3f(0.0f, 0.5f, -0.3f);

    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);

    BranchGroup scene = new BranchGroup();

    IndexedQuadArray qa = new IndexedQuadArray(9, QuadArray.COORDINATES | QuadArray.NORMALS, 16);
    qa.setCoordinate(0, new Point3f(-0.3f, 0.3f, -0.3f));
    qa.setCoordinate(1, new Point3f(0.0f, 0.3f, -0.3f));
    qa.setCoordinate(2, new Point3f(0.3f, 0.3f, -0.3f));
    qa.setCoordinate(3, new Point3f(-0.3f, 0.0f, 0.0f));
    qa.setCoordinate(4, new Point3f(0.0f, 0.0f, 0.0f));
    qa.setCoordinate(5, new Point3f(0.3f, 0.0f, 0.0f));
    qa.setCoordinate(6, new Point3f(-0.3f, -0.3f, 0.3f));
    qa.setCoordinate(7, new Point3f(0.0f, -0.3f, 0.3f));
    qa.setCoordinate(8, new Point3f(0.3f, -0.3f, 0.3f));
    Vector3f n = new Vector3f(0.0f, 0.6f, 0.8f);
    n.normalize();/*from   www  . j a va 2s .  c  o  m*/
    qa.setNormal(0, n);
    qa.setNormal(1, n);
    qa.setNormal(2, n);
    qa.setNormal(3, n);
    qa.setNormal(4, n);
    qa.setNormal(5, n);
    qa.setNormal(6, n);
    qa.setNormal(7, n);
    qa.setNormal(8, n);

    qa.setCoordinateIndex(0, 0);
    qa.setCoordinateIndex(1, 3);
    qa.setCoordinateIndex(2, 4);
    qa.setCoordinateIndex(3, 1);
    qa.setCoordinateIndex(4, 1);
    qa.setCoordinateIndex(5, 4);
    qa.setCoordinateIndex(6, 5);
    qa.setCoordinateIndex(7, 2);
    qa.setCoordinateIndex(8, 3);
    qa.setCoordinateIndex(9, 6);
    qa.setCoordinateIndex(10, 7);
    qa.setCoordinateIndex(11, 4);
    qa.setCoordinateIndex(12, 4);
    qa.setCoordinateIndex(13, 7);
    qa.setCoordinateIndex(14, 8);
    qa.setCoordinateIndex(15, 5);

    TransformGroup trans1 = createTG(-0.7f, 0.0f, -0.5f);
    scene.addChild(trans1);

    TransformGroup trans2 = createTG(0.0f, 0.0f, -0.5f);
    scene.addChild(trans2);

    TransformGroup trans3 = createTG(0.7f, 0.0f, -0.5f);
    scene.addChild(trans3);

    Appearance qAppear = createMatAppear(white, white, 5.0f);
    Shape3D p1 = new Shape3D(qa, qAppear);
    //    p1.setBoundsAutoCompute(false);
    p1.setBounds(bounds);
    p1.setCapability(Node.ALLOW_BOUNDS_READ);
    trans1.addChild(p1);

    Shape3D p2 = new Shape3D(qa, qAppear);
    p2.setBounds(bounds);
    p2.setCapability(Node.ALLOW_BOUNDS_READ);
    trans2.addChild(p2);

    Shape3D p3 = new Shape3D(qa, qAppear);
    p3.setBounds(bounds);
    p3.setCapability(Node.ALLOW_BOUNDS_READ);
    trans3.addChild(p3);

    AmbientLight lightA = new AmbientLight();
    lightA.setInfluencingBounds(new BoundingSphere());
    lightA.setCapability(Light.ALLOW_INFLUENCING_BOUNDS_READ);
    scene.addChild(lightA);

    DirectionalLight lightD = new DirectionalLight();
    lightD.setInfluencingBounds(bounds);
    lightD.setBoundsAutoCompute(false);
    lightD.setCapability(Light.ALLOW_INFLUENCING_BOUNDS_READ);
    lightD.setDirection(direction);
    lightD.setColor(red);
    trans1.addChild(lightD);

    PointLight lightP = new PointLight();
    lightP.setInfluencingBounds(bounds);
    lightP.setCapability(Light.ALLOW_INFLUENCING_BOUNDS_READ);
    lightP.setPosition(position);
    lightP.setColor(green);
    trans2.addChild(lightP);

    SpotLight lightS = new SpotLight();
    lightS.setInfluencingBounds(bounds);
    lightS.setCapability(Light.ALLOW_INFLUENCING_BOUNDS_READ);
    lightS.setPosition(position);
    lightS.setDirection(direction);
    lightS.setSpreadAngle(0.3f);
    lightS.setConcentration(1.0f);
    lightS.setColor(blue);
    trans3.addChild(lightS);

    Background background = new Background();
    background.setApplicationBounds(new BoundingSphere());
    background.setColor(1.0f, 1.0f, 1.0f);
    scene.addChild(background);

    scene.compile();
    System.out.print("bounds object: ");
    System.out.println(bounds);

    System.out.print("influencing bounds for lightA: ");
    System.out.println(lightA.getInfluencingBounds());

    System.out.print("influencing bounds for lightD: ");
    System.out.println(lightD.getInfluencingBounds());

    System.out.print("influencing bounds for lightP: ");
    System.out.println(lightP.getInfluencingBounds());

    System.out.print("influencing bounds for lightS: ");
    System.out.println(lightS.getInfluencingBounds());

    System.out.print("bounds for plane1: ");
    System.out.println(p1.getBounds());

    System.out.print("bounds for plane2: ");
    System.out.println(p2.getBounds());

    System.out.print("bounds for plane3: ");
    System.out.println(p3.getBounds());

    BoundingSphere bs0 = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 0.2);
    BoundingSphere bs1 = new BoundingSphere(new Point3d(-0.1, 0.0, 0.0), 0.2);
    BoundingSphere bs2 = new BoundingSphere(new Point3d(-0.2, 0.0, 0.0), 0.2);
    BoundingSphere bs3 = new BoundingSphere(new Point3d(-0.3, 0.0, 0.0), 0.2);
    BoundingSphere bs4 = new BoundingSphere(new Point3d(-0.4, 0.0, 0.0), 0.2);
    BoundingSphere bs5 = new BoundingSphere(new Point3d(-0.5, 0.0, 0.0), 0.2);
    BoundingSphere bs6 = new BoundingSphere(new Point3d(-0.6, 0.0, 0.0), 0.2);
    BoundingSphere bs7 = new BoundingSphere(new Point3d(-0.7, 0.0, 0.0), 0.2);
    BoundingSphere bs8 = new BoundingSphere(new Point3d(-0.8, 0.0, 0.0), 0.2);
    BoundingBox bb1 = new BoundingBox(bs1);
    BoundingBox bb2 = new BoundingBox(bs2);
    BoundingBox bb3 = new BoundingBox(bs3);
    BoundingBox bb4 = new BoundingBox(bs4);
    BoundingBox bb5 = new BoundingBox(bs5);
    BoundingBox bb6 = new BoundingBox(bs6);
    BoundingBox bb7 = new BoundingBox(bs7);
    BoundingBox bb8 = new BoundingBox(bs8);

    if (bs0.intersect(bs1))
        System.out.println("bs0 intersects bs1");
    if (bs0.intersect(bs2))
        System.out.println("bs0 intersects bs2");
    if (bs0.intersect(bs3))
        System.out.println("bs0 intersects bs3");
    if (bs0.intersect(bs4))
        System.out.println("bs0 intersects bs4");
    if (bs0.intersect(bs5))
        System.out.println("bs0 intersects bs5");
    if (bs0.intersect(bs6))
        System.out.println("bs0 intersects bs6");
    if (bs0.intersect(bs7))
        System.out.println("bs0 intersects bs7");
    if (bs0.intersect(bs8))
        System.out.println("bs0 intersects bs8");

    if (bs0.intersect(bb1))
        System.out.println("bs0 intersects bb1");
    if (bs0.intersect(bb2))
        System.out.println("bs0 intersects bb2");
    if (bs0.intersect(bb3))
        System.out.println("bs0 intersects bb3");
    if (bs0.intersect(bb4))
        System.out.println("bs0 intersects bb4");
    if (bs0.intersect(bb5))
        System.out.println("bs0 intersects bb5");
    if (bs0.intersect(bb6))
        System.out.println("bs0 intersects bb6");
    if (bs0.intersect(bb7))
        System.out.println("bs0 intersects bb7");
    if (bs0.intersect(bb8))
        System.out.println("bs0 intersects bb8");

    SimpleUniverse u = new SimpleUniverse(c);

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

    u.addBranchGraph(scene);
}

From source file:cgview.java

public BranchGroup createSceneGraph(CompressedGeometry cg) {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.7);//from  w  ww. j ava2s  .  c o  m
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(objTrans);

    // Add compressed geometry to the scene graph.
    CompressedGeometryHeader hdr = new CompressedGeometryHeader();
    cg.getCompressedGeometryHeader(hdr);

    // There isn't really enough information in the compressed geometry
    // header to unamiguously determine the proper rendering attributes.
    // The bufferDataPresent field specifies whether or not normals are
    // bundled with vertices, but the compressed buffer can still contain
    // normals that should be lit. Assume that any surface geometry
    // should be lit and that lines and points should not unless the
    // header contains the NORMAL_IN_BUFFER bit.
    Material m = new Material();
    if ((hdr.bufferType == hdr.TRIANGLE_BUFFER) || ((hdr.bufferDataPresent & hdr.NORMAL_IN_BUFFER) == 1))
        m.setLightingEnable(true);
    else
        m.setLightingEnable(false);

    Appearance a = new Appearance();
    a.setMaterial(m);

    objTrans.addChild(new Shape3D(cg, a));

    // Create mouse behavior scheduling bounds.
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // 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(1.0f, 1.0f, 1.0f);
    Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -0.9f);

    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;
}

From source file:TwistStripApp.java

public BranchGroup createSceneGraph() {

    BranchGroup contentRoot = new BranchGroup();

    // Create the transform group node and initialize it to the
    // identity. Add it to the root of the subgraph.
    TransformGroup objSpin = new TransformGroup();
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    contentRoot.addChild(objSpin);/*from  ww  w  . j a  v a 2  s . co  m*/

    Shape3D twist = new Twist();
    objSpin.addChild(twist);

    // Duplicate the twist strip geometry and set the
    // appearance of the new Shape3D object to line mode
    // without culling.
    // Add the POLYGON_FILLED and POLYGON_LINE strips
    // in the scene graph at the same point.
    // This will show the triangles of the original Mobius strip that
    // are clipped. The PolygonOffset is set to prevent stitching.
    PolygonAttributes polyAttrib = new PolygonAttributes();
    polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
    polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    polyAttrib.setPolygonOffset(0.001f);
    Appearance polyAppear = new Appearance();
    polyAppear.setPolygonAttributes(polyAttrib);
    objSpin.addChild(new Shape3D(twist.getGeometry(), polyAppear));

    Alpha rotationAlpha = new Alpha(-1, 16000);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin);

    // a bounding sphere specifies a region a behavior is active
    // create a sphere centered at the origin with radius of 1
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);

    // make background white
    Background background = new Background(1.0f, 1.0f, 1.0f);
    background.setApplicationBounds(bounds);
    contentRoot.addChild(background);

    // Let Java 3D perform optimizations on this scene graph.
    contentRoot.compile();

    return contentRoot;
}

From source file:BackgroundApp.java

public BranchGroup createBackGraph() {

    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    PointArray starGeom1 = new PointArray(7, PointArray.COORDINATES);
    Appearance starAppear1 = new Appearance();
    starGeom1.setCoordinate(0, new Point3f(0.79483311f, -0.58810995f, 0.14955615f));
    starGeom1.setCoordinate(1, new Point3f(0.44430932f, -0.55736839f, -0.70137505f));
    starGeom1.setCoordinate(2, new Point3f(0.94901367f, -0.30404968f, 0.08322775f));
    starGeom1.setCoordinate(3, new Point3f(0.68060123f, -0.43044807f, 0.59287173f));
    starGeom1.setCoordinate(4, new Point3f(-0.11641672f, 0.47273532f, 0.87348049f));
    starGeom1.setCoordinate(5, new Point3f(-0.10399289f, -0.98059412f, 0.16619437f));
    starGeom1.setCoordinate(6, new Point3f(0.08024400f, -0.96944100f, -0.23182900f));
    PointAttributes point1 = new PointAttributes(4.0f, false);
    starAppear1.setPointAttributes(point1);
    objRoot.addChild(new Shape3D(starGeom1, starAppear1));

    PointArray starGeom2 = new PointArray(18, PointArray.COORDINATES);
    starGeom2.setCoordinate(0, new Point3f(0.050844f, -0.992329f, 0.112678f));
    starGeom2.setCoordinate(1, new Point3f(-0.063091f, -0.997672f, 0.025869f));
    starGeom2.setCoordinate(2, new Point3f(0.096706f, -0.980384f, 0.171736f));
    starGeom2.setCoordinate(3, new Point3f(-0.562384f, 0.073568f, 0.823595f));
    starGeom2.setCoordinate(4, new Point3f(-0.863904f, 0.059045f, 0.500180f));
    starGeom2.setCoordinate(5, new Point3f(-0.727033f, 0.304149f, 0.615559f));
    starGeom2.setCoordinate(6, new Point3f(-0.724850f, 0.535590f, 0.433281f));
    starGeom2.setCoordinate(7, new Point3f(0.185904f, -0.976907f, -0.105311f));
    starGeom2.setCoordinate(8, new Point3f(0.738028f, -0.531886f, -0.415221f));
    starGeom2.setCoordinate(9, new Point3f(-0.402152f, 0.392690f, -0.827085f));
    starGeom2.setCoordinate(10, new Point3f(-0.020020f, -0.999468f, -0.025724f));
    starGeom2.setCoordinate(11, new Point3f(-0.384103f, -0.887075f, 0.256050f));
    starGeom2.setCoordinate(12, new Point3f(-0.224464f, -0.968946f, -0.103720f));
    starGeom2.setCoordinate(13, new Point3f(-0.828880f, -0.397932f, -0.393203f));
    starGeom2.setCoordinate(14, new Point3f(-0.010557f, -0.998653f, 0.050797f));
    starGeom2.setCoordinate(15, new Point3f(-0.282122f, 0.258380f, -0.923930f));
    starGeom2.setCoordinate(16, new Point3f(-0.941342f, -0.030364f, 0.336082f));
    starGeom2.setCoordinate(17, new Point3f(0.00057f, -0.99651f, -0.08344f));
    Appearance starAppear2 = new Appearance();
    PointAttributes point2 = new PointAttributes(2.0f, false);
    starAppear2.setPointAttributes(point2);
    objRoot.addChild(new Shape3D(starGeom2, starAppear2));

    PointArray starGeom3 = new PointArray(20, PointArray.COORDINATES);
    starGeom3.setCoordinate(0, new Point3f(0.07292f, -0.98862f, -0.13153f));
    starGeom3.setCoordinate(1, new Point3f(0.23133f, -0.87605f, -0.42309f));
    starGeom3.setCoordinate(2, new Point3f(-0.08215f, -0.64657f, 0.75840f));
    starGeom3.setCoordinate(3, new Point3f(-0.84545f, 0.53398f, 0.00691f));
    starGeom3.setCoordinate(4, new Point3f(-0.49365f, -0.83645f, -0.23795f));
    starGeom3.setCoordinate(5, new Point3f(0.06883f, -0.99319f, -0.09396f));
    starGeom3.setCoordinate(6, new Point3f(0.87582f, -0.40662f, 0.25997f));
    starGeom3.setCoordinate(7, new Point3f(-0.09095f, -0.99555f, 0.02467f));
    starGeom3.setCoordinate(8, new Point3f(0.45306f, -0.81575f, -0.35955f));
    starGeom3.setCoordinate(9, new Point3f(0.17669f, -0.97939f, 0.09776f));
    starGeom3.setCoordinate(10, new Point3f(0.27421f, -0.83963f, 0.46884f));
    starGeom3.setCoordinate(11, new Point3f(0.32703f, -0.94013f, -0.09584f));
    starGeom3.setCoordinate(12, new Point3f(-0.01615f, -0.99798f, -0.06132f));
    starGeom3.setCoordinate(13, new Point3f(-0.76665f, 0.45998f, -0.44791f));
    starGeom3.setCoordinate(14, new Point3f(-0.91025f, -0.07102f, 0.40791f));
    starGeom3.setCoordinate(15, new Point3f(-0.00240f, -0.97104f, -0.23887f));
    starGeom3.setCoordinate(16, new Point3f(0.91936f, -0.39244f, 0.02740f));
    starGeom3.setCoordinate(17, new Point3f(0.18290f, -0.97993f, 0.07920f));
    starGeom3.setCoordinate(18, new Point3f(-0.48755f, 0.61592f, 0.61884f));
    starGeom3.setCoordinate(19, new Point3f(-0.89375f, 0.36087f, -0.26626f));
    objRoot.addChild(new Shape3D(starGeom3));

    int[] stripCount = { 10 };
    LineStripArray orion = new LineStripArray(10, LineStripArray.COORDINATES, stripCount);
    orion.setCoordinate(0, new Point3f(0.978330f, -0.033900f, 0.204426f));
    orion.setCoordinate(1, new Point3f(0.968007f, -0.167860f, 0.186506f));
    orion.setCoordinate(2, new Point3f(0.981477f, -0.142660f, 0.127873f));
    orion.setCoordinate(3, new Point3f(0.983764f, -0.005220f, 0.179391f));
    orion.setCoordinate(4, new Point3f(0.981112f, 0.110597f, 0.158705f));
    orion.setCoordinate(5, new Point3f(0.967377f, 0.172516f, 0.185523f));
    orion.setCoordinate(6, new Point3f(0.961385f, 0.128845f, 0.243183f));
    orion.setCoordinate(7, new Point3f(0.978330f, -0.033900f, 0.204426f));
    orion.setCoordinate(8, new Point3f(0.981293f, -0.020980f, 0.191375f));
    orion.setCoordinate(9, new Point3f(0.983764f, -0.005220f, 0.179391f));
    objRoot.addChild(new Shape3D(orion));

    objRoot.compile();/*from ww w.j  ava 2  s  .  com*/
    return objRoot;
}

From source file:SimpleCollision2.java

/**
 * Creates the content branch of the scene graph.
 * /*  www  .j ava  2  s  .  c  om*/
 * @return BranchGroup with content attached.
 */
protected BranchGroup buildContentBranch() {
    //First create a different appearance for each cube
    Appearance app1 = new Appearance();
    Appearance app2 = new Appearance();
    Appearance app3 = new Appearance();
    Color3f ambientColour1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f ambientColour2 = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f ambientColour3 = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f diffuseColour2 = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f diffuseColour3 = new Color3f(1.0f, 1.0f, 1.0f);
    float shininess = 20.0f;
    app1.setMaterial(new Material(ambientColour1, emissiveColour, diffuseColour1, specularColour, shininess));
    app2.setMaterial(new Material(ambientColour2, emissiveColour, diffuseColour2, specularColour, shininess));
    app3.setMaterial(new Material(ambientColour3, emissiveColour, diffuseColour3, specularColour, shininess));

    //Build the vertex array for the cubes. We can use the same
    //data for each cube so we just define one set of data
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
            IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f),
            new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
            new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f),
            new Point3f(1.0f, -1.0f, -1.0f) };
    Vector3f[] cubeNormals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f),
            new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f),
            new Vector3f(0.0f, -1.0f, 0.0f) };
    int cubeCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 };
    int cubeNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setNormals(0, cubeNormals);
    indexedCube.setCoordinateIndices(0, cubeCoordIndices);
    indexedCube.setNormalIndices(0, cubeNormalIndices);

    //Create the three cubes
    leftCube = new Shape3D(indexedCube, app1);
    rightCube = new Shape3D(indexedCube, app2);
    moveCube = new Shape3D(indexedCube, app3);

    //Define some user data so that we can print meaningful messages
    leftCube.setUserData(new String("left cube"));
    rightCube.setUserData(new String("right cube"));

    //Create the content branch and add the lights
    BranchGroup contentBranch = new BranchGroup();
    addLights(contentBranch);

    //Set up the transform to position the left cube
    Transform3D leftGroupXfm = new Transform3D();
    leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
    leftGroup = new TransformGroup(leftGroupXfm);

    //Set up the transform to position the right cube
    Transform3D rightGroupXfm = new Transform3D();
    rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
    rightGroup = new TransformGroup(rightGroupXfm);

    //Create the movable cube's transform with a scale and
    //a translation. Set up the
    //capabilities so it can be moved by the behaviour
    Transform3D moveXfm = new Transform3D();
    moveXfm.set(0.7, new Vector3d(0.0, 2.0, 1.0));
    moveGroup = new TransformGroup(moveXfm);
    moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    moveGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    //Create the behaviour to move the movable cube
    PickTranslateBehavior pickTranslate = new PickTranslateBehavior(contentBranch, myCanvas3D, bounds);
    contentBranch.addChild(pickTranslate);

    //Create and add the two colision detectors
    CollisionDetector2 myColDetLeft = new CollisionDetector2(leftCube, bounds);
    contentBranch.addChild(myColDetLeft);
    CollisionDetector2 myColDetRight = new CollisionDetector2(rightCube, bounds);
    contentBranch.addChild(myColDetRight);

    //Set up the scene graph
    contentBranch.addChild(moveGroup);
    contentBranch.addChild(leftGroup);
    contentBranch.addChild(rightGroup);
    moveGroup.addChild(moveCube);
    leftGroup.addChild(leftCube);
    rightGroup.addChild(rightCube);

    return contentBranch;

}

From source file:SimpleCollision.java

/**
 * Creates the content branch of the scene graph.
 * //w  ww  .  j  a  v  a2  s  . c  om
 * @return BranchGroup with content attached.
 */
protected BranchGroup buildContentBranch() {
    //First create a different appearance for each cube
    Appearance app1 = new Appearance();
    Appearance app2 = new Appearance();
    Appearance app3 = new Appearance();
    Color3f ambientColour1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f ambientColour2 = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f ambientColour3 = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f diffuseColour2 = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f diffuseColour3 = new Color3f(1.0f, 1.0f, 1.0f);
    float shininess = 20.0f;
    app1.setMaterial(new Material(ambientColour1, emissiveColour, diffuseColour1, specularColour, shininess));
    app2.setMaterial(new Material(ambientColour2, emissiveColour, diffuseColour2, specularColour, shininess));
    app3.setMaterial(new Material(ambientColour3, emissiveColour, diffuseColour3, specularColour, shininess));

    //Create the vertex data for the cube. Since each shape is
    //a cube we can use the same vertex data for each cube
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
            IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f),
            new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
            new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f),
            new Point3f(1.0f, -1.0f, -1.0f) };
    Vector3f[] cubeNormals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f),
            new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f),
            new Vector3f(0.0f, -1.0f, 0.0f) };
    int cubeCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 };
    int cubeNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setNormals(0, cubeNormals);
    indexedCube.setCoordinateIndices(0, cubeCoordIndices);
    indexedCube.setNormalIndices(0, cubeNormalIndices);

    //Create the three cubes
    leftCube = new Shape3D(indexedCube, app1);
    rightCube = new Shape3D(indexedCube, app2);
    moveCube = new Shape3D(indexedCube, app3);

    //Define the user data so that we can print out the
    //name of the colliding cube.
    leftCube.setUserData(new String("left cube"));
    rightCube.setUserData(new String("right cube"));

    //Create the content branch and add the lights
    BranchGroup contentBranch = new BranchGroup();
    addLights(contentBranch);

    //Create and set up the movable cube's TransformGroup.
    //This scales and translates the cube and then sets the
    // read, write and pick reporting capabilities.
    Transform3D moveXfm = new Transform3D();
    moveXfm.set(0.7, new Vector3d(0.0, 2.0, 1.0));
    moveGroup = new TransformGroup(moveXfm);
    moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    moveGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    //Create the left cube's TransformGroup
    Transform3D leftGroupXfm = new Transform3D();
    leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
    leftGroup = new TransformGroup(leftGroupXfm);

    //Create the right cube's TransformGroup
    Transform3D rightGroupXfm = new Transform3D();
    rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
    rightGroup = new TransformGroup(rightGroupXfm);

    //Add the behaviour to allow us to move the cube
    PickTranslateBehavior pickTranslate = new PickTranslateBehavior(contentBranch, myCanvas3D, bounds);
    contentBranch.addChild(pickTranslate);

    //Add our CollisionDetector class to detect collisions with
    //the movable cube.
    CollisionDetector myColDet = new CollisionDetector(moveCube, bounds);
    contentBranch.addChild(myColDet);

    //Create the content branch hierarchy.
    contentBranch.addChild(moveGroup);
    contentBranch.addChild(leftGroup);
    contentBranch.addChild(rightGroup);
    moveGroup.addChild(moveCube);
    leftGroup.addChild(leftCube);
    rightGroup.addChild(rightCube);

    return contentBranch;

}

From source file:SimpleIndexedQuad.java

/**
 * Build a cube from an IndexedQuadArray. This method creates the vertices
 * as a set of eight points and the normals as a set of six vectors (one for
 * each face). The data is then defined such that each vertex has a
 * different normal associated with it when it is being used for a different
 * face.//from  w  w w  .ja v a 2 s.  com
 * 
 * @return Node that is the shape.
 */
protected Node buildShape() {
    //The shape. The constructor specifies 8 vertices, that both
    //vertices and normals are to be defined and that there are
    //24 normals to be specified (4 for each of the 6 faces).
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
            IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    //The vertex coordinates defined as an array of points.
    Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f),
            new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
            new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f),
            new Point3f(1.0f, -1.0f, -1.0f) };
    //The vertex normals defined as an array of vectors
    Vector3f[] normals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f),
            new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f),
            new Vector3f(0.0f, -1.0f, 0.0f) };
    //Define the indices used to reference vertex array
    int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 };
    //Define the indices used to reference normal array
    int normalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
    //Set the data
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setNormals(0, normals);
    indexedCube.setCoordinateIndices(0, coordIndices);
    indexedCube.setNormalIndices(0, normalIndices);
    //Define an appearance for the shape
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Create and return the shape
    return new Shape3D(indexedCube, app);
}

From source file:ExTexture.java

public Group buildScene() {
    // Get the current menu choices for appearance attributes
    int textureMode = ((Integer) modes[currentMode].value).intValue();
    Color3f color = (Color3f) colors[currentColor].value;
    Color3f blendColor = (Color3f) colors[currentBlendColor].value;

    // Turn on the example headlight
    setHeadlightEnable(true);// w  ww . jav a2  s .  c o  m

    // Default to examine navigation
    setNavigationType(Examine);

    // Disable scene graph compilation for this example
    setCompilable(false);

    // Create the scene group
    Group scene = new Group();

    // BEGIN EXAMPLE TOPIC
    // Set up a basic material
    Material mat = new Material();
    mat.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    mat.setSpecularColor(0.0f, 0.0f, 0.0f);
    mat.setLightingEnable(true);

    // Set up the texturing attributes with an initial
    // texture mode, texture transform, and blend color
    texatt = new TextureAttributes();
    texatt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    texatt.setTextureMode(textureMode);
    texatt.setTextureTransform(new Transform3D()); // Identity
    texatt.setTextureBlendColor(blendColor.x, blendColor.y, blendColor.z, 0.5f);

    // Enable changing these while the node component is live
    texatt.setCapability(TextureAttributes.ALLOW_MODE_WRITE);
    texatt.setCapability(TextureAttributes.ALLOW_BLEND_COLOR_WRITE);
    texatt.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE);

    // Create an appearance using these attributes
    app = new Appearance();
    app.setMaterial(mat);
    app.setTextureAttributes(texatt);
    app.setTexture(tex);

    // And enable changing these while the node component is live
    app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
    app.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);

    // Build a shape and enable changing its appearance
    shape = new Shape3D(buildGeometry(), app);
    shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    // END EXAMPLE TOPIC

    // Create some dummy appearance and tex attribute node components
    // In response to menu choices, we quickly switch the shape to
    // use one of these, then diddle with the main appearance or
    // tex attribute, then switch the shape back. This effectively
    // makes the appearance or tex attributes we want to change
    // become un-live during a change. We have to do this approach
    // because some texture features have no capability bits to set
    // to allow changes while live.
    dummyApp = new Appearance();
    dummyAtt = new TextureAttributes();

    scene.addChild(shape);

    return scene;
}