Arranging shaders into a pipeline

You can arrange multiple shaders into a pipeline so that the ComputeShade function for each shader modifies the TShadingSample instance successively. You need to create a TShader derived class that is a wrapper for this pipeline. Your implementation of ComputeShade should return the final color.

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 ) );
                  }
      };

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker