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
#ifndef WIN32
00193
00194
template<
typename P1,
typename P2>
00195 PropertyFactory<P1::NUM_PROPS + P2::NUM_PROPS, P1::DIM, typename P1::PropType>*
00196
combine(
const P1 *propFactory1,
const P2 *propFactory2);
00197
#endif
00198
00199
00200
00201
00202
template<
int D,
typename T>
00203
struct DistSqPropFactory:
public PropertyFactory<1, D, T> {
00204 ShGeneric<1, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00205 };
00206
00207
template<
int D,
typename T>
00208
struct Dist_1PropFactory:
public PropertyFactory<1, D, T> {
00209 ShGeneric<1, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00210 };
00211
00212
template<
int D,
typename T>
00213
struct Dist_InfPropFactory:
public PropertyFactory<1, D, T> {
00214 ShGeneric<1, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00215 };
00216
00217
template<
int D,
typename T>
00218
struct DistSqGradientPropFactory:
public PropertyFactory<D + 1, D, T> {
00219 ShGeneric<D + 1, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00220 };
00221
00222
template<
int D,
typename T>
00223
struct Dist_1GradientPropFactory:
public PropertyFactory<D + 1, D, T> {
00224 ShGeneric<D + 1, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00225 };
00226
00227
template<
int D,
typename T>
00228
struct Dist_InfGradientPropFactory:
public PropertyFactory<D + 1, D, T> {
00229 ShGeneric<D + 1, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00230 };
00231
00232
template<
int N,
int D,
typename T>
00233
struct CellnoisePropFactory:
public PropertyFactory<N, D, T> {
00234 CellnoisePropFactory(
bool useTexture): m_useTexture(useTexture) {}
00235 ShGeneric<N, T> operator()(
const ShGeneric<D, T> &p,
const Generator<D, T> &g)
const;
00236
00237
private:
00238
bool m_useTexture;
00239 };
00240
00241
template<
typename TexType,
typename T>
00242
struct Tex2DPropFactory:
public PropertyFactory<TexType::typesize, 2, T> {
00243 Tex2DPropFactory(
const ShBaseTexture2D<TexType> &tex,
const ShGeneric<1, T> &scale);
00244 ShGeneric<TexType::typesize, T> operator()(
const ShGeneric<2, T> &p,
const Generator<2, T> &g)
const
00245
{
00246
00247
return m_tex(
frac(g.cell * invScale * m_scale)) * ShConstAttrib1f(1.0f);
00248 }
00249
00250
private:
00251
const ShBaseTexture2D<TexType> &m_tex;
00252
const ShGeneric<1, T> &m_scale;
00253 ShConstAttrib2f invScale;
00254
00255 };
00256
00257
00263
template<
int K,
int D,
typename T>
00264 ShGeneric<K, T>
worley(
const ShGeneric<D, T> &p,
bool useTexture =
true);
00266
00270
template<
int K,
int L,
int P,
int D,
typename T>
00271
void worley(ShGeneric<K, T> result[],
const ShGeneric<D, T> &p,
00272
const GeneratorFactory<P, D, T> *genFactory,
00273
const PropertyFactory<L, D, T> *propFactory);
00274
00282
template<
int K,
int D,
typename T>
00283 ShProgram
shWorley(
bool useTexture);
00284
00285
template<
int K,
int N,
int P,
int D,
typename T>
00286 ShProgram
shWorley(
const GeneratorFactory<P, D, T> *genFactory,
00287
const PropertyFactory<N, D, T> *propFactory);
00289
00290
00291 }
00292
00293
#include "ShWorleyImpl.hpp"
00294
00295
#endif