Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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 
00193 // standard distance based property factories 
00194 // Re-write later to take function pointer (or ShProgram object)
00195 // to a distance function.
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     // Moved here from WorleyImpl.hpp because MSVC gets confused otherwise
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     // TODO remove invScale and restrict to RECT textures later 
00249 };
00250 
00251 #ifndef WIN32
00252 // MSVC++ .NET does not recognize these as being the same as their implementation.
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 } // namespace ShUtil
00292 
00293 #include "ShWorleyImpl.hpp"
00294 
00295 #endif

Generated on Mon Jan 24 18:36:37 2005 for Sh by  doxygen 1.4.1