2.3 Lighting effects | JAVA 3D Programming | Chapter 2

2.3 Lighting effects


MyJava3D includes some simple lighting calculations. The lighting equation sets the color of a line to be proportional to the angle between the surface and the light in the scene. The closer a surface is to being perpendicular to the vector representing a light ray, the brighter the surface should appear. Surfaces that are perpendicular to light rays will absorb light and appear brighter. MyJava3D includes a single white light and uses the Phong lighting equation to calculate the intensity for each triangle in the model (figure 2.6).

Figure 2.6 MyJava3D rendering without light intensity calculations


The computeIntensity method calculates the color intensity to use when rendering a triangle. It accepts a GeometryArray containing the 3D points for the geometry, an index that is the first point to be rendered, and a count of the number of points (vertices) that compose the item to be rendered.

The method then computes the average normal vector for the points to be rendered by inspecting the normal vectors stored within the GeometryArray. For a triangle (three vertices) this will be the vector normal to the plane of the surface.

The angle between the surface normal and the viewer is then calculated (beta). If the cosine of this angle is less than or equal to zero, the facet cannot be seen by the viewer and an intensity of zero will be returned. Otherwise, the method computes the angle between the light source position vector and the surface normal vector of the surface (theta). If the cosine of this angle is less than or equal to zero, none of the light from the light source illuminates the surface, so its light intensity is set to that of the ambient light. Otherwise, the surface normal vector is multiplied by the cosine of theta, the resulting vector is normalized, and then the light vector subtracted from it and the resulting vector normalized again. The angle between this vector and the viewer vector (alpha) is then determined. The intensity of the surface is the sum of the ambient light, the diffuse lighting from the surface multiplied by the cosine of the theta, and the specular light from the surface multiplied by the cosine of alpha raised to the glossiness power. The last term is the Phong shading, which creates the highlights that are seen in illuminated curved objects.


Note that in this simple MyJava3D example only one light is being used to illuminate the scene—in Java3D, OpenGL, or Direct3D many lights can be positioned within the scene and the rendering engine will compute the combined effects of all the lights on every surface.

Please refer to chapter 10 for a further discussion of lighting equations and example illustrations created using Java 3D.

From AwtRenderingEngine.java

       
private int computeIntensity( GeometryArray geometryArray,
int index, int numPoints )
{
int intensity = 0;
if ( computeIntensity != false )
{
// if we have a normal vector, compute the intensity
// under the lighting
if ( (geometryArray.getVertexFormat( ) GeometryArray.NORMALS) ==
GeometryArray.NORMALS )
{
double cos_theta;
double cos_alpha;
double cos_beta;
for( int n = 0; n  0.0 )
{
cos_theta = surf_norm.dot( light );
if ( cos_theta <= 0.0 )
{
intensity = (int) (lightMax * lightAmbient);
}
else
{
temp.set( surf_norm );
temp.scale( (float) cos_theta );
temp.normalize( );
temp.sub( light );
temp.normalize( );
cos_alpha = view.dot( temp );
intensity = (int) (lightMax * ( lightAmbient +
lightDiffuse * cos_theta + lightSpecular *
Math.pow( cos_alpha, lightGlossiness )));
}
}
}
}
return intensity;
}

       
 

Comments

Popular Posts

2.2.3 Drawing filled triangles | JAVA 3D Programming | Chapter 2

3.1.4 Java 2 development environment (optional) | JAVA 3D Programming | Chapter 3

4.7 Immediate mode vs. retained mode vs. mixed mode

4.4 Elements of scenegraph design | JAVA 3D Programming | Chapter 4

What is Java 3D and is it for me? | Chapter 1