Example usage for javax.media.j3d Material setLightingEnable

List of usage examples for javax.media.j3d Material setLightingEnable

Introduction

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

Prototype

public void setLightingEnable(boolean state) 

Source Link

Document

Enables or disables lighting for this appearance component object.

Usage

From source file:MixedTest.java

public void renderField(int fieldDesc) {
    super.renderField(fieldDesc);

    GraphicsContext3D g = getGraphicsContext3D();

    // first time initialization
    if (m_nRender == 0) {
        // set the start time
        m_StartTime = System.currentTimeMillis();

        // add a light to the graphics context
        DirectionalLight light = new DirectionalLight();
        light.setEnable(true);//from   www. j  ava 2  s. co m
        g.addLight((Light) light);

        // create the material for the points
        Appearance a = new Appearance();
        Material mat = new Material();
        mat.setLightingEnable(true);
        mat.setAmbientColor(0.5f, 1.0f, 1.0f);
        a.setMaterial(mat);
        a.setColoringAttributes(new ColoringAttributes(1.0f, 0.5f, 0.5f, ColoringAttributes.NICEST));

        // enlarge the points
        a.setPointAttributes(new PointAttributes(4, true));

        // make the appearance current in the graphics context
        g.setAppearance(a);
    }

    // set the current transformation for the graphics context
    g.setModelTransform(m_t3d);

    // finally render the PointArray
    g.draw(m_PointArray);

    // calculate and display the Frames Per Second for the
    // Immediate Mode rendering of the PointArray
    m_nRender++;

    if ((m_nRender % m_kReportInterval) == 0) {
        float fps = (float) 1000.0f / ((System.currentTimeMillis() - m_StartTime) / (float) m_kReportInterval);
        System.out.println("FPS:\t" + fps);

        m_StartTime = System.currentTimeMillis();
    }
}

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 a  va2 s .  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:SplineAnim.java

public BranchGroup createSceneGraph() {

    // Colors for lights and objects
    Color3f aColor = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f coneColor = new Color3f(0.9f, 0.1f, 0.1f);
    Color3f sphereColor = new Color3f(0.1f, 0.7f, 0.9f);
    Color3f bgColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f);

    // Root of the branch grsph
    BranchGroup root = new BranchGroup();

    // Create transforms such that all objects appears in the scene
    sceneTransform = new Transform3D();
    sceneTransform.setScale(0.14f);//from www  .  j a v a 2  s . co  m
    Transform3D yrot = new Transform3D();
    yrot.rotY(-Math.PI / 5.0d);
    sceneTransform.mul(yrot);
    sceneTransformGroup = new TransformGroup(sceneTransform);
    sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    root.addChild(sceneTransformGroup);

    // Create bounds for the background and lights
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0f);

    // Set up the background
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    sceneTransformGroup.addChild(bg);

    // Create the transform group node for the lights
    lightTransform1 = new Transform3D();
    lightTransform2 = new Transform3D();
    Vector3d lightPos1 = new Vector3d(0.0, 0.0, 2.0);
    Vector3d lightPos2 = new Vector3d(1.0, 0.0, -2.0);
    lightTransform1.set(lightPos1);
    lightTransform2.set(lightPos2);
    light1TransformGroup = new TransformGroup(lightTransform1);
    light2TransformGroup = new TransformGroup(lightTransform2);
    sceneTransformGroup.addChild(light1TransformGroup);
    sceneTransformGroup.addChild(light2TransformGroup);

    // Create lights
    AmbientLight ambLight = new AmbientLight(aColor);
    Light dirLight1;
    Light dirLight2;

    Vector3f lightDir1 = new Vector3f(lightPos1);
    Vector3f lightDir2 = new Vector3f(lightPos2);
    lightDir1.negate();
    lightDir2.negate();
    dirLight1 = new DirectionalLight(lightColor, lightDir1);
    dirLight2 = new DirectionalLight(lightColor, lightDir2);

    // Set the influencing bounds
    ambLight.setInfluencingBounds(bounds);
    dirLight1.setInfluencingBounds(bounds);
    dirLight2.setInfluencingBounds(bounds);

    // Add the lights into the scene graph
    sceneTransformGroup.addChild(ambLight);
    sceneTransformGroup.addChild(dirLight1);
    sceneTransformGroup.addChild(dirLight2);

    // Create a cone and add it to the scene graph.
    objTransform = new Transform3D();
    objTransformGroup = new TransformGroup(objTransform);
    objTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    sceneTransformGroup.addChild(objTransformGroup);

    Material m = new Material(coneColor, eColor, coneColor, sColor, 100.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Cone cone = new Cone(0.4f, 1.0f);
    cone.setAppearance(a);
    objTransformGroup.addChild(cone);

    // Create transform groups for each knot point
    // knot point 0
    Transform3D t3dKnot = new Transform3D();
    t3dKnot.set(pos0);
    TransformGroup k0TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k0TransformGroup);

    // knot point 1
    t3dKnot = new Transform3D();
    t3dKnot.set(pos1);
    TransformGroup k1TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k1TransformGroup);

    // knot point 2
    t3dKnot = new Transform3D();
    t3dKnot.set(pos2);
    TransformGroup k2TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k2TransformGroup);

    // knot point 3
    t3dKnot = new Transform3D();
    t3dKnot.set(pos3);
    TransformGroup k3TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k3TransformGroup);

    // knot point 4
    t3dKnot = new Transform3D();
    t3dKnot.set(pos4);
    TransformGroup k4TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k4TransformGroup);

    // knot point 5
    t3dKnot = new Transform3D();
    t3dKnot.set(pos5);
    TransformGroup k5TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k5TransformGroup);

    // Create spheres for each knot point's transform group
    ColoringAttributes sphereColorAttr = new ColoringAttributes();
    sphereColorAttr.setColor(sphereColor);
    Appearance sphereAppearance = new Appearance();
    sphereAppearance.setColoringAttributes(sphereColorAttr);
    k0TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k1TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k2TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k3TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k4TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k5TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));

    return root;
}

