tags:

views:

143

answers:

1

Hi..

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

+4  A: 

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:

glCompileShader(vtxShader);
glCompileShader(pxlShader);

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);
glLinkProgram(shaderId);

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

glUseProgram(shaderId);

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

For cleanup:

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

glDeleteShader(vtxShader);
glDeleteShader(pxlShader);

glDeleteProgram(shaderId);

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

LiraNuna