00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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;
00072 irrad *= (t < cosf) * (t - cosang) / (cosf - cosang) + (t >= cosf);
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