src/CombineShader.h

Go to the documentation of this file.
00001 #ifndef COMBINESHADER_H
00002 #define COMBINESHADER_H
00003 
00004 #include <vector>
00005 
00006 #include "RGBAColor.h"
00007 #include "defines.h"
00008 
00009 
00015 class CombineShader : public Shader
00016 {
00017 public:
00018     
00024     CombineShader(  Scene * scene,
00025                     bool    addShaders )
00026         : Shader(scene),
00027             mLiShader(NULL, 0.0f),
00028             mTrShader(NULL, 0.0f),
00029             mAddShaders(addShaders)
00030     {
00031     }
00032 
00035     virtual RGBAColor shade(const Ray & ray) const
00036     {
00037         //     for (std::vector<std::pair<Shader *, float> >::const_iterator it=mShaders.begin(); it != mShaders.end(); it++)
00038 //            result = lerp(result, it->first->shade(ray), it->second);
00039         //    result += it->first->shade(ray) * it->second;
00040         
00041         // shade lighting
00042         RGBAColor lighting;
00043         if (mLiShader.first)
00044             lighting = mLiShader.first->shade(ray);
00045         lighting.setA(1);
00046 
00047         // add Textures
00048         if (textureCount() > 0)
00049         {
00050             std::vector<Texture*>::const_iterator it = mTextures.begin();
00051             RGBAColor texes = (*it)->texel(ray.hit()->texCoord(ray));     
00052             ++it;
00053             for(; it!=mTextures.end(); ++it)
00054             {
00055                 // get texture color
00056                 const RGBAColor tex = (*it)->texel(ray.hit()->texCoord(ray));
00057 
00058                 // combine texture color with the computed result
00059                 texes = lerp(texes, tex, tex.a());
00060             }
00061 
00062             // add lighting
00063             lighting.scale(texes);
00064         }
00065 
00066         // set up adaptive recursion termination
00067         Ray tray = ray;
00068         tray.updateInfluence(ray, mAddShaders ? mTrShader.second : mTrShader.second * (1.0f - lighting.a()));
00069         // shade transparency
00070         RGBAColor transparency;
00071         if (mTrShader.first)
00072             transparency = mTrShader.first->shade(tray);
00073         transparency.setA(1);
00074 
00075         if (mAddShaders)
00076             return RGBAColor(transparency.rgb() * mTrShader.second + lighting.rgb() * mLiShader.second);
00077         else
00078             return RGBAColor(lerp(transparency.rgb() * mTrShader.second, lighting.rgb() * mLiShader.second, lighting.a()));
00079     }
00080 
00086     void setLightingShader(Shader * sh, float alpha)
00087     {
00088         if (!sh)
00089             alpha = 0;
00090         mLiShader = std::make_pair(sh, alpha);
00091     }
00092 
00093 
00099     void setTransparentShader(Shader * sh, float alpha)
00100     {
00101         if (!sh)
00102             alpha = 0;
00103         mTrShader = std::make_pair(sh, alpha);
00104     }
00105 
00106 private:
00108     std::pair<Shader *, float>  mLiShader;
00109 
00111     std::pair<Shader *, float>  mTrShader;
00112 
00114     bool                        mAddShaders;
00115 };
00116 
00117 #endif
00118 

Generated on Fri Feb 1 00:01:42 2008 for Grayfall by  doxygen 1.5.1