noise.cpp

Go to the documentation of this file.
00001 #include "noise.h"
00002 #include <syslog.h>
00003 #include <iostream>
00004 #if 10
00005 
00006 void Noise::init_noise(int seed)
00007 {
00008     srand(seed);
00009     int x1,y1,z1;
00010     for(int x = 0; x < MAX_NOISE; ++x)
00011     {
00012         for(int y = 0; y < MAX_NOISE; ++y)
00013         {
00014             for(int z = 0; z < MAX_NOISE; ++z)
00015             {
00016 
00017                 m_noiseMatrix[x][y][z] =  rand();
00018                 // Edges must be equal on each side for 
00019                 // interpolation to work correctly.
00020                 if(x == (MAX_NOISE-1) )
00021                     x1 = 0;
00022                 else
00023                     x1 = x;
00024 
00025                 if(y == (MAX_NOISE-1) )
00026                     y1 = 0;
00027                 else
00028                     y1 = y;
00029 
00030                 if(z == (MAX_NOISE -1) )
00031                     z1 = 0;
00032                 else
00033                     z1 = z;
00034 
00035                 m_noiseMatrix[x][y][z] = m_noiseMatrix[x1][y1][z1];
00036             } //for (z...
00037         }//for(y...
00038     }//for(x..
00039 }
00040     
00041 double Noise::get_noise(const Point3D& point) const
00042 {
00043     // Temp variables for interpolation
00044     int p000,p001, p010,p011, p100,p101, p110,p111;
00045     int p00, p01,  p10, p11;
00046     int p0,  p1;
00047     int d00, d01;
00048     int d10, d11;
00049     int d0, d1;
00050     int d;
00051 
00052     double f_x =  point.x ;
00053 
00054     double f_y =  point.y ;
00055 
00056     double f_z =  point.z ;
00057 
00058 #if 0
00059     f_x = fabs(f_x);
00060     f_y = fabs(f_y);
00061     f_z = fabs(f_z);
00062 
00063 #else
00064     while(f_x < 0.0)
00065         f_x += (MAX_NOISE -1 );
00066     while(f_y < 0.0)
00067         f_y += (MAX_NOISE -1 );
00068     while(f_z < 0.0)
00069         f_z += (MAX_NOISE -1 );
00070 #endif
00071 
00072 
00073     // TODO: replace with "double modf(double value, int* whole)"
00074     //
00075     // Whole portion of coordinates, wraped to size of noise matrix
00076     int x = (int)floor(f_x) % (MAX_NOISE - 1);
00077     int y = (int)floor(f_y) % (MAX_NOISE - 1);
00078     int z = (int)floor(f_z) % (MAX_NOISE - 1);
00079 
00080     // Fractional part of coordinates
00081     double ox = f_x - floor(f_x);
00082     double oy = f_y - floor(f_y);
00083     double oz = f_z - floor(f_z);
00084 
00085     // Store each matrix point that surround the point
00086     // we are interpolating in temp vars
00087     // to keep calculations later from being cluttered.
00088     p000 = m_noiseMatrix[x   ][y   ][z   ];
00089     p001 = m_noiseMatrix[x   ][y   ][z+1 ];
00090     p010 = m_noiseMatrix[x   ][y+1 ][z   ];
00091     p011 = m_noiseMatrix[x   ][y+1 ][z+1 ];
00092     p100 = m_noiseMatrix[x+1 ][y   ][z   ];
00093     p101 = m_noiseMatrix[x+1 ][y   ][z+1 ];
00094     p110 = m_noiseMatrix[x+1 ][y+1 ][z   ];
00095     p111 = m_noiseMatrix[x+1 ][y+1 ][z+1 ];
00096 
00097     d00 = p100 - p000;
00098     d01 = p101 - p001;
00099     d10 = p110 - p010;
00100     d11 = p111 - p011;
00101 
00102     p00 = ( (int) floor(d00 * ox) ) + p000;
00103     p01 = ( (int) floor(d01 * ox) ) + p001;
00104     p10 = ( (int) floor(d10 * ox) ) + p010;
00105     p11 = ( (int) floor(d11 * ox) ) + p011;
00106 
00107     d0 = p10 - p00;
00108     d1 = p11 - p01;
00109 
00110     p0 = (int) floor(d0 * oy) + p00;
00111     p1 = (int) floor(d1 * oy) + p01;
00112 
00113     d = p1 - p0;
00114 
00115     return ((d * oz) + (p0 * 1.0)) / (RAND_MAX * 1.0);
00116 }
00117 
00118 #endif

Generated on Tue Oct 30 22:12:15 2007 for mbrt by  doxygen 1.5.2