OpenGL Shading Language

Randi J. Rost, Bill Licea-Kane, Dan Ginsburg

Mentioned 12

The definitive OpenGL Shading Language reference AND tutorial - now fully updated and revised for the latest version * *Includes extensive new coverage, including a thorough discussion of GLSL for OpenGL ES, the emerging standard for handheld media devices. *Clear, approachable, and relevant examples that help programmers quickly address the challenges they're most likely to encounter. * Authored by experts who have been intimately involved in the design and evolution of the OpenGL Shading Language. OpenGL Shading Language, 3/e is the definitive guide to creating graphics applications with the newest version of the OpenGL Shading Language. Using this book, graphics programmers will learn how to exploit the full performance and flexibility of modern GPU hardware to create stunningly realistic and creative effects. One part reference, one part tutorial, this will be their go-to source from the moment they begin learning the language until they've become seasoned experts. This book's content has become ever more important to a wider spectrum of graphics programmers. For years, programmers treated graphics hardware as 'state machines' whose behavior could only be modified by changing bits of state. This severely limited the effects they could realistically achieve. The OpenGL Shading Language puts control of graphics hardware squarely in the programmer's hands: key stages of the graphics pipeline are now completely programmable. With this new freedom, of course, the programmer also gains new responsibilities. It's a fundamental paradigm shift -- and this book will help programmers make the leap. This edition covers significant enhancements in the new version of OpenGL Shading Language, including the latest version of GLSL for OpenGL ES, the emerging standard for handheld media devices. As in previous editions, however, it relies throughout on clear, approachable code samples that address the specific tasks and skills graphics programmers are most likely to need

More on

Mentioned in questions and answers.

I know there are some question about learning OpenGL. Here is what I know:

  • math for 3D
  • 3D theory

Here is what I want to know: - OpenGL 4.0 Core profile (or latter) - Shader Language 400 (or latter) - every part of above (if it do not work across vendors then it still do not bother me)

Here is what I DO NOT want to know - fixed function pipeline (will not use it ever!) - older OpenGL's - Compatibility profile

I prefer bigger portion of info like tutorials, series of articles, books.

PS If you know resources on opengl 3.x core profile, post them too

Have not read it, but OpenGL 4.0 Shading Language Cookbook

The 5th edition of OpenGL SuperBible has been recently released. This edition reflects OpenGL 3.3 which was released at the same time as OpenGL 4.0, the book only covers the core profile and assumes no prior OpenGL knowledge.

That's what I got from the book's description anyway. I have the 4th edition and it's an excellent resource for OpenGL 2.0, so I assume the new edition along with the latest OpenGL Shading Language book would be just what you're looking for.

Durian Software has an ongoing series of tutorials covering modern OpenGL. They are aimed at OpenGL 2.0 but avoid using any deprecated functionality in later versions.

I am aware that there were similar questions in past few years, but after doing some researches I still can't decide where from and what should I learn. I would also like to see your current, actual view on modern OpenGL programming with more C++ OOP and shader approach. And get sure that my actual understanding on some things is valid.

So... currently we have OpenGL 4.2 out, which as I read somewhere requires dx11 hardware (what does it mean?) and set of 'side' libraries, to for example create window.

There is the most common GLUT, which I extremely hate. One of main reason are function calls, which doesn't allow freedom in the way how we create main loop. As some people were telling, it was not meant for games.

There is also GLFW, which actually is quite nice and straight-forward to me. For some reason people use it with GLUT. ( which provides not only window initialisation, but also other utilities? )

And there is also SFML and SDL ( SDL < SFML imo ), whereas both of them sometimes need strange approach to work with OGL and in some cases are not really fast.

And we have also GLEW, which is extension loading utility... wait... isn't GLUT/GLFW already an extension? Is there any reason to use it, like are there any really important extensions to get interested with?

Untill now we have window creation (and some utilities), but... OGL doesn't take care of loading textures, neither 3D models. How many other libs do I need?

Let's mention education part now. There is (in)famous NeHe tutorial. Written in C with use of WinApi, with extremely unclear code and outdated solutions, yet still the most popular one. Some stuff like Red Book can be found, which are related to versions like 2.x or 3.x, however there are just few (and unfinished) tutorials mentioning 4.x.

