Help end child hunger

GLSL 1.2 Tutorial

In this tutorial shader programming using GLSL 1.2 will be covered. Although many parts are now considered deprecated, the essence of GLSL has remained constant. Besides, this tutorial will continue to be online because learning the basics of deprecated OpenGL is easier than learning the new versions. If you’re looking for a GLSL tutorial that deals only with non-deprecated features visit the GLSL Core tutorial, here in Lighthouse3D.

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

 

35 Responses to “GLSL 1.2 Tutorial”

  1. Great Tutorial! It’s the best!! :)

    In the example shaders, I don’t see the headers: “#version 110? or “#version 120?. Why? That are not absolutely needed? (sorry my english) :)

  2. hi everyone.. Is it possible to dynamically attaching or detaching shaders ….

  3. How to use GLSL with visual basic6 someone please teach me. Please.

  4. Hi,
    I have a general problem is not related to the tutorials U have given :D
    This is the code which I m trying to run
    http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html

    When I compile my program it goes fine but while executing(running) it gives this error :-/

    Status: VSmain shader compiled with errors
    Compilation details for VSmain shader:
    0:1(10): error: GLSL 4.10 is not supported. Supported versions are: 1.00 ES, 1.10, and 1.20

    Wen I checked, GLSL is not a standalone lib and how can I get the versions 1.00 running in here

    • Form the error reported you’re trying to compile a shader for version 4.1 using GLES. I’ve not seen the code but you can try to change the header of the shader to a supported version.

  5. After blindly running over internate for more than 10 days, this tutorial led me on track to starts with GLSL.
    Really nice tutorial.
    Many many thanks

  6. Your tutorials are awesome!
    Will you do any tutorial about shadow mapping??
    Bye!

    ps: sorry for bad english ;)

  7. Hey you really need to check this out while its still relevant!

  8. 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….

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

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

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

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

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

  14. Excellent tutorial, thank you very much!

  15. 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)?

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

    • overflow… learn software programming please

      • … it seems just to say ” This tutorial is always amazing “.

      • That would not be an overflow, but an infinite loop. Since no new ressources is used in each loop, the memory is going to stay stable. Please learn software proramming before asking nice people to learn software programming.

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>

© 2014 Lighthouse3d.com Suffusion theme by Sayontan Sinha