Sunday, August 18, 2013

Using CC3ShaderProgramMatchers

Last week, I was talking about how to load a custom shader in Cocos3D-XNA. Well, what's nice about cocos3d is that it comes pre-bundled with a set of shaders that provide support for basic colouring, multi-texturing and bump map effects. Getting access to these shaders is handled by the CC3ShaderProgramMatchers class. For example to load the corresponding single texture shader we write

CC3ShaderProgram program = new CC3ShaderProgramMatchers().SingleTextureProgram();  

Once we have the shader, we would like to configure its parameters. However, in this instance a user shouldn't have to be burdened with understanding the innards of a shader they didn't write themselves. Thankfully, cocos3d does the heavy lifting of associating all bundled shaders' attributes/uniforms to corresponding instance variables belonging to a host of different objects.  As an example, within our bundled shaders we have the (sample of ) material uniforms

uniform float4 u_cc3MaterialAmbientColor;
uniform float4 u_cc3MaterialDiffuseColor;
uniform float4 u_cc3MaterialSpecularColor;
uniform float4 u_cc3MaterialEmissionColor;   

which are bound to the ivars


of the currently set CC3Material object within the graphics context. Other classes of interest include: CC3Light, CC3MeshNode, CC3PointParticleEmitter (along with others), which are similarly tied to a number of different shader parameters. So now to use these pre-bundled shaders, a user simply has to worry about setting a collection of ivars and cocos3d will make sure the correct shader parameters are updated.

Next week, I'll be putting all this theory into practice with the port Cocos3D-XNA by showing the basics of creating and using a material object!

No comments:

Post a Comment