gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Server Racks
PDA Phones & Cases
Phone Cards
Best Price
Logo Design Custom
GPS Devices
KVM over IP
Boat Donations
Televisions
Career Education
Promotional Gifts
Web Hosting Directory
Car Donations
GPS

 


  Managing the Modern Network
Sponsored by HP
In a global economy where information crosses the globe in an instant, and where Web-based applications power business, it's more important than ever to ensure your network is safe from threats and optimized to deliver the data your business needs. »
 
  Business Service Management: Generate Revenue Through IT
Sponsored by HP
IT must now help organizations attract, retain and grow customer relationships and increase customer satisfaction. Business service management (BSM) helps lay the foundation by managing services in dynamic support of business requirements. Learn more. »
 
  Evaluating Software as a Service for Your Business
Sponsored by Webroot
Is Software as a Service just hype, or is something really going on here? See if your company can benefit as SaaS tries to change the face of the enterprise. »
 
  Storage Networking: Configuration and Planning
Sponsored by HP
The most critical part of setting up a SAN is configuring each individual disk array. This guide examines configurations for SAN-attached servers and disk arrays, and looks at the future of IP storage. »
 
  Is Your Disaster Recovery Plan Good Enough?
Sponsored by HP
Preparing for a disaster is more often than not part of the storage planning process, and it is one of the most difficult tasks, since it includes local hardware and software, networking equipment, and a test plan. Learn how to get disaster recovery right. »
 
Related Article -
Understanding the Discrete Cosine Transform in Java
Wireless Home Security and Java
Using the Full-Screen Exclusive Mode API in Java
Processing Image Pixels, Applying Image Convolution in Java, Part 2
Processing Image Pixels, Applying Image Convolution in Java
Processing Image Pixels, Understanding Image Convolution in Java
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Understanding Lighting in the Java 3D API
By Richard G. Baldwin

Java Programming Notes # 1540


Preface

This is the first part of a multi-part lesson designed to help you understand the use of lighting and illumination in 3D scenes produced using the Java 3D API.

Optical illusions

Creating scenes that appear to be three-dimensional on a flat two-dimensional screen is all about creating optical illusions.  There are many important aspects to creating these optical illusions in 3D programming, not the least of which is lighting and illumination.

A 3D simulator program

In this lesson, I will teach you about, and show examples of many of the important features of scene illumination in the Java 3D API.  I will also provide the source code for a complete Java 3D lighting simulator program that you can compile and run to experiment with light in the Java 3D API.  I will explain how that program works in subsequent parts of this multi-part lesson.

Viewing tip

You may find it useful to open another copy of this lesson in a separate browser window.  That will make it easier for you to scroll back and forth among the different figures, tables, and listings while you are reading about them.

Supplementary material

I recommend that you also study the other lessons in my extensive collection of online Java tutorials.  You will find those lessons published at Gamelan.com.  However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there.  You will find a consolidated index at www.DickBaldwin.com.

Technical Discussion

The Program

The program that I will present in this lesson can be used to experiment with most of the lighting and illumination features of the Java 3D API either individually or in combination.

In order to compile and run this program, you will need to download and install the Java 3D API.  As of the date of this writing, it was available at: http://java.sun.com/products/java-media/3D/.  The online documentation was available at: http://download.java.net/media/java3d/javadoc/1.4.0/.

In addition, you will need to download and install either Microsoft DirectX or OpenGL.

Our perception of 3D visual objects

From time to time, I will make references to Chapter 6 of a document published by Dennis J. Bouvier at the following URL.  http://java.sun.com/developer/onlineTraining/java3d/.  For example, here is a quotation from Bouvier:

"In the real world, the colors we perceive are a combination of the physical properties of the object, the characteristics of the light sources, the objects' relative positions to light sources, and the angle from which the object is viewed.  Java 3D uses a lighting model to approximate the physics of the real world."

The mathematical 3D model

According to Bouvier, Section E.2 of the Java 3D API Specification presents the mathematical equations of the Java 3D lighting model.  Still quoting Bouvier,

"The lighting model equation depends on three vectors: the surface normal (N), the light direction (L), and the direction to the viewer's eye (E) in addition to the Material properties of the object and the light characteristics."

