00001 #ifndef __SQUAREGEN
00002 #define __SQUAREGEN
00003 
00004 
00005 #include "GXStandardDefines.h"
00006 
00007 
00008 
00009 #include "GXSquareTemplate.h"
00010 #include "GXInteroplationTemplates.h"
00011 
00012 
00013 
00014 
00015 inline void BlakeUtilRandom(D3DXCOLOR& Min, D3DXCOLOR& Max, D3DXCOLOR& Out)
00016 {
00017         Out.r=BlakeUtilRandomFloat(Min.r, Max.r);
00018         Out.g=BlakeUtilRandomFloat(Min.g, Max.g);
00019         Out.b=BlakeUtilRandomFloat(Min.b, Max.b);
00020         Out.a=BlakeUtilRandomFloat(Min.a, Max.a);
00021 }
00022 
00023 
00024 
00025 inline void BlakeUtilRandom(float& Min, float& Max, float& Out)
00026 {
00027         Out=BlakeUtilRandomFloat(Min, Max);
00028 }
00029 
00030 
00031 
00032 
00033 template<typename type>
00034 void MakeRandom(type& Min, type& Max, 
00035                                 CGXSquare<type>& HeightMap,
00036                                 void (*pCallBack)(type*, const type*, const type*, float, void*, const D3DXVECTOR2*),
00037                                 void* pCallBackInfo)
00038 {
00039 
00040         UINT uSideSize=HeightMap.GetSideSize();
00041         float fSideSize=(float)uSideSize;
00042 
00043         for(UINT x=0;x<uSideSize;x++)
00044         {
00045                 for(UINT y=0;y<uSideSize;y++)
00046                 {
00047                         D3DXVECTOR2 vCoords(x/fSideSize, 
00048                                                                 y/fSideSize);
00049                         type val;
00050 
00051                         if(pCallBack)
00052                         {
00053                                 pCallBack(&val, &Min, &Max, BlakeUtilRandomFloat(0.0f, 1.0f), pCallBackInfo, &vCoords);
00054                         }
00055                         else
00056                         {
00057                                 BlakeUtilRandom(Min, Max, val);
00058                         }
00059 
00060                         HeightMap.SetVal(val, x, y);
00061                 }
00062         }
00063 }
00064 
00065 
00066                 
00067 
00068 template<typename type>
00069 bool TwoDSpline(CGXSquare<type>& ControlPoints, CGXSquare<type>& Map)
00070 {
00071 
00072         type* pArray=ControlPoints.GetArrayPointer();
00073         if(NULL==pArray)
00074         {
00075                 return false;
00076         }
00077 
00078         for(UINT x=0;x<Map.GetSideSize();x++)
00079         {
00080                 for(UINT y=0;y<Map.GetSideSize();y++)
00081                 {
00082                         float fWeightX=x/(float)Map.GetSideSize();
00083                         float fWeightY=y/(float)Map.GetSideSize();
00084 
00085                         type Val;
00086                         if(!BlakeUtil2DSpline(fWeightX, fWeightY, pArray,
00087                                                                          (ControlPoints.GetSideSize()*ControlPoints.GetSideSize()),
00088                                                                          &Val))
00089                         {
00090                                 return false;
00091                         }
00092 
00093                         Map.SetVal(Val,x,y);
00094                 }
00095         }
00096 
00097         return true;
00098 }
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 template<typename type>
00133 bool GXGenerateMipMap(UINT uIndex, CGXSquare<type>& Out,
00134                                         type& ZeroVal, type& Max, 
00135                                         float fFalloff, UINT uMipCount,
00136                                         void (*pCallBack)(type*, const type*, const type*, float, void*, const D3DXVECTOR2*),
00137                                         void* pCallBackInfo)
00138 {
00139         if(0==uMipCount)
00140         {
00141                 return false;
00142         }
00143 
00144         UINT uCtr;
00145 
00146         float fAdjustmentRatio=0;
00147         for(uCtr=0;uCtr<uMipCount;uCtr++)
00148         {
00149                 fAdjustmentRatio+=(float)(pow(fFalloff,uCtr));
00150         }
00151 
00152         type CurrentMax=Max/fAdjustmentRatio;
00153 
00154 
00155 
00156         UINT uCurrentSize=(UINT)(pow(2,uIndex));
00157         
00158 
00159         
00160         for(UINT uInnerCtr=0;uInnerCtr<uIndex;uInnerCtr++)
00161         {       
00162                 CurrentMax*=fFalloff;
00163         }
00164                 
00165 
00166         if(!Out.Init(uCurrentSize, &ZeroVal))
00167         {
00168                 return false;
00169         }
00170 
00171 
00172 
00173         MakeRandom(ZeroVal, CurrentMax, Out, 
00174                                 pCallBack, pCallBackInfo);
00175         
00176         
00177 
00178         return true;
00179 }
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 template<typename type>
00192 bool GXMakeNoise(type& ZeroVal, type& Max, float fFalloff, 
00193                                 UINT uMipCount,
00194                            CGXSquare<type>& Map,
00195                            void (*pCallBack)(type*, const type*, const type*, float, void*, const D3DXVECTOR2*),
00196                            void* pCallBackInfo)
00197 {
00198         
00199         if(!Map.Init(Map.GetSideSize(), &ZeroVal))
00200         {
00201                 return false;
00202         }
00203 
00204 
00205         if((uMipCount==0))
00206         {
00207                 uMipCount=(UINT)(log(Map.GetSideSize())/log(2)) + 1;
00208         }
00209 
00210 
00211         if(0==uMipCount)
00212         {
00213                 return false;
00214         }
00215 
00216         
00217         CGXSquare<type>* pMipChain=new CGXSquare<type>[uMipCount];
00218         if(!pMipChain)
00219         {
00220                 return false;
00221         }
00222 
00223         UINT uCtr;
00224 
00225 
00226         float fAdjustmentRatio=0;
00227         for(uCtr=0;uCtr<uMipCount;uCtr++)
00228         {
00229                 fAdjustmentRatio+=(float)(pow(fFalloff,uCtr));
00230         }
00231 
00232 
00233 
00234 
00235         
00236 
00237 
00238         for(uCtr=0;uCtr<uMipCount;uCtr++)
00239         {
00240                 if(!GXGenerateMipMap(uCtr, pMipChain[uCtr], ZeroVal, Max, fFalloff,
00241                                                         uMipCount, pCallBack, pCallBackInfo))
00242                 {
00243                         SAFE_ARRAY_DELETE(pMipChain);
00244                         return false;
00245                 }
00246         }
00247                 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274         
00275 
00276 
00277         for(uCtr=0;uCtr<uMipCount;uCtr++)
00278         {
00279                 Map+=pMipChain[uCtr];
00280         }
00281 
00282         
00283 
00284         SAFE_ARRAY_DELETE(pMipChain);
00285         return true;
00286 }
00287 
00288 
00289 
00290                         
00291 
00292 #endif