OpenGL VRML W3D            
  Home Tutorials Books Applications Tools Docs Models Textures  



GLSL Tutorial   

  GLSL Tutorial


The Graphics Pipeline
Pipeline Overview
Vertex Processor
Fragment Processor

OpenGL Setup for GLSL
Creating a Shader
Creating a Program
Source Code
Trouble Shooting: the InfoLog
Cleaning Up

Comm. OpenGL=> GLSL
Comm. Introduction
Uniform Variables
Attribute Variables

Shader Basics
Data Types and Variables
Statments and Functions
Varying Variables

Shader Examples
Shader Examples List

GLSL Hello World

Color Shader

Flatten Shader

Toon Shader
Toon Shader - Version I
Toon Shader - Version II
Toon Shader - Version III

OpenGL Directional Light I
OpenGL Directional Light II
Directional Light per Pixel
Point Light Per Pixel
Spot Light Per Pixel

Simple Texture
Combine Texture + Fragment

The gl_NormalMatrix
Normalization Issues


OpenGLTutorials @

Led Shader
View Frustum Culling
GLSL Tutorial
Maths Tutorial
Billboarding Tutorial
Picking Tutorial
Terrain Tutorial
Display Lists Tutorial
GLUT Tutorial

[Previous: Toon Shader - Version III] [Next: OpenGL Directional Light I]

The GLSL Tutorial is now available in the new layout. Check it out here.

Core GLSL Tutorial (GLSL version >= 3.3) is also available here.

GLSL Tutorial


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.

AmbientAmbient + DiffuseSpecular

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 LightSpot 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.

[Previous: Toon Shader - Version III] [Next: OpenGL Directional Light I]


Site designed and maintained by António Ramires Fernandes
Your comments, suggestions and references to further material are welcome!

Lighthouse 3D privacy statement