This program makes it easy for the user to vary the surface normal (indirectly), the light direction, and the Material properties of the object.  The program does not make it easy for the user to modify the direction to the viewer's eye.  However, the primary experimental object is a large sphere which, because of its curvature, inherently results in a wide range of directions from the object's surface to the user's eye.

Light sources

Returning to Bouvier,

"The lighting model incorporates three kinds of real world lighting reflections: ambient, diffuse, and specular.  Ambient reflection results from ambient light, constant low level light, in a scene.  Diffuse reflection is the normal reflection of a light source from a visual object.  Specular reflections are the highlight reflections of a light source from an object, which occur in certain situations."

This program makes it easy for the user to experiment with all three types of sources, plus another form of illumination referred to as emissive color.

Two user interfaces

This program produces two user interfaces.  One interface is the display of a 3D scene containing five spheres as shown in Figure 1.


Figure 1

(Note that the importation of the image into this HTML document introduced some visual artifacts that were not present in the original image produced by the program.)

The four small spheres

Four of the spheres are small with fixed reflective surface properties and a fixed number of facets.  When illuminated with white light, or colored light corresponding to the color of the sphere, these spheres appear as white, red, green, and blue as shown in Figure 1.

The larger fifth sphere

The fifth sphere is larger.  Almost everything about this sphere (other than its size and location) can be modified by the user through the second user interface shown in Figure 2.


Figure 2

The slider section

The user interface in Figure 2 consists of three main sections.  The section at the top contains three sliders that are used to adjust the values of selected scene properties.  The values indicated by the sliders are tied directly to the display shown in Figure 1, and the display changes in real time as each slider is adjusted to indicate different values.

A grid of radio buttons and text fields

The large middle portion of the interface contains a grid of radio buttons and text fields.  The radio buttons are used to select the scene properties that will be modified by adjusting the sliders at the top of the user interface.

When a radio button is selected, the label above each slider changes to appropriately reflect the value of the scene property currently assigned to the slider.  In addition, the values displayed in the text fields to the right of the selected radio button display the values indicated by the sliders, and the column headers for the text fields change to appropriately reflect the values of the scene properties currently associated with the selected sliders.

Shading and size

The four radio buttons at the bottom of the user interface are used to select between:

  • Gouraud and flat shading
  • Large display and small display

Figure 1 was produced as a large display using Gouraud shading.  You will see examples of both types of shading as well as large and small displays later in this lesson.

More than 50 scene property values

The user can vary the surface reflective properties of the large sphere and can also modify various properties of the lights shown in Figure 1.

The user interface in Figure 2 provides more than 50 scene property values that can be modified by the user to change the reflective properties of the large sphere and the various properties of the lights.  The ability for the user to modify the reflective surface properties of the large sphere makes it possible for the user to view the effects of those modifications in conjunction with varying light conditions.

Surface properties of the large sphere

The user can control almost all aspects of the following surface properties of the large sphere:

  • The number of facets on the surface of the sphere.
  • The type of shading: Gouraud shading versus Flat shading.
  • The shininess of the sphere.
  • The emissive color and intensity of the sphere.
  • The ambient color and intensity of the sphere.
  • The diffuse color and intensity of the sphere.
  • The specular color and intensity of the sphere.

Five independent light sources

In addition, the user has control over the following five lights along with the properties shown:

  • One AmbientLight object (color and intensity).
  • Two independent DirectionalLight objects (color, intensity, and direction).
  • One PointLight object (color, intensity, location, and attenuation).
  • One SpotLight object (color, intensity, direction, location, attenuation, spreading angle, and concentration).

An almost infinite number of combinations

Given the many properties of the five light sources and the many reflective properties of the spheres, the lights can be combined with the spheres to illuminate the scene in an almost infinite number of ways.

For example, having specified the various surface properties of the large sphere, the user can vary the properties of the lights and observe the manner in which the lights interact with the five spheres to illuminate the scene.

Real-time control

The properties of the large sphere and the lights are varied by selecting a radio button (see Figure 2) that corresponds to the property of interest and then adjusting the three sliders at the top of Figure 2.  The display of the scene changes in real time as the user adjusts the sliders, making it possible to easily view the effects of fixing all of the properties but one and then varying that property across a range of values by moving a slider.

