|
GLSL Tutorial
OpenGL Setup for GLSL - Overview
This section, OpenGL Setup for GLSL, assumes you've got a pair of shaders, a vertex shader and a fragment shader, and you want to use them in an OpenGL application. If you're not ready yet to write your own shaders there are plenty of places to get shaders from the internet. Try the site from the Orange Book. The tools for shader development, namely Shader Designer or Render Monkey, all have a lot of shader examples.
As far as OpenGL goes, setting your application is similar to the workflow of writing a C program. Each shader is like a C module, and it must be compiled separately, as in C. The set of compiled shaders, is then linked into a program, exactly as in C.
Both the ARB extensions and OpenGL2.0 are being used in here. If you are new to extensions or using OpenGL above version 1.1 (as supported by Microsoft) I suggest you take a look at GLEW. GLEW simplifies the usage of extensions and newer versions of OpenGL to a great deal since the new functions can be used right away.
If relying on extensions, because you have no support for OpenGL 2.0 yet, then two extensions are required:
- GL_ARB_fragment_shader
- GL_ARB_vertex_shader
A small example of a GLUT program using GLEW to check the extensions could be as shown below:
#include <GL/glew.h>
#include <GL/glut.h>
void main(int argc, char **argv) {
glutInit(&argc, argv);
...
glewInit();
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
printf("Ready for GLSL\n");
else {
printf("Not totally ready :( \n");
exit(1);
}
setShaders();
glutMainLoop();
}
To check for OpenGL 2.0 availability you could try something like this
#include <GL/glew.h>
#include <GL/glut.h>
void main(int argc, char **argv) {
glutInit(&argc, argv);
...
glewInit();
if (glewIsSupported("GL_VERSION_2_0"))
printf("Ready for OpenGL 2.0\n");
else {
printf("OpenGL 2.0 not supported\n");
exit(1);
}
setShaders();
glutMainLoop();
}
The figure bellow shows the necessary steps (in OpenGL 2.0 syntax) to create the shaders, the functions used will be detailed in latter sections.
In the next subsections the steps to create a program are detailed.
|