13464ebd5Sriastradh/* 23464ebd5Sriastradh * Mesa 3-D graphics library 33464ebd5Sriastradh * 43464ebd5Sriastradh * Copyright (C) 2006 Brian Paul All Rights Reserved. 53464ebd5Sriastradh * 63464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a 73464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"), 83464ebd5Sriastradh * to deal in the Software without restriction, including without limitation 93464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense, 103464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the 113464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions: 123464ebd5Sriastradh * 133464ebd5Sriastradh * The above copyright notice and this permission notice shall be included 143464ebd5Sriastradh * in all copies or substantial portions of the Software. 153464ebd5Sriastradh * 163464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 173464ebd5Sriastradh * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 183464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE. 233464ebd5Sriastradh */ 243464ebd5Sriastradh 253464ebd5Sriastradh/* 263464ebd5Sriastradh * SimplexNoise1234 273464ebd5Sriastradh * Copyright (c) 2003-2005, Stefan Gustavson 283464ebd5Sriastradh * 293464ebd5Sriastradh * Contact: stegu@itn.liu.se 303464ebd5Sriastradh */ 313464ebd5Sriastradh 323464ebd5Sriastradh/** 333464ebd5Sriastradh * \file 343464ebd5Sriastradh * \brief C implementation of Perlin Simplex Noise over 1, 2, 3 and 4 dims. 353464ebd5Sriastradh * \author Stefan Gustavson (stegu@itn.liu.se) 363464ebd5Sriastradh * 373464ebd5Sriastradh * 383464ebd5Sriastradh * This implementation is "Simplex Noise" as presented by 393464ebd5Sriastradh * Ken Perlin at a relatively obscure and not often cited course 403464ebd5Sriastradh * session "Real-Time Shading" at Siggraph 2001 (before real 413464ebd5Sriastradh * time shading actually took on), under the title "hardware noise". 423464ebd5Sriastradh * The 3D function is numerically equivalent to his Java reference 433464ebd5Sriastradh * code available in the PDF course notes, although I re-implemented 443464ebd5Sriastradh * it from scratch to get more readable code. The 1D, 2D and 4D cases 453464ebd5Sriastradh * were implemented from scratch by me from Ken Perlin's text. 463464ebd5Sriastradh * 473464ebd5Sriastradh * This file has no dependencies on any other file, not even its own 483464ebd5Sriastradh * header file. The header file is made for use by external code only. 493464ebd5Sriastradh */ 503464ebd5Sriastradh 513464ebd5Sriastradh 527ec681f3Smrg 533464ebd5Sriastradh#include "prog_noise.h" 543464ebd5Sriastradh 553464ebd5Sriastradh#define FASTFLOOR(x) ( ((x)>0) ? ((int)x) : (((int)x)-1) ) 563464ebd5Sriastradh 573464ebd5Sriastradh/* 583464ebd5Sriastradh * --------------------------------------------------------------------- 593464ebd5Sriastradh * Static data 603464ebd5Sriastradh */ 613464ebd5Sriastradh 623464ebd5Sriastradh/** 633464ebd5Sriastradh * Permutation table. This is just a random jumble of all numbers 0-255, 643464ebd5Sriastradh * repeated twice to avoid wrapping the index at 255 for each lookup. 653464ebd5Sriastradh * This needs to be exactly the same for all instances on all platforms, 663464ebd5Sriastradh * so it's easiest to just keep it as static explicit data. 673464ebd5Sriastradh * This also removes the need for any initialisation of this class. 683464ebd5Sriastradh * 693464ebd5Sriastradh * Note that making this an int[] instead of a char[] might make the 703464ebd5Sriastradh * code run faster on platforms with a high penalty for unaligned single 713464ebd5Sriastradh * byte addressing. Intel x86 is generally single-byte-friendly, but 723464ebd5Sriastradh * some other CPUs are faster with 4-aligned reads. 733464ebd5Sriastradh * However, a char[] is smaller, which avoids cache trashing, and that 743464ebd5Sriastradh * is probably the most important aspect on most architectures. 753464ebd5Sriastradh * This array is accessed a *lot* by the noise functions. 763464ebd5Sriastradh * A vector-valued noise over 3D accesses it 96 times, and a 773464ebd5Sriastradh * float-valued 4D noise 64 times. We want this to fit in the cache! 783464ebd5Sriastradh */ 79af69d88dSmrgstatic const unsigned char perm[512] = { 151, 160, 137, 91, 90, 15, 803464ebd5Sriastradh 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 813464ebd5Sriastradh 99, 37, 240, 21, 10, 23, 823464ebd5Sriastradh 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 833464ebd5Sriastradh 11, 32, 57, 177, 33, 843464ebd5Sriastradh 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 853464ebd5Sriastradh 134, 139, 48, 27, 166, 863464ebd5Sriastradh 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 873464ebd5Sriastradh 55, 46, 245, 40, 244, 883464ebd5Sriastradh 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 893464ebd5Sriastradh 18, 169, 200, 196, 903464ebd5Sriastradh 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 913464ebd5Sriastradh 226, 250, 124, 123, 923464ebd5Sriastradh 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 933464ebd5Sriastradh 17, 182, 189, 28, 42, 943464ebd5Sriastradh 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 953464ebd5Sriastradh 167, 43, 172, 9, 963464ebd5Sriastradh 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 973464ebd5Sriastradh 218, 246, 97, 228, 983464ebd5Sriastradh 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 993464ebd5Sriastradh 249, 14, 239, 107, 1003464ebd5Sriastradh 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 1013464ebd5Sriastradh 127, 4, 150, 254, 1023464ebd5Sriastradh 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 1033464ebd5Sriastradh 215, 61, 156, 180, 1043464ebd5Sriastradh 151, 160, 137, 91, 90, 15, 1053464ebd5Sriastradh 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 1063464ebd5Sriastradh 99, 37, 240, 21, 10, 23, 1073464ebd5Sriastradh 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 1083464ebd5Sriastradh 11, 32, 57, 177, 33, 1093464ebd5Sriastradh 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 1103464ebd5Sriastradh 134, 139, 48, 27, 166, 1113464ebd5Sriastradh 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 1123464ebd5Sriastradh 55, 46, 245, 40, 244, 1133464ebd5Sriastradh 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 1143464ebd5Sriastradh 18, 169, 200, 196, 1153464ebd5Sriastradh 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 1163464ebd5Sriastradh 226, 250, 124, 123, 1173464ebd5Sriastradh 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 1183464ebd5Sriastradh 17, 182, 189, 28, 42, 1193464ebd5Sriastradh 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 1203464ebd5Sriastradh 167, 43, 172, 9, 1213464ebd5Sriastradh 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 1223464ebd5Sriastradh 218, 246, 97, 228, 1233464ebd5Sriastradh 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 1243464ebd5Sriastradh 249, 14, 239, 107, 1253464ebd5Sriastradh 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 1263464ebd5Sriastradh 127, 4, 150, 254, 1273464ebd5Sriastradh 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 1283464ebd5Sriastradh 215, 61, 156, 180 1293464ebd5Sriastradh}; 1303464ebd5Sriastradh 1313464ebd5Sriastradh/* 1323464ebd5Sriastradh * --------------------------------------------------------------------- 1333464ebd5Sriastradh */ 1343464ebd5Sriastradh 1353464ebd5Sriastradh/* 1363464ebd5Sriastradh * Helper functions to compute gradients-dot-residualvectors (1D to 4D) 1373464ebd5Sriastradh * Note that these generate gradients of more than unit length. To make 1383464ebd5Sriastradh * a close match with the value range of classic Perlin noise, the final 1393464ebd5Sriastradh * noise values need to be rescaled to fit nicely within [-1,1]. 1403464ebd5Sriastradh * (The simplex noise functions as such also have different scaling.) 1413464ebd5Sriastradh * Note also that these noise functions are the most practical and useful 1423464ebd5Sriastradh * signed version of Perlin noise. To return values according to the 1433464ebd5Sriastradh * RenderMan specification from the SL noise() and pnoise() functions, 1443464ebd5Sriastradh * the noise values need to be scaled and offset to [0,1], like this: 1453464ebd5Sriastradh * float SLnoise = (SimplexNoise1234::noise(x,y,z) + 1.0) * 0.5; 1463464ebd5Sriastradh */ 1473464ebd5Sriastradh 1483464ebd5Sriastradhstatic float 1493464ebd5Sriastradhgrad1(int hash, float x) 1503464ebd5Sriastradh{ 1513464ebd5Sriastradh int h = hash & 15; 1523464ebd5Sriastradh float grad = 1.0f + (h & 7); /* Gradient value 1.0, 2.0, ..., 8.0 */ 1533464ebd5Sriastradh if (h & 8) 1543464ebd5Sriastradh grad = -grad; /* Set a random sign for the gradient */ 1553464ebd5Sriastradh return (grad * x); /* Multiply the gradient with the distance */ 1563464ebd5Sriastradh} 1573464ebd5Sriastradh 1583464ebd5Sriastradhstatic float 1593464ebd5Sriastradhgrad2(int hash, float x, float y) 1603464ebd5Sriastradh{ 1613464ebd5Sriastradh int h = hash & 7; /* Convert low 3 bits of hash code */ 1623464ebd5Sriastradh float u = h < 4 ? x : y; /* into 8 simple gradient directions, */ 1633464ebd5Sriastradh float v = h < 4 ? y : x; /* and compute the dot product with (x,y). */ 1643464ebd5Sriastradh return ((h & 1) ? -u : u) + ((h & 2) ? -2.0f * v : 2.0f * v); 1653464ebd5Sriastradh} 1663464ebd5Sriastradh 1673464ebd5Sriastradhstatic float 1683464ebd5Sriastradhgrad3(int hash, float x, float y, float z) 1693464ebd5Sriastradh{ 1703464ebd5Sriastradh int h = hash & 15; /* Convert low 4 bits of hash code into 12 simple */ 1713464ebd5Sriastradh float u = h < 8 ? x : y; /* gradient directions, and compute dot product. */ 1723464ebd5Sriastradh float v = h < 4 ? y : h == 12 || h == 14 ? x : z; /* Fix repeats at h = 12 to 15 */ 1733464ebd5Sriastradh return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); 1743464ebd5Sriastradh} 1753464ebd5Sriastradh 1763464ebd5Sriastradhstatic float 1773464ebd5Sriastradhgrad4(int hash, float x, float y, float z, float t) 1783464ebd5Sriastradh{ 1793464ebd5Sriastradh int h = hash & 31; /* Convert low 5 bits of hash code into 32 simple */ 1803464ebd5Sriastradh float u = h < 24 ? x : y; /* gradient directions, and compute dot product. */ 1813464ebd5Sriastradh float v = h < 16 ? y : z; 1823464ebd5Sriastradh float w = h < 8 ? z : t; 1833464ebd5Sriastradh return ((h & 1) ? -u : u) + ((h & 2) ? -v : v) + ((h & 4) ? -w : w); 1843464ebd5Sriastradh} 1853464ebd5Sriastradh 1863464ebd5Sriastradh/** 1873464ebd5Sriastradh * A lookup table to traverse the simplex around a given point in 4D. 1883464ebd5Sriastradh * Details can be found where this table is used, in the 4D noise method. 1893464ebd5Sriastradh * TODO: This should not be required, backport it from Bill's GLSL code! 1903464ebd5Sriastradh */ 19101e04c3fSmrgstatic const unsigned char simplex[64][4] = { 1923464ebd5Sriastradh {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1}, 1933464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0}, 1943464ebd5Sriastradh {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1}, 1953464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0}, 1963464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 1973464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 1983464ebd5Sriastradh {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0}, 1993464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, 2003464ebd5Sriastradh {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, 2013464ebd5Sriastradh {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0}, 2023464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 2033464ebd5Sriastradh {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 2043464ebd5Sriastradh {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 2053464ebd5Sriastradh {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0}, 2063464ebd5Sriastradh {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 2073464ebd5Sriastradh {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0} 2083464ebd5Sriastradh}; 2093464ebd5Sriastradh 2103464ebd5Sriastradh 2113464ebd5Sriastradh/** 1D simplex noise */ 2123464ebd5SriastradhGLfloat 2133464ebd5Sriastradh_mesa_noise1(GLfloat x) 2143464ebd5Sriastradh{ 2153464ebd5Sriastradh int i0 = FASTFLOOR(x); 2163464ebd5Sriastradh int i1 = i0 + 1; 2173464ebd5Sriastradh float x0 = x - i0; 2183464ebd5Sriastradh float x1 = x0 - 1.0f; 2193464ebd5Sriastradh float t1 = 1.0f - x1 * x1; 2203464ebd5Sriastradh float n0, n1; 2213464ebd5Sriastradh 2223464ebd5Sriastradh float t0 = 1.0f - x0 * x0; 2233464ebd5Sriastradh/* if(t0 < 0.0f) t0 = 0.0f; // this never happens for the 1D case */ 2243464ebd5Sriastradh t0 *= t0; 2253464ebd5Sriastradh n0 = t0 * t0 * grad1(perm[i0 & 0xff], x0); 2263464ebd5Sriastradh 2273464ebd5Sriastradh/* if(t1 < 0.0f) t1 = 0.0f; // this never happens for the 1D case */ 2283464ebd5Sriastradh t1 *= t1; 2293464ebd5Sriastradh n1 = t1 * t1 * grad1(perm[i1 & 0xff], x1); 2303464ebd5Sriastradh /* The maximum value of this noise is 8*(3/4)^4 = 2.53125 */ 2313464ebd5Sriastradh /* A factor of 0.395 would scale to fit exactly within [-1,1], but */ 2323464ebd5Sriastradh /* we want to match PRMan's 1D noise, so we scale it down some more. */ 2333464ebd5Sriastradh return 0.25f * (n0 + n1); 2343464ebd5Sriastradh} 2353464ebd5Sriastradh 2363464ebd5Sriastradh 2373464ebd5Sriastradh/** 2D simplex noise */ 2383464ebd5SriastradhGLfloat 2393464ebd5Sriastradh_mesa_noise2(GLfloat x, GLfloat y) 2403464ebd5Sriastradh{ 2413464ebd5Sriastradh#define F2 0.366025403f /* F2 = 0.5*(sqrt(3.0)-1.0) */ 2423464ebd5Sriastradh#define G2 0.211324865f /* G2 = (3.0-Math.sqrt(3.0))/6.0 */ 2433464ebd5Sriastradh 2443464ebd5Sriastradh float n0, n1, n2; /* Noise contributions from the three corners */ 2453464ebd5Sriastradh 2463464ebd5Sriastradh /* Skew the input space to determine which simplex cell we're in */ 2473464ebd5Sriastradh float s = (x + y) * F2; /* Hairy factor for 2D */ 2483464ebd5Sriastradh float xs = x + s; 2493464ebd5Sriastradh float ys = y + s; 2503464ebd5Sriastradh int i = FASTFLOOR(xs); 2513464ebd5Sriastradh int j = FASTFLOOR(ys); 2523464ebd5Sriastradh 2533464ebd5Sriastradh float t = (float) (i + j) * G2; 2543464ebd5Sriastradh float X0 = i - t; /* Unskew the cell origin back to (x,y) space */ 2553464ebd5Sriastradh float Y0 = j - t; 2563464ebd5Sriastradh float x0 = x - X0; /* The x,y distances from the cell origin */ 2573464ebd5Sriastradh float y0 = y - Y0; 2583464ebd5Sriastradh 2593464ebd5Sriastradh float x1, y1, x2, y2; 260af69d88dSmrg unsigned int ii, jj; 2613464ebd5Sriastradh float t0, t1, t2; 2623464ebd5Sriastradh 2633464ebd5Sriastradh /* For the 2D case, the simplex shape is an equilateral triangle. */ 2643464ebd5Sriastradh /* Determine which simplex we are in. */ 265af69d88dSmrg unsigned int i1, j1; /* Offsets for second (middle) corner of simplex in (i,j) coords */ 2663464ebd5Sriastradh if (x0 > y0) { 2673464ebd5Sriastradh i1 = 1; 2683464ebd5Sriastradh j1 = 0; 2693464ebd5Sriastradh } /* lower triangle, XY order: (0,0)->(1,0)->(1,1) */ 2703464ebd5Sriastradh else { 2713464ebd5Sriastradh i1 = 0; 2723464ebd5Sriastradh j1 = 1; 2733464ebd5Sriastradh } /* upper triangle, YX order: (0,0)->(0,1)->(1,1) */ 2743464ebd5Sriastradh 2753464ebd5Sriastradh /* A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and */ 2763464ebd5Sriastradh /* a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where */ 2773464ebd5Sriastradh /* c = (3-sqrt(3))/6 */ 2783464ebd5Sriastradh 2793464ebd5Sriastradh x1 = x0 - i1 + G2; /* Offsets for middle corner in (x,y) unskewed coords */ 2803464ebd5Sriastradh y1 = y0 - j1 + G2; 2813464ebd5Sriastradh x2 = x0 - 1.0f + 2.0f * G2; /* Offsets for last corner in (x,y) unskewed coords */ 2823464ebd5Sriastradh y2 = y0 - 1.0f + 2.0f * G2; 2833464ebd5Sriastradh 2843464ebd5Sriastradh /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */ 285af69d88dSmrg ii = i & 0xff; 286af69d88dSmrg jj = j & 0xff; 2873464ebd5Sriastradh 2883464ebd5Sriastradh /* Calculate the contribution from the three corners */ 2893464ebd5Sriastradh t0 = 0.5f - x0 * x0 - y0 * y0; 2903464ebd5Sriastradh if (t0 < 0.0f) 2913464ebd5Sriastradh n0 = 0.0f; 2923464ebd5Sriastradh else { 2933464ebd5Sriastradh t0 *= t0; 2943464ebd5Sriastradh n0 = t0 * t0 * grad2(perm[ii + perm[jj]], x0, y0); 2953464ebd5Sriastradh } 2963464ebd5Sriastradh 2973464ebd5Sriastradh t1 = 0.5f - x1 * x1 - y1 * y1; 2983464ebd5Sriastradh if (t1 < 0.0f) 2993464ebd5Sriastradh n1 = 0.0f; 3003464ebd5Sriastradh else { 3013464ebd5Sriastradh t1 *= t1; 3023464ebd5Sriastradh n1 = t1 * t1 * grad2(perm[ii + i1 + perm[jj + j1]], x1, y1); 3033464ebd5Sriastradh } 3043464ebd5Sriastradh 3053464ebd5Sriastradh t2 = 0.5f - x2 * x2 - y2 * y2; 3063464ebd5Sriastradh if (t2 < 0.0f) 3073464ebd5Sriastradh n2 = 0.0f; 3083464ebd5Sriastradh else { 3093464ebd5Sriastradh t2 *= t2; 3103464ebd5Sriastradh n2 = t2 * t2 * grad2(perm[ii + 1 + perm[jj + 1]], x2, y2); 3113464ebd5Sriastradh } 3123464ebd5Sriastradh 3133464ebd5Sriastradh /* Add contributions from each corner to get the final noise value. */ 3143464ebd5Sriastradh /* The result is scaled to return values in the interval [-1,1]. */ 3153464ebd5Sriastradh return 40.0f * (n0 + n1 + n2); /* TODO: The scale factor is preliminary! */ 3163464ebd5Sriastradh} 3173464ebd5Sriastradh 3183464ebd5Sriastradh 3193464ebd5Sriastradh/** 3D simplex noise */ 3203464ebd5SriastradhGLfloat 3213464ebd5Sriastradh_mesa_noise3(GLfloat x, GLfloat y, GLfloat z) 3223464ebd5Sriastradh{ 3233464ebd5Sriastradh/* Simple skewing factors for the 3D case */ 3243464ebd5Sriastradh#define F3 0.333333333f 3253464ebd5Sriastradh#define G3 0.166666667f 3263464ebd5Sriastradh 3273464ebd5Sriastradh float n0, n1, n2, n3; /* Noise contributions from the four corners */ 3283464ebd5Sriastradh 3293464ebd5Sriastradh /* Skew the input space to determine which simplex cell we're in */ 3303464ebd5Sriastradh float s = (x + y + z) * F3; /* Very nice and simple skew factor for 3D */ 3313464ebd5Sriastradh float xs = x + s; 3323464ebd5Sriastradh float ys = y + s; 3333464ebd5Sriastradh float zs = z + s; 3343464ebd5Sriastradh int i = FASTFLOOR(xs); 3353464ebd5Sriastradh int j = FASTFLOOR(ys); 3363464ebd5Sriastradh int k = FASTFLOOR(zs); 3373464ebd5Sriastradh 3383464ebd5Sriastradh float t = (float) (i + j + k) * G3; 3393464ebd5Sriastradh float X0 = i - t; /* Unskew the cell origin back to (x,y,z) space */ 3403464ebd5Sriastradh float Y0 = j - t; 3413464ebd5Sriastradh float Z0 = k - t; 3423464ebd5Sriastradh float x0 = x - X0; /* The x,y,z distances from the cell origin */ 3433464ebd5Sriastradh float y0 = y - Y0; 3443464ebd5Sriastradh float z0 = z - Z0; 3453464ebd5Sriastradh 3463464ebd5Sriastradh float x1, y1, z1, x2, y2, z2, x3, y3, z3; 347af69d88dSmrg unsigned int ii, jj, kk; 3483464ebd5Sriastradh float t0, t1, t2, t3; 3493464ebd5Sriastradh 3503464ebd5Sriastradh /* For the 3D case, the simplex shape is a slightly irregular tetrahedron. */ 3513464ebd5Sriastradh /* Determine which simplex we are in. */ 352af69d88dSmrg unsigned int i1, j1, k1; /* Offsets for second corner of simplex in (i,j,k) coords */ 353af69d88dSmrg unsigned int i2, j2, k2; /* Offsets for third corner of simplex in (i,j,k) coords */ 3543464ebd5Sriastradh 3553464ebd5Sriastradh/* This code would benefit from a backport from the GLSL version! */ 3563464ebd5Sriastradh if (x0 >= y0) { 3573464ebd5Sriastradh if (y0 >= z0) { 3583464ebd5Sriastradh i1 = 1; 3593464ebd5Sriastradh j1 = 0; 3603464ebd5Sriastradh k1 = 0; 3613464ebd5Sriastradh i2 = 1; 3623464ebd5Sriastradh j2 = 1; 3633464ebd5Sriastradh k2 = 0; 3643464ebd5Sriastradh } /* X Y Z order */ 3653464ebd5Sriastradh else if (x0 >= z0) { 3663464ebd5Sriastradh i1 = 1; 3673464ebd5Sriastradh j1 = 0; 3683464ebd5Sriastradh k1 = 0; 3693464ebd5Sriastradh i2 = 1; 3703464ebd5Sriastradh j2 = 0; 3713464ebd5Sriastradh k2 = 1; 3723464ebd5Sriastradh } /* X Z Y order */ 3733464ebd5Sriastradh else { 3743464ebd5Sriastradh i1 = 0; 3753464ebd5Sriastradh j1 = 0; 3763464ebd5Sriastradh k1 = 1; 3773464ebd5Sriastradh i2 = 1; 3783464ebd5Sriastradh j2 = 0; 3793464ebd5Sriastradh k2 = 1; 3803464ebd5Sriastradh } /* Z X Y order */ 3813464ebd5Sriastradh } 3823464ebd5Sriastradh else { /* x0<y0 */ 3833464ebd5Sriastradh if (y0 < z0) { 3843464ebd5Sriastradh i1 = 0; 3853464ebd5Sriastradh j1 = 0; 3863464ebd5Sriastradh k1 = 1; 3873464ebd5Sriastradh i2 = 0; 3883464ebd5Sriastradh j2 = 1; 3893464ebd5Sriastradh k2 = 1; 3903464ebd5Sriastradh } /* Z Y X order */ 3913464ebd5Sriastradh else if (x0 < z0) { 3923464ebd5Sriastradh i1 = 0; 3933464ebd5Sriastradh j1 = 1; 3943464ebd5Sriastradh k1 = 0; 3953464ebd5Sriastradh i2 = 0; 3963464ebd5Sriastradh j2 = 1; 3973464ebd5Sriastradh k2 = 1; 3983464ebd5Sriastradh } /* Y Z X order */ 3993464ebd5Sriastradh else { 4003464ebd5Sriastradh i1 = 0; 4013464ebd5Sriastradh j1 = 1; 4023464ebd5Sriastradh k1 = 0; 4033464ebd5Sriastradh i2 = 1; 4043464ebd5Sriastradh j2 = 1; 4053464ebd5Sriastradh k2 = 0; 4063464ebd5Sriastradh } /* Y X Z order */ 4073464ebd5Sriastradh } 4083464ebd5Sriastradh 4093464ebd5Sriastradh /* A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in 4103464ebd5Sriastradh * (x,y,z), a step of (0,1,0) in (i,j,k) means a step of 4113464ebd5Sriastradh * (-c,1-c,-c) in (x,y,z), and a step of (0,0,1) in (i,j,k) means a 4123464ebd5Sriastradh * step of (-c,-c,1-c) in (x,y,z), where c = 1/6. 4133464ebd5Sriastradh */ 4143464ebd5Sriastradh 4153464ebd5Sriastradh x1 = x0 - i1 + G3; /* Offsets for second corner in (x,y,z) coords */ 4163464ebd5Sriastradh y1 = y0 - j1 + G3; 4173464ebd5Sriastradh z1 = z0 - k1 + G3; 4183464ebd5Sriastradh x2 = x0 - i2 + 2.0f * G3; /* Offsets for third corner in (x,y,z) coords */ 4193464ebd5Sriastradh y2 = y0 - j2 + 2.0f * G3; 4203464ebd5Sriastradh z2 = z0 - k2 + 2.0f * G3; 4213464ebd5Sriastradh x3 = x0 - 1.0f + 3.0f * G3;/* Offsets for last corner in (x,y,z) coords */ 4223464ebd5Sriastradh y3 = y0 - 1.0f + 3.0f * G3; 4233464ebd5Sriastradh z3 = z0 - 1.0f + 3.0f * G3; 4243464ebd5Sriastradh 4253464ebd5Sriastradh /* Wrap the integer indices at 256 to avoid indexing perm[] out of bounds */ 426af69d88dSmrg ii = i & 0xff; 427af69d88dSmrg jj = j & 0xff; 428af69d88dSmrg kk = k & 0xff; 4293464ebd5Sriastradh 4303464ebd5Sriastradh /* Calculate the contribution from the four corners */ 4313464ebd5Sriastradh t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0; 4323464ebd5Sriastradh if (t0 < 0.0f) 4333464ebd5Sriastradh n0 = 0.0f; 4343464ebd5Sriastradh else { 4353464ebd5Sriastradh t0 *= t0; 4363464ebd5Sriastradh n0 = t0 * t0 * grad3(perm[ii + perm[jj + perm[kk]]], x0, y0, z0); 4373464ebd5Sriastradh } 4383464ebd5Sriastradh 4393464ebd5Sriastradh t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1; 4403464ebd5Sriastradh if (t1 < 0.0f) 4413464ebd5Sriastradh n1 = 0.0f; 4423464ebd5Sriastradh else { 4433464ebd5Sriastradh t1 *= t1; 4443464ebd5Sriastradh n1 = 4453464ebd5Sriastradh t1 * t1 * grad3(perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], x1, 4463464ebd5Sriastradh y1, z1); 4473464ebd5Sriastradh } 4483464ebd5Sriastradh 4493464ebd5Sriastradh t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2; 4503464ebd5Sriastradh if (t2 < 0.0f) 4513464ebd5Sriastradh n2 = 0.0f; 4523464ebd5Sriastradh else { 4533464ebd5Sriastradh t2 *= t2; 4543464ebd5Sriastradh n2 = 4553464ebd5Sriastradh t2 * t2 * grad3(perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], x2, 4563464ebd5Sriastradh y2, z2); 4573464ebd5Sriastradh } 4583464ebd5Sriastradh 4593464ebd5Sriastradh t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3; 4603464ebd5Sriastradh if (t3 < 0.0f) 4613464ebd5Sriastradh n3 = 0.0f; 4623464ebd5Sriastradh else { 4633464ebd5Sriastradh t3 *= t3; 4643464ebd5Sriastradh n3 = 4653464ebd5Sriastradh t3 * t3 * grad3(perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], x3, y3, 4663464ebd5Sriastradh z3); 4673464ebd5Sriastradh } 4683464ebd5Sriastradh 4693464ebd5Sriastradh /* Add contributions from each corner to get the final noise value. 4703464ebd5Sriastradh * The result is scaled to stay just inside [-1,1] 4713464ebd5Sriastradh */ 4723464ebd5Sriastradh return 32.0f * (n0 + n1 + n2 + n3); /* TODO: The scale factor is preliminary! */ 4733464ebd5Sriastradh} 4743464ebd5Sriastradh 4753464ebd5Sriastradh 4763464ebd5Sriastradh/** 4D simplex noise */ 4773464ebd5SriastradhGLfloat 4783464ebd5Sriastradh_mesa_noise4(GLfloat x, GLfloat y, GLfloat z, GLfloat w) 4793464ebd5Sriastradh{ 4803464ebd5Sriastradh /* The skewing and unskewing factors are hairy again for the 4D case */ 4813464ebd5Sriastradh#define F4 0.309016994f /* F4 = (Math.sqrt(5.0)-1.0)/4.0 */ 4823464ebd5Sriastradh#define G4 0.138196601f /* G4 = (5.0-Math.sqrt(5.0))/20.0 */ 4833464ebd5Sriastradh 4843464ebd5Sriastradh float n0, n1, n2, n3, n4; /* Noise contributions from the five corners */ 4853464ebd5Sriastradh 4863464ebd5Sriastradh /* Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in */ 4873464ebd5Sriastradh float s = (x + y + z + w) * F4; /* Factor for 4D skewing */ 4883464ebd5Sriastradh float xs = x + s; 4893464ebd5Sriastradh float ys = y + s; 4903464ebd5Sriastradh float zs = z + s; 4913464ebd5Sriastradh float ws = w + s; 4923464ebd5Sriastradh int i = FASTFLOOR(xs); 4933464ebd5Sriastradh int j = FASTFLOOR(ys); 4943464ebd5Sriastradh int k = FASTFLOOR(zs); 4953464ebd5Sriastradh int l = FASTFLOOR(ws); 4963464ebd5Sriastradh 4973464ebd5Sriastradh float t = (i + j + k + l) * G4; /* Factor for 4D unskewing */ 4983464ebd5Sriastradh float X0 = i - t; /* Unskew the cell origin back to (x,y,z,w) space */ 4993464ebd5Sriastradh float Y0 = j - t; 5003464ebd5Sriastradh float Z0 = k - t; 5013464ebd5Sriastradh float W0 = l - t; 5023464ebd5Sriastradh 5033464ebd5Sriastradh float x0 = x - X0; /* The x,y,z,w distances from the cell origin */ 5043464ebd5Sriastradh float y0 = y - Y0; 5053464ebd5Sriastradh float z0 = z - Z0; 5063464ebd5Sriastradh float w0 = w - W0; 5073464ebd5Sriastradh 5083464ebd5Sriastradh /* For the 4D case, the simplex is a 4D shape I won't even try to describe. 5093464ebd5Sriastradh * To find out which of the 24 possible simplices we're in, we need to 5103464ebd5Sriastradh * determine the magnitude ordering of x0, y0, z0 and w0. 5113464ebd5Sriastradh * The method below is a good way of finding the ordering of x,y,z,w and 5123464ebd5Sriastradh * then find the correct traversal order for the simplex we're in. 5133464ebd5Sriastradh * First, six pair-wise comparisons are performed between each possible pair 5143464ebd5Sriastradh * of the four coordinates, and the results are used to add up binary bits 5153464ebd5Sriastradh * for an integer index. 5163464ebd5Sriastradh */ 5173464ebd5Sriastradh int c1 = (x0 > y0) ? 32 : 0; 5183464ebd5Sriastradh int c2 = (x0 > z0) ? 16 : 0; 5193464ebd5Sriastradh int c3 = (y0 > z0) ? 8 : 0; 5203464ebd5Sriastradh int c4 = (x0 > w0) ? 4 : 0; 5213464ebd5Sriastradh int c5 = (y0 > w0) ? 2 : 0; 5223464ebd5Sriastradh int c6 = (z0 > w0) ? 1 : 0; 5233464ebd5Sriastradh int c = c1 + c2 + c3 + c4 + c5 + c6; 5243464ebd5Sriastradh 525af69d88dSmrg unsigned int i1, j1, k1, l1; /* The integer offsets for the second simplex corner */ 526af69d88dSmrg unsigned int i2, j2, k2, l2; /* The integer offsets for the third simplex corner */ 527af69d88dSmrg unsigned int i3, j3, k3, l3; /* The integer offsets for the fourth simplex corner */ 5283464ebd5Sriastradh 5293464ebd5Sriastradh float x1, y1, z1, w1, x2, y2, z2, w2, x3, y3, z3, w3, x4, y4, z4, w4; 530af69d88dSmrg unsigned int ii, jj, kk, ll; 5313464ebd5Sriastradh float t0, t1, t2, t3, t4; 5323464ebd5Sriastradh 5333464ebd5Sriastradh /* 5343464ebd5Sriastradh * simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some 5353464ebd5Sriastradh * order. Many values of c will never occur, since e.g. x>y>z>w 5363464ebd5Sriastradh * makes x<z, y<w and x<w impossible. Only the 24 indices which 5373464ebd5Sriastradh * have non-zero entries make any sense. We use a thresholding to 5383464ebd5Sriastradh * set the coordinates in turn from the largest magnitude. The 5393464ebd5Sriastradh * number 3 in the "simplex" array is at the position of the 5403464ebd5Sriastradh * largest coordinate. 5413464ebd5Sriastradh */ 5423464ebd5Sriastradh i1 = simplex[c][0] >= 3 ? 1 : 0; 5433464ebd5Sriastradh j1 = simplex[c][1] >= 3 ? 1 : 0; 5443464ebd5Sriastradh k1 = simplex[c][2] >= 3 ? 1 : 0; 5453464ebd5Sriastradh l1 = simplex[c][3] >= 3 ? 1 : 0; 5463464ebd5Sriastradh /* The number 2 in the "simplex" array is at the second largest coordinate. */ 5473464ebd5Sriastradh i2 = simplex[c][0] >= 2 ? 1 : 0; 5483464ebd5Sriastradh j2 = simplex[c][1] >= 2 ? 1 : 0; 5493464ebd5Sriastradh k2 = simplex[c][2] >= 2 ? 1 : 0; 5503464ebd5Sriastradh l2 = simplex[c][3] >= 2 ? 1 : 0; 5513464ebd5Sriastradh /* The number 1 in the "simplex" array is at the second smallest coordinate. */ 5523464ebd5Sriastradh i3 = simplex[c][0] >= 1 ? 1 : 0; 5533464ebd5Sriastradh j3 = simplex[c][1] >= 1 ? 1 : 0; 5543464ebd5Sriastradh k3 = simplex[c][2] >= 1 ? 1 : 0; 5553464ebd5Sriastradh l3 = simplex[c][3] >= 1 ? 1 : 0; 5563464ebd5Sriastradh /* The fifth corner has all coordinate offsets = 1, so no need to look that up. */ 5573464ebd5Sriastradh 5583464ebd5Sriastradh x1 = x0 - i1 + G4; /* Offsets for second corner in (x,y,z,w) coords */ 5593464ebd5Sriastradh y1 = y0 - j1 + G4; 5603464ebd5Sriastradh z1 = z0 - k1 + G4; 5613464ebd5Sriastradh w1 = w0 - l1 + G4; 5623464ebd5Sriastradh x2 = x0 - i2 + 2.0f * G4; /* Offsets for third corner in (x,y,z,w) coords */ 5633464ebd5Sriastradh y2 = y0 - j2 + 2.0f * G4; 5643464ebd5Sriastradh z2 = z0 - k2 + 2.0f * G4; 5653464ebd5Sriastradh w2 = w0 - l2 + 2.0f * G4; 5663464ebd5Sriastradh x3 = x0 - i3 + 3.0f * G4; /* Offsets for fourth corner in (x,y,z,w) coords */ 5673464ebd5Sriastradh y3 = y0 - j3 + 3.0f * G4; 5683464ebd5Sriastradh z3 = z0 - k3 + 3.0f * G4; 5693464ebd5Sriastradh w3 = w0 - l3 + 3.0f * G4; 5703464ebd5Sriastradh x4 = x0 - 1.0f + 4.0f * G4; /* Offsets for last corner in (x,y,z,w) coords */ 5713464ebd5Sriastradh y4 = y0 - 1.0f + 4.0f * G4; 5723464ebd5Sriastradh z4 = z0 - 1.0f + 4.0f * G4; 5733464ebd5Sriastradh w4 = w0 - 1.0f + 4.0f * G4; 5743464ebd5Sriastradh 5753464ebd5Sriastradh /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */ 576af69d88dSmrg ii = i & 0xff; 577af69d88dSmrg jj = j & 0xff; 578af69d88dSmrg kk = k & 0xff; 579af69d88dSmrg ll = l & 0xff; 5803464ebd5Sriastradh 5813464ebd5Sriastradh /* Calculate the contribution from the five corners */ 5823464ebd5Sriastradh t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0; 5833464ebd5Sriastradh if (t0 < 0.0f) 5843464ebd5Sriastradh n0 = 0.0f; 5853464ebd5Sriastradh else { 5863464ebd5Sriastradh t0 *= t0; 5873464ebd5Sriastradh n0 = 5883464ebd5Sriastradh t0 * t0 * grad4(perm[ii + perm[jj + perm[kk + perm[ll]]]], x0, y0, 5893464ebd5Sriastradh z0, w0); 5903464ebd5Sriastradh } 5913464ebd5Sriastradh 5923464ebd5Sriastradh t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1; 5933464ebd5Sriastradh if (t1 < 0.0f) 5943464ebd5Sriastradh n1 = 0.0f; 5953464ebd5Sriastradh else { 5963464ebd5Sriastradh t1 *= t1; 5973464ebd5Sriastradh n1 = 5983464ebd5Sriastradh t1 * t1 * 5993464ebd5Sriastradh grad4(perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]], 6003464ebd5Sriastradh x1, y1, z1, w1); 6013464ebd5Sriastradh } 6023464ebd5Sriastradh 6033464ebd5Sriastradh t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2; 6043464ebd5Sriastradh if (t2 < 0.0f) 6053464ebd5Sriastradh n2 = 0.0f; 6063464ebd5Sriastradh else { 6073464ebd5Sriastradh t2 *= t2; 6083464ebd5Sriastradh n2 = 6093464ebd5Sriastradh t2 * t2 * 6103464ebd5Sriastradh grad4(perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]], 6113464ebd5Sriastradh x2, y2, z2, w2); 6123464ebd5Sriastradh } 6133464ebd5Sriastradh 6143464ebd5Sriastradh t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3; 6153464ebd5Sriastradh if (t3 < 0.0f) 6163464ebd5Sriastradh n3 = 0.0f; 6173464ebd5Sriastradh else { 6183464ebd5Sriastradh t3 *= t3; 6193464ebd5Sriastradh n3 = 6203464ebd5Sriastradh t3 * t3 * 6213464ebd5Sriastradh grad4(perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]], 6223464ebd5Sriastradh x3, y3, z3, w3); 6233464ebd5Sriastradh } 6243464ebd5Sriastradh 6253464ebd5Sriastradh t4 = 0.6f - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4; 6263464ebd5Sriastradh if (t4 < 0.0f) 6273464ebd5Sriastradh n4 = 0.0f; 6283464ebd5Sriastradh else { 6293464ebd5Sriastradh t4 *= t4; 6303464ebd5Sriastradh n4 = 6313464ebd5Sriastradh t4 * t4 * 6323464ebd5Sriastradh grad4(perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]], x4, 6333464ebd5Sriastradh y4, z4, w4); 6343464ebd5Sriastradh } 6353464ebd5Sriastradh 6363464ebd5Sriastradh /* Sum up and scale the result to cover the range [-1,1] */ 6373464ebd5Sriastradh return 27.0f * (n0 + n1 + n2 + n3 + n4); /* TODO: The scale factor is preliminary! */ 6383464ebd5Sriastradh} 639