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