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 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
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 // Integer Power using template metaprogramming...
00065 // TODO find a better way than this.
00066 // sort on N-tuples needs to know at C++ compile time exactly 
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 // A Generator point represents the position of a generator relative
00079 // to the cell origin of a lookup position. 
00080 //
00081 template<int D, typename T>
00082 struct Generator {
00083   Generator() {}
00084   ShAttrib<D, SH_TEMP, T> pos; // position 
00085   ShAttrib<D, SH_TEMP, T> offset; // offset of the cell relative to lookup point's cell
00086   ShAttrib<D, SH_TEMP, T> cell; // integer cell (this should actually be typed T = int, but not possible yet) 
00087 };
00088 
00089 // Generator Factory 
00090 // A Point Generator must implement a function that 
00091 // sets a Generator array with P candidate generator points. 
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 // The foundation for the regular grid-based generators
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     // given a Generator that has its cell and offset filled in, this function
00106     // generates the actual pos.
00107     virtual void makePos(Generator<D, T> &g) const = 0; 
00108 };
00109 
00110 // The default generator - uses a uniform integer grid with one
00111 // generator point in each grid cell.
00112 // If PointType is d-dimensional, it generates 3^d points from 
00113 // p's cell and all adjacent grid cells.
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 // generates in a 2D tiling of the plane by squares (but each
00124 // row is offset by 0.5 from the previous one, giving a hexagonal 
00125 // structure with less adjacent cells than the grid lookup)
00126 /*
00127 template<typename T>
00128 struct HexGenFactory {
00129   HexGenFactory(bool useTexture): m_useTexture(useTexture) {}
00130   void operator()(const ShGeneric<D, T> &p, Generator<D, T> result[]) const;
00131 
00132   private:
00133     bool m_useTexture;
00134 };
00135 */
00136 
00137 
00138 // a null point generator - just makes a non-jittered grid
00139 // of generators (good for debugging, or certain kinds of patterns...)
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 // An animating point generator - uses same uniform integer grid
00147 // method as the default, except that it linearly interpolates
00148 // between pairs of generator point sets, moving through a sequence
00149 // of point sets as time increases. 
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 // Property Factory
00161 // This takes a Generator and attaches N properties to it.
00162 // The first property in the result is used to sort the generator points 
00163 // (and so should probably be a distance metric).
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 // when ShProgram objects can be called like functions, this will 
00175 // no longer be necessary as it is analogous to the Algebra combine operator.
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 // standard distance based property factories 
00191 // Re-write later to take function pointer (or ShProgram object)
00192 // to a distance function.
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     // Moved here from WorleyImpl.hpp because MSVC gets confused otherwise
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     // TODO remove invScale and restrict to RECT textures later 
00246 };
00247 
00248 #ifndef WIN32
00249 // MSVC++ .NET does not recognize these as being the same as their implementation.
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 } // namespace ShUtil
00289 
00290 #include "ShWorleyImpl.hpp"
00291 
00292 #endif

Generated on Wed Jun 15 18:12:42 2005 for Sh by  doxygen 1.4.3-20050530