dms::Light

Source Code

Class Specification

#include <dms/Light.h>

Light(void);
void apply(void);
void enable(void);
void disable(void);

void setAmbient(const Vector4& color);
void setDiffuse(const Vector4& color);
void setSpecular(const Vector4& color);
void setPosition(const Vector4& position);
void setInfinitePosition(const Vector3& position);
void setLocalPosition(const Vector3& position);
void setPosition(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void setInfinitePosition(GLfloat x, GLfloat y, GLfloat z);
void setLocalPosition(GLfloat x, GLfloat y, GLfloat z);
void setAttenuation(GLfloat constant,GLfloat linear,GLfloat quadratic);
void setSpotCutoff(GLfloat cutoff);
void setSpotDirection(const Vector3& direction);
void setSpotExponent(GLfloat exponent);

Vector4& ambient(void);
Vector4& diffuse(void);
Vector4& specular(void);
Vector4& position(void);
GLfloat attenuationConstant(void);
GLfloat attenuationLinear(void);
GLfloat attenuationQuadratic(void);
GLfloat spotCutoff(void);
Vector3& spotDirection(void);
GLfloat spotExponent(void);

Description

Light(void)

The constructor creates a light object with these default values:

ambient (0,0,0,1)
diffuse (1,1,1,1)
specular (1,1,1,1)
position (0,0,1,0)
attenuation 1, 0, 0
spot cutoff 180
spot direction (0,0,-1)
spot exponent 0

It also automatically assigns the light source a unique OpenGL light number, starting from GL_LIGHT0. If the maximum number of lights (GL_MAX_LIGHTS) is exceeded, all new lights will have the last allowable light number.

void apply(void)

Activates the light source, passing all of its settings to glLight().
Example:

dms::Light lightSource;
 
lightSource.apply();
void enable(void)
void disable(void)

enable() enables the light source, but does not make any calls to glLight().
disable() disables the light source.
These functions can be used to have a light source selectively affect certain objects, without its position being affected by any added transformations that are in use at different times. Call apply() at the point where you want the light source defined (and its position affected by transformations), and enable() at later points to simply re-enable it.
Example:

glLoadIdentity();
lightSource.apply();
...
light.disable();
...
glRotatef(30.0, 0.0, 1.0, 0.0);
light.enable(); /* position will be unaffected by the rotation */
void setAmbient(const Vector4& color)
Vector4& ambient(void)

Defines / returns the light source's ambient color.
Example:

light.setAmbient(dms::Color::grey20);
void setDiffuse(const Vector4& color)
Vector4& diffuse(void)

Defines / returns the light source's diffuse color.
Example:

dms::Vector4 reddish(1.0, 0.7, 0.7, 1.0);
light.setDiffuse(reddish);
void setSpecular(const Vector4& color)
Vector4& specular(void)

Defines / returns the light source's specular color.
Example:

dms::Vector4 white(1.0, 1.0, 1.0, 1.0);
light.setSpecular(white);
void setPosition(const Vector4& pos)
void setPosition(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
Vector4& position(void)

Defines / returns the light source's position. If pos[3] (or wpos[3] is non-zero, it's a local light source.
Example:

dms::Vector4 pos(10.0, 2.0, 0.0, 1.0);
light.setPosition(pos);
void setInfinitePosition(const Vector3& pos)
void setInfinitePosition(GLfloat x, GLfloat y, GLfloat z)

Defines the light source's position, making it an infinite light source.
Example:

light.setInfinitePosition(10.0, 2.0, 0.0);
void setLocalPosition(const Vector3& pos)
void setLocalPosition(GLfloat x, GLfloat y, GLfloat z)

Defines the light source's position, making it a local light source.
Example:

light.setLocalPosition(10.0, 2.0, 0.0);
void setAttenuation(GLfloat constant,GLfloat linear,GLfloat quadratic)
GLfloat attenuationConstant(void)
GLfloat attenuationLinear(void)
GLfloat attenuationQuadratic(void)

Defines / returns the light source's constant, linear, and quadratic attenuation factors. These are the values that are passed to glLight() for GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, and GL_QUADRATIC_ATTENUATION.
Example:

light.setAttenuation(1.0, 0.0, 1.0);  /* Make a fairly simple quadratic falloff */
void setSpotCutoff(GLfloat cutoff)
GLfloat spotCutoff(void)

Defines / returns the light source's spotlight cutoff angle, in degrees. A cutoff value of 180 will disable the spotlight effect.
Example:

light.setSpotCutoff(light.spotCutoff() + 1.0);  /* Increase the cutoff angle */
void setSpotDirection(const Vector3& dir)
Vector3& spotDirection(void)

Defines / returns the light source's spotlight direction vector.
Example:

dms::Vector3 dir(0.0, -1.0, -1.0);
light.setSpotDirection(dir);
void setSpotExponent(GLfloat exp)
GLfloat spotExponent(void)

Defines / returns the light source's spotlight falloff exponent. A exp value of 0 will disable the falloff of the spotlight - it will have a constant intensity throughout its cone.
Example:

light.setSpotExponent(0.5);