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