This code example implements a shader pipeline with class definitions for ShaderA, ShaderB, and ShaderC. The definitions follow the guidelines explained at the end of this example to ensure that the TShadingSample instance is valid throughout the following pipeline stages:
class ShaderA : public TShader { private: TShader * fShaderB; public: const TShader* GetChild(){ return fShaderB; }; virtual Boolean NeedColor() { return TRUE; }; virtual Boolean NeedNormal() { return GetChild()>NeedNormal(); }; ComputeShade( TShadingSample& sSample, const TSceneBundle ) { TRGBColor saveColor = *( sSample.fBaseColor ); *( sSample.fBaseColor ) *= 2.; //Scale up by 2 fShaderB>ComputeShade(TShadingSample& sSample, const TSceneBundle ); *sSample.fBaseColor = saveColor; //Restore base color. } }; class ShaderB : public TShader { private: TShader * fShaderC; public: const TShader* GetChild(){ return fShaderC; }; virtual Boolean NeedColor() { return TRUE; }; virtual Boolean NeedNormal() { return TRUE; }; ComputeShade( TShadingSample& sSample, const TSceneBundle ) { saveColor = *(sSample.fBaseColor ); saveNormal = sSample.fShadingNormal; *( sSample.fBaseColor ) *= 3.; //Scale up by 3 sSample.fShadingNormal *= 1.; //Reverse Normal fShaderC>ComputeShade(TShadingSample& sSample, const TSceneBundle ); *sSample.fBaseColor = saveColor; //Restore base color. *sSample.fShadingNormal = saveNormal; } }; class ShaderC : public TShader { public: virtual Boolean NeedColor() { return TRUE }; virtual Boolean NeedNormal() { return TRUE }; ComputeShade( TShadingSample& sSample, const TSceneBundle ) { TRGBColor tmpCol = *(sSample.fBaseColor ) + TRGBColor( sSample.fShadingNormal.fX, sSample.fShadingNormal.fY, sSample.fShadingNormal.fZ ); *( sSample.fResultantColor ) = tmpCol * ComputeDiffuse *( sSample.fShadingNormal, TSceneBundle ) ); } };