Creates an ambient light and a one directional light : Light « 3D « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 3D » Light 




Creates an ambient light and a one directional light
Creates an ambient light and a one directional light

/*
Essential Java 3D Fast

Ian Palmer

Publisher: Springer-Verlag

ISBN: 1-85233-394-4

*/

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Sphere;

/**
 * This builds a red sphere using the Sphere utility class and adds lights so
 * that you can see it shape. It creates a material for the sphere, creates an
 * ambient light and a one directional light.
 
 @author I.J.Palmer
 @version 1.0
 */
public class SimpleDirLight extends Frame implements ActionListener {
  protected Canvas3D myCanvas3D = new Canvas3D(null);

  protected Button myButton = new Button("Exit");

  /**
   * This function builds the view branch of the scene graph. It creates a
   * branch group and then creates the necessary view elements to give a
   * useful view of our content.
   
   @param c
   *            Canvas3D that will display the view
   @return BranchGroup that is the root of the view elements
   */
  protected BranchGroup buildViewBranch(Canvas3D c) {
    BranchGroup viewBranch = new BranchGroup();
    Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0.0f0.0f10.0f));
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    ViewPlatform myViewPlatform = new ViewPlatform();
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
    viewXfmGroup.addChild(myViewPlatform);
    viewBranch.addChild(viewXfmGroup);
    View myView = new View();
    myView.addCanvas3D(c);
    myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);
    return viewBranch;
  }

  /**
   * This creates some lights and adds them to the BranchGroup.
   
   @param b
   *            BranchGroup that the lights are added to.
   */
  protected void addLights(BranchGroup b) {
    // Create a bounds for the lights
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.00.0),
        100.0);
    //Set up the ambient light
    Color3f ambientColour = new Color3f(0.2f0.2f0.2f);
    AmbientLight ambientLight = new AmbientLight(ambientColour);
    ambientLight.setInfluencingBounds(bounds);
    //Set up the directional light
    Color3f lightColour = new Color3f(1.0f1.0f1.0f);
    Vector3f lightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight light = new DirectionalLight(lightColour, lightDir);
    light.setInfluencingBounds(bounds);
    //Add the lights to the BranchGroup
    b.addChild(ambientLight);
    b.addChild(light);
  }

  /**
   * This build the content branch of our scene graph. It creates a transform
   * group so that the shape is slightly tilted to reveal its 3D shape.
   
   @param shape
   *            Node that represents the geometry for the content
   @return BranchGroup that is the root of the content branch
   */
  protected BranchGroup buildContentBranch() {
    BranchGroup contentBranch = new BranchGroup();
    Transform3D rotateCube = new Transform3D();
    rotateCube.set(new AxisAngle4d(1.01.00.0, Math.PI / 4.0));
    TransformGroup rotationGroup = new TransformGroup(rotateCube);
    contentBranch.addChild(rotationGroup);
    //Create a new appearance
    Appearance app = new Appearance();
    //Create the colours for the material
    Color3f ambientColour = new Color3f(1.0f0.0f0.0f);
    Color3f diffuseColour = new Color3f(1.0f0.0f0.0f);
    Color3f specularColour = new Color3f(1.0f1.0f1.0f);
    Color3f emissiveColour = new Color3f(0.0f0.0f0.0f);
    //Define the shininess
    float shininess = 20.0f;
    //Set the material of the appearance
    app.setMaterial(new Material(ambientColour, emissiveColour,
        diffuseColour, specularColour, shininess));
    //Create and add a new sphere using the appearance
    rotationGroup.addChild(new Sphere(2.0f, Sphere.GENERATE_NORMALS, 120,
        app));
    //Use the addLights function to add the lights to the branch
    addLights(contentBranch);
    //Return the root of the content branch
    return contentBranch;
  }

  /**
   * Handles the exit button action to quit the program.
   */
  public void actionPerformed(ActionEvent e) {
    dispose();
    System.exit(0);
  }

  /**
   * This creates a default universe and locale, creates a window and uses the
   * functions defined in this class to build the view and content branches of
   * the scene graph.
   */
  public SimpleDirLight() {
    VirtualUniverse myUniverse = new VirtualUniverse();
    Locale myLocale = new Locale(myUniverse);
    myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
    myLocale.addBranchGraph(buildContentBranch());
    setTitle("SimpleDirLight");
    setSize(400400);
    setLayout(new BorderLayout());
    add("Center", myCanvas3D);
    add("South", myButton);
    myButton.addActionListener(this);
    setVisible(true);
  }

  public static void main(String[] args) {
    SimpleDirLight sdl = new SimpleDirLight();
  }
}

           
       














Related examples in the same category
1.Light DemoLight Demo
2.This builds a red sphere using the Sphere utility class and adds lightsThis builds a red sphere using the Sphere utility class and adds lights
3.ExDirectionalLight - illustrate use of directional lightsExDirectionalLight - illustrate use of directional lights
4.ExAmbientLight - illustrate use of ambient lightsExAmbientLight - illustrate use of ambient lights
5.ExPointLight - illustrate use of point lightsExPointLight - illustrate use of point lights
6.ExLightScope - illustrate use of light scope groupsExLightScope - illustrate use of light scope groups
7.Illustrate use of light influencing bounds, and bounding leaves Illustrate use of light influencing bounds, and bounding leaves
8.ExSpotLight - illustrate use of spot lightsExSpotLight - illustrate use of spot lights
9.AmbientLight, DirectionalLight, PointLight and SpotLightAmbientLight, DirectionalLight, PointLight and SpotLight
10.Lighting PlaneLighting Plane
11.Spot LightSpot Light
12.LightScopeApp creates a scene that is paritally lightLightScopeApp creates a scene that is paritally light
13.Light ViewerLight Viewer
14.Light BugLight Bug
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.