INTRODUCTION TO LIGHTING

 

WTK provides four different types of light nodes directed, point, and spot, ambient light. By default all universes receive an ambient light whose intensity is set to 0.4. WTK allows the user ONLY 8 active lights in the simulation at any time. These can be any combination of lights. With WTK there is already a default ambient light so you can only have 7 additional lights in the universe active at any one time. This is a restriction of OpenGL not of WTK. Lighting can greatly effect your frame rate the most expensive hit being taken from spot lighting. Ambient lighting is the least expensive, the directed lighting, then point lighting, and finally spot lighting is the most expensive to the frame rate.

figure 13

 

Ambient lighting

Ambient lighting is background lighting that illuminates all polygons equally regardless of their position and/or orientation. It is a property of the universe. Ambient lighting can be set from a value of 0.0 to 1.0 where 0.0 is off and 1.0 is on. You can create additional ambient lighting but the ambient light value for the universe will never exceed 1.0. In other words creating an additional ambient light will add to the existing ambient lighting until the universe ambient property reaches 1.0 after that any increases to the ambient lighting will have no affect. Inversely the ambient lighting level of the universe will never go below 0.0 in the same manner.

The default ambient light is created when the user calls WTuniverse_new and CAN NOT be accessed with the Release 6 call. In order

to turn the ambient lighting off in R6 you must create a new ambient light and set its value to -0.4. This can be expensive to the simulation since lighting is a calculation that must be made on each polygon visible in the simulation. As an alternative I have found using the v2.1 function WTlight_setambient to be effective in turning the universe ambient light off, there are other 2.1 functions that will work with the default ambient light including WTlight_setambientrgb, WTlight_getambient, and WTlight_getambientrgb.

Directed Lights

Lights which provide illumination as a function of the angle between the light direction and the polygon normal, or, in the case of gouraud shading, between the light direction and the vertex normal. This light creates light in a single direction as defined by the direction property of the light. As shown in figure 13.

 

Point Lights

Lights which emanates radially from the light position, and which provide illumination as a function of the angle between the vector from the light position to the polygon normal or the vertex normal in the case of gouraud shading. Point light intensity may attenuate with distance from the light position. See figure 13.

 

Spot Lights

Light which emanates radially from the light position, within a cone of a specified angle centered about the spot light direction. This type of light provides illumination as a function of the angle between a vector from the light position and the vertex normal. The light intensity may fall off with the angle from the spot light direction within the light cone, and may attenuate with distance from the light position.. See figure 13.

 

 

Light Properties

directed, Point, and spot lights have three types of color. Ambient, diffused, and specular. Once a light node is created these properties can be set for it – or you can accept the existing defaults. The easiest way of setting a lights color is by setting it diffused property and leaving the other color properties set at 0. There are other attributes available for different types of lights. The following chart lists the light properties and the light(s) to which they apply.

PROPERTY LIGHT TYPE(S)
Position Point, Spot
Direction Directed, Spot
Intensity Ambient, Directed, Point, Spot
Ambient Color Ambient, Directed, Point, Spot
Diffuse Color Directed, Point, Spot
Specular Color Directed, Point, Spot
Attenuation Point, Spot
Angle Spot
Exponent (fall off) Spot

chart 14

 

Calculating Color

When OpenGL calculates the final color for an object it takes the ambient, diffused, and specular properties of the lighting and material multiplies them together (individually) to produce a term. The terms are then added together to product the perceived color.

 

Determining Intensity

Intensity is determined by adding the contributions from each light in the scene. If the result is 0.0 then the polygon is black, and if the result is 1.0 (maximum brightness) then the polygon is white. At maximum brightness the polygon is rendered with the color assigned to it. At less the maximum brightness the polygon is rendered with a darker shade of that color.

 

Casting shadows

Polygons in WTK DONOT cast shadows. Therefore no light is blocked by one polygon being in front of another. Shadows can be precalculated using "radiosity processing", which turns the models into a mesh and stores shadowing and other lighting information as vertex colors in the new model.

 

Using Lighting Files

WTK supports a key-word driven lighting file format. Sample light files containing directed, point, and spot lights are provided with the release of WTK. Your systems current lighting configuration can be saved to a file using WTlightnode_save. Lights are read from file using WTlightnode_load. The following is an example of a lighting file that is provided with WTK called lights.

 

