1. GLSL Setup
Jump To:
shader.h Source
shader.cpp Source
main.cpp Source
shader.vert Source
shader.frag Source
Download
To start off the OpenGL Shader Language tutorials, I am going to take our basic
Smooth Rotation OpenGL tutorial and add to it. Now for those of you that didn’t
read about GLSL on the tutorials page, up the top, it will just give you a brief outline on what shaders can do as well as how it compares to DirectX, and I recommend taking a look.
If you don’t know anything about GLSL already, a shader program is made up of either a Vertex Shader and a Fragment Shader, or a Vertex Shader, Geometry Shader and Fragment Shader, which is supported on graphics cards from the Nvidia 8xxx cards onwards.
Here is a quick rundown on the different types of shader programs:
A Vertex Shader allows us to manipulate vertices, and gives us access to vertex normals, our lighting pipeline, object materials, and other functions.
A Fragment Shader allows us to manipulate the outputting pixels that we see on our screen, here we can create our effects such as bloom, lighting, texturing, fog, etc.
A Geometry Shader (which I will introduce in later tutorials), allows us to actually create and remove vertices as we see fit. This is great for dynamic level of detail, allows us to create ‘fuzzy’ polygons, explode shapes, and do a bunch of nifty effects.
I’d like to point out now, that this tutorial is aimed for OpenGL 2.0 and up, where GLSL is made a part of OpenGL. All prior versions will need to use ARB extensions.
Shader.H File: Being our first GLSL tutorial, this might be a little long as there is a bit to setting up your first application. It’s not really complicated, and the final code is quite short, but because there is such a range of things to do, this page is going to be a bit long. Having used Java quite a bit recently thanks to my University, I am growing a fondness for Object Oriented programming. This tutorial is going to take an Object Oriented style approach at GLSL shaders, and we will end up with a class that we can reuse each time we want to create a shader program. To start off with our class declaration, open up a new C++ header file called "shader.h". In this we want to start off with a new class, so type something along the lines of:
This will be a nice clean skeleton on which we are going to add everything. You may notice the extra long code for our header files which includes an if statement. This code is explained on the new OpenGL Tips page, in short, it means you can compile on Mac as well. Now, we want to add a few methods to the public: section of our class. We will give whoever uses this class the ability to create a shader using the constructor, or by using an init method. Both of which will take the file names for the vertex and fragment shader. So we need to add the lines: Shader(const char *vsFile, const char *fsFile); Ok, so we now know how to create our shaders. But what about using them? Well all we need are three methods, bind and unbind to enable and disable our shader, and id to get the number associated with our shader, so that we can pass variable through to the shader: void bind(); Nice, now all that is left is to do is to create three private variables. One for our shader program, one for our fragment shader and a final one for our vertex shader. I will be calling them: unsigned int shader_id; When we put all of this into shader.h, we get the following file, which is quite simple and does all that we need.
|
||||||||
Shader.CPP File: Now lets move onto our shader.cpp file. This is where we are going to fill all of our methods outlined above, and introduce a new method. So lets start from the beginning, with our constructors: Shader::Shader() { Shader::Shader(const char *vsFile, const char *fsFile) { Phew, now that that’s out of the way.. Lets move onto our init method. The first two lines we are going to add, are going to tell OpenGL to create a vertex shader and a fragment shader, and assign the id’s of these to our shader_vp and shader_fp variables. shader_vp = glCreateShader(GL_VERTEX_SHADER); const char* vsText = textFileRead(vsFile); if (vsText == NULL || fsText == NULL) { The next two lines pass our shader file contents to OpenGL to attach it to our shaders. This next two lines, tells OpenGL to compile our shaders, which we have already bound the source code to. shader_id = glCreateProgram(); So now we can read in a shader file and compile it as a shader program. So lets take a look at the methods which allow us to use this: This first method, id(), will return the shader_id program. This is used for when we bind our shader, or for when we want to pass variables through to our shader. This second method, bind(), will simply attach our shader, and anything drawn afterwards will use this shader, either until this shader is unbound or another shader is enabled. The third of our usage methods, is unbind(), this simply binds the shader 0, which is reserved for OpenGL, and will disable our current shader. So whats left now? We can create our shader, and we can use our shader. So why don’t we clean up our shader when we are done? I will be doing this from within the destructor method for our shader, and this should only be called once at the end of our application, otherwise we will have to recreate our shader, which is an expensive process. Our desctructor is made up of only 5 method calls to OpenGL. The first two, just detach our vertex shader and fragment shader from our shader project, and the final 3 just delete all of the shaders and then deletes the shader program. glDetachShader(shader_id, shader_fp); And that’s all there is to creating a shader program. It’s not that tricky, and it all makes sense when sit back and think about what it is doing. Now lets take a look at the main.cpp file below for this project.
|
||||||||
Main.CPP File: As most of the main.cpp file is based on the double buffered window tutorial, I am only going to explain the lines that are related to GLSL and implementing our new class. The first thing we are going to do here, is include our header file: #include “shader.h” Now that we have access to our shader class, we need to declare a Shader object. I am going to call it shader for lack of a better word in this tutorial: Shader shader; Now before we can do anything with our shaders, we need to call glewInit(), this sets up GLEW so that we can use the extensions required to use shaders. This code will go below our glutCreateWindow call in our main method: glewInit(); Simple enough so far. Now we want to initialize our shader. Inside our void init(void) method, add the following line: shader.init(“shader.vert”, “shader.frag”); For this tutorial, our shader programs are going to be called shader.vert and shader.frag and will be located in the same directory as the the compiled application. All that is left now is to use the shader. In our display method, we want the shader to be applied to our cube, so before we draw the cube we want to bind our shader, and afterwards we want to unbind it: shader.bind();
|
||||||||
Vertex Shader Source: This is pretty much the most basic of vertex shaders. Vertex shaders Anyway, this shader, just like all other GLSL shaders, has a main Inside our main function, I am setting the position of the vertices The variable gl_Position, is used to set the position of the current I don’t use ftransform() personally, and I believe it is even deprecated So all this shader will do is place the vertices we are drawing to where *Note* All vertex shaders are called for every vertex individually. Every vertex shader, needs to have the line
|
||||||||
Fragment Shader Source: The fragment shader needs a main function, just as the vertex shader does. But unlike a vertex shader, this is All fragment shaders must end with the line In the fragment shader, gl_FragColor will end up being GLSL has a few different types, I recommend taking a look at the And that is all there is to shaders (for now). If you have any questions, please email me at swiftless@gmail.com
Revision: 1.1
|
||||||||
Download: Download shader.h Source Code for this Tutorial Download shader.cpp Source Code for this Tutorial Download main.cpp Source Code for this Tutorial |