What to go with?

If you are writing a game, I would avoid things like GLUT, and write your own wrappers that will make the most sense for your game rendering architecture.

I would also avoid OpenGL 4.2 at this point, unless you only want to target specific hardware on specific platforms, because support is minimal. i.e., the latest version of Mac OSX Lion just added support for OpenGL 3.2.

For the most comprehensive coverage of machines made in the last few years, build your framework around OpenGL 2.1 and add additional support for newer OpenGL features where they make sense. The overall design should be the same. If you're only interested in targeting "current" machines, i.e. machines from late 2011 and forward, build your framework around OpenGL 3. Only the newest hardware supports 4.2, and only on Windows and some Linux. If you're interested in targeting mobile devices and consoles, use OpenGL ES 2.0.

GLEW loads and manages OpenGL Extensions, which are hardware extensions from different vendors, as opposed to GLUT which is a toolkit for building OpenGL applications, completely different things. I would highly recommend using GLEW, as it will provide a clean mechanism for determining which features are available on the hardware it is being run on, and will free you from the task of having to manually assign function pointers to the appropriate functions.

OpenGL SuperBible is a pretty good book, also check OpenGL Shading Language. Everything you do with modern OpenGL is going to involve the use of shaders - no more fixed functionality - so your biggest challenge is going to be understanding GLSL and how the shader pipelines work.

I'm currently learning modern OpenGL as well. I've also had hard time finding good resources, but here's what I've discovered so far.

I searched for a good book and ended up with OpenGL ES 2.0 Programming Guide, which I think is the best choice for learning modern OpenGL right now. Yes, the book is about OpenGL ES, but don't let that scare you. The good thing about OpenGL ES 2.0 is that all the slow parts of the API have been removed so you don't get any bad habits from learning it while it's still very close to desktop OpenGL otherwise, with only a few features missing, which I think you can learn rather easily after you've mastered OpenGL ES 2.0.

On the other hand, you don't have the mess with windowing libraries etc. that you have with desktop OpenGL and so the book on OpenGL ES won't help you there. I think it's very subjective which libraries to use, but so far I've managed fine with SDL, ImageMagick and Open Asset Import Library.

Now, the book has been a good help, but apart from that, there's also a nice collection of tutorials teaching modern OpenGL from ground up at OpenGL development on Linux. (I think it's valid on other OSes the name nevertheless.) The book, the tutorials and a glance or two every now and then to the Orange Book have been enough in getting me understand the basics of modern OpenGL. Note that I'm still not a master in the area, but it's definitely got me started.

I'm a beginner with OpenGL ES 2.0 and I'm looking for a good book/resource that will help me with my learning. I've found several books:

  • OpenGLĀ® ES 2.0 Programming Guide
  • iPhone 3D Programming: Developing Graphical Applications with OpenGL ES

but reading the Amazon reviews I saw that they either assume previous knowledge with OpenGL or are not written specifically for iOS. (I know OpenGL should be easy to port, but I'm looking for a book/resource with examples in C, not C++, that talks about OpenGL in the iOS context)

I also found this and it really helped me getting a grasp on the basic concepts, but unfortunately, they cover OpenGL ES 1.1 and are only describing the basics.

Any help would be appreciated!

It's a lot easier to find OpenGL ES 2.0 material for iOS (or any OS, really) than it used to be a year or so ago.

For something written from a pure iOS perspective, it's hard to beat Jeff LaMarche's chapters from his unpublished book, which start here. You linked to his OpenGL ES 1.1 tutorials, which are also great, but he didn't place his newer 2.0 material on that list.

iPhone 3D Programming by Philip Rideout is a great book that covers both OpenGL ES 1.1 and 2.0. It does not assume that you know OpenGL ES, and he does explain a good bit of the math and other fundamentals required to understand what he's talking about. He gets into some pretty advanced techniques towards the end. However, all of his code is in C++, rather than Objective-C, so that may be a little disconcerting for someone used to Cocoa development. Still, the core C API for OpenGL ES is the same, so it's easy to see what's going on.

If you're looking for particular effects, the OpenGL Shading Language book is still one of the primary resources you can refer to. While written for desktop OpenGL, most of the shading language and shaders presented there translate directly across to OpenGL ES 2.0, with only a little modification required.

The books ShaderX6, ShaderX7, GPU Pro, and GPU Pro 2 also have sections devoted to OpenGL ES 2.0, which provide some rendering and tuning hints that you won't find elsewhere. Those are more advanced (and expensive) books, though.

If you're just getting started with OpenGL ES 2.0, it might not be a bad idea to start using GLKit (available only on iOS 5.0), which simplifies some of the normal setup chores around your render buffers and simple shader-based effects. Apple's WWDC 2011 videos have some good material on this, but their 2009 and 2010 videos (if you can find them) provide a lot more introductory material around OpenGL ES 2.0.

Finally, as Andy mentions, I taught a class on the subject as part of my course on iTunes U, which you can download for free here. The course notes for that class can be found here or downloaded as a VoodooPad file here. I warn you that I go a little technical quite fast in the OpenGL ES 2.0 session, so you may want to watch the 1.1 session from the previous semester here. I also talk a little bit about what I've done with OpenGL ES 2.0 in this article about my open source application (whose source code can be grabbed from here, if you'd like to play with a functional OpenGL ES 2.0 iOS application).