// WorldToolKit light file

directed dir 0.078526 0.812874 0.577122 int 0.300000

diff 1.000000 1.000000 1.000000

spot pos 12.333322 6.505900 -3.478123 dir -0.979905 0.199333 0.007185

int 1.000000 angle 19.999994 exp 40.000000 att 0.100000 0.010000 0.004000

amb 1.000000 0.250000 1.000000 diff 1.000000 0.250000 1.000000

spec 1.000000 0.250000 1.000000

spot pos -3.147797 2.626527 2.653141 dir -0.434925 0.732769 0.523346

int 1.000000 angle 49.999999 exp 7.000000 att 0.100000 0.010000 0.004000

diff 0.250000 0.250000 1.000000

point pos 6.983410 8.199898 7.895465 int 0.400000 att 0.100000 0.010000 0.004000

diff 1.000000 1.000000 0.250000

ambient int 0.000000 amb 1.000000 1.000000 1.000000

 

 

pos=position dir=direction int=intensity diff=diffused exp=exponant(fall off)

att=attenuation angle=angle anb=ambient spec=specular

 

 

 

Ambient Lighting

Dealing with the default ambient light

WTlight_setambient

Sets the ambient light value for the default ambient light. This function is a v2.1 function and is included in R6 for backward compatibility.

SYNTAX:

void WTlight_setambient(float value);

ARGUMENTS:

value -- must be a value between 0.0 and 1.0

RETURN TYPE:

void

 

WTlight_getambient

function that will return the value of the default ambient light in the universe.

SYNTAX:

float WTlight_getambient(void);

ARGUMENTS:

none

RETURN TYPE:

The value of the default ambient light.

 

WTlight_setambientrgb

function that will set the default ambient lights color. The r,g,b values are in the range of 0 to 255 with 255,255,255 being white and 0,0,0 being black.

SYNTAX:

void WTlight_setambientrgb(unsigned char red, unsigned char green, unsigned char blue);

ARGUMENTS:

red -- the red component
green --
the blue component
blue --
the green component

RETURN TYPE:

void

 

WTlight_getambientrgb

function that will return the r,g,b value of the default ambient light. Caution should be taken not to alter the values that are returned by this function since they are pointers to the values the are used internally. If changes are made to these variables then the results are undefined.

SYNTAX:

void WTlight_getambientrgb(unsigned char *red, unsigned char *green, unsigned char *blue);

ARGUMENTS:

red -- a pointer to an unsigned char that will hold the red value
green --
a pointer to an unsigned char that will hold the green value
blue --
a pointer to an unsigned char that will hold the blue value

RETURN TYPE:

void

Creating new Ambient light nodes

WTlightnode_newambient

Creates a new ambient light and adds it as the last child of the parent specified. The default property values :

Ambient color = 1.0, 1.0, 1.0

Intensity = 0.4

SYNTAX:

WTnode *WTlightnode_newambient(WTnode *parent);

ARGUMENTS:

parent -- a pointer to the parent node to insert beneath,

RETURN TYPE:

A pointer to the newly created ambient light.

 

Assignment 14

project definition:

Directed Lighting

WTlightnode_newdirected

Creates a new directed light node and places it in the scene graph as the last child of the parent specified. If the parent argument is NULL then the node is created as an orphan that can later be added to the scene graph in the desired position. The default property values for a directed light are:

Ambient color = 0.0, 0.0, 0.0

Diffuse color = 1.0, 1.0, 1.0

Specular color = 1.0, 1.0, 1.0

Intensity = 1.0

Direction = 0.0, 0.0, 1.0

SYNTAX:

WTnode *WTlightnode_newdirected(WTnode *parent);

ARGUMENTS:

parent -- The node that the new light will be inserted beneath. If this argument is NULL then the node is created as an orphan.

RETURN TYPE:

A pointer to the newly created node.

 

WTmovlightnode_newdirected

Creates a new movable directed light node and places it in the scene graph as the last child of the parent specified. If the parent argument is NULL then the node is created as an orphan that can later be added to the scene graph in the desired position. The separator node created for this type of movable is a Transform separator. The default property values for a directed light are the same as if it were created as a regular node.

