- Forward


Lighting in OpenGL
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Parts of the Process
Back SMYC Forward
  • Specify normal vectors
  • Specify material properties
  • Create and position light sources
  • Specify properties of the lighting model
  • Enable lighting and light sources
Specifying Normal Vectors
Back SMYC Forward
  • Function:
    • glNormal*()
  • Approaches:
    • Face-by-face
    • Vertex-by-vertex
Specifying Normal Vectors (cont.)
Back SMYC Forward

An Example

openglexamples/lighting/lighting.c (Fragment: normals)
 
Specifying Material Properties
Back SMYC Forward
  • The Function:
    • void glMaterial*(GLenum face, GLenum name, TYPE value)
    • where GLenum is GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK
  • Specifying the Color of the Material (for Different Kinds of Reflection/Light):
    • Name: GL_AMBIENT, GL_DIFFUSE, GL_AMBIENT_AND_DIFFUSE, or GL_SPECULAR
    • Value: RGBA color
  • Specifying the Specular Exponent:
    • Name: GL_SHININESS
    • Value: A GLfloat
  • Specifying the Emissive Color of the Material:
    • Name: GL_EMISSION
    • Value: RGBA color
Specifying Material Properties (cont.)
Back SMYC Forward

An Example

openglexamples/lighting/lighting.c (Fragment: material)
 
Some Materials
Back SMYC Forward
openglexamples/lighting/materials.h
 
Creating and Positioning Light Sources
Back SMYC Forward
  • The Function:
    • void glLight*(GLenum light, GLenum name, TYPE value)
    • where light is GL_LIGHT0 ... GL_LIGHT7
  • Type of Reflection/Light:
    • Name: GL_AMBIENT, GL_DIFFUSE, or GL_SPECULAR
    • Value: RGBA color
  • Position of the Light:
    • Name: GL_POSITION
    • Value: A 3D or 4D point
  • Attenuation:
    • Name: GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, or GL_QUADRATIC_ATTENUATION
    • Value: A GLfloat
Light Sources (cont.)
Back SMYC Forward
  • Spotlights:
    • You can instruct a light source to emit a cone of light by setting additional parameters
  • Size of the Arc:
    • Name: GL_SPOT_CUTOFF
    • Value: An angle (180.0 is the default which corresponds to no cutoff)
  • Direction:
    • Name: GL_SPOT_DIRECTION
    • Value: A 3D point
  • Exponent:
    • Name: GL_SPOT_EXPONENT
    • Value: A GLfloat
Light Sources (cont.)
Back SMYC Forward

An Example

openglexamples/lighting/lighting.c (Fragment: lightsource)
 
Specifying the Lighting Model
Back SMYC Forward
  • The Function:
    • void glLightModel*(GLenum name, TYPE value)
  • Ambient Light:
    • Name: GL_LIGHT_MODEL_AMBIENT
    • Value: RGBA color
  • Specular Angles:
    • Name: GL_LIGHT_MODEL_LOCAL_VIEWER
    • Value: GL_TRUE for a local viewer GL_FALSE for a viewer at infinity
  • Number of Sides:
    • Name: GL_LIGHT_MODEL_TWO_SIDE
    • Value: GL_TRUE to light both sides or GL_FALSE to light the front
  • Different Specular and Ambient/Diffuse Color:
    • Name: GL_LIGHT_MODEL_COLOR_CONTROL
    • Value: GL_SINGLE_COLOR or GL_SEPARATE_SPECULAR_COLORS
Specifying the Lighting Model (cont.)
Back SMYC Forward

An Example

openglexamples/lighting/lighting.c (Fragment: lightingmodel)
 
Enabling Lighting and Lights
Back SMYC Forward
  • Enabling Lighting in General:
    • glEnable(GL_LIGHTING)
  • Enabling Specific Lights:
    • void glEnable(GLenum light)
    • where light is GL_LIGHT0 ... GL_LIGHT7
Enabling Lighting and Lights (cont.)
Back SMYC Forward

An Example

openglexamples/lighting/lighting.c (Fragment: enable)
 
A Complete Example
Back SMYC Forward

Using a Sphere

openglexamples/lighting/sphere.c
 
Another Complete Example
Back SMYC Forward

Using a File Containing the Model

openglexamples/lighting/solidmodel.c
 
There's Always More to Learn
Back -