Help those suffering in the Horn of Africa

GLSL 1.2 Tutorial

In this tutorial shader programming using GLSL 1.2 will be covered. Shaders are a hot topic and 3D games have shown that they can be put to good use to get remarkable effects. This tutorial aims at providing an introduction to the world of shaders.

The tutorial contains an introduction to the specification, but reading the OpenGL 2.0 and GLSL official specs is always recommended if you get serious about this. It is assumed that the reader is familiar with OpenGL programming, as this is required to understand some parts of the tutorial.

GLSL stands for GL Shading Language, often referred as glslang, and was defined by the Architectural Review Board of OpenGL, the governing body of OpenGL.

I won’t go into disputes, or comparisons, with Cg, Nvidia’s proposal for a shading language that is also compatible with OpenGL. The only reason I chose GLSL and not Cg for this tutorial, is GLSL closeness to OpenGL.

Before writing shaders, in any language, it is a good idea to understand the basics of the graphics pipeline. This will provide a context to introduce shaders, what types of shaders are available, and what shaders are supposed to do. It will also show what shaders can’t do, which is equally important.

After this introduction the OpenGL setup for GLSL is discussed. The necessary steps to use a shader in an OpenGL application are discussed in some detail. Finally it is shown how an OpenGL application can feed data to a shader making it more flexible and powerful.

Some basic concepts such as data types, variables, statements and function definition are then introduced.

The tutorial covers both the ARB extensions and OpenGL 2.0 versions. The former for greater compatibility; and the latter, because in the near future it will be the standard solution. The differences are small, and mostly have to do with slightly different function names and constants. Color coding has been used to help the reader to distinguish between them. The ARB stuff is presented in blue, and the OpenGL 2.0 in orange.

This dual coverage is prone to mistakes, so please let me know if you find something wrong.

Please bear in mind that this is work in progress and therefore bugs are likely to be present in the text or demos. Let me know if you find any bug, regardless of how insignificant, so that I can clean them up. Also suggestions are more than welcome. I hope you enjoy the tutorial.

Index

The Graphics Pipeline

OpenGL Setup for GLSL

Communication OpenGL => GLSL

Shader Basics

Shader Examples

Notes

 

18 Responses to “GLSL 1.2 Tutorial”

  1. While (true){System.out.println("This tutorial is amazing !!!!!!");}

  2. So is Glut and GLSL can be used together? I mean, i want to create 3d games. Should I study GLSL or GLUT or can i use them together(or select 1)?

  3. Excellent tutorial, thank you very much!

  4. Hello!

    First of all, it was thanks to this tutorial that I learned shaders. :)

    Now, do you have in mind updating the tutorial to GLSL 3.3 or greater?

    Cheers and thanks for everything!

    • Thanks. There is a code sample with OpenGL 3.3 already posted. I believe the GLSL code runs in GLSL 3.3.

      I’m planning on upgrading the tutorial but I’m unable to provide a date for it.

  5. Best shaders tutorial on the planet.. thanks :)

  6. No, you’re being spammed (see the name and url of the person you’r replying to)

  7. Hmm, I can’t for the life of me get this to work. Even a straight copy/paste (well, with a couple of very minor alterations just to get things “working”) has no effect. Everything compiles fine, but but program draws as it did before adding any of this new code.

    • Hi, could you be more specific please?

      • http://pastebin.com/GM3eSRqi

        I’m calling SetShaders() just before going into the main loop (which contains the my render function). Looking at the console everything appears to work fine, there are no errors either during the compile or while running the program, but there’s no visual difference between drawing with or without shaders.

        My render function loops through an array of structs and loads data from a VBO. At the moment it’s still using glPush/PopMatrix, could this be part of the problem?

        • Push and pop matrix should not be the problem, otherwise you wouldn’t get the same result. Its more likely that you are doing a glUseProgram(0), which disables shaders.

          By the way, always use the code from the site, not from somewhere else, unless there is a specific reason to do it. The site’s code is updated now and then…

          • Well now I feel foolish, turns out I needed the ARB extensions… Great tutorial, reader intelligence permitting =P

  8. Hey, awesome tutorials.
    I was just wondering if you had a PDF version or a printable version of this.

  9. how is keyboard and mouse handling events done in opengl (3.3)?

    • Hi,

      OpenGL does not manage keyboard and mouse type of devices. GLUT does, or alternatives like SDL, SMFL, etc….

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 Lighthouse3d.com Suffusion theme by Sayontan Sinha