Large or small display

The user interface shown in Figure 2 also makes it possible for the user to select between a large and a small display.  This was done mainly to support my publishing effort as I import the images into this tutorial lesson.  Figure 1 shows the large display.  You will see examples of the small display later.

Testing

This program was tested using J2SE 5.0, the Java 3D API version 1.4.0, WinXP and Microsoft DirectX.  Note that it is also possible to use OpenGL instead of DirectX, but I haven't tried it.

The Number of Facets

Normally we would like to think that the silhouette of a sphere is a perfect circle.  In the ideal case, that would be true.  However, 3D computer graphics do not often reflect the ideal case.

How to draw a circle

When you were in elementary school, you probably learned how to use a compass to draw a circle.  If you did it correctly, the circles were nice and smooth.  Unfortunately, however, there are no compasses inside computers.  Typically when we draw a circle with a computer, we compute the values that represent the circle at a set of regularly spaced points around the circumference of the circle and then connect those points with straight line segments.  The closer the points are spaced, the better will be the representation of the circle.

How to draw a sphere

Similarly, when we draw a sphere with a computer, we typically compute the values representing the surface of the sphere at a set of regularly spaced points on the surface of the sphere and then connect those points with straight line segments.  Those lines form the edges of flat polygons and it is this structure built from flat polygons that constitutes our representation of the sphere.  Just as in the case of the circle, the closer together the points are on the surface of the sphere, the better will be our representation of the sphere.

The result is that our computer generated sphere will have facets much like the facets on a diamond.

Divisions in the Sphere constructor

One of the overloaded constructors for the Sphere class in the Java 3D API has a parameter that Sun refers to as divisions.  This parameter seems to specify the number of flat polygons that would be encountered in making one trip around the equator of the sphere.  This is illustrated by the top two images in Figure 3.

Figure 3

Silhouettes of a sphere

The upper-left image in Figure 3 shows the silhouette of a Sphere object constructed with a value of 4 for divisions.  Granted it doesn't look much like a sphere.  It looks like a square instead.  However, that is what you get if you draw a circle by using straight line segments to connect only four points on the circumference of a circle.

The silhouette in the upper right of Figure 3 looks more like what we would expect to see for a sphere.  This is what you get when you use straight line segments to connect eight equally spaced points on the circumference of the circle.  It should be apparent that if I continue to increase the number of points on the circumference of the circle, the result will look more and more like a circle.

The Facets radio button

Returning now to Figure 2, the top radio button is labeled Facets.  The text box immediately to the right of the radio button contains the value 50.0.  That is the value that was used to draw the large sphere in Figure 1.  In other words, during a trip around the equator of the large sphere in Figure 1, you would encounter 50 flat spots.  The two top images in Figure 3 were produced with values of 4 and 8 respectively for the number of facets.

How to adjust the number of facets

To adjust the number of facets, you first select the Facets radio button.  This causes the label above the top slider to read:

Facets on Surface of Large Sphere

You then move the slider to indicate the number of facets that you want your sphere to have, reading the output in the text field to the right of the radio button.  The scale on the slider ranges from 0 to 100.  However, if you attempt to specify a value less than 4 with the slider, the number of facets is clamped to 4.

An example display that shows the facets

Now consider the bottom two images in Figure 3.  These are the same two spheres shown as silhouettes in the top two images.  However, the spheres in the bottom two images were not rendered as silhouettes.  Rather, they were rendered in a three-dimensional form instead.

Vertices and polygons

Hopefully you can see some of the regularly-spaced points on the surface of the spheres (commonly referred to as vertices) as well as the connecting straight lines and the planar polygons that are formed by the vertices and the lines that connect them.  It is these planar polygons that I refer to as facets. 

Note, however, that even though the lower right sphere in Figure 3 was created with a Facet value of 8, the sphere actually has more than eight actual facets.  When creating a sphere, the value that is specified for Facets in Figure 2 is the number of planar polygons that you would encounter in one trip around the equator of the sphere.

(Once again, I see some visual artifacts in the images in Figure 3 that were caused by importing them into this HTML document.  I don't know if you will see them or not.  However, they aren't in the original images so if you compile and run the program, you should not see them.)

How to use the program

