Blender V2.61 - r43446

noise.c

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  *
00029  */
00030 
00036 #ifdef _WIN32    
00037 #pragma warning (disable : 4244) // "conversion from double to float"
00038 #pragma warning (disable : 4305) // "truncation from const double to float" 
00039 #endif
00040 
00041 #include <math.h>
00042 
00043 #include "BLI_noise.h"
00044 
00045 /* local */
00046 static float noise3_perlin(float vec[3]);
00047 //static float turbulence_perlin(float *point, float lofreq, float hifreq);
00048 //static float turbulencep(float noisesize, float x, float y, float z, int nr);
00049 
00050 #define HASHVEC(x,y,z) hashvectf+3*hash[ (hash[ (hash[(z) & 255]+(y)) & 255]+(x)) & 255]
00051 
00052 /* needed for voronoi */
00053 #define HASHPNT(x,y,z) hashpntf+3*hash[ (hash[ (hash[(z) & 255]+(y)) & 255]+(x)) & 255]
00054 static float hashpntf[768] = {0.536902, 0.020915, 0.501445, 0.216316, 0.517036, 0.822466, 0.965315,
00055 0.377313, 0.678764, 0.744545, 0.097731, 0.396357, 0.247202, 0.520897,
00056 0.613396, 0.542124, 0.146813, 0.255489, 0.810868, 0.638641, 0.980742,
00057 0.292316, 0.357948, 0.114382, 0.861377, 0.629634, 0.722530, 0.714103,
00058 0.048549, 0.075668, 0.564920, 0.162026, 0.054466, 0.411738, 0.156897,
00059 0.887657, 0.599368, 0.074249, 0.170277, 0.225799, 0.393154, 0.301348,
00060 0.057434, 0.293849, 0.442745, 0.150002, 0.398732, 0.184582, 0.915200,
00061 0.630984, 0.974040, 0.117228, 0.795520, 0.763238, 0.158982, 0.616211,
00062 0.250825, 0.906539, 0.316874, 0.676205, 0.234720, 0.667673, 0.792225,
00063 0.273671, 0.119363, 0.199131, 0.856716, 0.828554, 0.900718, 0.705960,
00064 0.635923, 0.989433, 0.027261, 0.283507, 0.113426, 0.388115, 0.900176,
00065 0.637741, 0.438802, 0.715490, 0.043692, 0.202640, 0.378325, 0.450325,
00066 0.471832, 0.147803, 0.906899, 0.524178, 0.784981, 0.051483, 0.893369,
00067 0.596895, 0.275635, 0.391483, 0.844673, 0.103061, 0.257322, 0.708390,
00068 0.504091, 0.199517, 0.660339, 0.376071, 0.038880, 0.531293, 0.216116,
00069 0.138672, 0.907737, 0.807994, 0.659582, 0.915264, 0.449075, 0.627128,
00070 0.480173, 0.380942, 0.018843, 0.211808, 0.569701, 0.082294, 0.689488, 
00071 0.573060, 0.593859, 0.216080, 0.373159, 0.108117, 0.595539, 0.021768, 
00072 0.380297, 0.948125, 0.377833, 0.319699, 0.315249, 0.972805, 0.792270, 
00073 0.445396, 0.845323, 0.372186, 0.096147, 0.689405, 0.423958, 0.055675, 
00074 0.117940, 0.328456, 0.605808, 0.631768, 0.372170, 0.213723, 0.032700, 
00075 0.447257, 0.440661, 0.728488, 0.299853, 0.148599, 0.649212, 0.498381,
00076 0.049921, 0.496112, 0.607142, 0.562595, 0.990246, 0.739659, 0.108633, 
00077 0.978156, 0.209814, 0.258436, 0.876021, 0.309260, 0.600673, 0.713597, 
00078 0.576967, 0.641402, 0.853930, 0.029173, 0.418111, 0.581593, 0.008394, 
00079 0.589904, 0.661574, 0.979326, 0.275724, 0.111109, 0.440472, 0.120839, 
00080 0.521602, 0.648308, 0.284575, 0.204501, 0.153286, 0.822444, 0.300786, 
00081 0.303906, 0.364717, 0.209038, 0.916831, 0.900245, 0.600685, 0.890002, 
00082 0.581660, 0.431154, 0.705569, 0.551250, 0.417075, 0.403749, 0.696652, 
00083 0.292652, 0.911372, 0.690922, 0.323718, 0.036773, 0.258976, 0.274265, 
00084 0.225076, 0.628965, 0.351644, 0.065158, 0.080340, 0.467271, 0.130643,
00085 0.385914, 0.919315, 0.253821, 0.966163, 0.017439, 0.392610, 0.478792, 
00086 0.978185, 0.072691, 0.982009, 0.097987, 0.731533, 0.401233, 0.107570, 
00087 0.349587, 0.479122, 0.700598, 0.481751, 0.788429, 0.706864, 0.120086, 
00088 0.562691, 0.981797, 0.001223, 0.192120, 0.451543, 0.173092, 0.108960,
00089 0.549594, 0.587892, 0.657534, 0.396365, 0.125153, 0.666420, 0.385823, 
00090 0.890916, 0.436729, 0.128114, 0.369598, 0.759096, 0.044677, 0.904752, 
00091 0.088052, 0.621148, 0.005047, 0.452331, 0.162032, 0.494238, 0.523349, 
00092 0.741829, 0.698450, 0.452316, 0.563487, 0.819776, 0.492160, 0.004210, 
00093 0.647158, 0.551475, 0.362995, 0.177937, 0.814722, 0.727729, 0.867126, 
00094 0.997157, 0.108149, 0.085726, 0.796024, 0.665075, 0.362462, 0.323124,
00095 0.043718, 0.042357, 0.315030, 0.328954, 0.870845, 0.683186, 0.467922, 
00096 0.514894, 0.809971, 0.631979, 0.176571, 0.366320, 0.850621, 0.505555, 
00097 0.749551, 0.750830, 0.401714, 0.481216, 0.438393, 0.508832, 0.867971, 
00098 0.654581, 0.058204, 0.566454, 0.084124, 0.548539, 0.902690, 0.779571, 
00099 0.562058, 0.048082, 0.863109, 0.079290, 0.713559, 0.783496, 0.265266, 
00100 0.672089, 0.786939, 0.143048, 0.086196, 0.876129, 0.408708, 0.229312, 
00101 0.629995, 0.206665, 0.207308, 0.710079, 0.341704, 0.264921, 0.028748, 
00102 0.629222, 0.470173, 0.726228, 0.125243, 0.328249, 0.794187, 0.741340, 
00103 0.489895, 0.189396, 0.724654, 0.092841, 0.039809, 0.860126, 0.247701, 
00104 0.655331, 0.964121, 0.672536, 0.044522, 0.690567, 0.837238, 0.631520, 
00105 0.953734, 0.352484, 0.289026, 0.034152, 0.852575, 0.098454, 0.795529, 
00106 0.452181, 0.826159, 0.186993, 0.820725, 0.440328, 0.922137, 0.704592,
00107 0.915437, 0.738183, 0.733461, 0.193798, 0.929213, 0.161390, 0.318547,
00108 0.888751, 0.430968, 0.740837, 0.193544, 0.872253, 0.563074, 0.274598, 
00109 0.347805, 0.666176, 0.449831, 0.800991, 0.588727, 0.052296, 0.714761, 
00110 0.420620, 0.570325, 0.057550, 0.210888, 0.407312, 0.662848, 0.924382, 
00111 0.895958, 0.775198, 0.688605, 0.025721, 0.301913, 0.791408, 0.500602, 
00112 0.831984, 0.828509, 0.642093, 0.494174, 0.525880, 0.446365, 0.440063, 
00113 0.763114, 0.630358, 0.223943, 0.333806, 0.906033, 0.498306, 0.241278,
00114 0.427640, 0.772683, 0.198082, 0.225379, 0.503894, 0.436599, 0.016503, 
00115 0.803725, 0.189878, 0.291095, 0.499114, 0.151573, 0.079031, 0.904618, 
00116 0.708535, 0.273900, 0.067419, 0.317124, 0.936499, 0.716511, 0.543845, 
00117 0.939909, 0.826574, 0.715090, 0.154864, 0.750150, 0.845808, 0.648108, 
00118 0.556564, 0.644757, 0.140873, 0.799167, 0.632989, 0.444245, 0.471978, 
00119 0.435910, 0.359793, 0.216241, 0.007633, 0.337236, 0.857863, 0.380247, 
00120 0.092517, 0.799973, 0.919000, 0.296798, 0.096989, 0.854831, 0.165369, 
00121 0.568475, 0.216855, 0.020457, 0.835511, 0.538039, 0.999742, 0.620226, 
00122 0.244053, 0.060399, 0.323007, 0.294874, 0.988899, 0.384919, 0.735655, 
00123 0.773428, 0.549776, 0.292882, 0.660611, 0.593507, 0.621118, 0.175269, 
00124 0.682119, 0.794493, 0.868197, 0.632150, 0.807823, 0.509656, 0.482035, 
00125 0.001780, 0.259126, 0.358002, 0.280263, 0.192985, 0.290367, 0.208111, 
00126 0.917633, 0.114422, 0.925491, 0.981110, 0.255570, 0.974862, 0.016629,
00127 0.552599, 0.575741, 0.612978, 0.615965, 0.803615, 0.772334, 0.089745, 
00128 0.838812, 0.634542, 0.113709, 0.755832, 0.577589, 0.667489, 0.529834,
00129 0.325660, 0.817597, 0.316557, 0.335093, 0.737363, 0.260951, 0.737073, 
00130 0.049540, 0.735541, 0.988891, 0.299116, 0.147695, 0.417271, 0.940811, 
00131 0.524160, 0.857968, 0.176403, 0.244835, 0.485759, 0.033353, 0.280319, 
00132 0.750688, 0.755809, 0.924208, 0.095956, 0.962504, 0.275584, 0.173715,
00133 0.942716, 0.706721, 0.078464, 0.576716, 0.804667, 0.559249, 0.900611, 
00134 0.646904, 0.432111, 0.927885, 0.383277, 0.269973, 0.114244, 0.574867, 
00135 0.150703, 0.241855, 0.272871, 0.199950, 0.079719, 0.868566, 0.962833, 
00136 0.789122, 0.320025, 0.905554, 0.234876, 0.991356, 0.061913, 0.732911, 
00137 0.785960, 0.874074, 0.069035, 0.658632, 0.309901, 0.023676, 0.791603, 
00138 0.764661, 0.661278, 0.319583, 0.829650, 0.117091, 0.903124, 0.982098, 
00139 0.161631, 0.193576, 0.670428, 0.857390, 0.003760, 0.572578, 0.222162, 
00140 0.114551, 0.420118, 0.530404, 0.470682, 0.525527, 0.764281, 0.040596, 
00141 0.443275, 0.501124, 0.816161, 0.417467, 0.332172, 0.447565, 0.614591, 
00142 0.559246, 0.805295, 0.226342, 0.155065, 0.714630, 0.160925, 0.760001, 
00143 0.453456, 0.093869, 0.406092, 0.264801, 0.720370, 0.743388, 0.373269, 
00144 0.403098, 0.911923, 0.897249, 0.147038, 0.753037, 0.516093, 0.739257, 
00145 0.175018, 0.045768, 0.735857, 0.801330, 0.927708, 0.240977, 0.591870,
00146 0.921831, 0.540733, 0.149100, 0.423152, 0.806876, 0.397081, 0.061100, 
00147 0.811630, 0.044899, 0.460915, 0.961202, 0.822098, 0.971524, 0.867608, 
00148 0.773604, 0.226616, 0.686286, 0.926972, 0.411613, 0.267873, 0.081937, 
00149 0.226124, 0.295664, 0.374594, 0.533240, 0.237876, 0.669629, 0.599083, 
00150 0.513081, 0.878719, 0.201577, 0.721296, 0.495038, 0.079760, 0.965959,
00151 0.233090, 0.052496, 0.714748, 0.887844, 0.308724, 0.972885, 0.723337,
00152 0.453089, 0.914474, 0.704063, 0.823198, 0.834769, 0.906561, 0.919600,
00153 0.100601, 0.307564, 0.901977, 0.468879, 0.265376, 0.885188, 0.683875,
00154 0.868623, 0.081032, 0.466835, 0.199087, 0.663437, 0.812241, 0.311337,
00155 0.821361, 0.356628, 0.898054, 0.160781, 0.222539, 0.714889, 0.490287,
00156 0.984915, 0.951755, 0.964097, 0.641795, 0.815472, 0.852732, 0.862074,
00157 0.051108, 0.440139, 0.323207, 0.517171, 0.562984, 0.115295, 0.743103,
00158 0.977914, 0.337596, 0.440694, 0.535879, 0.959427, 0.351427, 0.704361,
00159 0.010826, 0.131162, 0.577080, 0.349572, 0.774892, 0.425796, 0.072697,
00160 0.500001, 0.267322, 0.909654, 0.206176, 0.223987, 0.937698, 0.323423,
00161 0.117501, 0.490308, 0.474372, 0.689943, 0.168671, 0.719417, 0.188928,
00162 0.330464, 0.265273, 0.446271, 0.171933, 0.176133, 0.474616, 0.140182,
00163 0.114246, 0.905043, 0.713870, 0.555261, 0.951333};
00164 
00165 unsigned char hash[512]= {
00166 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
00167 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
00168 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
00169 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
00170 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
00171 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
00172 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
00173 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
00174 };
00175 
00176 
00177 float hashvectf[768]= {
00178 0.33783,0.715698,-0.611206,-0.944031,-0.326599,-0.045624,-0.101074,-0.416443,-0.903503,0.799286,0.49411,-0.341949,-0.854645,0.518036,0.033936,0.42514,-0.437866,-0.792114,-0.358948,0.597046,0.717377,-0.985413,0.144714,0.089294,-0.601776,-0.33728,-0.723907,-0.449921,0.594513,0.666382,0.208313,-0.10791,
00179 0.972076,0.575317,0.060425,0.815643,0.293365,-0.875702,-0.383453,0.293762,0.465759,0.834686,-0.846008,-0.233398,-0.47934,-0.115814,0.143036,-0.98291,0.204681,-0.949036,-0.239532,0.946716,-0.263947,0.184326,-0.235596,0.573822,0.784332,0.203705,-0.372253,-0.905487,0.756989,-0.651031,0.055298,0.497803,
00180 0.814697,-0.297363,-0.16214,0.063995,-0.98468,-0.329254,0.834381,0.441925,0.703827,-0.527039,-0.476227,0.956421,0.266113,0.119781,0.480133,0.482849,0.7323,-0.18631,0.961212,-0.203125,-0.748474,-0.656921,-0.090393,-0.085052,-0.165253,0.982544,-0.76947,0.628174,-0.115234,0.383148,0.537659,0.751068,
00181 0.616486,-0.668488,-0.415924,-0.259979,-0.630005,0.73175,0.570953,-0.087952,0.816223,-0.458008,0.023254,0.888611,-0.196167,0.976563,-0.088287,-0.263885,-0.69812,-0.665527,0.437134,-0.892273,-0.112793,-0.621674,-0.230438,0.748566,0.232422,0.900574,-0.367249,0.22229,-0.796143,0.562744,-0.665497,-0.73764,
00182 0.11377,0.670135,0.704803,0.232605,0.895599,0.429749,-0.114655,-0.11557,-0.474243,0.872742,0.621826,0.604004,-0.498444,-0.832214,0.012756,0.55426,-0.702484,0.705994,-0.089661,-0.692017,0.649292,0.315399,-0.175995,-0.977997,0.111877,0.096954,-0.04953,0.994019,0.635284,-0.606689,-0.477783,-0.261261,
00183 -0.607422,-0.750153,0.983276,0.165436,0.075958,-0.29837,0.404083,-0.864655,-0.638672,0.507721,0.578156,0.388214,0.412079,0.824249,0.556183,-0.208832,0.804352,0.778442,0.562012,0.27951,-0.616577,0.781921,-0.091522,0.196289,0.051056,0.979187,-0.121216,0.207153,-0.970734,-0.173401,-0.384735,0.906555,
00184 0.161499,-0.723236,-0.671387,0.178497,-0.006226,-0.983887,-0.126038,0.15799,0.97934,0.830475,-0.024811,0.556458,-0.510132,-0.76944,0.384247,0.81424,0.200104,-0.544891,-0.112549,-0.393311,-0.912445,0.56189,0.152222,-0.813049,0.198914,-0.254517,-0.946381,-0.41217,0.690979,-0.593811,-0.407257,0.324524,
00185 0.853668,-0.690186,0.366119,-0.624115,-0.428345,0.844147,-0.322296,-0.21228,-0.297546,-0.930756,-0.273071,0.516113,0.811798,0.928314,0.371643,0.007233,0.785828,-0.479218,-0.390778,-0.704895,0.058929,0.706818,0.173248,0.203583,0.963562,0.422211,-0.904297,-0.062469,-0.363312,-0.182465,0.913605,0.254028,
00186 -0.552307,-0.793945,-0.28891,-0.765747,-0.574554,0.058319,0.291382,0.954803,0.946136,-0.303925,0.111267,-0.078156,0.443695,-0.892731,0.182098,0.89389,0.409515,-0.680298,-0.213318,0.701141,0.062469,0.848389,-0.525635,-0.72879,-0.641846,0.238342,-0.88089,0.427673,0.202637,-0.532501,-0.21405,0.818878,
00187 0.948975,-0.305084,0.07962,0.925446,0.374664,0.055817,0.820923,0.565491,0.079102,0.25882,0.099792,-0.960724,-0.294617,0.910522,0.289978,0.137115,0.320038,-0.937408,-0.908386,0.345276,-0.235718,-0.936218,0.138763,0.322754,0.366577,0.925934,-0.090637,0.309296,-0.686829,-0.657684,0.66983,0.024445,
00188 0.742065,-0.917999,-0.059113,-0.392059,0.365509,0.462158,-0.807922,0.083374,0.996399,-0.014801,0.593842,0.253143,-0.763672,0.974976,-0.165466,0.148285,0.918976,0.137299,0.369537,0.294952,0.694977,0.655731,0.943085,0.152618,-0.295319,0.58783,-0.598236,0.544495,0.203796,0.678223,0.705994,-0.478821,
00189 -0.661011,0.577667,0.719055,-0.1698,-0.673828,-0.132172,-0.965332,0.225006,-0.981873,-0.14502,0.121979,0.763458,0.579742,0.284546,-0.893188,0.079681,0.442474,-0.795776,-0.523804,0.303802,0.734955,0.67804,-0.007446,0.15506,0.986267,-0.056183,0.258026,0.571503,-0.778931,-0.681549,-0.702087,-0.206116,
00190 -0.96286,-0.177185,0.203613,-0.470978,-0.515106,0.716095,-0.740326,0.57135,0.354095,-0.56012,-0.824982,-0.074982,-0.507874,0.753204,0.417969,-0.503113,0.038147,0.863342,0.594025,0.673553,-0.439758,-0.119873,-0.005524,-0.992737,0.098267,-0.213776,0.971893,-0.615631,0.643951,0.454163,0.896851,-0.441071,
00191 0.032166,-0.555023,0.750763,-0.358093,0.398773,0.304688,0.864929,-0.722961,0.303589,0.620544,-0.63559,-0.621948,-0.457306,-0.293243,0.072327,0.953278,-0.491638,0.661041,-0.566772,-0.304199,-0.572083,-0.761688,0.908081,-0.398956,0.127014,-0.523621,-0.549683,-0.650848,-0.932922,-0.19986,0.299408,0.099426,
00192 0.140869,0.984985,-0.020325,-0.999756,-0.002319,0.952667,0.280853,-0.11615,-0.971893,0.082581,0.220337,0.65921,0.705292,-0.260651,0.733063,-0.175537,0.657043,-0.555206,0.429504,-0.712189,0.400421,-0.89859,0.179352,0.750885,-0.19696,0.630341,0.785675,-0.569336,0.241821,-0.058899,-0.464111,0.883789,
00193 0.129608,-0.94519,0.299622,-0.357819,0.907654,0.219238,-0.842133,-0.439117,-0.312927,-0.313477,0.84433,0.434479,-0.241211,0.053253,0.968994,0.063873,0.823273,0.563965,0.476288,0.862152,-0.172516,0.620941,-0.298126,0.724915,0.25238,-0.749359,-0.612122,-0.577545,0.386566,0.718994,-0.406342,-0.737976,
00194 0.538696,0.04718,0.556305,0.82959,-0.802856,0.587463,0.101166,-0.707733,-0.705963,0.026428,0.374908,0.68457,0.625092,0.472137,0.208405,-0.856506,-0.703064,-0.581085,-0.409821,-0.417206,-0.736328,0.532623,-0.447876,-0.20285,-0.870728,0.086945,-0.990417,0.107086,0.183685,0.018341,-0.982788,0.560638,
00195 -0.428864,0.708282,0.296722,-0.952576,-0.0672,0.135773,0.990265,0.030243,-0.068787,0.654724,0.752686,0.762604,-0.551758,0.337585,-0.819611,-0.407684,0.402466,-0.727844,-0.55072,-0.408539,-0.855774,-0.480011,0.19281,0.693176,-0.079285,0.716339,0.226013,0.650116,-0.725433,0.246704,0.953369,-0.173553,
00196 -0.970398,-0.239227,-0.03244,0.136383,-0.394318,0.908752,0.813232,0.558167,0.164368,0.40451,0.549042,-0.731323,-0.380249,-0.566711,0.730865,0.022156,0.932739,0.359741,0.00824,0.996552,-0.082306,0.956635,-0.065338,-0.283722,-0.743561,0.008209,0.668579,-0.859589,-0.509674,0.035767,-0.852234,0.363678,
00197 -0.375977,-0.201965,-0.970795,-0.12915,0.313477,0.947327,0.06546,-0.254028,-0.528259,0.81015,0.628052,0.601105,0.49411,-0.494385,0.868378,0.037933,0.275635,-0.086426,0.957336,-0.197937,0.468903,-0.860748,0.895599,0.399384,0.195801,0.560791,0.825012,-0.069214,0.304199,-0.849487,0.43103,0.096375,
00198 0.93576,0.339111,-0.051422,0.408966,-0.911072,0.330444,0.942841,-0.042389,-0.452362,-0.786407,0.420563,0.134308,-0.933472,-0.332489,0.80191,-0.566711,-0.188934,-0.987946,-0.105988,0.112518,-0.24408,0.892242,-0.379791,-0.920502,0.229095,-0.316376,0.7789,0.325958,0.535706,-0.912872,0.185211,-0.36377,
00199 -0.184784,0.565369,-0.803833,-0.018463,0.119537,0.992615,-0.259247,-0.935608,0.239532,-0.82373,-0.449127,-0.345947,-0.433105,0.659515,0.614349,-0.822754,0.378845,-0.423676,0.687195,-0.674835,-0.26889,-0.246582,-0.800842,0.545715,-0.729187,-0.207794,0.651978,0.653534,-0.610443,-0.447388,0.492584,-0.023346,
00200 0.869934,0.609039,0.009094,-0.79306,0.962494,-0.271088,-0.00885,0.2659,-0.004913,0.963959,0.651245,0.553619,-0.518951,0.280548,-0.84314,0.458618,-0.175293,-0.983215,0.049805,0.035339,-0.979919,0.196045,-0.982941,0.164307,-0.082245,0.233734,-0.97226,-0.005005,-0.747253,-0.611328,0.260437,0.645599,
00201 0.592773,0.481384,0.117706,-0.949524,-0.29068,-0.535004,-0.791901,-0.294312,-0.627167,-0.214447,0.748718,-0.047974,-0.813477,-0.57959,-0.175537,0.477264,-0.860992,0.738556,-0.414246,-0.53183,0.562561,-0.704071,0.433289,-0.754944,0.64801,-0.100586,0.114716,0.044525,-0.992371,0.966003,0.244873,-0.082764,
00202 };
00203 
00204 /**************************/
00205 /*  IMPROVED PERLIN NOISE */
00206 /**************************/
00207 
00208 static float lerp(float t, float a, float b)
00209 {
00210     return (a+t*(b-a));
00211 }
00212 
00213 static float npfade(float t)
00214 {
00215     return (t*t*t*(t*(t*6.0f-15.0f)+10.0f));
00216 }
00217 
00218 static float grad(int hash, float x, float y, float z)
00219 {
00220     int h = hash & 15;                     // CONVERT LO 4 BITS OF HASH CODE
00221     float u = h<8 ? x : y,                 // INTO 12 GRADIENT DIRECTIONS.
00222                 v = h<4 ? y : h==12||h==14 ? x : z;
00223     return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
00224 }
00225 
00226 /* instead of adding another permutation array, just use hash table defined above */
00227 static float newPerlin(float x, float y, float z)
00228 {
00229     int A, AA, AB, B, BA, BB;
00230     float u=floor(x), v=floor(y), w=floor(z);
00231     int X=((int)u) & 255, Y=((int)v) & 255, Z=((int)w) & 255;   // FIND UNIT CUBE THAT CONTAINS POINT
00232     x -= u;             // FIND RELATIVE X,Y,Z
00233     y -= v;             // OF POINT IN CUBE.
00234     z -= w;
00235     u = npfade(x);      // COMPUTE FADE CURVES
00236     v = npfade(y);      // FOR EACH OF X,Y,Z.
00237     w = npfade(z);
00238     A = hash[X  ]+Y;  AA = hash[A]+Z;  AB = hash[A+1]+Z;      // HASH COORDINATES OF
00239     B = hash[X+1]+Y;  BA = hash[B]+Z;  BB = hash[B+1]+Z;      // THE 8 CUBE CORNERS,
00240     return lerp(w, lerp(v, lerp(u, grad(hash[AA  ], x  , y  , z   ),  // AND ADD
00241                                                                 grad(hash[BA  ], x-1, y  , z   )), // BLENDED
00242                                                 lerp(u, grad(hash[AB  ], x  , y-1, z   ),  // RESULTS
00243                                                                 grad(hash[BB  ], x-1, y-1, z   ))),// FROM  8
00244                                 lerp(v, lerp(u, grad(hash[AA+1], x  , y  , z-1 ),  // CORNERS
00245                                                                 grad(hash[BA+1], x-1, y  , z-1 )), // OF CUBE
00246                                                 lerp(u, grad(hash[AB+1], x  , y-1, z-1 ),
00247                                                                 grad(hash[BB+1], x-1, y-1, z-1 ))));
00248 }
00249 
00250 /* for use with BLI_gNoise()/BLI_gTurbulence(), returns unsigned improved perlin noise */
00251 static float newPerlinU(float x, float y, float z)
00252 {
00253     return (0.5f+0.5f*newPerlin(x, y, z));
00254 }
00255 
00256 
00257 /**************************/
00258 /* END OF IMPROVED PERLIN */
00259 /**************************/
00260 
00261 /* Was BLI_hnoise(), removed noisesize, so other functions can call it without scaling. */
00262 static float orgBlenderNoise(float x, float y, float z)
00263 {
00264     register float cn1, cn2, cn3, cn4, cn5, cn6, i, *h;
00265     float fx, fy, fz, ox, oy, oz, jx, jy, jz;
00266     float n= 0.5;
00267     int ix, iy, iz, b00, b01, b10, b11, b20, b21;
00268 
00269     fx= floor(x);
00270     fy= floor(y);
00271     fz= floor(z);
00272 
00273     ox= x- fx;
00274     oy= y- fy;
00275     oz= z- fz;
00276 
00277     ix= (int)fx;
00278     iy= (int)fy;
00279     iz= (int)fz;
00280 
00281     jx= ox-1;
00282     jy= oy-1;
00283     jz= oz-1;
00284 
00285     cn1=ox*ox; cn2=oy*oy; cn3=oz*oz;
00286     cn4=jx*jx; cn5=jy*jy; cn6=jz*jz;
00287 
00288     cn1= 1.0f-3.0f*cn1+2.0f*cn1*ox;
00289     cn2= 1.0f-3.0f*cn2+2.0f*cn2*oy;
00290     cn3= 1.0f-3.0f*cn3+2.0f*cn3*oz;
00291     cn4= 1.0f-3.0f*cn4-2.0f*cn4*jx;
00292     cn5= 1.0f-3.0f*cn5-2.0f*cn5*jy;
00293     cn6= 1.0f-3.0f*cn6-2.0f*cn6*jz;
00294 
00295     b00= hash[ hash[ix & 255]+(iy & 255)];
00296     b10= hash[ hash[(ix+1) & 255]+(iy & 255)];
00297     b01= hash[ hash[ix & 255]+((iy+1) & 255)];
00298     b11= hash[ hash[(ix+1) & 255]+((iy+1) & 255)];
00299 
00300     b20=iz & 255; b21= (iz+1) & 255;
00301 
00302         /* 0 */
00303     i= (cn1*cn2*cn3);
00304         h=hashvectf+ 3*hash[b20+b00];
00305         n+= i*(h[0]*ox+h[1]*oy+h[2]*oz);
00306         /* 1 */
00307     i= (cn1*cn2*cn6);
00308         h=hashvectf+ 3*hash[b21+b00];
00309         n+= i*(h[0]*ox+h[1]*oy+h[2]*jz);
00310         /* 2 */
00311     i= (cn1*cn5*cn3);
00312         h=hashvectf+ 3*hash[b20+b01];
00313         n+= i*(h[0]*ox+h[1]*jy+h[2]*oz);
00314         /* 3 */
00315     i= (cn1*cn5*cn6);
00316         h=hashvectf+ 3*hash[b21+b01];
00317         n+= i*(h[0]*ox+h[1]*jy+h[2]*jz);
00318         /* 4 */
00319     i= cn4*cn2*cn3;
00320         h=hashvectf+ 3*hash[b20+b10];
00321         n+= i*(h[0]*jx+h[1]*oy+h[2]*oz);
00322         /* 5 */
00323     i= cn4*cn2*cn6;
00324         h=hashvectf+ 3*hash[b21+b10];
00325         n+= i*(h[0]*jx+h[1]*oy+h[2]*jz);
00326         /* 6 */
00327     i= cn4*cn5*cn3;
00328         h=hashvectf+ 3*hash[b20+b11];
00329         n+=  i*(h[0]*jx+h[1]*jy+h[2]*oz);
00330         /* 7 */
00331     i= (cn4*cn5*cn6);
00332         h=hashvectf+ 3*hash[b21+b11];
00333         n+= i*(h[0]*jx+h[1]*jy+h[2]*jz);
00334 
00335     if(n<0.0f) n=0.0f; else if(n>1.0f) n=1.0f;
00336     return n;
00337 }
00338 
00339 /* as orgBlenderNoise(), returning signed noise */
00340 static float orgBlenderNoiseS(float x, float y, float z)
00341 {
00342     return (2.0f*orgBlenderNoise(x, y, z)-1.0f);
00343 }
00344 
00345 /* separated from orgBlenderNoise above, with scaling */
00346 float BLI_hnoise(float noisesize, float x, float y, float z)
00347 {
00348     if(noisesize==0.0f) return 0.0f;
00349     x= (1.0f+x)/noisesize;
00350     y= (1.0f+y)/noisesize;
00351     z= (1.0f+z)/noisesize;
00352     return orgBlenderNoise(x, y, z);
00353 }
00354 
00355 
00356 /* original turbulence functions */
00357 float BLI_turbulence(float noisesize, float x, float y, float z, int nr)
00358 {
00359     float s, d= 0.5, div=1.0;
00360 
00361     s= BLI_hnoise(noisesize, x, y, z);
00362     
00363     while(nr>0) {
00364     
00365         s+= d*BLI_hnoise(noisesize*d, x, y, z);
00366         div+= d;
00367         d*= 0.5f;
00368 
00369         nr--;
00370     }
00371     return s/div;
00372 }
00373 
00374 float BLI_turbulence1(float noisesize, float x, float y, float z, int nr)
00375 {
00376     float s, d= 0.5, div=1.0;
00377 
00378     s= fabsf( (-1.0f+2.0f*BLI_hnoise(noisesize, x, y, z)));
00379     
00380     while(nr>0) {
00381     
00382         s+= fabsf(d* (-1.0f+2.0f*BLI_hnoise(noisesize*d, x, y, z)));
00383         div+= d;
00384         d*= 0.5f;
00385         
00386         nr--;
00387     }
00388     return s/div;
00389 }
00390 
00391 /* ********************* FROM PERLIN HIMSELF: ******************** */
00392 
00393 static char p[512+2]= {
00394 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
00395 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
00396 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
00397 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
00398 0xA2,0xA0,0x19,0x3B,0xF8,0xEB,0xAA,0xEE,0xF3,0x1C,0x67,0x28,0x1D,0xED,0x0,0xDE,0x95,0x2E,0xDC,0x3F,0x3A,0x82,0x35,0x4D,0x6C,0xBA,0x36,0xD0,0xF6,0xC,0x79,0x32,0xD1,0x59,0xF4,0x8,0x8B,0x63,0x89,0x2F,0xB8,0xB4,0x97,0x83,0xF2,0x8F,0x18,0xC7,0x51,0x14,0x65,0x87,0x48,0x20,0x42,0xA8,0x80,0xB5,0x40,0x13,0xB2,0x22,0x7E,0x57,
00399 0xBC,0x7F,0x6B,0x9D,0x86,0x4C,0xC8,0xDB,0x7C,0xD5,0x25,0x4E,0x5A,0x55,0x74,0x50,0xCD,0xB3,0x7A,0xBB,0xC3,0xCB,0xB6,0xE2,0xE4,0xEC,0xFD,0x98,0xB,0x96,0xD3,0x9E,0x5C,0xA1,0x64,0xF1,0x81,0x61,0xE1,0xC4,0x24,0x72,0x49,0x8C,0x90,0x4B,0x84,0x34,0x38,0xAB,0x78,0xCA,0x1F,0x1,0xD7,0x93,0x11,0xC1,0x58,0xA9,0x31,0xF9,0x44,0x6D,
00400 0xBF,0x33,0x9C,0x5F,0x9,0x94,0xA3,0x85,0x6,0xC6,0x9A,0x1E,0x7B,0x46,0x15,0x30,0x27,0x2B,0x1B,0x71,0x3C,0x5B,0xD6,0x6F,0x62,0xAC,0x4F,0xC2,0xC0,0xE,0xB1,0x23,0xA7,0xDF,0x47,0xB0,0x77,0x69,0x5,0xE9,0xE6,0xE7,0x76,0x73,0xF,0xFE,0x6E,0x9B,0x56,0xEF,0x12,0xA5,0x37,0xFC,0xAE,0xD9,0x3,0x8E,0xDD,0x10,0xB9,0xCE,0xC9,0x8D,
00401 0xDA,0x2A,0xBD,0x68,0x17,0x9F,0xBE,0xD4,0xA,0xCC,0xD2,0xE8,0x43,0x3D,0x70,0xB7,0x2,0x7D,0x99,0xD8,0xD,0x60,0x8A,0x4,0x2C,0x3E,0x92,0xE5,0xAF,0x53,0x7,0xE0,0x29,0xA6,0xC5,0xE3,0xF5,0xF7,0x4A,0x41,0x26,0x6A,0x16,0x5E,0x52,0x2D,0x21,0xAD,0xF0,0x91,0xFF,0xEA,0x54,0xFA,0x66,0x1A,0x45,0x39,0xCF,0x75,0xA4,0x88,0xFB,0x5D,
00402 0xA2,0xA0};
00403 
00404 
00405 static float g[512+2][3]= {
00406     {0.33783, 0.715698, -0.611206},
00407     {-0.944031, -0.326599, -0.045624},
00408     {-0.101074, -0.416443, -0.903503},
00409     {0.799286, 0.49411, -0.341949},
00410     {-0.854645, 0.518036, 0.033936},
00411     {0.42514, -0.437866, -0.792114},
00412     {-0.358948, 0.597046, 0.717377},
00413     {-0.985413, 0.144714, 0.089294},
00414     {-0.601776, -0.33728, -0.723907},
00415     {-0.449921, 0.594513, 0.666382},
00416     {0.208313, -0.10791, 0.972076},
00417     {0.575317, 0.060425, 0.815643},
00418     {0.293365, -0.875702, -0.383453},
00419     {0.293762, 0.465759, 0.834686},
00420     {-0.846008, -0.233398, -0.47934},
00421     {-0.115814, 0.143036, -0.98291},
00422     {0.204681, -0.949036, -0.239532},
00423     {0.946716, -0.263947, 0.184326},
00424     {-0.235596, 0.573822, 0.784332},
00425     {0.203705, -0.372253, -0.905487},
00426     {0.756989, -0.651031, 0.055298},
00427     {0.497803, 0.814697, -0.297363},
00428     {-0.16214, 0.063995, -0.98468},
00429     {-0.329254, 0.834381, 0.441925},
00430     {0.703827, -0.527039, -0.476227},
00431     {0.956421, 0.266113, 0.119781},
00432     {0.480133, 0.482849, 0.7323},
00433     {-0.18631, 0.961212, -0.203125},
00434     {-0.748474, -0.656921, -0.090393},
00435     {-0.085052, -0.165253, 0.982544},
00436     {-0.76947, 0.628174, -0.115234},
00437     {0.383148, 0.537659, 0.751068},
00438     {0.616486, -0.668488, -0.415924},
00439     {-0.259979, -0.630005, 0.73175},
00440     {0.570953, -0.087952, 0.816223},
00441     {-0.458008, 0.023254, 0.888611},
00442     {-0.196167, 0.976563, -0.088287},
00443     {-0.263885, -0.69812, -0.665527},
00444     {0.437134, -0.892273, -0.112793},
00445     {-0.621674, -0.230438, 0.748566},
00446     {0.232422, 0.900574, -0.367249},
00447     {0.22229, -0.796143, 0.562744},
00448     {-0.665497, -0.73764, 0.11377},
00449     {0.670135, 0.704803, 0.232605},
00450     {0.895599, 0.429749, -0.114655},
00451     {-0.11557, -0.474243, 0.872742},
00452     {0.621826, 0.604004, -0.498444},
00453     {-0.832214, 0.012756, 0.55426},
00454     {-0.702484, 0.705994, -0.089661},
00455     {-0.692017, 0.649292, 0.315399},
00456     {-0.175995, -0.977997, 0.111877},
00457     {0.096954, -0.04953, 0.994019},
00458     {0.635284, -0.606689, -0.477783},
00459     {-0.261261, -0.607422, -0.750153},
00460     {0.983276, 0.165436, 0.075958},
00461     {-0.29837, 0.404083, -0.864655},
00462     {-0.638672, 0.507721, 0.578156},
00463     {0.388214, 0.412079, 0.824249},
00464     {0.556183, -0.208832, 0.804352},
00465     {0.778442, 0.562012, 0.27951},
00466     {-0.616577, 0.781921, -0.091522},
00467     {0.196289, 0.051056, 0.979187},
00468     {-0.121216, 0.207153, -0.970734},
00469     {-0.173401, -0.384735, 0.906555},
00470     {0.161499, -0.723236, -0.671387},
00471     {0.178497, -0.006226, -0.983887},
00472     {-0.126038, 0.15799, 0.97934},
00473     {0.830475, -0.024811, 0.556458},
00474     {-0.510132, -0.76944, 0.384247},
00475     {0.81424, 0.200104, -0.544891},
00476     {-0.112549, -0.393311, -0.912445},
00477     {0.56189, 0.152222, -0.813049},
00478     {0.198914, -0.254517, -0.946381},
00479     {-0.41217, 0.690979, -0.593811},
00480     {-0.407257, 0.324524, 0.853668},
00481     {-0.690186, 0.366119, -0.624115},
00482     {-0.428345, 0.844147, -0.322296},
00483     {-0.21228, -0.297546, -0.930756},
00484     {-0.273071, 0.516113, 0.811798},
00485     {0.928314, 0.371643, 0.007233},
00486     {0.785828, -0.479218, -0.390778},
00487     {-0.704895, 0.058929, 0.706818},
00488     {0.173248, 0.203583, 0.963562},
00489     {0.422211, -0.904297, -0.062469},
00490     {-0.363312, -0.182465, 0.913605},
00491     {0.254028, -0.552307, -0.793945},
00492     {-0.28891, -0.765747, -0.574554},
00493     {0.058319, 0.291382, 0.954803},
00494     {0.946136, -0.303925, 0.111267},
00495     {-0.078156, 0.443695, -0.892731},
00496     {0.182098, 0.89389, 0.409515},
00497     {-0.680298, -0.213318, 0.701141},
00498     {0.062469, 0.848389, -0.525635},
00499     {-0.72879, -0.641846, 0.238342},
00500     {-0.88089, 0.427673, 0.202637},
00501     {-0.532501, -0.21405, 0.818878},
00502     {0.948975, -0.305084, 0.07962},
00503     {0.925446, 0.374664, 0.055817},
00504     {0.820923, 0.565491, 0.079102},
00505     {0.25882, 0.099792, -0.960724},
00506     {-0.294617, 0.910522, 0.289978},
00507     {0.137115, 0.320038, -0.937408},
00508     {-0.908386, 0.345276, -0.235718},
00509     {-0.936218, 0.138763, 0.322754},
00510     {0.366577, 0.925934, -0.090637},
00511     {0.309296, -0.686829, -0.657684},
00512     {0.66983, 0.024445, 0.742065},
00513     {-0.917999, -0.059113, -0.392059},
00514     {0.365509, 0.462158, -0.807922},
00515     {0.083374, 0.996399, -0.014801},
00516     {0.593842, 0.253143, -0.763672},
00517     {0.974976, -0.165466, 0.148285},
00518     {0.918976, 0.137299, 0.369537},
00519     {0.294952, 0.694977, 0.655731},
00520     {0.943085, 0.152618, -0.295319},
00521     {0.58783, -0.598236, 0.544495},
00522     {0.203796, 0.678223, 0.705994},
00523     {-0.478821, -0.661011, 0.577667},
00524     {0.719055, -0.1698, -0.673828},
00525     {-0.132172, -0.965332, 0.225006},
00526     {-0.981873, -0.14502, 0.121979},
00527     {0.763458, 0.579742, 0.284546},
00528     {-0.893188, 0.079681, 0.442474},
00529     {-0.795776, -0.523804, 0.303802},
00530     {0.734955, 0.67804, -0.007446},
00531     {0.15506, 0.986267, -0.056183},
00532     {0.258026, 0.571503, -0.778931},
00533     {-0.681549, -0.702087, -0.206116},
00534     {-0.96286, -0.177185, 0.203613},
00535     {-0.470978, -0.515106, 0.716095},
00536     {-0.740326, 0.57135, 0.354095},
00537     {-0.56012, -0.824982, -0.074982},
00538     {-0.507874, 0.753204, 0.417969},
00539     {-0.503113, 0.038147, 0.863342},
00540     {0.594025, 0.673553, -0.439758},
00541     {-0.119873, -0.005524, -0.992737},
00542     {0.098267, -0.213776, 0.971893},
00543     {-0.615631, 0.643951, 0.454163},
00544     {0.896851, -0.441071, 0.032166},
00545     {-0.555023, 0.750763, -0.358093},
00546     {0.398773, 0.304688, 0.864929},
00547     {-0.722961, 0.303589, 0.620544},
00548     {-0.63559, -0.621948, -0.457306},
00549     {-0.293243, 0.072327, 0.953278},
00550     {-0.491638, 0.661041, -0.566772},
00551     {-0.304199, -0.572083, -0.761688},
00552     {0.908081, -0.398956, 0.127014},
00553     {-0.523621, -0.549683, -0.650848},
00554     {-0.932922, -0.19986, 0.299408},
00555     {0.099426, 0.140869, 0.984985},
00556     {-0.020325, -0.999756, -0.002319},
00557     {0.952667, 0.280853, -0.11615},
00558     {-0.971893, 0.082581, 0.220337},
00559     {0.65921, 0.705292, -0.260651},
00560     {0.733063, -0.175537, 0.657043},
00561     {-0.555206, 0.429504, -0.712189},
00562     {0.400421, -0.89859, 0.179352},
00563     {0.750885, -0.19696, 0.630341},
00564     {0.785675, -0.569336, 0.241821},
00565     {-0.058899, -0.464111, 0.883789},
00566     {0.129608, -0.94519, 0.299622},
00567     {-0.357819, 0.907654, 0.219238},
00568     {-0.842133, -0.439117, -0.312927},
00569     {-0.313477, 0.84433, 0.434479},
00570     {-0.241211, 0.053253, 0.968994},
00571     {0.063873, 0.823273, 0.563965},
00572     {0.476288, 0.862152, -0.172516},
00573     {0.620941, -0.298126, 0.724915},
00574     {0.25238, -0.749359, -0.612122},
00575     {-0.577545, 0.386566, 0.718994},
00576     {-0.406342, -0.737976, 0.538696},
00577     {0.04718, 0.556305, 0.82959},
00578     {-0.802856, 0.587463, 0.101166},
00579     {-0.707733, -0.705963, 0.026428},
00580     {0.374908, 0.68457, 0.625092},
00581     {0.472137, 0.208405, -0.856506},
00582     {-0.703064, -0.581085, -0.409821},
00583     {-0.417206, -0.736328, 0.532623},
00584     {-0.447876, -0.20285, -0.870728},
00585     {0.086945, -0.990417, 0.107086},
00586     {0.183685, 0.018341, -0.982788},
00587     {0.560638, -0.428864, 0.708282},
00588     {0.296722, -0.952576, -0.0672},
00589     {0.135773, 0.990265, 0.030243},
00590     {-0.068787, 0.654724, 0.752686},
00591     {0.762604, -0.551758, 0.337585},
00592     {-0.819611, -0.407684, 0.402466},
00593     {-0.727844, -0.55072, -0.408539},
00594     {-0.855774, -0.480011, 0.19281},
00595     {0.693176, -0.079285, 0.716339},
00596     {0.226013, 0.650116, -0.725433},
00597     {0.246704, 0.953369, -0.173553},
00598     {-0.970398, -0.239227, -0.03244},
00599     {0.136383, -0.394318, 0.908752},
00600     {0.813232, 0.558167, 0.164368},
00601     {0.40451, 0.549042, -0.731323},
00602     {-0.380249, -0.566711, 0.730865},
00603     {0.022156, 0.932739, 0.359741},
00604     {0.00824, 0.996552, -0.082306},
00605     {0.956635, -0.065338, -0.283722},
00606     {-0.743561, 0.008209, 0.668579},
00607     {-0.859589, -0.509674, 0.035767},
00608     {-0.852234, 0.363678, -0.375977},
00609     {-0.201965, -0.970795, -0.12915},
00610     {0.313477, 0.947327, 0.06546},
00611     {-0.254028, -0.528259, 0.81015},
00612     {0.628052, 0.601105, 0.49411},
00613     {-0.494385, 0.868378, 0.037933},
00614     {0.275635, -0.086426, 0.957336},
00615     {-0.197937, 0.468903, -0.860748},
00616     {0.895599, 0.399384, 0.195801},
00617     {0.560791, 0.825012, -0.069214},
00618     {0.304199, -0.849487, 0.43103},
00619     {0.096375, 0.93576, 0.339111},
00620     {-0.051422, 0.408966, -0.911072},
00621     {0.330444, 0.942841, -0.042389},
00622     {-0.452362, -0.786407, 0.420563},
00623     {0.134308, -0.933472, -0.332489},
00624     {0.80191, -0.566711, -0.188934},
00625     {-0.987946, -0.105988, 0.112518},
00626     {-0.24408, 0.892242, -0.379791},
00627     {-0.920502, 0.229095, -0.316376},
00628     {0.7789, 0.325958, 0.535706},
00629     {-0.912872, 0.185211, -0.36377},
00630     {-0.184784, 0.565369, -0.803833},
00631     {-0.018463, 0.119537, 0.992615},
00632     {-0.259247, -0.935608, 0.239532},
00633     {-0.82373, -0.449127, -0.345947},
00634     {-0.433105, 0.659515, 0.614349},
00635     {-0.822754, 0.378845, -0.423676},
00636     {0.687195, -0.674835, -0.26889},
00637     {-0.246582, -0.800842, 0.545715},
00638     {-0.729187, -0.207794, 0.651978},
00639     {0.653534, -0.610443, -0.447388},
00640     {0.492584, -0.023346, 0.869934},
00641     {0.609039, 0.009094, -0.79306},
00642     {0.962494, -0.271088, -0.00885},
00643     {0.2659, -0.004913, 0.963959},
00644     {0.651245, 0.553619, -0.518951},
00645     {0.280548, -0.84314, 0.458618},
00646     {-0.175293, -0.983215, 0.049805},
00647     {0.035339, -0.979919, 0.196045},
00648     {-0.982941, 0.164307, -0.082245},
00649     {0.233734, -0.97226, -0.005005},
00650     {-0.747253, -0.611328, 0.260437},
00651     {0.645599, 0.592773, 0.481384},
00652     {0.117706, -0.949524, -0.29068},
00653     {-0.535004, -0.791901, -0.294312},
00654     {-0.627167, -0.214447, 0.748718},
00655     {-0.047974, -0.813477, -0.57959},
00656     {-0.175537, 0.477264, -0.860992},
00657     {0.738556, -0.414246, -0.53183},
00658     {0.562561, -0.704071, 0.433289},
00659     {-0.754944, 0.64801, -0.100586},
00660     {0.114716, 0.044525, -0.992371},
00661     {0.966003, 0.244873, -0.082764},
00662     {0.33783, 0.715698, -0.611206},
00663     {-0.944031, -0.326599, -0.045624},
00664     {-0.101074, -0.416443, -0.903503},
00665     {0.799286, 0.49411, -0.341949},
00666     {-0.854645, 0.518036, 0.033936},
00667     {0.42514, -0.437866, -0.792114},
00668     {-0.358948, 0.597046, 0.717377},
00669     {-0.985413, 0.144714, 0.089294},
00670     {-0.601776, -0.33728, -0.723907},
00671     {-0.449921, 0.594513, 0.666382},
00672     {0.208313, -0.10791, 0.972076},
00673     {0.575317, 0.060425, 0.815643},
00674     {0.293365, -0.875702, -0.383453},
00675     {0.293762, 0.465759, 0.834686},
00676     {-0.846008, -0.233398, -0.47934},
00677     {-0.115814, 0.143036, -0.98291},
00678     {0.204681, -0.949036, -0.239532},
00679     {0.946716, -0.263947, 0.184326},
00680     {-0.235596, 0.573822, 0.784332},
00681     {0.203705, -0.372253, -0.905487},
00682     {0.756989, -0.651031, 0.055298},
00683     {0.497803, 0.814697, -0.297363},
00684     {-0.16214, 0.063995, -0.98468},
00685     {-0.329254, 0.834381, 0.441925},
00686     {0.703827, -0.527039, -0.476227},
00687     {0.956421, 0.266113, 0.119781},
00688     {0.480133, 0.482849, 0.7323},
00689     {-0.18631, 0.961212, -0.203125},
00690     {-0.748474, -0.656921, -0.090393},
00691     {-0.085052, -0.165253, 0.982544},
00692     {-0.76947, 0.628174, -0.115234},
00693     {0.383148, 0.537659, 0.751068},
00694     {0.616486, -0.668488, -0.415924},
00695     {-0.259979, -0.630005, 0.73175},
00696     {0.570953, -0.087952, 0.816223},
00697     {-0.458008, 0.023254, 0.888611},
00698     {-0.196167, 0.976563, -0.088287},
00699     {-0.263885, -0.69812, -0.665527},
00700     {0.437134, -0.892273, -0.112793},
00701     {-0.621674, -0.230438, 0.748566},
00702     {0.232422, 0.900574, -0.367249},
00703     {0.22229, -0.796143, 0.562744},
00704     {-0.665497, -0.73764, 0.11377},
00705     {0.670135, 0.704803, 0.232605},
00706     {0.895599, 0.429749, -0.114655},
00707     {-0.11557, -0.474243, 0.872742},
00708     {0.621826, 0.604004, -0.498444},
00709     {-0.832214, 0.012756, 0.55426},
00710     {-0.702484, 0.705994, -0.089661},
00711     {-0.692017, 0.649292, 0.315399},
00712     {-0.175995, -0.977997, 0.111877},
00713     {0.096954, -0.04953, 0.994019},
00714     {0.635284, -0.606689, -0.477783},
00715     {-0.261261, -0.607422, -0.750153},
00716     {0.983276, 0.165436, 0.075958},
00717     {-0.29837, 0.404083, -0.864655},
00718     {-0.638672, 0.507721, 0.578156},
00719     {0.388214, 0.412079, 0.824249},
00720     {0.556183, -0.208832, 0.804352},
00721     {0.778442, 0.562012, 0.27951},
00722     {-0.616577, 0.781921, -0.091522},
00723     {0.196289, 0.051056, 0.979187},
00724     {-0.121216, 0.207153, -0.970734},
00725     {-0.173401, -0.384735, 0.906555},
00726     {0.161499, -0.723236, -0.671387},
00727     {0.178497, -0.006226, -0.983887},
00728     {-0.126038, 0.15799, 0.97934},
00729     {0.830475, -0.024811, 0.556458},
00730     {-0.510132, -0.76944, 0.384247},
00731     {0.81424, 0.200104, -0.544891},
00732     {-0.112549, -0.393311, -0.912445},
00733     {0.56189, 0.152222, -0.813049},
00734     {0.198914, -0.254517, -0.946381},
00735     {-0.41217, 0.690979, -0.593811},
00736     {-0.407257, 0.324524, 0.853668},
00737     {-0.690186, 0.366119, -0.624115},
00738     {-0.428345, 0.844147, -0.322296},
00739     {-0.21228, -0.297546, -0.930756},
00740     {-0.273071, 0.516113, 0.811798},
00741     {0.928314, 0.371643, 0.007233},
00742     {0.785828, -0.479218, -0.390778},
00743     {-0.704895, 0.058929, 0.706818},
00744     {0.173248, 0.203583, 0.963562},
00745     {0.422211, -0.904297, -0.062469},
00746     {-0.363312, -0.182465, 0.913605},
00747     {0.254028, -0.552307, -0.793945},
00748     {-0.28891, -0.765747, -0.574554},
00749     {0.058319, 0.291382, 0.954803},
00750     {0.946136, -0.303925, 0.111267},
00751     {-0.078156, 0.443695, -0.892731},
00752     {0.182098, 0.89389, 0.409515},
00753     {-0.680298, -0.213318, 0.701141},
00754     {0.062469, 0.848389, -0.525635},
00755     {-0.72879, -0.641846, 0.238342},
00756     {-0.88089, 0.427673, 0.202637},
00757     {-0.532501, -0.21405, 0.818878},
00758     {0.948975, -0.305084, 0.07962},
00759     {0.925446, 0.374664, 0.055817},
00760     {0.820923, 0.565491, 0.079102},
00761     {0.25882, 0.099792, -0.960724},
00762     {-0.294617, 0.910522, 0.289978},
00763     {0.137115, 0.320038, -0.937408},
00764     {-0.908386, 0.345276, -0.235718},
00765     {-0.936218, 0.138763, 0.322754},
00766     {0.366577, 0.925934, -0.090637},
00767     {0.309296, -0.686829, -0.657684},
00768     {0.66983, 0.024445, 0.742065},
00769     {-0.917999, -0.059113, -0.392059},
00770     {0.365509, 0.462158, -0.807922},
00771     {0.083374, 0.996399, -0.014801},
00772     {0.593842, 0.253143, -0.763672},
00773     {0.974976, -0.165466, 0.148285},
00774     {0.918976, 0.137299, 0.369537},
00775     {0.294952, 0.694977, 0.655731},
00776     {0.943085, 0.152618, -0.295319},
00777     {0.58783, -0.598236, 0.544495},
00778     {0.203796, 0.678223, 0.705994},
00779     {-0.478821, -0.661011, 0.577667},
00780     {0.719055, -0.1698, -0.673828},
00781     {-0.132172, -0.965332, 0.225006},
00782     {-0.981873, -0.14502, 0.121979},
00783     {0.763458, 0.579742, 0.284546},
00784     {-0.893188, 0.079681, 0.442474},
00785     {-0.795776, -0.523804, 0.303802},
00786     {0.734955, 0.67804, -0.007446},
00787     {0.15506, 0.986267, -0.056183},
00788     {0.258026, 0.571503, -0.778931},
00789     {-0.681549, -0.702087, -0.206116},
00790     {-0.96286, -0.177185, 0.203613},
00791     {-0.470978, -0.515106, 0.716095},
00792     {-0.740326, 0.57135, 0.354095},
00793     {-0.56012, -0.824982, -0.074982},
00794     {-0.507874, 0.753204, 0.417969},
00795     {-0.503113, 0.038147, 0.863342},
00796     {0.594025, 0.673553, -0.439758},
00797     {-0.119873, -0.005524, -0.992737},
00798     {0.098267, -0.213776, 0.971893},
00799     {-0.615631, 0.643951, 0.454163},
00800     {0.896851, -0.441071, 0.032166},
00801     {-0.555023, 0.750763, -0.358093},
00802     {0.398773, 0.304688, 0.864929},
00803     {-0.722961, 0.303589, 0.620544},
00804     {-0.63559, -0.621948, -0.457306},
00805     {-0.293243, 0.072327, 0.953278},
00806     {-0.491638, 0.661041, -0.566772},
00807     {-0.304199, -0.572083, -0.761688},
00808     {0.908081, -0.398956, 0.127014},
00809     {-0.523621, -0.549683, -0.650848},
00810     {-0.932922, -0.19986, 0.299408},
00811     {0.099426, 0.140869, 0.984985},
00812     {-0.020325, -0.999756, -0.002319},
00813     {0.952667, 0.280853, -0.11615},
00814     {-0.971893, 0.082581, 0.220337},
00815     {0.65921, 0.705292, -0.260651},
00816     {0.733063, -0.175537, 0.657043},
00817     {-0.555206, 0.429504, -0.712189},
00818     {0.400421, -0.89859, 0.179352},
00819     {0.750885, -0.19696, 0.630341},
00820     {0.785675, -0.569336, 0.241821},
00821     {-0.058899, -0.464111, 0.883789},
00822     {0.129608, -0.94519, 0.299622},
00823     {-0.357819, 0.907654, 0.219238},
00824     {-0.842133, -0.439117, -0.312927},
00825     {-0.313477, 0.84433, 0.434479},
00826     {-0.241211, 0.053253, 0.968994},
00827     {0.063873, 0.823273, 0.563965},
00828     {0.476288, 0.862152, -0.172516},
00829     {0.620941, -0.298126, 0.724915},
00830     {0.25238, -0.749359, -0.612122},
00831     {-0.577545, 0.386566, 0.718994},
00832     {-0.406342, -0.737976, 0.538696},
00833     {0.04718, 0.556305, 0.82959},
00834     {-0.802856, 0.587463, 0.101166},
00835     {-0.707733, -0.705963, 0.026428},
00836     {0.374908, 0.68457, 0.625092},
00837     {0.472137, 0.208405, -0.856506},
00838     {-0.703064, -0.581085, -0.409821},
00839     {-0.417206, -0.736328, 0.532623},
00840     {-0.447876, -0.20285, -0.870728},
00841     {0.086945, -0.990417, 0.107086},
00842     {0.183685, 0.018341, -0.982788},
00843     {0.560638, -0.428864, 0.708282},
00844     {0.296722, -0.952576, -0.0672},
00845     {0.135773, 0.990265, 0.030243},
00846     {-0.068787, 0.654724, 0.752686},
00847     {0.762604, -0.551758, 0.337585},
00848     {-0.819611, -0.407684, 0.402466},
00849     {-0.727844, -0.55072, -0.408539},
00850     {-0.855774, -0.480011, 0.19281},
00851     {0.693176, -0.079285, 0.716339},
00852     {0.226013, 0.650116, -0.725433},
00853     {0.246704, 0.953369, -0.173553},
00854     {-0.970398, -0.239227, -0.03244},
00855     {0.136383, -0.394318, 0.908752},
00856     {0.813232, 0.558167, 0.164368},
00857     {0.40451, 0.549042, -0.731323},
00858     {-0.380249, -0.566711, 0.730865},
00859     {0.022156, 0.932739, 0.359741},
00860     {0.00824, 0.996552, -0.082306},
00861     {0.956635, -0.065338, -0.283722},
00862     {-0.743561, 0.008209, 0.668579},
00863     {-0.859589, -0.509674, 0.035767},
00864     {-0.852234, 0.363678, -0.375977},
00865     {-0.201965, -0.970795, -0.12915},
00866     {0.313477, 0.947327, 0.06546},
00867     {-0.254028, -0.528259, 0.81015},
00868     {0.628052, 0.601105, 0.49411},
00869     {-0.494385, 0.868378, 0.037933},
00870     {0.275635, -0.086426, 0.957336},
00871     {-0.197937, 0.468903, -0.860748},
00872     {0.895599, 0.399384, 0.195801},
00873     {0.560791, 0.825012, -0.069214},
00874     {0.304199, -0.849487, 0.43103},
00875     {0.096375, 0.93576, 0.339111},
00876     {-0.051422, 0.408966, -0.911072},
00877     {0.330444, 0.942841, -0.042389},
00878     {-0.452362, -0.786407, 0.420563},
00879     {0.134308, -0.933472, -0.332489},
00880     {0.80191, -0.566711, -0.188934},
00881     {-0.987946, -0.105988, 0.112518},
00882     {-0.24408, 0.892242, -0.379791},
00883     {-0.920502, 0.229095, -0.316376},
00884     {0.7789, 0.325958, 0.535706},
00885     {-0.912872, 0.185211, -0.36377},
00886     {-0.184784, 0.565369, -0.803833},
00887     {-0.018463, 0.119537, 0.992615},
00888     {-0.259247, -0.935608, 0.239532},
00889     {-0.82373, -0.449127, -0.345947},
00890     {-0.433105, 0.659515, 0.614349},
00891     {-0.822754, 0.378845, -0.423676},
00892     {0.687195, -0.674835, -0.26889},
00893     {-0.246582, -0.800842, 0.545715},
00894     {-0.729187, -0.207794, 0.651978},
00895     {0.653534, -0.610443, -0.447388},
00896     {0.492584, -0.023346, 0.869934},
00897     {0.609039, 0.009094, -0.79306},
00898     {0.962494, -0.271088, -0.00885},
00899     {0.2659, -0.004913, 0.963959},
00900     {0.651245, 0.553619, -0.518951},
00901     {0.280548, -0.84314, 0.458618},
00902     {-0.175293, -0.983215, 0.049805},
00903     {0.035339, -0.979919, 0.196045},
00904     {-0.982941, 0.164307, -0.082245},
00905     {0.233734, -0.97226, -0.005005},
00906     {-0.747253, -0.611328, 0.260437},
00907     {0.645599, 0.592773, 0.481384},
00908     {0.117706, -0.949524, -0.29068},
00909     {-0.535004, -0.791901, -0.294312},
00910     {-0.627167, -0.214447, 0.748718},
00911     {-0.047974, -0.813477, -0.57959},
00912     {-0.175537, 0.477264, -0.860992},
00913     {0.738556, -0.414246, -0.53183},
00914     {0.562561, -0.704071, 0.433289},
00915     {-0.754944, 0.64801, -0.100586},
00916     {0.114716, 0.044525, -0.992371},
00917     {0.966003, 0.244873, -0.082764},
00918     {0.33783, 0.715698, -0.611206},
00919     {-0.944031, -0.326599, -0.045624},
00920 };
00921 
00922 
00923 
00924 #define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])
00925 
00926 #define setup(i,b0,b1,r0,r1) \
00927         t = vec[i] + 10000.0f; \
00928         b0 = ((int)t) & 255; \
00929         b1 = (b0+1) & 255; \
00930         r0 = t - floorf(t); \
00931         r1 = r0 - 1.0f;
00932 
00933 
00934 static float noise3_perlin(float vec[3])
00935 {
00936     int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
00937     float rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
00938     register int i, j;
00939 
00940 
00941     setup(0, bx0,bx1, rx0,rx1);
00942     setup(1, by0,by1, ry0,ry1);
00943     setup(2, bz0,bz1, rz0,rz1);
00944 
00945     i = p[ bx0 ];
00946     j = p[ bx1 ];
00947 
00948     b00 = p[ i + by0 ];
00949     b10 = p[ j + by0 ];
00950     b01 = p[ i + by1 ];
00951     b11 = p[ j + by1 ];
00952 
00953 #define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
00954 
00955 #define surve(t) ( t * t * (3.0f - 2.0f * t) )
00956 
00957 /* lerp moved to improved perlin above */
00958 
00959     sx = surve(rx0);
00960     sy = surve(ry0);
00961     sz = surve(rz0);
00962 
00963 
00964     q = g[ b00 + bz0 ] ;
00965     u = at(rx0,ry0,rz0);
00966     q = g[ b10 + bz0 ] ;
00967     v = at(rx1,ry0,rz0);
00968     a = lerp(sx, u, v);
00969 
00970     q = g[ b01 + bz0 ] ;
00971     u = at(rx0,ry1,rz0);
00972     q = g[ b11 + bz0 ] ;
00973     v = at(rx1,ry1,rz0);
00974     b = lerp(sx, u, v);
00975 
00976     c = lerp(sy, a, b);          /* interpolate in y at lo x */
00977 
00978     q = g[ b00 + bz1 ] ;
00979     u = at(rx0,ry0,rz1);
00980     q = g[ b10 + bz1 ] ;
00981     v = at(rx1,ry0,rz1);
00982     a = lerp(sx, u, v);
00983 
00984     q = g[ b01 + bz1 ] ;
00985     u = at(rx0,ry1,rz1);
00986     q = g[ b11 + bz1 ] ;
00987     v = at(rx1,ry1,rz1);
00988     b = lerp(sx, u, v);
00989 
00990     d = lerp(sy, a, b);          /* interpolate in y at hi x */
00991 
00992     return 1.5f * lerp(sz, c, d); /* interpolate in z */
00993 }
00994 
00995 #if 0
00996 static float turbulence_perlin(float *point, float lofreq, float hifreq)
00997 {
00998     float freq, t, p[3];
00999 
01000     p[0] = point[0] + 123.456;
01001     p[1] = point[1];
01002     p[2] = point[2];
01003 
01004     t = 0;
01005     for (freq = lofreq ; freq < hifreq ; freq *= 2.) {
01006         t += fabsf(noise3_perlin(p)) / freq;
01007         p[0] *= 2.;
01008         p[1] *= 2.;
01009         p[2] *= 2.;
01010     }
01011     return t - 0.3; /* readjust to make mean value = 0.0 */
01012 }
01013 #endif
01014 
01015 /* for use with BLI_gNoise/gTurbulence, returns signed noise */
01016 static float orgPerlinNoise(float x, float y, float z)
01017 {
01018     float v[3];
01019 
01020     v[0] = x;
01021     v[1] = y;
01022     v[2] = z;
01023     return noise3_perlin(v);
01024 }
01025 
01026 /* for use with BLI_gNoise/gTurbulence, returns unsigned noise */
01027 static float orgPerlinNoiseU(float x, float y, float z)
01028 {
01029     float v[3];
01030 
01031     v[0] = x;
01032     v[1] = y;
01033     v[2] = z;
01034     return (0.5f+0.5f*noise3_perlin(v));
01035 }
01036 
01037 /* *************** CALL AS: *************** */
01038 
01039 float BLI_hnoisep(float noisesize, float x, float y, float z)
01040 {
01041     float vec[3];
01042 
01043     vec[0]= x/noisesize;
01044     vec[1]= y/noisesize;
01045     vec[2]= z/noisesize;
01046 
01047     return noise3_perlin(vec);
01048 }
01049 
01050 /*static float turbulencep(float noisesize, float x, float y, float z, int nr)
01051 {
01052     float vec[3];
01053 
01054     vec[0]= x/noisesize;
01055     vec[1]= y/noisesize;
01056     vec[2]= z/noisesize;
01057     nr++;
01058     return turbulence_perlin(vec, 1.0, (float)(1<<nr));
01059 }*/
01060 
01061 /******************/
01062 /* VORONOI/WORLEY */
01063 /******************/
01064 
01065 /* distance metrics for voronoi, e parameter only used in Minkovsky */
01066 /* Camberra omitted, didn't seem useful */
01067 
01068 /* distance squared */
01069 static float dist_Squared(float x, float y, float z, float e) { (void)e; return (x*x + y*y + z*z); }
01070 /* real distance */
01071 static float dist_Real(float x, float y, float z, float e) { (void)e; return sqrtf(x*x + y*y + z*z); }
01072 /* manhattan/taxicab/cityblock distance */
01073 static float dist_Manhattan(float x, float y, float z, float e) { (void)e; return (fabsf(x) + fabsf(y) + fabsf(z)); }
01074 /* Chebychev */
01075 static float dist_Chebychev(float x, float y, float z, float e)
01076 {
01077     float t;
01078     (void)e;
01079 
01080     x = fabsf(x);
01081     y = fabsf(y);
01082     z = fabsf(z);
01083     t = (x>y)?x:y;
01084     return ((z>t)?z:t);
01085 }
01086 
01087 /* minkovsky preset exponent 0.5 */
01088 static float dist_MinkovskyH(float x, float y, float z, float e)
01089 {
01090     float d = sqrtf(fabsf(x)) + sqrtf(fabsf(y)) + sqrtf(fabsf(z));
01091     (void)e;
01092     return (d*d);
01093 }
01094 
01095 /* minkovsky preset exponent 4 */
01096 static float dist_Minkovsky4(float x, float y, float z, float e)
01097 {
01098     (void)e;
01099     x *= x;
01100     y *= y;
01101     z *= z;
01102     return sqrtf(sqrtf(x*x + y*y + z*z));
01103 }
01104 
01105 /* Minkovsky, general case, slow, maybe too slow to be useful */
01106 static float dist_Minkovsky(float x, float y, float z, float e)
01107 {
01108     return powf(powf(fabsf(x), e) + powf(fabsf(y), e) + powf(fabsf(z), e), 1.0f/e);
01109 }
01110 
01111 
01112 /* Not 'pure' Worley, but the results are virtually the same.
01113      Returns distances in da and point coords in pa */
01114 void voronoi(float x, float y, float z, float* da, float* pa, float me, int dtype)
01115 {
01116     int xx, yy, zz, xi, yi, zi;
01117     float xd, yd, zd, d, *p;
01118 
01119     float (*distfunc)(float, float, float, float);
01120     switch (dtype) {
01121         case 1:
01122             distfunc = dist_Squared;
01123             break;
01124         case 2:
01125             distfunc = dist_Manhattan;
01126             break;
01127         case 3:
01128             distfunc = dist_Chebychev;
01129             break;
01130         case 4:
01131             distfunc = dist_MinkovskyH;
01132             break;
01133         case 5:
01134             distfunc = dist_Minkovsky4;
01135             break;
01136         case 6:
01137             distfunc = dist_Minkovsky;
01138             break;
01139         case 0:
01140         default:
01141             distfunc = dist_Real;
01142     }
01143 
01144     xi = (int)(floor(x));
01145     yi = (int)(floor(y));
01146     zi = (int)(floor(z));
01147     da[0] = da[1] = da[2] = da[3] = 1e10f;
01148     for (xx=xi-1;xx<=xi+1;xx++) {
01149         for (yy=yi-1;yy<=yi+1;yy++) {
01150             for (zz=zi-1;zz<=zi+1;zz++) {
01151                 p = HASHPNT(xx, yy, zz);
01152                 xd = x - (p[0] + xx);
01153                 yd = y - (p[1] + yy);
01154                 zd = z - (p[2] + zz);
01155                 d = distfunc(xd, yd, zd, me);
01156                 if (d<da[0]) {
01157                     da[3]=da[2];  da[2]=da[1];  da[1]=da[0];  da[0]=d;
01158                     pa[9]=pa[6];  pa[10]=pa[7];  pa[11]=pa[8];
01159                     pa[6]=pa[3];  pa[7]=pa[4];  pa[8]=pa[5];
01160                     pa[3]=pa[0];  pa[4]=pa[1];  pa[5]=pa[2];
01161                     pa[0]=p[0]+xx;  pa[1]=p[1]+yy;  pa[2]=p[2]+zz;
01162                 }
01163                 else if (d<da[1]) {
01164                     da[3]=da[2];  da[2]=da[1];  da[1]=d;
01165                     pa[9]=pa[6];  pa[10]=pa[7];  pa[11]=pa[8];
01166                     pa[6]=pa[3];  pa[7]=pa[4];  pa[8]=pa[5];
01167                     pa[3]=p[0]+xx;  pa[4]=p[1]+yy;  pa[5]=p[2]+zz;
01168                 }
01169                 else if (d<da[2]) {
01170                     da[3]=da[2];  da[2]=d;
01171                     pa[9]=pa[6];  pa[10]=pa[7];  pa[11]=pa[8];
01172                     pa[6]=p[0]+xx;  pa[7]=p[1]+yy;  pa[8]=p[2]+zz;
01173                 }
01174                 else if (d<da[3]) {
01175                     da[3]=d;
01176                     pa[9]=p[0]+xx;  pa[10]=p[1]+yy;  pa[11]=p[2]+zz;
01177                 }
01178             }
01179         }
01180     }
01181 }
01182 
01183 /* returns different feature points for use in BLI_gNoise() */
01184 static float voronoi_F1(float x, float y, float z)
01185 {
01186     float da[4], pa[12];
01187     voronoi(x, y, z, da, pa, 1, 0);
01188     return da[0];
01189 }
01190 
01191 static float voronoi_F2(float x, float y, float z)
01192 {
01193     float da[4], pa[12];
01194     voronoi(x, y, z, da, pa, 1, 0);
01195     return da[1];
01196 }
01197 
01198 static float voronoi_F3(float x, float y, float z)
01199 {
01200     float da[4], pa[12];
01201     voronoi(x, y, z, da, pa, 1, 0);
01202     return da[2];
01203 }
01204 
01205 static float voronoi_F4(float x, float y, float z)
01206 {
01207     float da[4], pa[12];
01208     voronoi(x, y, z, da, pa, 1, 0);
01209     return da[3];
01210 }
01211 
01212 static float voronoi_F1F2(float x, float y, float z)
01213 {
01214     float da[4], pa[12];
01215     voronoi(x, y, z, da, pa, 1, 0);
01216     return (da[1]-da[0]);
01217 }
01218 
01219 /* Crackle type pattern, just a scale/clamp of F2-F1 */
01220 static float voronoi_Cr(float x, float y, float z)
01221 {
01222     float t = 10*voronoi_F1F2(x, y, z);
01223     if (t>1.f) return 1.f;
01224     return t;
01225 }
01226 
01227 
01228 /* Signed version of all 6 of the above, just 2x-1, not really correct though (range is potentially (0, sqrt(6)).
01229    Used in the musgrave functions */
01230 static float voronoi_F1S(float x, float y, float z)
01231 {
01232     float da[4], pa[12];
01233     voronoi(x, y, z, da, pa, 1, 0);
01234     return (2.0f*da[0]-1.0f);
01235 }
01236 
01237 static float voronoi_F2S(float x, float y, float z)
01238 {
01239     float da[4], pa[12];
01240     voronoi(x, y, z, da, pa, 1, 0);
01241     return (2.0f*da[1]-1.0f);
01242 }
01243 
01244 static float voronoi_F3S(float x, float y, float z)
01245 {
01246     float da[4], pa[12];
01247     voronoi(x, y, z, da, pa, 1, 0);
01248     return (2.0f*da[2]-1.0f);
01249 }
01250 
01251 static float voronoi_F4S(float x, float y, float z)
01252 {
01253     float da[4], pa[12];
01254     voronoi(x, y, z, da, pa, 1, 0);
01255     return (2.0f*da[3]-1.0f);
01256 }
01257 
01258 static float voronoi_F1F2S(float x, float y, float z)
01259 {
01260     float da[4], pa[12];
01261     voronoi(x, y, z, da, pa, 1, 0);
01262     return (2.0f*(da[1]-da[0])-1.0f);
01263 }
01264 
01265 /* Crackle type pattern, just a scale/clamp of F2-F1 */
01266 static float voronoi_CrS(float x, float y, float z)
01267 {
01268     float t = 10*voronoi_F1F2(x, y, z);
01269     if (t>1.f) return 1.f;
01270     return (2.0f*t-1.0f);
01271 }
01272 
01273 
01274 /***************/
01275 /* voronoi end */
01276 /***************/
01277 
01278 /*************/
01279 /* CELLNOISE */
01280 /*************/
01281 
01282 /* returns unsigned cellnoise */
01283 static float cellNoiseU(float x, float y, float z)
01284 {
01285     int xi = (int)(floor(x));
01286     int yi = (int)(floor(y));
01287     int zi = (int)(floor(z));
01288     unsigned int n = xi + yi*1301 + zi*314159;
01289     n ^= (n<<13);
01290     return ((float)(n*(n*n*15731 + 789221) + 1376312589) / 4294967296.0f);
01291 }
01292 
01293 /* idem, signed */
01294 float cellNoise(float x, float y, float z)
01295 {
01296     return (2.0f*cellNoiseU(x, y, z)-1.0f);
01297 }
01298 
01299 /* returns a vector/point/color in ca, using point hasharray directly */
01300 void cellNoiseV(float x, float y, float z, float *ca)
01301 {
01302     int xi = (int)(floor(x));
01303     int yi = (int)(floor(y));
01304     int zi = (int)(floor(z));
01305     float *p = HASHPNT(xi, yi, zi);
01306     ca[0] = p[0];
01307     ca[1] = p[1];
01308     ca[2] = p[2];
01309 }
01310 
01311 
01312 /*****************/
01313 /* end cellnoise */
01314 /*****************/
01315 
01316 /* newnoise: generic noise function for use with different noisebases */
01317 float BLI_gNoise(float noisesize, float x, float y, float z, int hard, int noisebasis)
01318 {
01319     float (*noisefunc)(float, float, float);
01320 
01321     switch (noisebasis) {
01322         case 1:
01323             noisefunc = orgPerlinNoiseU;
01324             break;
01325         case 2:
01326             noisefunc = newPerlinU;
01327             break;
01328         case 3:
01329             noisefunc = voronoi_F1;
01330             break;
01331         case 4:
01332             noisefunc = voronoi_F2;
01333             break;
01334         case 5:
01335             noisefunc = voronoi_F3;
01336             break;
01337         case 6:
01338             noisefunc = voronoi_F4;
01339             break;
01340         case 7:
01341             noisefunc = voronoi_F1F2;
01342             break;
01343         case 8:
01344             noisefunc = voronoi_Cr;
01345             break;
01346         case 14:
01347             noisefunc = cellNoiseU;
01348             break;
01349         case 0:
01350         default: {
01351             noisefunc = orgBlenderNoise;
01352             /* add one to make return value same as BLI_hnoise */
01353             x += 1;
01354             y += 1;
01355             z += 1;
01356         }
01357     }
01358 
01359     if (noisesize!=0.0f) {
01360         noisesize = 1.0f/noisesize;
01361         x *= noisesize;
01362         y *= noisesize;
01363         z *= noisesize;
01364     }
01365     
01366     if (hard) return fabsf(2.0f*noisefunc(x, y, z)-1.0f);
01367     return noisefunc(x, y, z);
01368 }
01369 
01370 /* newnoise: generic turbulence function for use with different noisebasis */
01371 float BLI_gTurbulence(float noisesize, float x, float y, float z, int oct, int hard, int noisebasis)
01372 {
01373     float (*noisefunc)(float, float, float);
01374     float sum, t, amp=1, fscale=1;
01375     int i;
01376     
01377     switch (noisebasis) {
01378         case 1:
01379             noisefunc = orgPerlinNoiseU;
01380             break;
01381         case 2:
01382             noisefunc = newPerlinU;
01383             break;
01384         case 3:
01385             noisefunc = voronoi_F1;
01386             break;
01387         case 4:
01388             noisefunc = voronoi_F2;
01389             break;
01390         case 5:
01391             noisefunc = voronoi_F3;
01392             break;
01393         case 6:
01394             noisefunc = voronoi_F4;
01395             break;
01396         case 7:
01397             noisefunc = voronoi_F1F2;
01398             break;
01399         case 8:
01400             noisefunc = voronoi_Cr;
01401             break;
01402         case 14:
01403             noisefunc = cellNoiseU;
01404             break;
01405         case 0:
01406         default:
01407             noisefunc = orgBlenderNoise;
01408             x += 1;
01409             y += 1;
01410             z += 1;
01411     }
01412 
01413     if (noisesize!=0.0f) {
01414         noisesize = 1.0f/noisesize;
01415         x *= noisesize;
01416         y *= noisesize;
01417         z *= noisesize;
01418     }
01419 
01420     sum = 0;
01421     for (i=0;i<=oct;i++, amp*=0.5f, fscale*=2.0f) {
01422         t = noisefunc(fscale*x, fscale*y, fscale*z);
01423         if (hard) t = fabsf(2.0f*t-1.0f);
01424         sum += t * amp;
01425     }
01426     
01427     sum *= ((float)(1<<oct)/(float)((1<<(oct+1))-1));
01428 
01429     return sum;
01430 
01431 }
01432 
01433 
01434 /*
01435  * The following code is based on Ken Musgrave's explanations and sample
01436  * source code in the book "Texturing and Modelling: A procedural approach"
01437  */
01438 
01439 /*
01440  * Procedural fBm evaluated at "point"; returns value stored in "value".
01441  *
01442  * Parameters:
01443  *    ``H''  is the fractal increment parameter
01444  *    ``lacunarity''  is the gap between successive frequencies
01445  *    ``octaves''  is the number of frequencies in the fBm
01446  */
01447 float mg_fBm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
01448 {
01449     float   rmd, value=0.0, pwr=1.0, pwHL=powf(lacunarity, -H);
01450     int i;
01451 
01452     float (*noisefunc)(float, float, float);
01453     switch (noisebasis) {
01454         case 1:
01455             noisefunc = orgPerlinNoise;
01456             break;
01457         case 2:
01458             noisefunc = newPerlin;
01459             break;
01460         case 3:
01461             noisefunc = voronoi_F1S;
01462             break;
01463         case 4:
01464             noisefunc = voronoi_F2S;
01465             break;
01466         case 5:
01467             noisefunc = voronoi_F3S;
01468             break;
01469         case 6:
01470             noisefunc = voronoi_F4S;
01471             break;
01472         case 7:
01473             noisefunc = voronoi_F1F2S;
01474             break;
01475         case 8:
01476             noisefunc = voronoi_CrS;
01477             break;
01478         case 14:
01479             noisefunc = cellNoise;
01480             break;
01481         case 0:
01482         default: {
01483             noisefunc = orgBlenderNoiseS;
01484         }
01485     }
01486     
01487     for (i=0; i<(int)octaves; i++) {
01488         value += noisefunc(x, y, z) * pwr;
01489         pwr *= pwHL;
01490         x *= lacunarity;
01491         y *= lacunarity;
01492         z *= lacunarity;
01493     }
01494 
01495     rmd = octaves - floorf(octaves);
01496     if (rmd!=0.f) value += rmd * noisefunc(x, y, z) * pwr;
01497 
01498     return value;
01499 
01500 } /* fBm() */
01501 
01502 
01503 /*
01504  * Procedural multifractal evaluated at "point";
01505  * returns value stored in "value".
01506  *
01507  * Parameters:
01508  *    ``H''  determines the highest fractal dimension
01509  *    ``lacunarity''  is gap between successive frequencies
01510  *    ``octaves''  is the number of frequencies in the fBm
01511  *    ``offset''  is the zero offset, which determines multifractality (NOT USED??)
01512  */
01513  /* this one is in fact rather confusing,
01514      * there seem to be errors in the original source code (in all three versions of proc.text&mod),
01515     * I modified it to something that made sense to me, so it might be wrong... */
01516 float mg_MultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis)
01517 {
01518     float   rmd, value=1.0, pwr=1.0, pwHL=powf(lacunarity, -H);
01519     int i;
01520 
01521     float (*noisefunc)(float, float, float);
01522     switch (noisebasis) {
01523         case 1:
01524             noisefunc = orgPerlinNoise;
01525             break;
01526         case 2:
01527             noisefunc = newPerlin;
01528             break;
01529         case 3:
01530             noisefunc = voronoi_F1S;
01531             break;
01532         case 4:
01533             noisefunc = voronoi_F2S;
01534             break;
01535         case 5:
01536             noisefunc = voronoi_F3S;
01537             break;
01538         case 6:
01539             noisefunc = voronoi_F4S;
01540             break;
01541         case 7:
01542             noisefunc = voronoi_F1F2S;
01543             break;
01544         case 8:
01545             noisefunc = voronoi_CrS;
01546             break;
01547         case 14:
01548             noisefunc = cellNoise;
01549             break;
01550         case 0:
01551         default: {
01552             noisefunc = orgBlenderNoiseS;
01553         }
01554     }
01555 
01556     for (i=0; i<(int)octaves; i++) {
01557         value *= (pwr * noisefunc(x, y, z) + 1.0f);
01558         pwr *= pwHL;
01559         x *= lacunarity;
01560         y *= lacunarity;
01561         z *= lacunarity;
01562     }
01563     rmd = octaves - floorf(octaves);
01564     if (rmd!=0.0f) value *= (rmd * noisefunc(x, y, z) * pwr + 1.0f);
01565 
01566     return value;
01567 
01568 } /* multifractal() */
01569 
01570 /*
01571  * Heterogeneous procedural terrain function: stats by altitude method.
01572  * Evaluated at "point"; returns value stored in "value".
01573  *
01574  * Parameters:
01575  *       ``H''  determines the fractal dimension of the roughest areas
01576  *       ``lacunarity''  is the gap between successive frequencies
01577  *       ``octaves''  is the number of frequencies in the fBm
01578  *       ``offset''  raises the terrain from `sea level'
01579  */
01580 float mg_HeteroTerrain(float x, float y, float z, float H, float lacunarity, float octaves, float offset, int noisebasis)
01581 {
01582     float   value, increment, rmd;
01583     int i;
01584     float pwHL = powf(lacunarity, -H);
01585     float pwr = pwHL;   /* starts with i=1 instead of 0 */
01586 
01587     float (*noisefunc)(float, float, float);
01588     switch (noisebasis) {
01589         case 1:
01590             noisefunc = orgPerlinNoise;
01591             break;
01592         case 2:
01593             noisefunc = newPerlin;
01594             break;
01595         case 3:
01596             noisefunc = voronoi_F1S;
01597             break;
01598         case 4:
01599             noisefunc = voronoi_F2S;
01600             break;
01601         case 5:
01602             noisefunc = voronoi_F3S;
01603             break;
01604         case 6:
01605             noisefunc = voronoi_F4S;
01606             break;
01607         case 7:
01608             noisefunc = voronoi_F1F2S;
01609             break;
01610         case 8:
01611             noisefunc = voronoi_CrS;
01612             break;
01613         case 14:
01614             noisefunc = cellNoise;
01615             break;
01616         case 0:
01617         default: {
01618             noisefunc = orgBlenderNoiseS;
01619         }
01620     }
01621 
01622     /* first unscaled octave of function; later octaves are scaled */
01623     value = offset + noisefunc(x, y, z);
01624     x *= lacunarity;
01625     y *= lacunarity;
01626     z *= lacunarity;
01627 
01628     for (i=1; i<(int)octaves; i++) {
01629         increment = (noisefunc(x, y, z) + offset) * pwr * value;
01630         value += increment;
01631         pwr *= pwHL;
01632         x *= lacunarity;
01633         y *= lacunarity;
01634         z *= lacunarity;
01635     }
01636 
01637     rmd = octaves - floorf(octaves);
01638     if (rmd!=0.0f) {
01639         increment = (noisefunc(x, y, z) + offset) * pwr * value;
01640         value += rmd * increment;
01641     }
01642     return value;
01643 }
01644 
01645 
01646 /* Hybrid additive/multiplicative multifractal terrain model.
01647  *
01648  * Some good parameter values to start with:
01649  *
01650  *      H:           0.25
01651  *      offset:      0.7
01652  */
01653 float mg_HybridMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
01654 {
01655     float result, signal, weight, rmd;
01656     int i;
01657     float pwHL = powf(lacunarity, -H);
01658     float pwr = pwHL;   /* starts with i=1 instead of 0 */
01659     float (*noisefunc)(float, float, float);
01660 
01661     switch (noisebasis) {
01662         case 1:
01663             noisefunc = orgPerlinNoise;
01664             break;
01665         case 2:
01666             noisefunc = newPerlin;
01667             break;
01668         case 3:
01669             noisefunc = voronoi_F1S;
01670             break;
01671         case 4:
01672             noisefunc = voronoi_F2S;
01673             break;
01674         case 5:
01675             noisefunc = voronoi_F3S;
01676             break;
01677         case 6:
01678             noisefunc = voronoi_F4S;
01679             break;
01680         case 7:
01681             noisefunc = voronoi_F1F2S;
01682             break;
01683         case 8:
01684             noisefunc = voronoi_CrS;
01685             break;
01686         case 14:
01687             noisefunc = cellNoise;
01688             break;
01689         case 0:
01690         default: {
01691             noisefunc = orgBlenderNoiseS;
01692         }
01693     }
01694 
01695     result = noisefunc(x, y, z) + offset;
01696     weight = gain * result;
01697     x *= lacunarity;
01698     y *= lacunarity;
01699     z *= lacunarity;
01700 
01701     for (i=1; (weight>0.001f) && (i<(int)octaves); i++) {
01702         if (weight>1.0f)  weight=1.0f;
01703         signal = (noisefunc(x, y, z) + offset) * pwr;
01704         pwr *= pwHL;
01705         result += weight * signal;
01706         weight *= gain * signal;
01707         x *= lacunarity;
01708         y *= lacunarity;
01709         z *= lacunarity;
01710     }
01711 
01712     rmd = octaves - floorf(octaves);
01713     if (rmd!=0.f) result += rmd * ((noisefunc(x, y, z) + offset) * pwr);
01714 
01715     return result;
01716 
01717 } /* HybridMultifractal() */
01718 
01719 
01720 /* Ridged multifractal terrain model.
01721  *
01722  * Some good parameter values to start with:
01723  *
01724  *      H:           1.0
01725  *      offset:      1.0
01726  *      gain:        2.0
01727  */
01728 float mg_RidgedMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis)
01729 {
01730     float result, signal, weight;
01731     int i;
01732     float pwHL = powf(lacunarity, -H);
01733     float pwr = pwHL;   /* starts with i=1 instead of 0 */
01734     
01735     float (*noisefunc)(float, float, float);
01736     switch (noisebasis) {
01737         case 1:
01738             noisefunc = orgPerlinNoise;
01739             break;
01740         case 2:
01741             noisefunc = newPerlin;
01742             break;
01743         case 3:
01744             noisefunc = voronoi_F1S;
01745             break;
01746         case 4:
01747             noisefunc = voronoi_F2S;
01748             break;
01749         case 5:
01750             noisefunc = voronoi_F3S;
01751             break;
01752         case 6:
01753             noisefunc = voronoi_F4S;
01754             break;
01755         case 7:
01756             noisefunc = voronoi_F1F2S;
01757             break;
01758         case 8:
01759             noisefunc = voronoi_CrS;
01760             break;
01761         case 14:
01762             noisefunc = cellNoise;
01763             break;
01764         case 0:
01765         default: {
01766             noisefunc = orgBlenderNoiseS;
01767         }
01768     }
01769 
01770     signal = offset - fabsf(noisefunc(x, y, z));
01771     signal *= signal;
01772     result = signal;
01773 
01774 
01775     for( i=1; i<(int)octaves; i++ ) {
01776         x *= lacunarity;
01777         y *= lacunarity;
01778         z *= lacunarity;
01779         weight = signal * gain;
01780         if (weight>1.0f) weight=1.0f; else if (weight<0.0f) weight=0.0f;
01781         signal = offset - fabsf(noisefunc(x, y, z));
01782         signal *= signal;
01783         signal *= weight;
01784         result += signal * pwr;
01785         pwr *= pwHL;
01786     }
01787 
01788     return result;
01789 } /* RidgedMultifractal() */
01790 
01791 /* "Variable Lacunarity Noise"
01792  * A distorted variety of Perlin noise.
01793  */
01794 float mg_VLNoise(float x, float y, float z, float distortion, int nbas1, int nbas2)
01795 {
01796     float rv[3];
01797     float (*noisefunc1)(float, float, float);
01798     float (*noisefunc2)(float, float, float);
01799 
01800     switch (nbas1) {
01801         case 1:
01802             noisefunc1 = orgPerlinNoise;
01803             break;
01804         case 2:
01805             noisefunc1 = newPerlin;
01806             break;
01807         case 3:
01808             noisefunc1 = voronoi_F1S;
01809             break;
01810         case 4:
01811             noisefunc1 = voronoi_F2S;
01812             break;
01813         case 5:
01814             noisefunc1 = voronoi_F3S;
01815             break;
01816         case 6:
01817             noisefunc1 = voronoi_F4S;
01818             break;
01819         case 7:
01820             noisefunc1 = voronoi_F1F2S;
01821             break;
01822         case 8:
01823             noisefunc1 = voronoi_CrS;
01824             break;
01825         case 14:
01826             noisefunc1 = cellNoise;
01827             break;
01828         case 0:
01829         default: {
01830             noisefunc1 = orgBlenderNoiseS;
01831         }
01832     }
01833 
01834     switch (nbas2) {
01835         case 1:
01836             noisefunc2 = orgPerlinNoise;
01837             break;
01838         case 2:
01839             noisefunc2 = newPerlin;
01840             break;
01841         case 3:
01842             noisefunc2 = voronoi_F1S;
01843             break;
01844         case 4:
01845             noisefunc2 = voronoi_F2S;
01846             break;
01847         case 5:
01848             noisefunc2 = voronoi_F3S;
01849             break;
01850         case 6:
01851             noisefunc2 = voronoi_F4S;
01852             break;
01853         case 7:
01854             noisefunc2 = voronoi_F1F2S;
01855             break;
01856         case 8:
01857             noisefunc2 = voronoi_CrS;
01858             break;
01859         case 14:
01860             noisefunc2 = cellNoise;
01861             break;
01862         case 0:
01863         default: {
01864             noisefunc2 = orgBlenderNoiseS;
01865         }
01866     }
01867 
01868     /* get a random vector and scale the randomization */
01869     rv[0] = noisefunc1(x+13.5f, y+13.5f, z+13.5f) * distortion;
01870     rv[1] = noisefunc1(x, y, z) * distortion;
01871     rv[2] = noisefunc1(x-13.5f, y-13.5f, z-13.5f) * distortion;
01872     return noisefunc2(x+rv[0], y+rv[1], z+rv[2]);   /* distorted-domain noise */
01873 }
01874 
01875 /****************/
01876 /* musgrave end */
01877 /****************/