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