Creating a shader

You can derive directly from TShader to create your own shader if your shader can generate colors using only the information present in TShadingSample. Shaders derived directly from TShader are best for image previewing, where you want the maximum rendering speed, because they produce just enough shading information to reveal the 3D dimensionality of the graphics.

The following code creates a simple shader that assumes the light is at the location of the camera. Under this condition, the shadeSample.fShadingNormal.fZ field of the shading normal is the dot product between the light vector and the normal vector. When both the light and the object are white, the shadeSample.fShadingNormal.fZ component intensifies the color. This implementation requires that the drawing port generate a valid shading normal.

      class TLightAtTheEyeShader : public TShader
      {
          public:
      
              void ComputeShade( TShadingSample* shadeSample, const TSceneBundle& )
              {
                  shadeSample.fResultantColor>SetColor(
                      TRGBColor(  shadeSample.fShadingNormal.fZ,
                                  shadeSample.fShadingNormal.fZ,
                                  shadeSample.fShadingNormal.fZ ) );
              }
      
              Boolean NeedNormal(){ return TRUE; }
      
      };
The following code creates a simple shader that is independent of the camera. It uses the fZ component of the shading normal as an index to a simple color map. The value extracted from that color map is the color for the display.

      class TSimpleReflectShader : public TShader
      {
          public:
      
              void ComputeShade( TShadingSample& shadeSample, const TSceneBundle& )
              {
                  double y = (1. + shadeSample.fShadingNormal.fY )/2.;
      
                  for( long i = 0; i < totalMapEntries1; i++ )
                  {
                      if( y >= mapRange[i] && y < mapRange[i+1] )
                      {
                          double val =
                              ( y  mapRange[i] ) / ( mapRange[i+1]  mapRange[i] );
                          val = mapValue[i] * ( 1. val ) + mapValue[i+1] * val;
                          break;
                      }
                  }
      
                  shadeSample.fResultantColor>SetColor( TRGBColor( val, val, val ) );
      
              }
      
          private:
              static double mapValue[7];
              static double mapRange[7];
      };
      
      TSimpleReflectShader::mapValue = { 0., .2, 1., .3, .1, 1., 0. };
      TSimpleReflectShader::mapRange = { 0., .3, .5, .65, .8, .85, 1. };

[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