Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShKernelLightImpl.hpp

Go to the documentation of this file.
00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Bryan Chan, Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #ifndef SHUTIL_KERNELLIGHTIMPL_HPP 
00028 #define SHUTIL_KERNELLIGHTIMPL_HPP 
00029 
00030 #include <sstream>
00031 #include "ShSyntax.hpp"
00032 #include "ShPosition.hpp"
00033 #include "ShManipulator.hpp"
00034 #include "ShAlgebra.hpp"
00035 #include "ShProgram.hpp"
00036 #include "ShNibbles.hpp"
00037 #include "ShKernelLight.hpp"
00038 
00043 namespace ShUtil {
00044 
00045 using namespace SH;
00046 
00047 template <typename T>
00048 ShProgram ShKernelLight::pointLight() {
00049   ShProgram kernel =  SH_BEGIN_PROGRAM() {
00050     typename T::InputType SH_DECL(lightColor);
00051     typename T::OutputType SH_DECL(irrad) = lightColor;
00052   } SH_END;
00053   return kernel;
00054 }
00055 
00056 template<typename T>
00057 ShProgram ShKernelLight::spotLight() {
00058   ShProgram kernel =  SH_BEGIN_PROGRAM() {
00059     typename T::InputType SH_DECL(lightColor);
00060     ShInputAttrib1f SH_DECL(falloff);
00061     ShInputAttrib1f SH_DECL(lightAngle);
00062     ShInputVector3f SH_DECL(lightDir);
00063     ShInputVector3f SH_DECL(lightVec);
00064 
00065     typename T::OutputType SH_DECL(irrad); 
00066 
00067     lightDir = normalize(lightDir);
00068     lightVec = normalize(lightVec);
00069     ShAttrib1f t = -lightDir | lightVec;
00070     ShAttrib1f cosf = cos(falloff);
00071     ShAttrib1f cosang = cos(lightAngle);
00072 
00073     irrad = lightColor;
00074     irrad *= t > cosang; // if outside light angle, always 0 
00075     irrad *= (t < cosf) * (t - cosang) / (cosf - cosang) + (t >= cosf); // linear blend between start of falloff and 0 
00076   } SH_END;
00077   return kernel;
00078 }
00079 
00080 template<typename T>
00081 ShProgram ShKernelLight::texLight2D(const ShBaseTexture2D<T> &tex) {
00082   ShProgram kernel =  SH_BEGIN_PROGRAM() {
00083     ShInputAttrib1f SH_DECL(scaling);
00084     ShInputAttrib1f SH_DECL(lightAngle);
00085     ShInputVector3f SH_DECL(lightDir);
00086     ShInputVector3f SH_DECL(lightUp);
00087     ShInputVector3f SH_DECL(lightVec);
00088 
00089     typename T::OutputType SH_DECL(irrad); 
00090 
00091     lightDir = normalize(lightDir);
00092     lightUp = normalize(lightUp);
00093     lightVec = normalize(lightVec);
00094     ShVector3f lightHoriz = cross(lightDir, lightUp);
00095 
00096     ShAttrib2f texcoord;
00097     texcoord(0) = frac(((-lightVec | lightHoriz) + ShConstAttrib1f(0.5f)) * scaling);
00098     texcoord(1) = frac(((-lightVec | lightUp) + ShConstAttrib1f(0.5f)) * scaling);
00099 
00100     irrad = tex(texcoord); 
00101   } SH_END;
00102   return kernel;
00103 }
00104 
00105 }
00106 
00107 #endif

Generated on Mon Jan 24 18:36:32 2005 for Sh by  doxygen 1.4.1