OpenGL VRML W3D            
  Home Tutorials Books Applications Tools Docs Models Textures  

 

              Bugs

GLSL Tutorial   

  GLSL Tutorial

Index
Introduction

The Graphics Pipeline
Pipeline Overview
Vertex Processor
Fragment Processor

OpenGL Setup for GLSL
Overview
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

Lighting
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
Multitexturing

Notes
The gl_NormalMatrix
Normalization Issues


Google

OpenGLTutorials @ Lighthouse3d.com

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



   
[Previous: Combine Texture + Fragment] [Next: The gl_NormalMatrix]

GLSL Tutorial


MultiTexture


Multitexturing is also really easy in GLSL. All we have to do is to access both textures. And since in here we're going to use the same texture coordinates we don't even have to rewrite the vertex shader. The fragment shader also suffers a minor change to add both textures colors.
	varying vec3 lightDir,normal;
	uniform sampler2D tex,l3d;
	
	void main()
	{
		vec3 ct,cf;
		vec4 texel;
		float intensity,at,af;
		
		intensity = max(dot(lightDir,normalize(normal)),0.0);
		
		cf = intensity * (gl_FrontMaterial.diffuse).rgb + 
						  gl_FrontMaterial.ambient.rgb;
		af = gl_FrontMaterial.diffuse.a;
		
		texel = texture2D(tex,gl_TexCoord[0].st)+
			  texture2D(l3d,gl_TexCoord[0].st);
		ct = texel.rgb;
		at = texel.a;
		
		
		
		gl_FragColor = vec4(ct * cf, at * af);	
	}

Texture Unit 0 Texture Unit 1 Textured Cube

And now for something a little different: a glow in the dark effect. We want the second texture to glow in the dark, i.e. it will be fully bright when the light doesn't hit, and it will be dimmed as it gets more light.

Additive Multi-Texture Glowing Multi-Texture
We have to recompute the final color in two steps: first we compute a color which is the first texture modulated with the fragments color, and afterwards we add the second texture unit depending on the intensity.

If intensity is zero then we want the second texture in its full strength. When the intensity is 1 we only want a 10% contribution of the second texture unit. For all the other values of intensity we want to interpolate. We can achieve this with the smoothstep function. This function has the following signature:

	genType smoothStep(genType edge0, genType edge1, genType x);
The result will be zero if x <= edge0, 1 if x >= edge1 and performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. In our case we want to call the function as follows:
	coef = smoothStep(1.0, 0.2, intensity);
The following fragment shader does the trick:
varying vec3 lightDir,normal;
uniform sampler2D tex,l3d;

void main()
{
	vec3 ct,cf,c;
	vec4 texel;
	float intensity,at,af,a;
	
	intensity = max(dot(lightDir,normalize(normal)),0.0);
	
	cf = intensity * (gl_FrontMaterial.diffuse).rgb + 
					  gl_FrontMaterial.ambient.rgb;
	af = gl_FrontMaterial.diffuse.a;
	
	texel = texture2D(tex,gl_TexCoord[0].st);
		
	ct = texel.rgb;
	at = texel.a;
	
	c = cf * ct;
	a = af * at;
		
	float coef = smoothstep(1.0,0.2,intensity);
	c += coef *  vec3(texture2D(l3d,gl_TexCoord[0].st));
	
	gl_FragColor = vec4(c, a);	
}
A Shader Designer project is available in here.

[Previous: Combine Texture + Fragment] [Next: The gl_NormalMatrix]

       


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

Lighthouse 3D privacy statement