1. GLSL Setup
Posted on : 25-03-2010 | By : Swiftless | In : GLSL
Tags: GLSL, OpenGL, programming, setup, tutorial
28
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.
|