Now you basically know how to use the program.  To specify the number of facets for the large sphere in Figure 1, select the radio button labeled Facets and then use the top slider to specify the number of facets that occur during one trip around the equator of the sphere.  This same procedure is used to adjust all of the scene properties except that many of the scene properties have three property values instead of only one property value.

The Shading Model

Now that you know about the planar polygons (facets) that comprise the surface of the sphere, the next thing that you need to learn about is the shading model.  In a nutshell, the shading model is used to determine the colors that will appear at the different points on each facet.

Two techniques for shading

Java 3D gives you two choices for determining those colors:

I'm not going to attempt to explain the technical details of these two shading techniques.  Rather, I will simply suggest that you click on the two links provided in the above list to read what Wikipedia has to say about them.  If you feel that you need more information on the topic after you read those explanations, go to Google and search for the keywords.  I'm confident that you will find more material on the topic than you will have the time to read.

Example of Gouraud and Flat shading

What I am going to do, however, is to show you an extreme example of the difference between the two shading techniques and show you how to select one or the other when you are running the program.

The image on the left in Figure 4 was produced using Gouraud shading.  The image on the right was produced by applying Flat shading to the same 3D sphere.

Figure 4

(Note that I made some minor color adjustments to the rightmost image in Figure 4 to cause the two dark brown polygons with the Flat shading to be distinguishable from the black background.)

Another example

I confess that I chose the example in Figure 4 specifically to emphasize the difference between Gouraud and Flat shading.  However, that is not the case for Figure 5.

Figure 5

Figure 5 shows two renderings of the same large sphere.  This is simply a red sphere with a Facets value of 50 being illuminated by a directional-light source located somewhere above my right shoulder and aimed directly at the center of the large sphere.  The image on the left was rendered using Gouraud shading and the image on the right was rendered using Flat shading.  Even with this relatively large number of facets, the Flat shading approach causes the facets to be very visible on the large sphere.

Not a high quality shading technique

As you can see, Flat shading is probably not what you would want to use for high quality 3D representations of visual objects.  However, depending on your hardware, it may be much faster to compute Flat shading than Gouraud shading, so you may need to use it on some objects for improved speed if speed is an issue in your 3D applications.

Switching between the two shading techniques

Once you have established the scene properties and your scene is visible in the format of Figure 1, all you need to do to switch between Gouraud shading and Flat shading with this program is to select the appropriate radio button at the bottom of the control panel shown in Figure 2.  In a subsequent installment of this lesson, I will teach you how to write the Java code to switch between the two types of shading.

The Small Spheres

Figure 6 gives us another look at the four small spheres.


Figure 6

Calibration spheres

I placed these spheres in the scene to serve a calibration function.  Each sphere has fixed surface reflection properties (except that they do switch between Gouraud shading and Flat shading in accordance with the selected shading button).  As I add new light sources to the scene for the purpose of illuminating the large sphere shown in Figure 1, I can check to see how the small spheres are being illuminated by those same light sources.  The results should be very predictable.

Locations of the spheres

The large sphere in Figure 1 is centered at the origin in the three-dimensional space having coordinates of x, y, and z.  The x-axis is the horizontal axis with positive values to the right.  The y-axis is the vertical axis with positive values going up the screen.  The z-axis is perpendicular to the screen with positive values coming out of the screen toward the viewer.

The location coordinates for the four small spheres are given in Table 1 later in this lesson.  The red and blue spheres in Figure 6 each have their centers on the z-plane.  In other words, their z coordinate value is 0.0.  A line drawn through their centers would lie on the z-plane and would go through the origin of the z-plane.  However, as you can see, their x and y coordinates are not zero.  Rather, the line connecting their centers would be at an angle of 45-degrees relative to the x-axis.

The locations of the white and green spheres is such that they lie on a tilted plane that goes through the origin and also goes through the line connecting the centers of the red and blue spheres.  The white sphere is closest to the viewer (positive z-coordinate value) while the green sphere is further away from the viewer (negative z-coordinate value).  If you were to view the four small spheres from a vantage point perpendicular to that plane, they would lie on the four corners of a square inscribed on that plane.

Location and nature of the light source

While the surface reflection properties and the locations of the four small spheres are fixed, the locations of light sources that can be used to illuminate them are not fixed.

