Main Page | Class Hierarchy | Class List | File List | Class Members

noise.h

00001 /* ************************************************************************* 00002 noise.h - description 00003 ------------------- 00004 begin : Die Jul 15 2003 00005 copyright : (C) 2003 by Micha Riser 00006 email : mriser@gmx.net 00007 00008 $Id: noise_8h-source.html,v 1.1 2005/02/04 21:02:27 micha Exp $ 00009 00010 ************************************************************************* */ 00011 00012 /* ************************************************************************* 00013 * * 00014 * This program is free software; you can redistribute it and/or modify * 00015 * it under the terms of the GNU General Public License as published by * 00016 * the Free Software Foundation; either version 2 of the License, or * 00017 * (at your option) any later version. * 00018 * * 00019 ************************************************************************* */ 00020 00021 #ifndef NOISE_H 00022 #define NOISE_H 00023 00024 #include "types.h" 00025 #include "vector.h" 00026 #include "function.h" 00027 #include "random.h" 00028 00031 template<class D, class R> class Noise { 00032 00033 public: 00034 Noise() {} 00035 virtual ~Noise() {} 00036 virtual void seed(long seedVal) = 0; 00037 00039 virtual R evalN(const D&) const = 0; 00040 00042 virtual R eval01(const D&) const = 0; 00043 00045 R sum1FNoise(D p, int octaves, DBL a, DBL b) const { 00046 R sum(0); 00047 DBL ai = 1; 00048 for(int i=0; i<octaves; i++, ai/=a, p*=b) 00049 sum += evalN(p)*ai; 00050 return sum; 00051 } 00052 00053 }; 00054 00055 00059 class VNoise: public Noise<Vector3,Vector3> { 00060 00061 typedef Noise<Vector3,DBL> NBase; 00062 00063 public: 00068 VNoise(NBase* ngx, NBase* ngy, NBase* ngz, long seedValue = 123): 00069 genx(ngx), geny(ngy), genz(ngz) 00070 { 00071 seed(seedValue); 00072 } 00073 00074 ~VNoise() { 00075 delete(genx); delete(geny); delete(genz); 00076 } 00077 00078 void seed(long seedValue) { 00079 genx->seed(seedValue); 00080 geny->seed(seedValue+1); 00081 genz->seed(seedValue+2); 00082 } 00083 00085 Vector3 evalN(const Vector3& p) const { 00086 return Vector3(genx->evalN(p),geny->evalN(p),genz->evalN(p)); 00087 } 00088 00090 Vector3 eval01(const Vector3& p) const { 00091 return Vector3(genx->eval01(p),geny->eval01(p),genz->eval01(p)); 00092 } 00093 00094 private: 00095 NBase *genx, *geny, *genz; 00096 00097 }; 00098 00099 00103 class PerlinNoiseCommon: public Noise<Vector3,DBL> { 00104 00105 public: 00106 PerlinNoiseCommon(DBL stddev): STDDEV(stddev) {}; 00107 00108 DBL evalN(const Vector3& p) const { 00109 return evaluate(p) / STDDEV; 00110 } 00111 00112 DBL eval01(const Vector3& p) const { 00113 // <1% of the values are clipped 00114 return clip01( evaluate(p) / (STDDEV*2*2.58)+.5 ); 00115 } 00116 00117 protected: 00118 const DBL STDDEV; 00120 unsigned int permutationTbl[514]; // 256*2 + 2 00121 Vector3 gradTbl[514]; 00122 00127 DBL evaluate(const Vector3& point) const; 00128 00129 DBL sCurve(DBL v) const {return ((6*v-15)*v+10)*pow3(v);} 00130 00131 DBL linInterp(DBL t, DBL a, DBL b) const {return t*(b - a) + a;} 00132 00133 void setupValues(DBL comp, int & gridIdx0, int & gridIdx1, DBL& dist0, DBL& dist1) const; 00134 00135 }; 00136 00137 00138 class PerlinNoiseBaseGradients: public PerlinNoiseCommon { 00139 public: 00140 PerlinNoiseBaseGradients(long sd = 123456789): 00141 PerlinNoiseCommon(0.256883) {seed(sd);} 00142 00143 void seed(long seedVal); 00144 00145 private: 00146 static const Vector3 baseGradients[16]; 00147 00148 }; 00149 00150 00151 class PerlinNoiseRandomGradients: public PerlinNoiseCommon { 00152 public: 00153 PerlinNoiseRandomGradients(long sd = 123456890): 00154 PerlinNoiseCommon(0.1496) {seed(sd);} 00155 00156 void seed(long seedVal); 00157 00158 }; 00159 00160 00161 typedef PerlinNoiseRandomGradients PerlinNoise; 00162 //typedef PerlinNoiseBaseGradients PerlinNoise; 00163 00164 #endif

Generated on Thu Jan 27 12:16:05 2005 for raytracer.kdevelop by doxygen 1.3.8