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: Creating a Shader] [Next: Source Code]

GLSL Tutorial


OpenGL Setup for GLSL - Creating a Program


The following figure shows the necessary steps to get a shader program ready and going.


The first step is creating an object which will act as a program container. The function available for this purpose returns a handle for the container.

The syntax for this function, in OpenGL 2.0 syntax is as follows:


GLuint glCreateProgram(void);


And using the ARB extension is:


GLhandleARB glCreateProgramObjectARB(void);


You can create as many programs as you want. Once rendering, you can switch from program to program, and even go back to fixed functionality during a single frame. For instance you may want to draw a teapot with refraction and reflection shaders, while having a cube map displayed for background using OpenGL's fixed functionality.

The next step involves attaching the shaders created in the previous subsection to the program you've just created. The shaders do not need to be compiled at this time; they don't even have to have source code. All that is required to attach a shader to a program is the shader container.

To attach a shader to a program use the OpenGL 2.0 function:


void glAttachShader(GLuint program, GLuint shader);

Parameters:

program - the handler to the program.
shader - the handler to the shader you want to attach.

And using the ARB extension is:


void glAttachObjectARB(GLhandleARB program, GLhandleARB shader);

Parameters:

program - the handler to the program.
shader - the handler to the shader you want to attach.

If you have a pair vertex/fragment of shaders you'll need to attach both to the program. You can have many shaders of the same type (vertex or fragment) attached to the same program, just like a C program can have many modules. For each type of shader there can only be one shader with a main function, also as in C.

You can attach a shader to multiple programs, for instance if you plan to use the same vertex shader in several programs.

The final step is to link the program. In order to carry out this step the shaders must be compiled as described in the previous subsection.

The syntax for the link function, in OpenGL 2.0, is as follows:


void glLinkProgram(GLuint program);

Parameters:

program - the handler to the program.

The syntax for the link function, using the ARB extensions, is:


void glLinkProgramARB(GLhandleARB program);

Parameters:

program - the handler to the program.

After the link operation the shader's source can be modified, and the shaders recompiled without affecting the program.

As shown in the figure above, after linking the program, there is a function to actually load and use the program, (ARB extension) glUseProgramObjectARB, or (OpenGL 2.0) glUseProgram. Each program is assigned an handler, and you can have as many programs linked and ready to use as you want (and your hardware allows).

The syntax for this function is as follows (OpenGL 2.0 notation):


void glUseProgram(GLuint prog);

Parameters:

prog - the handler to the program you want to use, or zero to return to fixed functionality

The syntax using the ARB extensions is as follows:


void glUseProgramObjectARB(GLhandleARB prog);

Parameters:

prog - the handler to the program you want to use, or zero to return to fixed functionality

If a program is in use, and it is linked again, it will automatically be placed in use again, so in this case you don't need to call this function again. If the parameter is zero then the fixed functionality is activated.

[Previous: Creating a Shader] [Next: Source Code]

       


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

Lighthouse 3D privacy statement