The light source that is illuminating the four spheres in Figure 6 is an invisible omnidirectional source of white light located at the origin in the three-dimensional space.  This places the light source in the middle of the four spheres.  As you can see, the appropriate portion of each of the spheres is illuminated for an omnidirectional-light source at that location.

I'm unaware of any source of light in the physical world that behaves this way.  The light source itself is invisible, but the impact of that light source on the spheres is visible.

A heat analogy

This is sort of like having an omnidirectional source of heat at the origin that causes the spheres to glow in their own distinctive colors when they become warm.  (It would probably be possible to build a system in the physical world that behaves in this fashion.)

For the record, this light source is an object of the PointLight class, which I will discuss in more detail later.

The scene properties

The scene properties for the four small spheres are shown in Table 1.

 

White

Red

Green

Blue

Facets 50 50 50 50
Shininess 128 128 128 128
Emissive Color 0.1, 0.1, 0.1 0.1, 0.1, 0.1 0.1, 0.1, 0.1 0.1, 0.1, 0.1
Ambient Color 1.0, 1.0, 1.0 1.0, 0.0, 0.0 0.0, 1.0, 0.0 0.0, 0.0, 1.0
Diffuse Color 1.0, 1.0, 1.0 1.0, 0.0, 0.0 0.0, 1.0, 0.0 0.0, 0.0, 1.0
Specular Color 1.0, 1.0, 1.0 1.0, 1.0, 1.0 1.0, 1.0, 1.0 1.0, 1.0, 1.0
Coordinates -0.5, -0.5, 0.5 -0.5, 0.5, 0.0 0.5, 0.5, -0.5 0.5, -0.5, -0.0
Table 1

The column headers identify each of the spheres according to the dominant colors of the spheres in Figure 6.  The row headers identify each of the scene properties in the same order as Figure 2 with the addition of the location coordinates of the spheres in the bottom row.

Color information

For the rows that contain color (surface reflection) information, the three values in each cell correspond to the values for red, green, and blue respectively.

(In Java 3D, color values are given in the range from 0.0 to 1.0 as opposed to the possibly more familiar 8-bit integer range from 0 to 255.)

The White sphere

For example, the information in the Diffuse Color cell for the White sphere indicates that this sphere will reflect red, green, and blue light from a diffuse light source equally well.

  • red = 1.0
  • green = 1.0
  • blue = 1.0

Therefore, when this sphere is illuminated by a diffuse source of white light, it will appear to be white or gray depending on the intensity of the light.

The Red sphere

On the other hand, the information in the Diffuse Color cell for the Red sphere indicates that it will reflect red light very well, but won't reflect green or blue light at all.

  • red = 1.0
  • green = 0.0
  • blue = 0.0

Therefore, when this sphere is illuminated by a diffuse source of white light, it will appear to be some shade of red depending on the intensity of the light.  The same is true if it is illuminated by a diffuse source of red light.

Location coordinate information

For the bottom row containing location coordinate information, the values in each cell correspond to the x, y, and z coordinates values respectively.  These coordinates specify the locations of the four small spheres that I attempted to describe verbally earlier.

Similarities and differences between the small spheres

Much of the information in Table 1 will mean more to you as we proceed through an explanation of the various scene properties.  For now, suffice it to say that many of the values are the same among the four spheres.  The major differences between the small spheres are the ambient color, the diffuse color, and the location coordinates.  The emissive color and the specular color of all four spheres are the same, as are the number of facets and the shininess.

Emissive Color

That brings us to the scene property that is probably the easiest to deal with:  Emissive Color.

Here is part of what Wikipedia has to say about emissive light (not necessarily with respect to the Java 3D API):

"The materials also specify "emissive light" for the polygons. The emissive light is light that the polygon itself emits (for example your monitor). In case the polygon is emissively lit you can think of it as a visible light source, which is NOT emitting light to the surrounding objects!"

Here is some of what Bouvier has to say about emissive color, specifically with regard to the Java 3D API:

"The Material object allows the specification of an emissive color. This can be used to create the effect of a
glow-in-the-dark object. Having an emissive color does not make the visual object a light source; it will not
illuminate other visual objects."

A stand-alone scene property