I am inspired to start programming some things in OpenGL, using c++.

Can anyone list here what tools should be installed to start this process.


IDE Compiler OpenGL download etc?

Here is my piece of (a former OpenGL toyer's) advice on the choice of libraries and tools (as for the compiler, you can just use any free modern one out there (i.e., gcc or VSExpress C++)):

  1. Do not forget to look at GLEW (The OpenGL Extension Wrangler Library) or GLee (GL Easy Extension library), especially if you'd like to be stick with more modern OpenGL features (including advanced shaders).

  2. Something named like Shader Debugger will probably also be of great benefit when toying with shaders, as it will give the opportunity to see shader effect immediately without even compiling any C/C++ sources.

  3. Looking at the "traditional" (covered in the (free now) "Red book") functionality from the modern (i.e. shaders again:)) viewpoint may also be enlightening, the "Orange book" ("OpenGL Shading Language (3rd Edition))" has a chapter on emulating OpenGL fixed functionality with shaders.

I am trying to use GLSL with openGL 2.0.

Can anyone give me a good tutorial to follow, so that I can setup GLSL properly.

Regards Zeeshan

Depending on what you are trying to achieve and what is your current knowledge, you can take different approaches.

If you are trying to learn OpenGL 2.0 while also learning GLSL, I suggest getting the Red book and the Orange book as a set, as they go hand in hand.

If you want a less comprehensive guide that will just get you started, check out the OpenGL bible.

If I misunderstood your question and you already know OpenGL, and want to study more about GLSL in particular, here's a good phong shading example that shows the basics.

Compiling a shader source is really simple,

First you need to allocate a shader slot for your source, just like you allocate a texture, using glCreateShader:

GLuint vtxShader = glCreateShader(GL_VERTEX_SHADER);
GLuint pxlShader = glCreateShader(GL_FRAGMENT_SHADER);

After that you need to load your source code somehow. Since this is really a platform dependent solution, this is up to you.

After obtaining the source, you set it using glShaderSource:

glShaderSource(vtxShader, 1, &vsSource, 0);
glShaderSource(pxlShader, 1, &psSource, 0);

Then you compile your sources with glCompileShader:


Link the shaders to each other, first allocate a program using glCreateProgram, attach the shaders into the program using glAttachShader, and link them using glLinkProgram:

GLuint shaderId = glCreateProgram();
glAttachShader(shaderId, vtxShader);
glAttachShader(shaderId, pxlShader);

Then, just like a texture, you bind it to the current rendering stage using glUseProgram:


To unbind it, use an id of 0 or another shader ID.

For cleanup:

glDetachShader(shaderId, vtxShader);
glDetachShader(shaderId, pxlShader);



And that's mostly everything to it, you can use the glUniform function family alongside with glGetUniform to set parameters as well.

I'd like to know how to create custom filters for GPUImage. Right now I can create a sepia custom filter from this code(this was provided by Brad Larson on GitHub as an example):

varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;

void main()
    lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
    lowp vec4 outputColor;
    outputColor.r = (textureColor.r * 0.393) + (textureColor.g * 0.769) + (textureColor.b * 0.189);
    outputColor.g = (textureColor.r * 0.349) + (textureColor.g * 0.686) + (textureColor.b * 0.168);    
    outputColor.b = (textureColor.r * 0.272) + (textureColor.g * 0.534) + (textureColor.b * 0.131);
    outputColor.a = 1.0;

    gl_FragColor = outputColor;

I'm curious to know how Brad Larson knew these numbers and what it means. I have searched everywhere and did not find a tutorial on this. Can someone please guide me to create my own custom photo Filters with Photoshop and then translate it into a .fsh code file?

For example, if I am to change an image into a pink tone in Photoshop. How do I get these numbers in the code above?

Your question is a little broad, as you can literally write an entire book on how to create custom shaders, but it's a commonly asked one, so I can at least point people in the right direction.

Filters in GPUImage are written in the OpenGL Shading Language (GLSL). There are slight differences between the OpenGL targets (Mac, desktop Linux) and OpenGL ES ones (iOS, embedded Linux) in that shaders on the latter use precision qualifiers that are missing on the former. Beyond that, the syntax is the same.

Shader programs are composed of a matched pair of a vertex shader and a fragment shader. A vertex shader operates over each vertex and usually handles geometric manipulations. A fragment shader operates over each fragment (pixel, generally) and calculates the color to be output to the screen at that fragment.

GPUImage deals with image processing, so most of the time you'll be working only with fragment shaders and relying on one of the stock vertex shaders. The above is an example of a fragment shader that takes in each pixel of an input texture (the image from the previous step in the processing pipeline), manipulates its color values, and writes out the result to the gl_FragColor builtin.

The first line in the main() function:

lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);

