Java Reference
In-Depth Information
Figure 6. Second code segment for J3d.java
25. System.out.println("1-Directional light");
26. System.out.println("2-Point light");
27. System.out.println("3-Spot light");
28. System.out.println("other-Ambient light");
29. in = new BufferedReader(new InputStreamReader(System.in));
30. try { String userInput = in.readLine(); light=Integer.parseInt(userInput); }
31. catch (IOException ioe) { System.out.println("IO error trying to read input!"); System.exit(1); }
32.
33.
// create light source at the origin
34.
lit = new AmbientLight(new Color3f( 0.5f, 0.5f, 0.5f));
35.
if (light==1){ lit = new DirectionalLight(new Color3f( 0.5f, 1.0f, 0.5f), new Vector3f( 0.2f,-0.5f,-1.0f));}
36.
if (light==2){ lit = new PointLight(new Color3f(1,1,1), new Point3f(0,0,0), new Point3f(1,1,0));}
37.
if (light==3){ lit = new SpotLight(new Color3f(1f,1f,1f), new Point3f(0.0f,0.0f,0.0f),
38.
new Point3f(1.0f,1.0f,0.0f), new Vector3f(0.0f,0.0f,1.0f), (float)(Math.PI/2.0f),0f);}
39.
lit.setInfluencingBounds(new BoundingSphere());
40.
41.
// make the light source move around the box
42.
Alpha alpha = new Alpha(-1,5000);
43.
Transform3D y = new Transform3D();
44.
float knots[] = {0.0f , 0.5f , 1.0f};
45.
Point3f pts[] = { new Point3f(-2f,0f, 2f), new Point3f( 2f,0f, 2f), new Point3f(-2f,0f, 2f)};
46.
PositionPathInterpolator posinterpolator = new PositionPathInterpolator(alpha,sphere1,y,knots,pts);
47.
RotationInterpolator rotinterpolator = new RotationInterpolator(alpha,sphere2);
48.
posinterpolator.setSchedulingBounds(new BoundingSphere());
49.
rotinterpolator.setSchedulingBounds(new BoundingSphere());
50.
sphere2.addChild(rotinterpolator);
51.
sphere1.addChild(posinterpolator);
52.
sphere2.addChild(shape);
53.
sphere2.addChild(lit);
54.
sphere1.addChild(sphere2);
55.
sphere1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
56.
sphere2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
57.
root.addChild(sphere1);
58.
return root;
59. }
direction of the light is to be changed during run time. Also, care must be taken to ensure
that the vertex and face normal of the objects being illuminated correctly has been properly
set up, else biasing of the reflections may take place.
Figure 8 shows the result obtained from the code segment of Figures 5 and 6 when a
directional light is selected. Comparing with the results in Figure 7 for ambient light, the
directional characteristics of the light can be easily seen. Specifically, rather than the entire
object, only the appropriate sides of the plane polygons have been lightened up.
Search WWH ::




Custom Search