SYNTAX:

WTnode *WTmovlightnode_newdirected(WTnode *parent);

ARGUMENTS:

parent -- The node that the new light will be inserted beneath. If this argument is NULL then the node is created as an orphan.

RETURN TYPE:

A pointer to the newly created node.

 

Point lighting

WTlightnode_newpoint

Creates a new point light node and places it in the scene graph as the last child of the parent specified. If the parent argument is NULL then the node is created as an orphan that can later be added to the scene graph in the desired position. The default property values for a point light are:

Ambient = 0.0, 0.0, 0.0

Diffuse = 1.0, 1.0, 1.0

Specular = 1.0, 1.0, 1.0

Intensity = 1.0

Position = 0.0, 0.0, 0.0

Attenuation = 1.0, 0.0, 0.0

SYNTAX:

WTnode *WTlightnode_newpoint(WTnode *parent);

ARGUMENTS:

parent -- The node that the new light will be inserted beneath. If this argument is NULL then the node is created as an orphan.

RETURN TYPE:

A pointer to the newly created node.

 

WTmovlighnode_newpoint

Creates a new point light node and places it in the scene graph as the last child of the parent specified. If the parent argument is NULL then the node is created as an orphan that can later be added to the scene graph in the desired position. . The separator node created for this type of movable is a Transform separator. The default property values for a point light are the same as if it were created as a regular point light node.

SYNTAX:

WTnode *WTmovlightnode_newpoint(WTnode *parent);

ARGUMENTS:

parent -- The node that the new light will be inserted beneath. If this argument is NULL then the node is created as an orphan.

RETURN TYPE:

A pointer to the newly created node.

 

Spot Lighting

WTlightnode_newspot

Creates a spot light and adds it as the last child of the parent specified. If the parent argument is NULL then the node is created as an orphan that can later be added to the scene graph in the desired position. The default values for a spot light are:

Ambient = 0.0, 0.0, 0.0

Diffuse = 1.0, 1.0, 1.0

Specular = 1.0, 1.0, 1.0

Intensity = 1.0

Direction = 0.0, 0.0, 1.0

Position = 0.0, 0.0, 0.0

Exponent = 0.0 (no fall off)

Angle = PI/8 rad or 22.5 degrees (this is a half angle from the direction vector whose origin is at the light position) The total

angle would then equal 45 degrees or PI/4 rad

Attenuation = 1.0, 0.0, 0.0

SYNTAX:

WTnode *WTlightnode_newspot(WTnode *parent);

ARGUMENTS:

parent -- The node that the new light will be inserted beneath. If this argument is NULL then the node is created as an orphan.

RETURN TYPE:

A pointer to the newly created node.

 

WTmovlightnode_newspot

Creates a new spot light node and places it in the scene graph as the last child of the parent specified. If the parent argument is NULL then the node is created as an orphan that can later be added to the scene graph in the desired position. . The separator node created for this type of movable is a Transform separator. The default property values for a spot light are the same as if it were created as a regular spot light node.

SYNTAX:

WTnode *WTmovlightnode_newspot(WTnode *parent);

ARGUMENTS:

parent -- The node that the new light will be inserted beneath. If this argument is NULL then the node is created as an orphan.

RETURN TYPE:

A pointer to the newly created node.

 

Loading Lighting files

Lighting information can be stored in files as described previously. The function to load the light file is WTlightnode_load. Lighting files must be located in the current working directory for WTK to find them.

 

Assignment 15

project definition:

 

Light Properties

Not all lights have the same properties, see chart 14, but all the properties have Get and Set methods that allow the user to change the default values assigned to the light by WTK.

NOTE: All these functions work on light nodes regardless if they were created as movable or regular light nodes.

WTlightnode_setposition

Sets the 3D position of a light in world coordinates. Valid lights are Point and Spot

SYNTAX:

FLAG WTlightnode_setposition(WTnode *light, WTp3 position);

ARGUMENTS:

light -- The light in question
position --
The new position in world coordinates

RETURN TYPE:

A flag indicating the success or failure of the function. The function fails if the light passed in is not a spot or point.

 

WTlightnode_getposition

Stores the world coordinate position of the light in the WTp3 passed in.

SYNTAX:

