ShKernelLightImpl.hpp

Go to the documentation of this file.
00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHUTIL_KERNELLIGHTIMPL_HPP 
00021 #define SHUTIL_KERNELLIGHTIMPL_HPP 
00022 
00023 #include <sstream>
00024 #include "sh/ShSyntax.hpp"
00025 #include "sh/ShPosition.hpp"
00026 #include "sh/ShManipulator.hpp"
00027 #include "sh/ShAlgebra.hpp"
00028 #include "sh/ShProgram.hpp"
00029 #include "sh/ShNibbles.hpp"
00030 #include "ShKernelLight.hpp"
00031 
00036 namespace ShUtil {
00037 
00038 using namespace SH;
00039 
00040 template <typename T>
00041 ShProgram ShKernelLight::pointLight() {
00042   ShProgram kernel =  SH_BEGIN_PROGRAM() {
00043     typename T::InputType SH_DECL(lightColor);
00044     typename T::OutputType SH_DECL(irrad) = lightColor;
00045   } SH_END;
00046   return kernel;
00047 }
00048 
00049 template<typename T>
00050 ShProgram ShKernelLight::spotLight() {
00051   ShProgram kernel =  SH_BEGIN_PROGRAM() {
00052     typename T::InputType SH_DECL(lightColor);
00053     ShInputAttrib1f SH_DECL(falloff);
00054     ShInputAttrib1f SH_DECL(lightAngle);
00055     ShInputVector3f SH_DECL(lightDir);
00056     ShInputVector3f SH_DECL(lightVec);
00057 
00058     typename T::OutputType SH_DECL(irrad); 
00059 
00060     lightDir = normalize(lightDir);
00061     lightVec = normalize(lightVec);
00062     ShAttrib1f t = -lightDir | lightVec;
00063     ShAttrib1f cosf = cos(falloff);
00064     ShAttrib1f cosang = cos(lightAngle);
00065 
00066     irrad = lightColor;
00067     irrad *= t > cosang; // if outside light angle, always 0 
00068     irrad *= (t < cosf) * (t - cosang) / (cosf - cosang) + (t >= cosf); // linear blend between start of falloff and 0 
00069   } SH_END;
00070   return kernel;
00071 }
00072 
00073 template<typename T>
00074 ShProgram ShKernelLight::texLight2D(const ShBaseTexture2D<T> &tex) {
00075   ShProgram kernel =  SH_BEGIN_PROGRAM() {
00076     ShInputAttrib1f SH_DECL(scaling);
00077     ShInputAttrib1f SH_DECL(lightAngle);
00078     ShInputVector3f SH_DECL(lightDir);
00079     ShInputVector3f SH_DECL(lightUp);
00080     ShInputVector3f SH_DECL(lightVec);
00081 
00082     typename T::OutputType SH_DECL(irrad); 
00083 
00084     lightDir = normalize(lightDir);
00085     lightUp = normalize(lightUp);
00086     lightVec = normalize(lightVec);
00087     ShVector3f lightHoriz = cross(lightDir, lightUp);
00088 
00089     ShAttrib2f texcoord;
00090     texcoord(0) = frac(((-lightVec | lightHoriz) + ShConstAttrib1f(0.5f)) * scaling);
00091     texcoord(1) = frac(((-lightVec | lightUp) + ShConstAttrib1f(0.5f)) * scaling);
00092 
00093     irrad = tex(texcoord); 
00094   } SH_END;
00095   return kernel;
00096 }
00097 
00098 }
00099 
00100 #endif

Generated on Thu Feb 16 14:51:32 2006 for Sh by  doxygen 1.4.6