Example usage for javax.media.j3d DirectionalLight DirectionalLight

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

Introduction

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

Prototype

public DirectionalLight(Color3f color, Vector3f direction) 

Source Link

Document

Constructs and initializes a directional light.

Usage

From source file:TransformExplorer.java

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

    // Create a TransformGroup to scale the scene down by 3.5x
    TransformGroup objScale = new TransformGroup();
    Transform3D scaleTrans = new Transform3D();
    scaleTrans.set(1 / 3.5f); // scale down by 3.5x
    objScale.setTransform(scaleTrans);//from  www.java  2  s .  c  o m
    objRoot.addChild(objScale);

    // Create a TransformGroup and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // the mouse behaviors 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 the primitives to the scene
    objTrans.addChild(createConeTransformGroup()); // the cone
    rotAxis = new RotAxis(rotAxisLength); // the axis
    objTrans.addChild(rotAxis);
    coordSys = new CoordSys(coordSysLength); // the coordSys
    objTrans.addChild(coordSys);

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

    // The book used a white background for the figures
    //Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
    //bg.setApplicationBounds(bounds);
    //objTrans.addChild(bg);

    // 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, 1.0f);
    Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    return objRoot;
}

From source file:TextureByReference.java

public BranchGroup createSceneGraph() {

    // create the root of the branch group
    BranchGroup objRoot = new BranchGroup();

    // create the transform group node and initialize it
    // enable the TRANSFORM_WRITE capability so that it can be modified
    // at runtime. Add it to the root of the subgraph
    Transform3D rotate = new Transform3D();
    TransformGroup objTrans = new TransformGroup(rotate);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);//from   w w w  . jav a2s .  co  m

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

    // set up some light
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -1.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);

    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(bounds);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(bounds);
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    Appearance appearance = new Appearance();

    // enable the TEXTURE_WRITE so we can modify it at runtime
    appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    // load the first texture
    TextureLoader loader = new TextureLoader(urls[0], TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this);
    // get the texture from the loader
    Texture2D tex = (Texture2D) loader.getTexture();

    // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip
    // get the ImageComponent because we need it anyway
    ImageComponent2D imageComp = (ImageComponent2D) tex.getImage(0);
    BufferedImage bImage = imageComp.getImage();
    // convert the image
    bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR);
    // flip the image
    ImageOps.flipImage(bImage);
    imageComp.set(bImage);

    tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
    tex.setBoundaryModeS(Texture.CLAMP);
    tex.setBoundaryModeT(Texture.CLAMP);
    tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f);

    // set the image of the texture
    tex.setImage(0, imageComp);

    // set the texture on the appearance
    appearance.setTexture(tex);

    // set texture attributes
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    appearance.setTextureAttributes(texAttr);

    // set material properties
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    appearance.setMaterial(new Material(white, black, white, black, 1.0f));

    // create a scale transform
    Transform3D scale = new Transform3D();
    scale.set(.6);
    TransformGroup objScale = new TransformGroup(scale);
    objTrans.addChild(objScale);

    tetra = new Tetrahedron(true);
    tetra.setAppearance(appearance);
    objScale.addChild(tetra);

    // create the behavior
    animate = new AnimateTexturesBehavior(tex, urls, appearance, this);
    animate.setSchedulingBounds(bounds);

    objTrans.addChild(animate);

    // add a rotation behavior so we can see all sides of the tetrahedron
    Transform3D yAxis = new Transform3D();
    Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
    RotationInterpolator rotator = new RotationInterpolator(rotorAlpha, objTrans, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

    // have java3d perform optimizations on this scene graph
    objRoot.compile();

    return objRoot;
}

From source file:Human1.java

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

    // Create a TransformGroup to scale the scene down by 3.5x
    // TODO: move view platform instead of scene using orbit behavior
    TransformGroup objScale = new TransformGroup();
    Transform3D scaleTrans = new Transform3D();
    scaleTrans.set(1 / 3.5f); // scale down by 3.5x
    objScale.setTransform(scaleTrans);/*from   www  .  java 2s  .  co m*/
    objRoot.addChild(objScale);

    // Create a TransformGroup and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // the mouse behaviors 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 the primitives to the scene
    createHuman(); // the human
    objTrans.addChild(Human_body);

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

    Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
    bg.setApplicationBounds(bounds);
    objTrans.addChild(bg);

    // set up the mouse rotation behavior
    MouseRotate mr = new MouseRotate();
    mr.setTransformGroup(objTrans);
    mr.setSchedulingBounds(bounds);
    mr.setFactor(0.007);
    objTrans.addChild(mr);

    // 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, 1.0f);
    Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    return objRoot;
}

From source file:FourByFour.java

/**
 * Create the scenegraph for the 3D view.
 *//*from w  w  w  .jav a 2  s.c  o  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;
}

From source file:GearTest.java

BranchGroup createBranchEnvironment() {
    // Create the root of the branch graph
    BranchGroup branchRoot = new BranchGroup();

    // Create a bounds for the background and lights
    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);
    branchRoot.addChild(bgNode);//from w  w  w .  ja v  a 2s  .  co  m

    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    branchRoot.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, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    branchRoot.addChild(light1);

    DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    branchRoot.addChild(light2);

    return branchRoot;
}

From source file:AppearanceExplorer.java

Group setupLights() {

    Group group = new Group();

    // set up the BoundingSphere for all the lights
    BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);

    // Set up the ambient light
    AmbientLight lightAmbient = new AmbientLight(medGrey);
    lightAmbient.setInfluencingBounds(bounds);
    lightAmbient.setCapability(Light.ALLOW_STATE_WRITE);
    group.addChild(lightAmbient);/*from   w  w  w  .ja va  2s  .co  m*/

    lightSwitch = new Switch();
    lightSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
    group.addChild(lightSwitch);

    // Set up the directional light
    Vector3f lightDirection1 = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight lightDirectional1 = new DirectionalLight(white, lightDirection1);
    lightDirectional1.setInfluencingBounds(bounds);
    lightDirectional1.setCapability(Light.ALLOW_STATE_WRITE);
    lightSwitch.addChild(lightDirectional1);

    Point3f lightPos1 = new Point3f(-4.0f, 8.0f, 16.0f);
    Point3f lightAttenuation1 = new Point3f(1.0f, 0.0f, 0.0f);
    PointLight pointLight1 = new PointLight(brightWhite, lightPos1, lightAttenuation1);
    pointLight1.setInfluencingBounds(bounds);
    lightSwitch.addChild(pointLight1);

    Point3f lightPos2 = new Point3f(-16.0f, 8.0f, 4.0f);
    //Point3f lightPos = new Point3f(-4.0f, 2.0f, 1.0f);
    Point3f lightAttenuation2 = new Point3f(1.0f, 0.0f, 0.0f);
    PointLight pointLight2 = new PointLight(white, lightPos2, lightAttenuation2);
    pointLight2.setInfluencingBounds(bounds);
    lightSwitch.addChild(pointLight2);

    return group;
}