The emissive color scene property is easy to deal with because, unlike many of the other scene properties, it stands completely on its own and doesn't require interaction with other scene properties.  As described above, a visual object can emit its own light.  This is controlled by the emissive color scene property shown in Figure 2.  When a visual object emits its own light, it does not illuminate adjacent objects.

Unlike the physical world

Once again, I am unaware of any source of light in the physical world that behaves in this manner.  Most illuminated objects that I am aware of will cause adjacent objects to also become illuminated to some extent.

Color and intensity is variable

In any event, given that restriction, the emissive color scene property can be used to cause an object to be illuminated in any color at any of the allowable intensity levels.  This is shown in Figure 7 where the large sphere is illuminated with four different colors and intensity levels by emitting its own light.

Figure 7

Small spheres are not illuminated by the large sphere

Note that the small spheres in the scene in Figure 7 are not illuminated by the light being emitted by the large sphere.  However, if you look very carefully, you may be able to see that each of the small spheres also emits a small amount of light on its own.  This is indicated by the values in the Emissive Color row of Table 1.

No shading, Gouraud or otherwise

Also note that even though the visual objects in Figure 7 are spheres, there is no shading to create an optical illusion causing them to look like spheres.  Rather, they look more like colored disks.  Visual objects that are illuminated by emitting their own light are not shaded in the Java 3D API.  Rather, the entire surface of the object is illuminated uniformly.

How to use emissive light

To illuminate the large sphere by causing it to emit its own light, select the Emissive Color radio button in Figure 2.  Then adjust the three sliders to cause the color mix and the intensity of the illumination to be what you need.

The color values that you specify by adjusting the sliders will be displayed in the three text fields next to the radio button in real time as you adjust the sliders.  Also, the color and intensity of the large sphere in the display will change in real time as you adjust the sliders.

Ambient Reflection and the AmbientLight Class

Things are about to get a little more complicated.  Up to now, just about everything that we have done has been done on a standalone basis.  However, from this point forward, you will have to think in terms of the reaction of a surface having specified reflection properties to a light source having its own set of light-radiation properties.

Light sources and reflections

We saw earlier what Bouvier has to say about the three kinds of real-world lighting reflections, and the simulation of those three kinds of reflections in the lighting model of the Java 3D API:

  • Ambient reflections
  • Diffuse reflections
  • Specular reflections

At this point, we are interested in ambient reflections only.  We will take up diffuse and specular reflections later in the lesson.

A link between ambient color and ambient light

Figure 2 contains the following two radio buttons, which are the main topic of this portion of the lesson:

  • Ambient Color
  • AmbientLight

The Ambient Color radio button makes it possible for you to control a set of surface reflection properties that will cause the large sphere to react only to light radiated from an object constructed from the AmbientLight class.

The AmbientLight radio button makes it possible for you to control a set of properties belonging to an object constructed from the AmbientLight class.

What is ambient light?

In the Java 3D API, ambient light is light that arrives uniformly from all directions.  All visual objects in a scene that is illuminated by ambient light will be uniformly illuminated according to their surface reflection properties for ambient light.

A real-world example

As a real-world example, a sphere illuminated by ambient light is similar to what you would experience if you were to hang a basketball from a clothes line in an open field on a very cloudy day.  To a large extent, the basketball would be uniformly illuminated over its entire surface (although the top may be a little brighter than the bottom).

Although the sun would have been the original (highly directional) source of the light, that light would have been so heavily filtered, scattered, and reflected by the cloud layer, the ground, and the other physical objects in the region that the light would appear to come from all directions uniformly.  As a result, there would be no shadows and the entire surface of the basketball would be almost uniformly illuminated.  Light sources constructed from the AmbientLight class in the Java 3D API simulate this kind of light with the illumination being completely uniform over the surface of illuminated objects.

How does ambient color (reflection) fit into this picture?

Ambient surface reflection properties and light radiated from AmbientLight objects are uniquely tied together.  Both must exist with non-zero states for either to have an impact on the scene.  If there is no ambient light illuminating the scene, changing the AmbientLight surface reflection properties of a visual object will have no impact on the scene.  Conversely, if there are no visual objects in the scene having non-zero values for the ambient surface reflection properties, even high intensity ambient light sources will have no impact on the scene.

Examples of ambient light

