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_KERNELSURFACEIMPL_HPP
00028 #define SHUTIL_KERNELSURFACEIMPL_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 "ShKernelSurface.hpp"
00038 #include "ShFunc.hpp"
00039
00044 namespace ShUtil {
00045
00046 using namespace SH;
00047
00048 template<typename T>
00049 ShProgram ShKernelSurface::diffuse() {
00050 ShProgram kernel = SH_BEGIN_FRAGMENT_PROGRAM {
00051 typename T::InputType SH_DECL(kd);
00052 typename T::InputType SH_DECL(irrad);
00053 ShInputNormal3f SH_DECL(normal);
00054 ShInputVector3f SH_DECL(lightVec);
00055 ShInputPosition4f SH_DECL(posh);
00056
00057 irrad *= pos(dot(normalize(normal), normalize(lightVec)));
00058 typename T::OutputType SH_DECL(result);
00059 result = irrad * kd;
00060 } SH_END;
00061 return kernel;
00062 }
00063
00064 template<typename T>
00065 ShProgram ShKernelSurface::specular() {
00066 ShProgram kernel = SH_BEGIN_FRAGMENT_PROGRAM {
00067 typename T::InputType SH_DECL(ks);
00068 ShInputAttrib1f SH_DECL(specExp);
00069 typename T::InputType SH_DECL(irrad);
00070
00071 ShInputNormal3f SH_DECL(normal);
00072 ShInputVector3f SH_DECL(halfVec);
00073 ShInputVector3f SH_DECL(lightVec);
00074 ShInputPosition4f SH_DECL(posh);
00075
00076 normal = normalize(normal);
00077 halfVec = normalize(halfVec);
00078 lightVec = normalize(lightVec);
00079 irrad *= pos(normal | lightVec);
00080
00081 typename T::OutputType SH_DECL(result);
00082 result = irrad * ks * pow(pos(normal | halfVec),specExp);
00083 } SH_END;
00084 return kernel;
00085 }
00086
00087
00088 template<typename T>
00089 ShProgram ShKernelSurface::phong() {
00090 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") {
00091 typename T::InputType SH_DECL(kd);
00092 typename T::InputType SH_DECL(ks);
00093 ShInputAttrib1f SH_DECL(specExp);
00094 typename T::InputType SH_DECL(irrad);
00095
00096 ShInputNormal3f SH_DECL(normal);
00097 ShInputVector3f SH_DECL(halfVec);
00098 ShInputVector3f SH_DECL(lightVec);
00099 ShInputPosition4f SH_DECL(posh);
00100
00101 typename T::OutputType SH_DECL(result);
00102
00103 normal = normalize(normal);
00104 halfVec = normalize(halfVec);
00105 lightVec = normalize(lightVec);
00106 irrad *= pos(normal | lightVec);
00107 result = irrad * (kd + ks * pow(pos(normal | halfVec), specExp));
00108 } SH_END;
00109 return kernel;
00110 }
00111
00112 template<typename T>
00113 ShProgram ShKernelSurface::gooch() {
00114 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") {
00115 typename T::InputType SH_DECL(kd);
00116 typename T::InputType SH_DECL(cool);
00117 typename T::InputType SH_DECL(warm);
00118 typename T::InputType SH_DECL(irrad);
00119
00120 ShInputNormal3f SH_DECL(normal);
00121 ShInputVector3f SH_DECL(lightVec);
00122 ShInputPosition4f SH_DECL(posh);
00123
00124 typename T::OutputType SH_DECL(result);
00125
00126 normal = normalize(normal);
00127 lightVec = normalize(lightVec);
00128 result = lerp(mad((normal | lightVec), ShConstAttrib1f(0.5f), ShConstAttrib1f(0.5f)) * irrad, warm, cool) * kd;
00129 } SH_END;
00130 return kernel;
00131 }
00132
00133 template<typename T>
00134 ShProgram ShKernelSurface::null() {
00135 ShProgram kernel = SH_BEGIN_PROGRAM("gpu:fragment") {
00136 typename T::InputType SH_DECL(irrad);
00137 ShInputPosition4f SH_DECL(posh);
00138
00139 typename T::OutputType SH_DECL(result) = irrad;
00140 } SH_END;
00141 return kernel;
00142 }
00143
00144 }
00145
00146 #endif