Tucano
0.1
A library for rapid prototyping with modern OpenGL and GLSL
|
The Effect and Shader Classes hold the core of the OpenGL/GLSL applications. A typical application has a Class (inheriting from Effect) that contains Shader objects. Many Effects are multipass and contain many shaders.
It is possible to build an application without using the Effect Class at all, however there a few advantages in using the Effect:
Let's look at a simple Effect from Tucano's Collection, the NormalMap. This effect renders a mesh using normals directions as color (normal map). We only need one Shader to create this effect.
First we create our class:
This class contains a single Shader:
We need to overload the initialization method to load the shader. The loadShader
method will look in the set shader directory for files beginning with "normalmap" and shader extensions, in this case it will find two (.vert and .frag). The method also initializes the shaders and keeps references to them for reloading and deleting.
Note that the above method assumes that the shader files (.vert, .frag, etc...) are in the "/shaders" directory relative to your executing directory. You may need to change this directory before calling the initialization using the setShadersDir method.
If you are using pointers for the Shaders, you can call normalmap_shader = loadShader("normalmap");
Now, all we need is the core of the NormalMap application. We can implement it as a method that receives a Mesh and a camera Trackball, and renders the Mesh using the normal directions as colors:
And that's it. In the application's draw routine, we clear the screen and call the render method above. To see a visual representation of the trackball just render it! Just make sure that depth test is enabled. Here is a sample code for a typical draw method (you can find a similar method in the GLWidget Class of the phongViewer sample):
Here are the vertex and fragment shader codes for the files "normalmap.vert" and "normalmap.frag" (they are all included in the effects/shaders
directory):
Let's say we want to create an effect called BlurredNormals. Its only an illustrative example. This effect uses the normalmap above and then blurs the final resulting image. It's a two pass method: first pass stores the normalmap result in an FBO; second pass blurs the resulting image from the first pass. If you are not familiar with FBOs, check out the Framebuffer Tutorial.
We now need two Shaders plus a Quad Mesh and an FBO for the screen space blur:
The initialization method is completed as follows:
Note that you need to pass the size of your viewport to initialize the FBO, we assume we know this and its (w,h). The last parameter of the FBO says that we only need one texture attachment. It's usually a good idea to initialize and re-initialize the FBO every time your window is resized (checkout the QtTrackballWidget for an example).
We show another way to set the FBO size below, after this example.
The render method now needs two passes, one to store the result in a FBO, and the second to blur the image:
And that is all!! The blur shaders files (meanfilter.vert and meanfilter.frag) are in the effects/shaders directory of the Tucano Lib.
During the render method check if the FBO size is the same as the Viewport size, otherwise re-create the FBO (it will be initialized with zero)
Note that usually the first two elements of the viewport vector are zero (ex. [0, 0, w, h]). However, we have left it with as four elements for completeness.