Four examples of the interaction between ambient surface reflection properties and an ambient light source are shown in Figure 8.

Figure 8

The ambient light source

For all four images shown in Figure 8, the color and intensity properties of the ambient light source were:

  • red = 0.50
  • green = 0.75
  • blue = 1.00

This is colored light.  It is not white light because there are different intensity levels from each of the primary colors.

The large sphere is not initially visible

The large sphere is not visible in the upper-left image in Figure 8.  This is because all three ambient reflection properties were set to 0 for the large sphere for this image.  However, the four small spheres are visible.  This is because each of the small spheres has non-zero values for its ambient reflection properties as shown in the Ambient Color row of Table 1.

Reflection color properties react to light source colors

In order for a visual object to be visible when subjected to any light source in the Java 3D API, the reflection color properties of the object must contain color components that are also contained in the light.  For example, a visual object having only red reflection properties subjected to a light source having only green or blue color components will appear to be black.

When there is a color match between a color component in the surface reflection properties and a color component in the light source, the numeric values of the matching color components are used to determine the intensity of that color component in the resulting overall color of the visual object.

Ambient reflection properties react to ambient light source

The AmbientLight color values for the light source combined with the ambient reflection properties for each of the small spheres resulted in the colors shown for the small spheres in each of the images in Figure 8.

For example, the small sphere in the lower left of each image doesn't appear to be white as was the case in Figure 1.  Rather, even though the ambient reflection properties of this sphere contain the maximum possible values for red, green, and blue, (and the sphere would appear to be white when subjected to white light) in this case it was subjected to colored light and therefore appears to be more blue than white.

Add a red reflection property to the large sphere

The red ambient reflection property value for the large sphere was increased from 0.0 to 1.0 for the upper-right image in Figure 8.  This caused the large sphere to become visible with a somewhat subdued red color.

(Even though the value of the red reflection property for the large sphere was increased to its maximum possible value, the contribution of red light in the source was only at half its maximum value, resulting in a red color with subdued intensity.)

Add some green and blue reflection capability

For the lower-left image in Figure 8, the green ambient reflection property value for the large sphere was increased from 0.0 to 0.75 producing the olive drab color shown.

For the lower-right image, the blue ambient reflection property value for the large sphere was increased from 0.0 to 0.5, producing the gray color shown.

Reversed property value order

After increasing the ambient reflection property values for the large sphere as described above, the value order of the ambient reflection property values for the large sphere was exactly the reverse of the value order of the ambient color property values for the light source as shown below:

  • Sphere:  red = 1.0, green = 0.75, blue = 0.5
  • Source:  red = 0.5, green = 0.75, blue = 1.0

This resulted in the large sphere appearing to be colored gray in the lower-right image of Figure 8.  The color gray suggests that the color property values from the light source were combined with the color property values from the large sphere in such a way as to produce nearly equal contributions of each of the primary colors in the final color.  So far, I have been unable to find the specifications as to how color values in the reflection properties are combined with color values in the light source.  However, I suspect that they are simply multiplied together.  If so, that would result in a final overall color for the large sphere with the following color component values:

red = 0.5, green = 0.56, blue = 0.5

As suggested above, these color component values are nearly equal, which would result in a gray color.

This further demonstrates that the color of a visual object depends on a combination of the reflective color properties of the object and the radiated color properties of the light source.

No shading

Another important aspect of ambient light is also illustrated by Figure 8.  In particular, there is no shading with ambient light.  This is because the ambient light is assumed to originate uniformly from all directions simultaneously so that the entire surface of the visual object is assumed to be uniformly illuminated.

Doesn't contribute to the 3D optical illusion

Because of the lack of shading, neither emissive color nor ambient color contribute very much to the optical illusion that is required to cause images displayed on a two-dimensional screen to appear to be three dimensional.  For that, we need some form of directional light, and that is where we are going next.

More Combinations of Light Source and Surface Reflection Properties

Referring back to Figure 2, we have two more kinds of reflective properties to consider:

  • Diffuse Color
  • Specular Color

We also have three more kinds of light sources to consider.

  • DirectionalLight (parallel rays)
  • PointLight (omnidirectional rays)
  • SpotLight (cone-shaped rays )

