00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020 #ifndef SHUTIL_FUNCIMPL_HPP
00021 #define SHUTIL_FUNCIMPL_HPP
00022
00023 #include <cmath>
00024 #include <numeric>
00025 #include "ShAttrib.hpp"
00026 #include "ShSwizzle.hpp"
00027 #include "ShVariable.hpp"
00028 #include "ShFunc.hpp"
00029 #include "ShLib.hpp"
00030
00031 namespace ShUtil {
00032
00033 using namespace SH;
00034
00035 template<int N, typename T>
00036 ShGeneric<N, T> deprecated_smoothstep(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b,
00037 const ShGeneric<N, T> x) {
00038 ShGeneric<N, T> t = (x - a) / (b - a);
00039
00040 t = clamp(t, 0.0f, 1.0f);
00041 return t * t * mad(-2.0f, t, ShConstAttrib1f(3.0f));
00042 }
00043
00044 static const int LCG_REPS = 5;
00051
00052
00053 template<int N, typename T>
00054 ShGeneric<N, T> hashlcg(const ShGeneric<N, T>& p) {
00055 ShAttrib<N, SH_TEMP, T> result = frac(p * 0.01);
00056
00057
00058 ShGeneric<N, T> a = fillcast<N>(
00059 ShConstAttrib4f(M_PI * M_PI * M_PI * M_PI, std::exp(4.0),
00060 std::pow(13.0, M_PI / 2.0), std::sqrt(1997.0)));
00061 ShGeneric<N, T> m = fillcast<N>(
00062 ShConstAttrib4f(std::sqrt(2.0), 1.0 / M_PI, std::sqrt(3.0),
00063 std::exp(-1.0)));
00064
00065 for(int i = 0; i < LCG_REPS; ++i) result = frac(mad(result, a, m));
00066 return result;
00067 }
00068
00072 template<typename T>
00073 ShGeneric<3, T> changeBasis(const ShGeneric<3, T> &b0,
00074 const ShGeneric<3, T> &b1, const ShGeneric<3, T> &b2, const ShGeneric<3, T> &v) {
00075 ShAttrib<3, SH_TEMP, T> result;
00076 result(0) = b0 | v;
00077 result(1) = b1 | v;
00078 result(2) = b2 | v;
00079 return result;
00080 }
00081
00084 template <int N, typename T>
00085 ShAttrib4f bernstein_basis_4(const ShGeneric<N, T>& t)
00086 {
00087 ShAttrib4f r;
00088 ShAttrib<N,SH_TEMP, T> it = fillcast<N>(ShAttrib<1,SH_TEMP, T>(1.0f)) - t;
00089 r(0) = it*it*it;
00090 r(1) = 3.0*it*it*t;
00091 r(2) = 3.0*it*t*t;
00092 r(3) = t*t*t;
00093 return r;
00094 }
00095
00098 template <int N, typename T>
00099 ShGeneric<N, T> bezier(const ShGeneric<N, T>& t, const ShAttrib4f& p)
00100 {
00101 ShAttrib4f B = bernstein_basis_4(t);
00102 ShGeneric<N, T> r = B[0] * p[0];
00103 for (int i=1; i<4; i++) {
00104 r += B[i] * p[i];
00105 }
00106 return r;
00107 }
00108
00109 }
00110
00111 #endif // SHUTIL_FUNCIMPL_HPP