uses the texture2D() function to read the pixel color in the inputImageTexture at a given coordinate (passed in from the vertex shader in the first stage). These coordinates are normalized to the 0.0-1.0 range, and therefore are independent of the input image size.

The values are loaded into a vector type (vec4) that contains multiple components within a single type. In this case, the color components for red, green, blue, and alpha are stored in this four-component vector and can be accessed via .r, .g, .b, and .a. The color component values are also normalized to the 0.0-1.0 range, in case you're used to working with 0-255 values.

In the particular case of a sepia tone filter, I'm attempting to apply a well-known color matrix for a sepia tone effect to convert the incoming color to an outgoing one. That requires matrix multiplication, which I do explicitly in the code above. In the actual framework, this is done as matrix math using builtin types within the shader.

There are many, many ways to manipulate colors to achieve certain effects. The GPUImage framework is full of them, based largely on things like color conversion standards published by Adobe or other organizations. For a given effect, you should identify if one of these existing implementations will do what you want before setting out to write your own.

If you do want to write your own, first figure out the math required to translate incoming colors into whatever output you want. Once you have that, writing the shader code is easy.

The lookup filter in GPUImage takes another approach, and that's to start with a lookup image that you manipulate in Photoshop under the filter conditions you wish to mimic. You then take that filtered lookup image and attach it to the lookup filter. It translates between the incoming colors and their equivalents in the lookup to provide arbitrary color manipulation.

Once you have your shader, you can create a new filter around that in a few different ways. I should say that my new GPUImage 2 framework greatly simplifies that process, if you're willing to forgo some backwards compatibility.

I'm following a real complete tutorial for 2D game developing on android using OpenGL, and I encountered the following

static final String VERTEX_SHADER_CODE =
        "uniform mat4 u_mvpMatrix;" +
        "attribute vec4 a_position;" +

        "void main() {" +
        "  gl_Position = u_mvpMatrix * a_position;" +

static final String FRAGMENT_SHADER_CODE =
        "precision mediump float;" +
        "uniform vec4 u_color;" +

        "void main() {" +
        "  gl_FragColor = u_color;" +

I'm new to OpenGL so I'd like to know what does that code means? or where can I read more about it.

I also read about GL Program, I'd like to know what is that too.

These are two pieces of code that will get executed by the graphics chip. The code is written in GLSL ES, a language based on GLSL 1.20 from desktop OpenGL.

The first piece of code is the vertex shader; it is executed for each vertex. The second is the fragment shader; it is executed for each pixel of rasterized geometry. Your examples do just the bare minimum, or maybe even less than that. For example, shader code usually performs lighting computations. These computations are missing here; the rendered output will not look like 3D objects yet. Later tutorials will add these computations.

