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: Spot Light Per Pixel] [Next: Combine Texture + Fragment]

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

GLSL Tutorial


Simple Texture


In order to perform texturing operations in GLSL we need to have access to the texture coordinates per vertex. GLSL provides some attribute variables, one for each texture unit:
	attribute vec4 gl_MultiTexCoord0;
	attribute vec4 gl_MultiTexCoord1;
	attribute vec4 gl_MultiTexCoord2;
	attribute vec4 gl_MultiTexCoord3;
	attribute vec4 gl_MultiTexCoord4;
	attribute vec4 gl_MultiTexCoord5;
	attribute vec4 gl_MultiTexCoord6;
	attribute vec4 gl_MultiTexCoord7;
GLSL also provides access to the texture matrices for each texture unit in an uniform array.
	uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];
The vertex shader has access to the attributes defined above to get the texture coordinates specified in the OpenGL application. Then it must compute the texture coordinate for the vertex and store it in the pre defined varying variable gl_TexCoord[i], where i indicates the texture unit.

The simple following instruction sets the vertex texture coordinate for texture unit 0 just by copying the texture coordinate specified in the OpenGL application.

	gl_TexCoord[0]  = gl_MultiTexCoord0;
A simple example of a vertex shader to setup texture coordinates for a texture, using texture unit 0, could be:
	void main() {
	
		gl_TexCoord[0] = gl_MultiTexCoord0;
		gl_Position = ftransform();
	} 
If we wanted to use the texture matrix then we could write:
	void main() {
	
		gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
		gl_Position = ftransform();
	} 
As mentioned before gl_TexCoord is a varying variable, i.e. it will be used in the fragment shader to access the interpolated texture coordinate.

In order to access the texture values it is necessary to declare a special type of variable in the frament shader. For a 2D texture we could write:

	uniform sampler2D tex;
Data types for 1D and 3D textures are also available, the general format is: sampleriD, where i is the dimensionality of the texture. The user defined tex variable contains the texture unit we are going to use, in this case 0. The function that gives us a texel, a pixel in the texture image, is texture2D. This function receives a sampler2D, the texture coordinates, and it returns the texel value. The signature is as follows:
	vec4 texture2D(sampler2D, vec2);
The returned value takes into account all the texture settings as defined in the OpenGL application, for instance the filtering, mipmap, clamp, etc... Our fragment shader can then be written as:
	uniform sampler2D tex;
	
	void main()
	{
		vec4 color = texture2D(tex,gl_TexCoord[0].st);
		gl_FragColor = color;
	}
Notice the usage of selector st when accessing gl_TexCoord. As mentioned in section Data Types and Variables, when accessing texture coordinates the following selectors can be used: s,t,p,q. (Note that r is not used to avoid conlficts with rgb selectors).

TextureTextured Cube

A Shader Designer project is available in here.

[Previous: Spot Light Per Pixel] [Next: Combine Texture + Fragment]

       


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

Lighthouse 3D privacy statement