Help end child hunger


Prev: Toon - Version III Next: Directional Lights I

In OpenGL there are three types of lights: directional, point, and spotlight. In this tutorial we’ll start to implement a directional light. First we’ll start with an implementation in GLSL that mimics the OpenGL way of lighting.

We’ll build the shader incrementally starting with ambient light up to specular lighting.

Ambient Ambient + Diffuse Specular

Then we’ll move on to lighting per pixel in order to get better results.


Next we’ll implement point and spot lights per pixel. These last tutorials are heavily based on the directional lights tutorial because most of the code is common.

Point Light Spot Light

As mentioned in the toon shader tutorial GLSL offers access to the OpenGL state that contains data for the light setting. This data describes the individual light’s setting as well as global parameters.

	struct gl_LightSourceParameters {
		vec4 ambient;
		vec4 diffuse;
		vec4 specular;
		vec4 position;
		vec4 halfVector;
		vec3 spotDirection;
		float spotExponent;
		float spotCutoff; // (range: [0.0,90.0], 180.0)
		float spotCosCutoff; // (range: [1.0,0.0],-1.0)
		float constantAttenuation;
		float linearAttenuation;
		float quadraticAttenuation;

	uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
	struct gl_LightModelParameters {

		vec4 ambient;
	uniform gl_LightModelParameters gl_LightModel;

Material properties are accessible in GLSL as well:

	struct gl_MaterialParameters {
		vec4 emission;
		vec4 ambient;
		vec4 diffuse;
		vec4 specular;
		float shininess;

	uniform gl_MaterialParameters gl_FrontMaterial;
	uniform gl_MaterialParameters gl_BackMaterial;

Most of these parameters, both for lighting and materials are familiar to those used to build applications in OpenGL. We shall use these properties to implement our directional light.



Prev: Toon - Version III Next: Directional Lights I

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: