Light Sources Lecture 8 SE309-Computer graphics
Outline • Light Sources • The Default Model • Light Source LIGHT0 • Directional Light Sources • Moving the Light Sources • Types of Light • Ambient Light • Diffuse Light • Specular Light • Assignment
Lighting and Reflection • The shade of a pixel is determined by two things: • The properties of the light source(s). • The material properties. • The light sources determine the color and intensity of the light striking the surface. • The material properties determine how that light is reflected.
Types of Light Source • Definition (Ambient Light Source) • An ambient light source has no position in space. The light strikes objects equally from all directions. • Definition (Point Light Source) • A point light source is located at a point in space. The light strikes different objects from different directions depending on their position relative to the light source. • Definition (Directional Light Source) • A directional light source is located “at infinity.” The light strikes all objects from the same direction.
The Lighting Model • To use lighting, the lighting model must be enabled. glEnable(GL_LIGHTING); • When lighting is enabled, shading is determined by the lighting and material properties only. • The glColor() function has no effect. • (Unless you enable GL_COLOR_MATERIAL). • We will talk about that later on.
General Ambient Light • The only default lighting is the general ambient light, set to (0:2; 0:2; 0:2). • This is independent of all regular light sources. • The function glLightModel*() can be used to change the general ambient light. float amb[] = {1.0, 1.0, 1.0, 1.0}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
Light Source LIGHT0 • OpenGL supports up to 8 separate light sources, each with its own characteristics. • They are named LIGHT0, LIGHT1, . . . , LIGHT7. • The defaults for LIGHT0 are different from the defaults for LIGHT1 through LIGHT7. • Otherwise, the 8 light sources are handled in the same way.
Defaults for Light Sources • Defaults for LIGHT0. • Ambient (0:0; 0:0; 0:0). • Diffuse (1:0; 1:0; 1:0). • Specular (1:0; 1:0; 1:0). • Position (0:0; 0:0; 1:0). • Defaults for LIGHT1 through LIGHT7. • All types of light (0:0; 0:0; 0:0). • Position (0:0; 0:0; 1:0).
Enabling Lights • Each light must be enabled. • To enable LIGHT0, make the function call glEnable(GL_LIGHT0); • The other light sources are enabled in a similar way.
Point vs. Directional Sources • At each point of a surface, the incident light has a direction. • With a point source, the direction varies with position of the illuminated object. • With a directional source, direction is the same for all objects. • Furthermore, with a point source, the intensity may vary with the distance from the source if we enable light attenuation.
Positioning the Lights • Example (Positioning LIGHT0) floatlight_position[] = {10.0, 10.0, 10.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Positioning Lights at Infinity • A light source may be placed “at infinity” by setting the fourth coordinate to 0:0. float light_position[] = {1.0, 1.0, 1.0, 0.0}; • The default type is directional (position (0; 0; 1; 0)). • The above example gives the light the direction vector (0; 0; -1), but no position in space. • In other words, for objects in all locations, the light will shine from the direction (0; 0; -1).
Positioning the Light Sources • The light sources may be set relative to the world coordinates (fixed) or relative to the camera (moveable). • If they are relative to world coordinates, then the illumination of the objects remains the same when the camera is moved. • If they are relative to the camera, then the lights move with the camera, changing the illumination of the objects.
Positioning the Light Sources • To make the light source fixed (in world coordinates), the light’s position should be set after the camera is set. • Then when the camera moves the scene, the lights move with the scene. • To make the light source move with the camera, the light’s position should be set before the camera is set. • Then the lights will not move with the scene.
Positioning the Light Sources • Example (Fixed Light Sources) setView(); setLights(); • Example (Moveable Light Sources) setLights(); setView();
Ambient Light • Ambient light illuminates objects equally in all directions. • In real life, ambient light is light that has been reflected off so many surfaces that it is impossible to identify the source.
Ambient Reflection • The ambient reflection depends on • The ambient light inherent in the scene. • The ambient light given off by the light sources. • The ambient property of the surface.
Setting the Light Properties • The function glLight*() is used to set the various light properties, such as ambient light. • It takes three parameters • The light source. • The light property (ambient, diffuse, etc.) • The light intensity.
Setting the Ambient Light • Example (Setting the Ambient Light for LIGHT0) float light_ambient[] = {0.3, 0.3, 0.3, 1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
Diffuse Light • Intensity of reflected diffuse light • Depends on angle of incidence. • Reflects equally in all directions, i.e., does not depend on the viewing angle. • It can be computed more efficiently if the light is directional, since there is one less variable.
Setting the Diffuse Light • Example (Setting the Diffuse Light for LIGHT0) float light_diffuse[] = {0.5, 0.5, 0.5, 1.0}; glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
Specular Light • Intensity of reflected specular light varies with viewing direction. • Maximum intensity is in the direction pointing directly along the angle of reflection.
Specular Reflection • Specular reflection creates the appearance of “shininess.” • Surfaces with a high specular reflection appear very shiny. • Surfaces with a low specular reflection appear matte.
Specular Reflection • Computing specular reflection is less efficient than computing diffuse reflection, since it depends on the direction to the viewer as well as the position of the light source. • The calculations can be speeded up if the viewer is placed “at infinity.”
Setting the Specular Light • Example (Setting the Specular Light for LIGHT0) float light_specular[] = {1.0, 1.0, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
Homework • Read Chapter5 – Light sources. • Read Chapter5 – Light sources in OpenGL.