In addition, we have a shininess property that I have been ignoring up to this point.

All possible combinations

Any of the three kinds of light sources in the above list will react with either of the two kinds of reflective properties in the above list.  Furthermore, the shininess property comes into play any time specular reflection is involved.  This gives us six combinations of sources and properties that we need to understand.  We also need to understand how and when shininess impacts the results and to understand the existence or lack of shadows.  This leaves us with the following major topics to cover in the remainder of this installment of the lesson:

  • Diffuse Reflection and the DirectionalLight Class
  • The Shininess Property
  • Specular Reflection and the DirectionalLight Class
  • Shadows
  • Diffuse Reflection and the PointLight Class
  • Specular Reflection and the PointLight Class
  • Diffuse Reflection and the SpotLight Class
  • Specular Reflection and the PointLight Class

Diffuse Reflection and the DirectionalLight Class

What is diffuse reflection?

Here is part of what Wikipedia has to say about diffuse reflection:

"Diffuse reflection is the reflection of light from an uneven or granular surface such that an incident ray is seemingly reflected at a number of angles. It is the complement to specular reflection. If a surface is completely nonspecular, the reflected light will be evenly spread over the hemisphere surrounding the surface (2×π steradians).

The most familiar example of the distinction between specular and diffuse reflection would be matte and glossy paints as used in home painting. Matte paints have a higher proportion of diffuse reflection, while gloss paints have a greater part of specular reflection."

What is a directional-light source?

Bouvier has this to say about light radiated from a DirectionalLight object in Java 3D:

"A DirectionalLight source approximates very distant light sources such as the sun. Unlike AmbientLight sources, DirectionalLight sources provide light shining in one direction only.  For objects lit with a DirectionalLight source, the Light vector is constant. ... Since all light vectors from a DirectionalLight source are parallel, the light does not attenuate.  In other words, the intensity of a DirectionalLight source does not vary by the distance between the visual object and the DirectionalLight source."

Assumptions regarding a DirectionalLight source

I will paraphrase much of what I have learned about the DirectionalLight class.  An object of the class is intended to simulate light sources that satisfy the following assumptions:

  • The light source is assumed to be very far away, such as the sun for example.
  • All the light rays travel in parallel paths in the same direction.
  • The light wave has a flat wavefront that is at least as wide as the scene that is being illuminated by the directional-light source.
  • There is no attenuation of light energy regardless of the distance to the light source.

Cannot directly control the actual location of a directional-light source

As with the other kinds of light sources, you can control the relative intensity of each of the color components radiated by a directional-light source.

However, unlike point-light and spotlight sources, you cannot control the actual location of the directional-light source.  The location of the light source is assumed to be somewhere far outside the 3D space enclosed by the scene.

Can control the direction to the directional-light source

However, you can control the direction of the light being radiated by the directional-light source.  Since all light radiated by a directional-light source is parallel, this means that you can also control the direction to the directional-light source.

Controlling the direction of the light

To control the direction of the light being radiated from a directional-light source, you define a 3D vector with its tail at the origin in the 3D space.  You define the vector by providing the coordinates of the head of the vector.  The direction of the light is then assumed to be parallel to the direction of that vector.  To change the direction of the light, you change one or more coordinate values that specify the location of the head of the vector.

A lever analogy

In effect, the source is located on the end of a very long lever that goes through and pivots at the origin in 3D space.  The direction of the light is parallel to the long dimension of the lever.

Your vector can be thought of as representing a very short segment of that lever as it emerges from the origin on the opposite side of the origin from the directional-light source.  You can rotate your vector in 3D space by changing the coordinates of the head of the vector.  This has the effect of causing the other end of the lever to rotate in the reverse direction in 3D space.  This, in turn causes the location of the source on the end of the lever to move.

You can think of this as a 3D lever with the length on one side of the fulcrum (pivot point) being much longer than the length on the other side of the fulcrum.  When one end goes down, the other end must go up, and vice versa.

Behind, up, and to the right

For example, if you want to cause the directional-light source to be up to the right and behind the viewer, you cause the vector to point down to the left and into the screen.  This can be accomplished by using the following coordinate values for the head of the vector:

  • x = -1.0
  • y = -1.0
  • z = -1.0

These are the vector coordinates that were used along with a d