Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShWorley.hpp

Go to the documentation of this file.
00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Bryan Chan, Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 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 // Integer Power using template metaprogramming... 00068 // TODO find a better way than this. 00069 // sort on N-tuples needs to know at C++ compile time exactly 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 // A Generator point represents the position of a generator relative 00082 // to the cell origin of a lookup position. 00083 // 00084 template<int D, typename T> 00085 struct Generator { 00086 Generator() {} 00087 ShAttrib<D, SH_TEMP, T> pos; // position 00088 ShAttrib<D, SH_TEMP, T> offset; // offset of the cell relative to lookup point's cell 00089 ShAttrib<D, SH_TEMP, T> cell; // integer cell (this should actually be typed T = int, but not possible yet) 00090 }; 00091 00092 // Generator Factory 00093 // A Point Generator must implement a function that 00094 // sets a Generator array with P candidate generator points. 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 // The foundation for the regular grid-based generators 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 // given a Generator that has its cell and offset filled in, this function 00109 // generates the actual pos. 00110 virtual void makePos(Generator<D, T> &g) const = 0; 00111 }; 00112 00113 // The default generator - uses a uniform integer grid with one 00114 // generator point in each grid cell. 00115 // If PointType is d-dimensional, it generates 3^d points from 00116 // p's cell and all adjacent grid cells. 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 // generates in a 2D tiling of the plane by squares (but each 00127 // row is offset by 0.5 from the previous one, giving a hexagonal 00128 // structure with less adjacent cells than the grid lookup) 00129 /* 00130 template<typename T> 00131 struct HexGenFactory { 00132 HexGenFactory(bool useTexture): m_useTexture(useTexture) {} 00133 void operator()(const ShGeneric<D, T> &p, Generator<D, T> result[]) const; 00134 00135 private: 00136 bool m_useTexture; 00137 }; 00138 */ 00139 00140 00141 // a null point generator - just makes a non-jittered grid 00142 // of generators (good for debugging, or certain kinds of patterns...) 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 // An animating point generator - uses same uniform integer grid 00150 // method as the default, except that it linearly interpolates 00151 // between pairs of generator point sets, moving through a sequence 00152 // of point sets as time increases. 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 // Property Factory 00164 // This takes a Generator and attaches N properties to it. 00165 // The first property in the result is used to sort the generator points 00166 // (and so should probably be a distance metric). 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 // when ShProgram objects can be called like functions, this will 00178 // no longer be necessary as it is analogous to the Algebra combine operator. 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 // MSVC++ .NET does not recognize this as being the same as its implementation. 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 // standard distance based property factories 00200 // Re-write later to take function pointer (or ShProgram object) 00201 // to a distance function. 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 // Moved here from WorleyImpl.hpp because MSVC gets confused otherwise 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 // TODO remove invScale and restrict to RECT textures later 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 } // namespace ShUtil 00292 00293 #include "ShWorleyImpl.hpp" 00294 00295 #endif

Generated on Mon Oct 18 14:17:40 2004 for Sh by doxygen 1.3.7