I just wanted to try out some shaders on a flat image. Turns out that writing a C program, which just takes a picture as a texture and applies, let’s say a gaussian blur, as a fragment shader on it is not that easy: You have to initialize OpenGL which are like 100 lines of code, then understanding the GLBuffers, etc.. Also to communicate with the windowing system one has to use GLUT which is another framework..
Turns out that Nvidia’s Fx composer is nice to play with shaders.. But I still would like to have a simple C or C++ program which just applies a given fragment shader to an image and displays the result. Does anybody have an example or is there a framework?
First of all, I’d avoid using glut — it’s buggy, hasn’t been updated in roughly a decade, and its design doesn’t really fit very well with what most people want today (e.g., though you can use it for animations, it’s really intended primarily to produce a static display). I pointed out a number of alternatives to glut in a previous answer.
That (mostly) leaves the code to compile, link, and use shaders. I’ve written a small class I find handy for this purpose:
For a simple demo, a couple of “pass-through” shaders (just imitate the fixed-functionality pipeline):
Which you’d use something like:
If you’re going to use, for example, a number of different fragment shaders in the course of drawing your scene, you simply define a static object for each, then execute
prog1();,prog2();, etc., just prior drawing the objects you want shaded with each shader. E.g.,Edit: As @rotoglup quite correctly points out, this use of
staticvariables delays destruction until after the OpenGL context has been destroyed, so when the destructors attempt to useglDeleteProgram/glDeleteShader, results are unpredictable (at best).While this may be excusable in a demo program, it’s decidedly undesirable in real use. At the same time, you generally do not want to re-compile your shader(s) every time you enter the function(s) that use them.
To avoid both problems, you generally want to create your shader objects as members of a class instance whose lifetime is, in turn, tied to the lifetime of whatever it’s going to shade:
This will compile/link the shader program once when you create a character of that type, and destroy it when you destroy that character.
Of course, in a few cases, this isn’t exactly desirable either. Just for example, consider a 3D version of the ancient “kill lots of targets” games like Galaga or Centipede. For games like this, you’re creating and destroying lots of essentially identical targets relatively quickly. Given a large number of essentially identical targets, you probably want to use something like a
shared_ptr<shader_prog>to create a single instance of the shader that’s shared between all the instances of a particular target type. Given that you re-use the same target types many times, you may want to go a bit further even than that, so you maintain the same shaders through the entire game, not just when a particular type of target is being shown.In any case, we’re getting a bit off-track here. The point is that compiling and linking shaders is a fairly expensive process, so you normally want to manage their lifetime to avoid creating and destroying them a lot more often than truly necessary (though that’s not to say that it’s critical to create them all at the beginning of the game and only destroy them at the end, either).