Hello!

I try to learn how the light classes works in Java3D.

I have created a small app that contains 3 gaslights. Below each gaslight
there
is a surface, cylinder, box and a instance of a class called Ground (a
Shape3D
rectangle).

The gaslight emitts a spotlight downwards. The light that hits the
cylndertop
creates a very nice effect. But in some how it only seems to hit the
cylinder. I
would like to be able to create the same effect on my other objects below a
gaslight.

Perhaps it is something wrong with the apparence, I do not know?

I also have problems to regulate the size of the spreadangle and
concentration
of the light. If I change it it doesn't seems to have any effect. This code
is
inside the class GasLight.

So if you would like to test it, and see if you can help me out. I would be
greatfull. Below is all the code, ready to be compiled and lounched.

To turn of the the directionlight and the ambientlight just comment the
line:
simpleUniverse.addBranchGraph( addLight() );

Best regards
Fredrik Andersson

The code
import java.applet.*;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.loaders.*;
import java.io.*;

import java.util.*;

public class TestLight extends Applet
{
       Canvas3D canvas3D;
       SimpleUniverse simpleUniverse;

       public void init()
       {
               long start = System.currentTimeMillis();
               setLayout(new BorderLayout());
               GraphicsConfiguration config = 
SimpleUniverse.getPreferredConfiguration();
               canvas3D = new Canvas3D(config);
               add("Center", canvas3D);
               simpleUniverse = new SimpleUniverse(canvas3D);

               //Comment this row to get rid of other light.
               simpleUniverse.addBranchGraph( addLight() );

               //Light left
               Transform3D transform3D1 = new Transform3D();
               transform3D1.set( new Vector3d( -4.0, -1.25, -13.0 ) );
               GasLight gasLight1 = new GasLight( transform3D1 );
               BranchGroup branchGroup1 = gasLight1;

               //Light middle
               Transform3D transform3D2 = new Transform3D();
               transform3D2.set( new Vector3d( 0.0, -1.25, -13.0 ) );
               GasLight gasLight2 = new GasLight( transform3D2 );
               BranchGroup branchGroup2 = gasLight2;

               //Light right
               Transform3D transform3D3 = new Transform3D();
               transform3D3.set( new Vector3d( 4.0, -1.25, -13.0 ) );
               GasLight gasLight3 = new GasLight( transform3D3 );
               BranchGroup branchGroup3 = gasLight3;

               //Ground left
               BranchGroup branchGroup4 = new BranchGroup();
               Transform3D transform3D4 = new Transform3D();
               TransformGroup cylinder = new TransformGroup();
               transform3D4.setTranslation( new Vector3d( -4.0, -1.25, -13.0 ) );
               cylinder.setTransform(transform3D4);
               Cylinder groundCylinder = new Cylinder(2.0f, 0.01f);
               cylinder.addChild(groundCylinder);
               branchGroup4.addChild(cylinder);

               //Ground middle
               BranchGroup branchGroup5 = new BranchGroup();
               Transform3D transform3D5 = new Transform3D();
               TransformGroup box = new TransformGroup();
               transform3D5.setTranslation( new Vector3d( 0.0, -2.25, -13.0 ) );
               box.setTransform(transform3D5);
               Box groundBox = new Box();
               box.addChild(groundBox);
               branchGroup5.addChild(box);

               //Ground right
               BranchGroup branchGroup6 = new BranchGroup();
               TransformGroup ground = new TransformGroup();
               Transform3D transform3D6 = new Transform3D();
               transform3D6.set( new Vector3d( 3.0, -1.25, -14.0 ) );
               ground.setTransform(transform3D6);
               Ground groundPlate = new Ground(2.0f, 0.0f, 2.0f);
               ground.addChild(groundPlate);
               branchGroup6.addChild(ground);

               simpleUniverse.addBranchGraph( branchGroup1 );
               simpleUniverse.addBranchGraph( branchGroup2 );
               simpleUniverse.addBranchGraph( branchGroup3 );
               simpleUniverse.addBranchGraph( branchGroup4 );
               simpleUniverse.addBranchGraph( branchGroup5 );
               simpleUniverse.addBranchGraph( branchGroup6 );


long end = System.currentTimeMillis(); System.out.println((end-start)/1000); }

       private BranchGroup addLight()
       {
               BranchGroup branchGroup = new BranchGroup();
               BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),
100.0);

               AmbientLight ambientLight = new AmbientLight(new Color3f(1f, 1f, 1f));
               ambientLight.setInfluencingBounds(bounds);

               DirectionalLight directionalLight = new DirectionalLight(new Color3f(1f,
1f,
1f), new Vector3f( 0.0f, 0.0f, -1.0f ));
               directionalLight.setInfluencingBounds(bounds);

               branchGroup.addChild(ambientLight);
               branchGroup.addChild(directionalLight);

               branchGroup.compile();
               return branchGroup;
       }

       public static void main(String[] args)
       {
               Frame frame = new MainFrame(new TestLight(), 800, 400);
   }
}

class GasLight extends BranchGroup
{
       public GasLight(Transform3D transform3D)
       {
               TransformGroup holeTransformGroup = createTransformGroup();
               holeTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
               holeTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
               holeTransformGroup.setTransform(transform3D);
               setCapability(BranchGroup.ALLOW_CHILDREN_READ );

       addChild( holeTransformGroup );

       compile();
       }

       public TransformGroup createTransformGroup()
       {
               TransformGroup holeTransformGroup = new TransformGroup();
               Transform3D transform3D = new Transform3D();

               //LowerCylinder
               TransformGroup lowerCylinder = new TransformGroup();
               transform3D.setTranslation( new Vector3d( 0.0, 0.5, 0.0 ) );
               lowerCylinder.setTransform(transform3D);
               lowerCylinder.addChild(new Cylinder(0.2f, 1.0f));

               //UpperCylinder
               TransformGroup upperCylinder = new TransformGroup();
               transform3D.setTranslation( new Vector3d( 0.0, 2.0, 0.0 ) );
               upperCylinder.setTransform(transform3D);
               upperCylinder.addChild(new Cylinder(0.1f, 2.0f));

               Appearance appearance = new Appearance();
               ColoringAttributes coloringAttributes = new ColoringAttributes(new
Color3f(1.0f, 1.0f, 1.0f), ColoringAttributes.FASTEST);
               appearance.setColoringAttributes(coloringAttributes);

               //LightBulb
               TransformGroup lightBulb = new TransformGroup();
               transform3D.setTranslation( new Vector3d( 0.0, 3.0, 0.0) );
               lightBulb.setTransform(transform3D);
               Sphere lightBulbSphere = new Sphere(0.3f);
               lightBulbSphere.setAppearance(appearance);
               lightBulb.addChild(lightBulbSphere);

               //Top
               TransformGroup top = new TransformGroup();
               transform3D.setTranslation( new Vector3d( 0.0, 3.3, 0.0 ) );
               top.setTransform(transform3D);
               top.addChild(new Cone(0.4f, 0.2f));

               holeTransformGroup.addChild(lowerCylinder);
               holeTransformGroup.addChild(upperCylinder);
               holeTransformGroup.addChild(lightBulb);
               holeTransformGroup.addChild(top);
               holeTransformGroup.addChild(createSpotLight());

               return holeTransformGroup;
       }

       private SpotLight createSpotLight()
       {
               BoundingSphere boundingSphere = new BoundingSphere( new Point3d(0.0, 0.0
,0.0), 0.5);
               Point3f pos = new Point3f(0.0f, 3.0f, 0.0f);
       float spread = 0.5f;
       float concentration = 50.0f;

       SpotLight spotLight = new SpotLight();
       spotLight.setDirection(new Vector3f( 0.0f, -1.0f, 0.0f ));
       spotLight.setInfluencingBounds(boundingSphere);
       spotLight.setPosition(pos);
       spotLight.setSpreadAngle(spread);
       spotLight.setConcentration(concentration);
       return spotLight;
   }

   public Appearance createMatAppear()
   {
       Appearance appear = new Appearance();
       Material material = new Material();
       material.setDiffuseColor(new Color3f(0.5f, 0.5f, 0.5f));
       material.setSpecularColor(new Color3f(0.8f, 0.8f, 0.8f));
       material.setShininess(10.0f);
       appear.setMaterial(material);

       return appear;
   }
}

class Ground extends Shape3D
{

       public Ground(float xLength, float yHeight, float zLength)
       {
               QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES |
GeometryArray.TEXTURE_COORDINATE_2);

               Point3f point3f = new Point3f(0,  yHeight, 0);
               quadArray.setCoordinate(0, point3f);

               point3f.set(0, yHeight, zLength);
               quadArray.setCoordinate(1, point3f);

               point3f.set(xLength, yHeight,  zLength);
               quadArray.setCoordinate(2, point3f);

               point3f.set(xLength,  yHeight, 0);
               quadArray.setCoordinate(3, point3f);

               setAppearance(createMaterialAppearance());

               setGeometry(quadArray);
       }

       public Appearance createMaterialAppearance()
       {
               Appearance appearance = new Appearance();
               ColoringAttributes coloringAttributes = new ColoringAttributes(new
Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
               appearance.setColoringAttributes(coloringAttributes);

               PolygonAttributes polygonAttributes = new PolygonAttributes();
               polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
               polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
               polygonAttributes.setBackFaceNormalFlip(true);
               appearance.setPolygonAttributes(polygonAttributes);

               Material material = new Material();
               material.setDiffuseColor(new Color3f(0.0f, 0.0f, 0.5f));
               appearance.setMaterial(material);

               return appearance;
       }
}

_________________________________________________________________
Auktioner: Tjäna en hacka på gamla prylar http://tradera.msn.se

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