FLAG WTlightnode_getposition(WTnode *light, WTp3 position);

ARGUMENTS:

light -- The light in question
position --
The WTp3 that will receive the position information

RETURN TYPE:

A flag indicating the success or failure of the functions. If the light passed in is not a point or spot the function fails

 

WTlightnode_setdirection

Sets the direction vector for the specified light node. Valid lights are spot and directed.

SYNTAX:

void WTlightnode_setdirection(WTnode *light, WTp3 directionvector);

ARGUMENTS:

light -- The light in question
directionvector --
A WTp3 containing the desired direction vector

RETURN TYPE:

void

 

WTlightnode_getdirection
Stores the direction vector of a light in the WTp3 passes in.

SYNTAX:

void WTlightnode_getdirection(WTnode *light, WTp3 directionvector);

ARGUMENTS:

light -- The light in question.
directionvector --
The WTp3 that will store the direction vector.

RETURN TYPE:

void

 

WTlightnode_setintensity

Sets the intensity of the light passed in. Valid lights are ambient, directed, point, and spot

SYNTAX:

void WTlightnode_setintensity(WTnode *light, float intensity);

ARGUMENTS:

light -- The light in question
intensity --
a value from 0.0 to 1.0 where 1.0 is the maximum intensity.

RETURN TYPE:

void

 

WTlightnode_getintensity

Returns the intensity value of the light passed in.

SYNTAX:

float WTlightnode_getintensity(WTnode *light);

ARGUMENTS:

light -- The light in question.

RETURN TYPE:

A value for 0.0 to 1.0 indicating the lights intensity

 

WTlightnode_setambient

Sets the ambient r,g,b color value for a light. The r,g,b values are in the range of 0.0 - 1.0. Valid lights are ambient, directed, point, and spot

SYNTAX:

void WTlightnode_setambient(WTnode *light, float red, float green, float blue);

ARGUMENTS:

light -- The light in question
red --
The red value
green --
The green value
blue --
the blue value

RETURN TYPE:

void

 

WTlightnode_getambient

Obtains the ambient r,g,b value for the light in question. Valid lights are ambient, directed, point., and spot.

SYNTAX:

void WTlightnode_getambient(WTnode *light, float *red, float *green, float *blue);

ARGUMENTS:

light – The light in question
red --
a pointer to a float where the red value will be stored
green --
a pointer to a float where the green value will be stored
blue
-- a pointer to a float where the blue value will be stored.

RETURN TYPE:

void

 

WTlightnode_setdiffuse

Sets the diffuse r,g,b, value for the light passed in. The r,g,b, values are in the range of 0.0 to 1.0. This function works with directed, point, and spot lights

SYNTAX:

void WTlightnode_setdiffuse(WTnode *light, float red, float green, float blue);

ARGUMENTS:

light -- The light in question
red --
The red value
green --
The green value
blue --
the blue value

RETURN TYPE:

void

 

WTlightnode_getdiffuse

Obtains the ambient r,g,b value for the light in question. Valid lights are ambient, directed, point., and spot.

SYNTAX:

void WTlightnode_getdiffuse(WTnode *light, float *red, float *green, float *blue);

ARGUMENTS:

light – The light in question
red --
a pointer to a float where the red value will be stored
green --
a pointer to a float where the green value will be stored
blue
-- a pointer to a float where the blue value will be stored.

RETURN TYPE:

void

WTlightnode_setspecular

Sets the diffuse r,g,b, value for the light passed in. The r,g,b, values are in the range of 0.0 to 1.0. This function works with directed, point, and spot lights

SYNTAX:

void WTlightnode_setspecular(WTnode *light, float red, float green, float blue);

ARGUMENTS:

light -- The light in question
red --
The red value
green --
The green value
blue --
the blue value

RETURN TYPE:

void

 

WTlightnode_getspecular

Obtains the ambient r,g,b value for the light in question. Valid lights are ambient, directed, point., and spot.

SYNTAX:

void WTlightnode_getspecular(WTnode *light, float *red, float *green, float *blue);

ARGUMENTS:

light – The light in question
red --
a pointer to a float where the red value will be stored
green --
a pointer to a float where the green value will be stored
blue
-- a pointer to a float where the blue value will be stored.

RETUEN TYPE:

void