If you start with OpenGL and GLSL on Android, it is a good idea to read an OpenGL ES2.0 book like this one. There are also books specific for GLSL, but general OpenGL books cover everything you need when you get started.

One more note: Some Android tutorials and example apps store shader code in string literals like you posted. It is actually quite a pain to write code like this (no line breaks, no syntax highlighting). You might want to read you shader programs from separate text files instead.

I'm looking for a few NPR (Non-Photorealistic Rendering) GLSL shader examples, mostly sketch or toon inspired. Does anyone have some links? :)

Chapter 18 of the Orange Book (OpenGL Shading Language) is all about NPR in GLSL.

OpenGL (Open Graphics Library) is an API used to interact with GPUs (Graphics Processing Units). Originally developed by SGI (Silicon Graphics Inc.) in the early 90's, the current API is being developed by the Khronos Group.

OpenGL is platform independent and available for desktop, workstation and mobile devices. It is designed to provide hardware-accelerated rendering and is typically used for applications such as CAD software and computer games.

When posting in the tag, remember to always specify your target OpenGL version in order to get more precise answers. The tags , , and exist for this purpose. OpenGL ES (OpenGL for Embedded Systems) has its own tag - .

Official Documentation

  • The Red Book - Tutorial GL
  • The Orange Book - Tutorial for GLSL
  • The Green Book - Tutorial for GLX
  • The Blue Book - API references for GL (out of print; replaced by online reference pages)
  • The White Book - Tutorial for WGL


Khronos Group announced API at GDC (Game Developers Conference) 2015. Vulkan, previously known as glNext or the "Next Generation OpenGL Initiative", is widely thought of as the successor to OpenGL and is rumoured to share many similarities to the API. It is a complete redesign aimed to unify the OpenGL and OpenGL-ES API's into one common API that will not be backwards compatible.

External resources

Inofficial Tutorials


Release Dates

  • OpenGL 1.0 - January 1992
  • OpenGL 1.1 - January 1997
  • OpenGL 1.2.1 - October 1998
  • OpenGL 1.3 - August 2001
  • OpenGL 1.4 - July 2002
  • OpenGL 1.5 - July 2003
  • OpenGL 2.0 - September 2004
  • OpenGL 2.1 - July 2006
  • OpenGL 3.0 - August 2008
  • OpenGL 3.1 - March 2009
  • OpenGL 3.2 - August 2009
  • OpenGL 3.3 - March 2010
  • OpenGL 4.0 - March 2010
  • OpenGL 4.1 - July 2010
  • OpenGL 4.2 - August 2011
  • OpenGL 4.3 - August 2012
  • OpenGL 4.4 - July 2013
  • OpenGL 4.5 - August 2014

For a while now I have been searching for a good tutorial which teaches you OpenGL for 2D (bitmapping / topdown game) with the ability to add 3D characters, sadly I have yet found a good tutorial.

Almost 2 years ago I have finished the SDL tutorials for 2D from , but now I want to be able to add 3D characters to a topdown game, so I have to go with OpenGL.

So, if you know of a good tutorial which teaches you the basics of 2D OpenGL with inclusion of being able to add 3D characters and objects, let me know!

Here is an example of what I would like to be able to create: (Game: Gun Bros), as you can see, the world is created in a 2D bitmap-system, but the characters are in 3D.

And here are a couple of tutorial websites I have found myself but not 100% sure if any of these really could help me: - Pure 3D programming. - Pure 2D programming (This is the basics I guess I need for the 2D part) - Also 2D, but I haven't taken a look at it yet.

If you want to dive into openGL, I would recommend the orange book. We used it in class, and it has a lot of really good information. If you're developing this for the web, I would highly recommend this webGL tutorial. I learned a lot from that tutorial. As far as the specific implementation of a top-down game, I've not run across a tutorial. You could probably use very simple .objs and then do some bump mapping onto them. Mapping textures onto 3d objects requires uv mapping. For uv mapping, I'm a fan of this uv tutorial

Could anyone recommend some beginner opengl books from a shader approach? Also, looking for some good 3d math references.