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_WORLEY_HPP
00025 #define SHUTIL_WORLEY_HPP
00026
00027 #include <vector>
00028 #include "ShAttrib.hpp"
00029 #include "ShColor.hpp"
00030 #include "ShTexture.hpp"
00031
00032 namespace ShUtil {
00033
00034 using namespace SH;
00064
00065
00066
00067 template<int N, int M>
00068 struct _IntPow {
00069 static const int value = N * _IntPow<N, M-1>::value;
00070 };
00071
00072 template<int N>
00073 struct _IntPow<N, 1> {
00074 static const int value = N;
00075 };
00076
00077
00078
00079
00080
00081 template<int D, typename T>
00082 struct Generator {
00083 Generator() {}
00084 ShAttrib<D, SH_TEMP, T> pos;
00085 ShAttrib<D, SH_TEMP, T> offset;
00086 ShAttrib<D, SH_TEMP, T> cell;
00087 };
00088
00089
00090
00091
00092 template<int P, int D, typename T>
00093 struct GeneratorFactory {
00094 static const int NUM_POINTS = P;
00095 virtual ~GeneratorFactory() {}
00096 virtual void operator()(const ShGeneric<D, T> &p, Generator<D, T> result[]) const = 0;
00097 };
00098
00099
00100 template<int D, typename T>
00101 struct GridGenFactory: public GeneratorFactory<_IntPow<3, D>::value, D, T> {
00102 void operator()(const ShGeneric<D, T> &p, Generator<D, T> result[]) const;
00103
00104 private:
00105
00106
00107 virtual void makePos(Generator<D, T> &g) const = 0;
00108 };
00109
00110
00111
00112
00113
00114 template<int D, typename T>
00115 struct DefaultGenFactory: public GridGenFactory<D, T> {
00116 DefaultGenFactory(bool useTexture): m_useTexture(useTexture) {}
00117
00118 private:
00119 void makePos(Generator<D, T> &g) const;
00120 bool m_useTexture;
00121 };
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 template<int D, typename T>
00141 struct NullGenFactory: public GridGenFactory<D, T> {
00142 private:
00143 void makePos(Generator<D, T> &g) const;
00144 };
00145
00146
00147
00148
00149
00150 template<int D, typename T>
00151 struct LerpGenFactory: GridGenFactory<D, T> {
00152 LerpGenFactory(const ShGeneric<1, T> &time, bool useTexture);
00153
00154 private:
00155 void makePos(Generator<D, T> &g) const;
00156 const ShGeneric<1, T> &m_time;
00157 bool m_useTexture;
00158 };
00159
00160
00161
00162
00163
00164 template<int N, int D, typename T>
00165 struct PropertyFactory {
00166 static const int NUM_PROPS = N;
00167 static const int DIM = D;
00168 typedef T PropType;
00169
00170 virtual ~PropertyFactory() {}
00171 virtual ShGeneric<N, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const = 0;
00172 };
00173
00174
00175
00176 template<int N, int D, typename T, typename P1, typename P2>
00177 struct CombinedPropFactory:
00178 public PropertyFactory<N, D, T> {
00179 CombinedPropFactory(const P1 *propFactory1,
00180 const P2 *propFactory2);
00181
00182 ShGeneric<N, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00183
00184 private:
00185 const P1* m_propFactory1;
00186 const P2* m_propFactory2;
00187 };
00188
00189
00190
00191
00192
00193 template<int D, typename T>
00194 struct DistSqPropFactory: public PropertyFactory<1, D, T> {
00195 ShGeneric<1, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00196 };
00197
00198 template<int D, typename T>
00199 struct Dist_1PropFactory: public PropertyFactory<1, D, T> {
00200 ShGeneric<1, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00201 };
00202
00203 template<int D, typename T>
00204 struct Dist_InfPropFactory: public PropertyFactory<1, D, T> {
00205 ShGeneric<1, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00206 };
00207
00208 template<int D, typename T>
00209 struct DistSqGradientPropFactory: public PropertyFactory<D + 1, D, T> {
00210 ShGeneric<D + 1, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00211 };
00212
00213 template<int D, typename T>
00214 struct Dist_1GradientPropFactory: public PropertyFactory<D + 1, D, T> {
00215 ShGeneric<D + 1, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00216 };
00217
00218 template<int D, typename T>
00219 struct Dist_InfGradientPropFactory: public PropertyFactory<D + 1, D, T> {
00220 ShGeneric<D + 1, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00221 };
00222
00223 template<int N, int D, typename T>
00224 struct CellnoisePropFactory: public PropertyFactory<N, D, T> {
00225 CellnoisePropFactory(bool useTexture): m_useTexture(useTexture) {}
00226 ShGeneric<N, T> operator()(const ShGeneric<D, T> &p, const Generator<D, T> &g) const;
00227
00228 private:
00229 bool m_useTexture;
00230 };
00231
00232 template<typename TexType, typename T>
00233 struct Tex2DPropFactory: public PropertyFactory<TexType::typesize, 2, T> {
00234 Tex2DPropFactory(const ShBaseTexture2D<TexType> &tex, const ShGeneric<1, T> &scale);
00235 ShGeneric<TexType::typesize, T> operator()(const ShGeneric<2, T> &p, const Generator<2, T> &g) const
00236 {
00237
00238 return m_tex(frac(g.cell * invScale * m_scale)) * ShConstAttrib1f(1.0f);
00239 }
00240
00241 private:
00242 const ShBaseTexture2D<TexType> &m_tex;
00243 const ShGeneric<1, T> &m_scale;
00244 ShConstAttrib2f invScale;
00245
00246 };
00247
00248 #ifndef WIN32
00249
00250 template<typename P1, typename P2>
00251 PropertyFactory<P1::NUM_PROPS + P2::NUM_PROPS, P1::DIM, typename P1::PropType>*
00252 combine(const P1 *propFactory1, const P2 *propFactory2);
00253
00259 template<int K, int D, typename T>
00260 ShGeneric<K, T> worley(const ShGeneric<D, T> &p, bool useTexture = true);
00262
00266 template<int K, int L, int P, int D, typename T>
00267 void worley(ShGeneric<K, T> result[], const ShGeneric<D, T> &p,
00268 const GeneratorFactory<P, D, T> *genFactory,
00269 const PropertyFactory<L, D, T> *propFactory);
00270
00278 template<int K, int D, typename T>
00279 ShProgram shWorley(bool useTexture);
00280
00281 template<int K, int N, int P, int D, typename T>
00282 ShProgram shWorley(const GeneratorFactory<P, D, T> *genFactory,
00283 const PropertyFactory<N, D, T> *propFactory);
00285
00286 #endif // ifndef WIN32
00287
00288 }
00289
00290 #include "ShWorleyImpl.hpp"
00291
00292 #endif