Help end child hunger

Setup for GLSL

 
Prev: Fragment Processor Next: Creating a Shader
 

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.

 

Prev: Fragment Processor Next: Creating a Shader
 

  10 Responses to “Setup for GLSL”

  1. Good tutorial. Thank you

  2. Hi,
    I am new to this and using this tutorial i have downloaded the shader designer.
    I’m using windows 7 but there seems to be some problem. I cant edit the vertex shader part of window or fragment shader part in this shader designer tool for win 7. can any one help me out ?

    -Sam

  3. dude, i really can’t tell you how much this site has cleared up for me in terms of graphics programming. thanks a ton

  4. Hi!
    I have a problem …
    glGetString(GL_EXTENSIONS) returns null.
    How can I do to “install” the extensions ?
    thx for your tuto!
    Alexis

    • Hi,

      As for glGetString giving NULL you can find an explanation, and a solution, in here.

      However, if you just want to use extensions try using GLEW, it loads the extensions for you.

      Antonio

  5. Extensions can be confusing, think of them as plugins, they “Extend” the OpenGL framework adding new features to the library.

    Nvidia and ATI both as well as the OpenGL board (ARB) have extensions built into graphics drivers etc, for example, Nvidia may have an extension that makes use of it’s hardware that isn’t natively built into OpenGL, the only way to use this is via an extension.

    Now, how do we determine if your particular NVIDIA or ATI card supports a particular extension? GLEW. GLEW is a tool basically to interrogate the graphics driver to determine if it supports a certain OPENGL version or extension. It’s no good trying to enable an extension that isn’t supported in your particular graphics card or driver. So we need some way of checking what is available, you may develop your game / app on a high end NVIDIA card, but you may distribute it and someone runs it on a low / mid range card that doesn’t support the high end shader functions etc, GLEW is how you tell your application what the particular card is capable of.

    Hope this helps.

  6. Lost me with all this GLEW stuff and extensions 🙁

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: