Tutorial Shader Setup Overview 2
From delta3d
This second tutorial will cover the preferred method for shader management in Delta3D: using the ShaderManager. For an insight into how osg handles this task and for the actual shader code referred to in this example, please refer to Tutorial Shader Setup Overview I.
The ShaderManager found in the dtCore library automates much of the setup we previously saw with osg. We just need to provide defintions(xml) of our shaders to the manager and it will take care of their creation and distribution.
Here is an example of a shader definition.
<?xml version="1.0" encoding="UTF-8"?>
<shaderlist>
<shadergroup name="ExampleGroup">
<shader name="ExampleShader" default="yes">
<source type="Vertex">shaders/example.vert</source>
<source type="Fragment">shaders/example.frag</source>
<parameter name="diffuseTexture">
<texture2D textureUnit="0">
<source type="Auto"/>
</texture2D>
</parameter>
<parameter name="alpha">
<oscillator cycletimein="2.0" cycletimemax="4.0" oscillation="UpAndDown"/>
</parameter>
</shader>
</shadergroup>
</shaderlist>
This will provide the ShaderManager the ability to create your shader and assign it to a node without having to do all the manual setup we saw in the previous tutorial. Notice that it we set up our shader uniforms by specifying them as parameters. The ShaderManager even has a built-in oscillator type that will dynamically update the value of our "alpha" uniform.
Here, we feed the shader definition to the ShaderManager.
// Load in our shaders so that our app can easily access and use them dtCore::ShaderManager::GetInstance().LoadShaderDefinitions( "shaders/shaders.xml" );
Now when we want to use the shader, we ask the ShaderManager for it.
dtCore::ShaderManager &shaderManager = dtCore::ShaderManager::GetInstance(); const dtCore::ShaderProgram *programPrototype = shaderManager.FindShaderPrototype( "ExampleShader" );
Once you have the shader, you just need to assign it to the osg node(s) you want to apply it to.
// Applied to the node // Note that the actually shader that was applied to the node was the programInstance // that we get returned back from this function. dtCore::ShaderProgram *programInstance = shaderManager.AssignShaderFromPrototype( *programPrototype, *osgNode );
This example accomplishes the same thing that we saw in the first tutorial, only this was much cleaner. The real benefits of using the ShaderManager become more obvious once you start to juggle multiple shaders used throughout your application. It manages the sharing of them as a resource and allows you to conveniently organize them by group.
If you have any question please contact GMan at the delta3D forums.