From source file:PickTest.java

private Group createObject(int index, double scale, double xpos, double ypos) {

    Shape3D shape = null;/*from   w w w.j  a  v a  2  s . c  o m*/
    Geometry geom = null;

    // Create a transform group node to scale and position the object.
    Transform3D t = new Transform3D();
    t.set(scale, new Vector3d(xpos, ypos, 0.0));
    TransformGroup objTrans = new TransformGroup(t);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    // Create a second transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime.
    TransformGroup spinTg = new TransformGroup();
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    spinTg.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    Appearance appearance = new Appearance();

    switch (index) {
    case 0:
        geom = new GullCG();
        break;
    case 1:
        geom = new TetrahedronTA();
        break;
    case 2:
        geom = new OctahedronTFA();
        break;
    case 3:
        geom = new IcosahedronTSA();
        break;
    case 4:
        geom = new CubeIQA();
        break;
    case 5:
        geom = new TetrahedronITA();
        break;
    case 6:
        geom = new OctahedronITFA();
        break;
    case 7:
        geom = new IcosahedronITSA();
        break;
    case 8:
        geomMorph[0] = new ColorPyramidUp();
        geomMorph[1] = new ColorCube();
        geomMorph[2] = new ColorPyramidDown();
        break;
    case 9:
        geom = new TetrahedronLA();
        break;
    case 10:
        geom = new TetrahedronILA();
        break;
    case 11:
        geom = new TetrahedronLSA();
        break;
    case 12:
        geom = new TetrahedronILSA();
        break;
    case 13:
        geom = new TetrahedronPA();
        break;
    case 14:
        geom = new TetrahedronIPA();
        break;
    // TODO: other geo types, Text3D?
    case 15:
        geom = new TetrahedronTA();
        break;
    }

    Material m = new Material();

    if (index == 8) {
        m.setLightingEnable(false);
        appearance.setMaterial(m);
        morph = new Morph((GeometryArray[]) geomMorph, appearance);
        morph.setCapability(Morph.ALLOW_WEIGHTS_READ);
        morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
        PickTool.setCapabilities(morph, PickTool.INTERSECT_FULL);
        spinTg.addChild(morph);
    } else {
        // Geometry picking require this to be set.
        if (index == 0)
            m.setLightingEnable(true);
        else
            m.setLightingEnable(false);
        appearance.setMaterial(m);

        if ((index == 13) || (index == 14)) {
            PointAttributes pa = new PointAttributes();
            pa.setPointSize(4.0f);
            appearance.setPointAttributes(pa);
        }

        shape = new Shape3D(geom, appearance);
        shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
        shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
        shape.setCapability(Shape3D.ENABLE_PICK_REPORTING);
        PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
        spinTg.addChild(shape);
    }

    // add it to the scene graph.
    objTrans.addChild(spinTg);

    return objTrans;
}

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  w w  .  j  a v a  2s.  c  om*/

    // 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: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);/*from ww  w .jav a2s  .c  om*/

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

From source file:FourByFour.java

/**
 * Create the scenegraph for the 3D view.
 *//*w  w  w  .  j  a va2s  .co  m*/
public BranchGroup createScene3D() {

    // Define colors
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f red = new Color3f(0.80f, 0.20f, 0.2f);
    Color3f ambient = new Color3f(0.25f, 0.25f, 0.25f);
    Color3f diffuse = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f specular = new Color3f(0.9f, 0.9f, 0.9f);
    Color3f ambientRed = new Color3f(0.2f, 0.05f, 0.0f);
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);

    // Create the branch group
    BranchGroup branchGroup = new BranchGroup();

    // Create the bounding leaf node
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
    BoundingLeaf boundingLeaf = new BoundingLeaf(bounds);
    branchGroup.addChild(boundingLeaf);

    // Create the background
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    branchGroup.addChild(bg);

    // Create the ambient light
    AmbientLight ambLight = new AmbientLight(white);
    ambLight.setInfluencingBounds(bounds);
    branchGroup.addChild(ambLight);

    // Create the directional light
    Vector3f dir = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight dirLight = new DirectionalLight(white, dir);
    dirLight.setInfluencingBounds(bounds);
    branchGroup.addChild(dirLight);

    // Create the pole appearance
    Material poleMaterial = new Material(ambient, black, diffuse, specular, 110.f);
    poleMaterial.setLightingEnable(true);
    Appearance poleAppearance = new Appearance();
    poleAppearance.setMaterial(poleMaterial);

    // Create the transform group node
    TransformGroup transformGroup = new TransformGroup();
    transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    branchGroup.addChild(transformGroup);

    // Create the poles
    Poles poles = new Poles(poleAppearance);
    transformGroup.addChild(poles.getChild());

    // Add the position markers to the transform group
    transformGroup.addChild(positions.getChild());

    // Let the positions object know about the transform group
    positions.setTransformGroup(transformGroup);

    // Create the mouse pick and drag behavior node
    PickDragBehavior behavior = new PickDragBehavior(canvas2D, canvas3D, positions, branchGroup,
            transformGroup);
    behavior.setSchedulingBounds(bounds);
    transformGroup.addChild(behavior);

    return branchGroup;
}