OpenGL: Lighting
from http://www.eecs.tulane.edu/www/Terry/OpenGL/
OpenGL provides two types of light sources: directional and positional.
A directional light source is considered to be an infinite distance away from
the objects in the scene. Thus, its rays of light are considered parallel by
the time they reach the object. A positional light is near or within the scene
and the direction of its rays are taken into account in lighting calculations.
Positional lights have a greater performance cost than directional lights due
to these additional calculations.
OpenGL uses Phong Lighting Model for
Specular reflected light.
Issues in Lighting with OpenGL
- In OpenGL, we can define up to eight light sources, specified by GL_LIGHT0,
GL_LIGHT1, ... GL_LIGHTX where X is equal to the constant GL_MAX_LIGHTS. Each
source has various properties, which have a default value, and must be enabled
(and can be disabled). Use your virtual world coordinates to specify the position
of a source of light. We also have to enable lighting itself.
- The command glLightfv() is used to specify the
position of the light and whether it is directional or positional. It is also
used to specify the values of the color components of the light source such
as ambient color, diffuse color, specular color, emissive color, and shininess.
- Once the light sources are defined, normal vectors and material properties
of the objects in the scene must also be defined. An object's normal vectors
define its orientation relative to light sources. Normal vectors can be specified
for each vertex and/or shared by vertices. The command glNormal()
is used to make this assignment.
- The color components specified for lights have different meaning than those
specified for materials. For lights, the numbers correspond to the percentage
of full intensity of each color. Therefore, the brightest possible white
light has RGB values of (1.0, 1.0, 1.0). For materials, the numbers correspond
to the reflected proportions of those colors. The command glMaterialfv()
is used to define material properties by specifying the values
of the color components of materials.
- The command glColorMaterial() is used to minimize
the performance costs associated with changing material properties. This command
should be used whenever a single material property, such as diffuse color,
must be changed for most vertices in the scene. Any change to the current
color made by a call to glColor() immediately updates the material property
specified by glColorMaterial(). The capability state variable GL_COLOR_MATERIAL
must be enabled by the call glEnable(GL_COLOR_MATERIAL).
- Lastly, lighting must be enabled by a call to glEnable(GL_LIGHTING)
and each light in the scene must be enabled by calls to glEnable(GL_LIGHTi)
where GL_LIGHTi is the symbolic name of the light.
This example is a modification of Polygons_List.c to use lighting. The
three sided pyramid was changed to a four-sided pyramid to make calculating
the normal vectors easier.
Light.c Output
Light.c Source Code
(NOTE: ABOVE CODE IS IN TK windowing NOT GLUT windowing
system)
Notice in the source code the order in which normals are defined for GL_QUAD_STRIP.
In the GL_QUAD_STRIP section, Normal A applies to vertices 1, 2, 3, and 4; Normal
B applies to vertices 3, 4, 5, and 6; Normal C applies to vertices 5, 6, 7,
and 8; and Normal D applies to vertices 7, 8, 9, and 10. Due to the way that
OpenGL renders objects, the correct normal value must be set before the last
vertex in the current object is specified so that the correct normal value is
assigned to that object.
The STEPS
|
1) Enable Lighting
glEnable(GL_LIGHTING);
|
2) Setup each Light Source you want to create GL_LIGHTx
(where 0<=x<=GL_MAX_LIGHTS)
glLightfv(GL_LIGHT1, GL_POSITION, position1);
glLightfv(GL_LIGHT1, GL_AMBIENT, lightambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightdiffuse1);
|
3) Enable each Light Created in Step 2
glEnable(GL_LIGHTx)
|
Spotlights
Spotlights are point sources but emit light over a narrow range of angles.
Imagine a cone of light coming from the spotlight. What must we do to
create one is to alter the following parameters using the glLightx() function
on the light under question:
- We have to specify its spread. Specify the angle between the axis
of the cone and a ray along its edge. GL_SPOT_CUTOFF default 180 giving
a spread of 360 - no cone Values for the cutoff angle must lie between
0 and 90 (unless it is 180 in which case it is not a spotlight).
- We also specify a direction for spotlights. They have position and
direction.
GL_SPOT_DIRECTION default 0.0,0.0,-1.0 (pointing down negative z-axis)
- Finally, we can control the intesity of the distribution of light
within the cone. This controls how concentrated the light is. The intensity
of the light is highest in the centre of the cone. It is attenuated
towards the edges. The higher we set the spot exponent the more focused
the light source (appears - brighter, shinier, more intense)
GL_SPOT_EXPONENT default=0
|