1848b8605Smrg/* 2848b8605Smrg * Mesa 3-D graphics library 3848b8605Smrg * 4848b8605Smrg * Copyright (C) 2006 Brian Paul All Rights Reserved. 5848b8605Smrg * 6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7848b8605Smrg * copy of this software and associated documentation files (the "Software"), 8848b8605Smrg * to deal in the Software without restriction, including without limitation 9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the 11848b8605Smrg * Software is furnished to do so, subject to the following conditions: 12848b8605Smrg * 13848b8605Smrg * The above copyright notice and this permission notice shall be included 14848b8605Smrg * in all copies or substantial portions of the Software. 15848b8605Smrg * 16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE. 23848b8605Smrg */ 24848b8605Smrg 25848b8605Smrg/* 26848b8605Smrg * SimplexNoise1234 27848b8605Smrg * Copyright (c) 2003-2005, Stefan Gustavson 28848b8605Smrg * 29848b8605Smrg * Contact: stegu@itn.liu.se 30848b8605Smrg */ 31848b8605Smrg 32848b8605Smrg/** 33848b8605Smrg * \file 34848b8605Smrg * \brief C implementation of Perlin Simplex Noise over 1, 2, 3 and 4 dims. 35848b8605Smrg * \author Stefan Gustavson (stegu@itn.liu.se) 36848b8605Smrg * 37848b8605Smrg * 38848b8605Smrg * This implementation is "Simplex Noise" as presented by 39848b8605Smrg * Ken Perlin at a relatively obscure and not often cited course 40848b8605Smrg * session "Real-Time Shading" at Siggraph 2001 (before real 41848b8605Smrg * time shading actually took on), under the title "hardware noise". 42848b8605Smrg * The 3D function is numerically equivalent to his Java reference 43848b8605Smrg * code available in the PDF course notes, although I re-implemented 44848b8605Smrg * it from scratch to get more readable code. The 1D, 2D and 4D cases 45848b8605Smrg * were implemented from scratch by me from Ken Perlin's text. 46848b8605Smrg * 47848b8605Smrg * This file has no dependencies on any other file, not even its own 48848b8605Smrg * header file. The header file is made for use by external code only. 49848b8605Smrg */ 50848b8605Smrg 51848b8605Smrg 52848b8605Smrg#include "main/imports.h" 53848b8605Smrg#include "prog_noise.h" 54848b8605Smrg 55848b8605Smrg#define FASTFLOOR(x) ( ((x)>0) ? ((int)x) : (((int)x)-1) ) 56848b8605Smrg 57848b8605Smrg/* 58848b8605Smrg * --------------------------------------------------------------------- 59848b8605Smrg * Static data 60848b8605Smrg */ 61848b8605Smrg 62848b8605Smrg/** 63848b8605Smrg * Permutation table. This is just a random jumble of all numbers 0-255, 64848b8605Smrg * repeated twice to avoid wrapping the index at 255 for each lookup. 65848b8605Smrg * This needs to be exactly the same for all instances on all platforms, 66848b8605Smrg * so it's easiest to just keep it as static explicit data. 67848b8605Smrg * This also removes the need for any initialisation of this class. 68848b8605Smrg * 69848b8605Smrg * Note that making this an int[] instead of a char[] might make the 70848b8605Smrg * code run faster on platforms with a high penalty for unaligned single 71848b8605Smrg * byte addressing. Intel x86 is generally single-byte-friendly, but 72848b8605Smrg * some other CPUs are faster with 4-aligned reads. 73848b8605Smrg * However, a char[] is smaller, which avoids cache trashing, and that 74848b8605Smrg * is probably the most important aspect on most architectures. 75848b8605Smrg * This array is accessed a *lot* by the noise functions. 76848b8605Smrg * A vector-valued noise over 3D accesses it 96 times, and a 77848b8605Smrg * float-valued 4D noise 64 times. We want this to fit in the cache! 78848b8605Smrg */ 79848b8605Smrgstatic const unsigned char perm[512] = { 151, 160, 137, 91, 90, 15, 80848b8605Smrg 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 81848b8605Smrg 99, 37, 240, 21, 10, 23, 82848b8605Smrg 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 83848b8605Smrg 11, 32, 57, 177, 33, 84848b8605Smrg 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 85848b8605Smrg 134, 139, 48, 27, 166, 86848b8605Smrg 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 87848b8605Smrg 55, 46, 245, 40, 244, 88848b8605Smrg 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 89848b8605Smrg 18, 169, 200, 196, 90848b8605Smrg 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 91848b8605Smrg 226, 250, 124, 123, 92848b8605Smrg 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 93848b8605Smrg 17, 182, 189, 28, 42, 94848b8605Smrg 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 95848b8605Smrg 167, 43, 172, 9, 96848b8605Smrg 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 97848b8605Smrg 218, 246, 97, 228, 98848b8605Smrg 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 99848b8605Smrg 249, 14, 239, 107, 100848b8605Smrg 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 101848b8605Smrg 127, 4, 150, 254, 102848b8605Smrg 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 103848b8605Smrg 215, 61, 156, 180, 104848b8605Smrg 151, 160, 137, 91, 90, 15, 105848b8605Smrg 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 106848b8605Smrg 99, 37, 240, 21, 10, 23, 107848b8605Smrg 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 108848b8605Smrg 11, 32, 57, 177, 33, 109848b8605Smrg 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 110848b8605Smrg 134, 139, 48, 27, 166, 111848b8605Smrg 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 112848b8605Smrg 55, 46, 245, 40, 244, 113848b8605Smrg 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 114848b8605Smrg 18, 169, 200, 196, 115848b8605Smrg 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 116848b8605Smrg 226, 250, 124, 123, 117848b8605Smrg 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 118848b8605Smrg 17, 182, 189, 28, 42, 119848b8605Smrg 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 120848b8605Smrg 167, 43, 172, 9, 121848b8605Smrg 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 122848b8605Smrg 218, 246, 97, 228, 123848b8605Smrg 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 124848b8605Smrg 249, 14, 239, 107, 125848b8605Smrg 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 126848b8605Smrg 127, 4, 150, 254, 127848b8605Smrg 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 128848b8605Smrg 215, 61, 156, 180 129848b8605Smrg}; 130848b8605Smrg 131848b8605Smrg/* 132848b8605Smrg * --------------------------------------------------------------------- 133848b8605Smrg */ 134848b8605Smrg 135848b8605Smrg/* 136848b8605Smrg * Helper functions to compute gradients-dot-residualvectors (1D to 4D) 137848b8605Smrg * Note that these generate gradients of more than unit length. To make 138848b8605Smrg * a close match with the value range of classic Perlin noise, the final 139848b8605Smrg * noise values need to be rescaled to fit nicely within [-1,1]. 140848b8605Smrg * (The simplex noise functions as such also have different scaling.) 141848b8605Smrg * Note also that these noise functions are the most practical and useful 142848b8605Smrg * signed version of Perlin noise. To return values according to the 143848b8605Smrg * RenderMan specification from the SL noise() and pnoise() functions, 144848b8605Smrg * the noise values need to be scaled and offset to [0,1], like this: 145848b8605Smrg * float SLnoise = (SimplexNoise1234::noise(x,y,z) + 1.0) * 0.5; 146848b8605Smrg */ 147848b8605Smrg 148848b8605Smrgstatic float 149848b8605Smrggrad1(int hash, float x) 150848b8605Smrg{ 151848b8605Smrg int h = hash & 15; 152848b8605Smrg float grad = 1.0f + (h & 7); /* Gradient value 1.0, 2.0, ..., 8.0 */ 153848b8605Smrg if (h & 8) 154848b8605Smrg grad = -grad; /* Set a random sign for the gradient */ 155848b8605Smrg return (grad * x); /* Multiply the gradient with the distance */ 156848b8605Smrg} 157848b8605Smrg 158848b8605Smrgstatic float 159848b8605Smrggrad2(int hash, float x, float y) 160848b8605Smrg{ 161848b8605Smrg int h = hash & 7; /* Convert low 3 bits of hash code */ 162848b8605Smrg float u = h < 4 ? x : y; /* into 8 simple gradient directions, */ 163848b8605Smrg float v = h < 4 ? y : x; /* and compute the dot product with (x,y). */ 164848b8605Smrg return ((h & 1) ? -u : u) + ((h & 2) ? -2.0f * v : 2.0f * v); 165848b8605Smrg} 166848b8605Smrg 167848b8605Smrgstatic float 168848b8605Smrggrad3(int hash, float x, float y, float z) 169848b8605Smrg{ 170848b8605Smrg int h = hash & 15; /* Convert low 4 bits of hash code into 12 simple */ 171848b8605Smrg float u = h < 8 ? x : y; /* gradient directions, and compute dot product. */ 172848b8605Smrg float v = h < 4 ? y : h == 12 || h == 14 ? x : z; /* Fix repeats at h = 12 to 15 */ 173848b8605Smrg return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); 174848b8605Smrg} 175848b8605Smrg 176848b8605Smrgstatic float 177848b8605Smrggrad4(int hash, float x, float y, float z, float t) 178848b8605Smrg{ 179848b8605Smrg int h = hash & 31; /* Convert low 5 bits of hash code into 32 simple */ 180848b8605Smrg float u = h < 24 ? x : y; /* gradient directions, and compute dot product. */ 181848b8605Smrg float v = h < 16 ? y : z; 182848b8605Smrg float w = h < 8 ? z : t; 183848b8605Smrg return ((h & 1) ? -u : u) + ((h & 2) ? -v : v) + ((h & 4) ? -w : w); 184848b8605Smrg} 185848b8605Smrg 186848b8605Smrg/** 187848b8605Smrg * A lookup table to traverse the simplex around a given point in 4D. 188848b8605Smrg * Details can be found where this table is used, in the 4D noise method. 189848b8605Smrg * TODO: This should not be required, backport it from Bill's GLSL code! 190848b8605Smrg */ 191b8e80941Smrgstatic const unsigned char simplex[64][4] = { 192848b8605Smrg {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1}, 193848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0}, 194848b8605Smrg {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1}, 195848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0}, 196848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 197848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 198848b8605Smrg {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0}, 199848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, 200848b8605Smrg {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, 201848b8605Smrg {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0}, 202848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 203848b8605Smrg {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 204848b8605Smrg {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 205848b8605Smrg {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0}, 206848b8605Smrg {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 207848b8605Smrg {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0} 208848b8605Smrg}; 209848b8605Smrg 210848b8605Smrg 211848b8605Smrg/** 1D simplex noise */ 212848b8605SmrgGLfloat 213848b8605Smrg_mesa_noise1(GLfloat x) 214848b8605Smrg{ 215848b8605Smrg int i0 = FASTFLOOR(x); 216848b8605Smrg int i1 = i0 + 1; 217848b8605Smrg float x0 = x - i0; 218848b8605Smrg float x1 = x0 - 1.0f; 219848b8605Smrg float t1 = 1.0f - x1 * x1; 220848b8605Smrg float n0, n1; 221848b8605Smrg 222848b8605Smrg float t0 = 1.0f - x0 * x0; 223848b8605Smrg/* if(t0 < 0.0f) t0 = 0.0f; // this never happens for the 1D case */ 224848b8605Smrg t0 *= t0; 225848b8605Smrg n0 = t0 * t0 * grad1(perm[i0 & 0xff], x0); 226848b8605Smrg 227848b8605Smrg/* if(t1 < 0.0f) t1 = 0.0f; // this never happens for the 1D case */ 228848b8605Smrg t1 *= t1; 229848b8605Smrg n1 = t1 * t1 * grad1(perm[i1 & 0xff], x1); 230848b8605Smrg /* The maximum value of this noise is 8*(3/4)^4 = 2.53125 */ 231848b8605Smrg /* A factor of 0.395 would scale to fit exactly within [-1,1], but */ 232848b8605Smrg /* we want to match PRMan's 1D noise, so we scale it down some more. */ 233848b8605Smrg return 0.25f * (n0 + n1); 234848b8605Smrg} 235848b8605Smrg 236848b8605Smrg 237848b8605Smrg/** 2D simplex noise */ 238848b8605SmrgGLfloat 239848b8605Smrg_mesa_noise2(GLfloat x, GLfloat y) 240848b8605Smrg{ 241848b8605Smrg#define F2 0.366025403f /* F2 = 0.5*(sqrt(3.0)-1.0) */ 242848b8605Smrg#define G2 0.211324865f /* G2 = (3.0-Math.sqrt(3.0))/6.0 */ 243848b8605Smrg 244848b8605Smrg float n0, n1, n2; /* Noise contributions from the three corners */ 245848b8605Smrg 246848b8605Smrg /* Skew the input space to determine which simplex cell we're in */ 247848b8605Smrg float s = (x + y) * F2; /* Hairy factor for 2D */ 248848b8605Smrg float xs = x + s; 249848b8605Smrg float ys = y + s; 250848b8605Smrg int i = FASTFLOOR(xs); 251848b8605Smrg int j = FASTFLOOR(ys); 252848b8605Smrg 253848b8605Smrg float t = (float) (i + j) * G2; 254848b8605Smrg float X0 = i - t; /* Unskew the cell origin back to (x,y) space */ 255848b8605Smrg float Y0 = j - t; 256848b8605Smrg float x0 = x - X0; /* The x,y distances from the cell origin */ 257848b8605Smrg float y0 = y - Y0; 258848b8605Smrg 259848b8605Smrg float x1, y1, x2, y2; 260848b8605Smrg unsigned int ii, jj; 261848b8605Smrg float t0, t1, t2; 262848b8605Smrg 263848b8605Smrg /* For the 2D case, the simplex shape is an equilateral triangle. */ 264848b8605Smrg /* Determine which simplex we are in. */ 265848b8605Smrg unsigned int i1, j1; /* Offsets for second (middle) corner of simplex in (i,j) coords */ 266848b8605Smrg if (x0 > y0) { 267848b8605Smrg i1 = 1; 268848b8605Smrg j1 = 0; 269848b8605Smrg } /* lower triangle, XY order: (0,0)->(1,0)->(1,1) */ 270848b8605Smrg else { 271848b8605Smrg i1 = 0; 272848b8605Smrg j1 = 1; 273848b8605Smrg } /* upper triangle, YX order: (0,0)->(0,1)->(1,1) */ 274848b8605Smrg 275848b8605Smrg /* A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and */ 276848b8605Smrg /* a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where */ 277848b8605Smrg /* c = (3-sqrt(3))/6 */ 278848b8605Smrg 279848b8605Smrg x1 = x0 - i1 + G2; /* Offsets for middle corner in (x,y) unskewed coords */ 280848b8605Smrg y1 = y0 - j1 + G2; 281848b8605Smrg x2 = x0 - 1.0f + 2.0f * G2; /* Offsets for last corner in (x,y) unskewed coords */ 282848b8605Smrg y2 = y0 - 1.0f + 2.0f * G2; 283848b8605Smrg 284848b8605Smrg /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */ 285848b8605Smrg ii = i & 0xff; 286848b8605Smrg jj = j & 0xff; 287848b8605Smrg 288848b8605Smrg /* Calculate the contribution from the three corners */ 289848b8605Smrg t0 = 0.5f - x0 * x0 - y0 * y0; 290848b8605Smrg if (t0 < 0.0f) 291848b8605Smrg n0 = 0.0f; 292848b8605Smrg else { 293848b8605Smrg t0 *= t0; 294848b8605Smrg n0 = t0 * t0 * grad2(perm[ii + perm[jj]], x0, y0); 295848b8605Smrg } 296848b8605Smrg 297848b8605Smrg t1 = 0.5f - x1 * x1 - y1 * y1; 298848b8605Smrg if (t1 < 0.0f) 299848b8605Smrg n1 = 0.0f; 300848b8605Smrg else { 301848b8605Smrg t1 *= t1; 302848b8605Smrg n1 = t1 * t1 * grad2(perm[ii + i1 + perm[jj + j1]], x1, y1); 303848b8605Smrg } 304848b8605Smrg 305848b8605Smrg t2 = 0.5f - x2 * x2 - y2 * y2; 306848b8605Smrg if (t2 < 0.0f) 307848b8605Smrg n2 = 0.0f; 308848b8605Smrg else { 309848b8605Smrg t2 *= t2; 310848b8605Smrg n2 = t2 * t2 * grad2(perm[ii + 1 + perm[jj + 1]], x2, y2); 311848b8605Smrg } 312848b8605Smrg 313848b8605Smrg /* Add contributions from each corner to get the final noise value. */ 314848b8605Smrg /* The result is scaled to return values in the interval [-1,1]. */ 315848b8605Smrg return 40.0f * (n0 + n1 + n2); /* TODO: The scale factor is preliminary! */ 316848b8605Smrg} 317848b8605Smrg 318848b8605Smrg 319848b8605Smrg/** 3D simplex noise */ 320848b8605SmrgGLfloat 321848b8605Smrg_mesa_noise3(GLfloat x, GLfloat y, GLfloat z) 322848b8605Smrg{ 323848b8605Smrg/* Simple skewing factors for the 3D case */ 324848b8605Smrg#define F3 0.333333333f 325848b8605Smrg#define G3 0.166666667f 326848b8605Smrg 327848b8605Smrg float n0, n1, n2, n3; /* Noise contributions from the four corners */ 328848b8605Smrg 329848b8605Smrg /* Skew the input space to determine which simplex cell we're in */ 330848b8605Smrg float s = (x + y + z) * F3; /* Very nice and simple skew factor for 3D */ 331848b8605Smrg float xs = x + s; 332848b8605Smrg float ys = y + s; 333848b8605Smrg float zs = z + s; 334848b8605Smrg int i = FASTFLOOR(xs); 335848b8605Smrg int j = FASTFLOOR(ys); 336848b8605Smrg int k = FASTFLOOR(zs); 337848b8605Smrg 338848b8605Smrg float t = (float) (i + j + k) * G3; 339848b8605Smrg float X0 = i - t; /* Unskew the cell origin back to (x,y,z) space */ 340848b8605Smrg float Y0 = j - t; 341848b8605Smrg float Z0 = k - t; 342848b8605Smrg float x0 = x - X0; /* The x,y,z distances from the cell origin */ 343848b8605Smrg float y0 = y - Y0; 344848b8605Smrg float z0 = z - Z0; 345848b8605Smrg 346848b8605Smrg float x1, y1, z1, x2, y2, z2, x3, y3, z3; 347848b8605Smrg unsigned int ii, jj, kk; 348848b8605Smrg float t0, t1, t2, t3; 349848b8605Smrg 350848b8605Smrg /* For the 3D case, the simplex shape is a slightly irregular tetrahedron. */ 351848b8605Smrg /* Determine which simplex we are in. */ 352848b8605Smrg unsigned int i1, j1, k1; /* Offsets for second corner of simplex in (i,j,k) coords */ 353848b8605Smrg unsigned int i2, j2, k2; /* Offsets for third corner of simplex in (i,j,k) coords */ 354848b8605Smrg 355848b8605Smrg/* This code would benefit from a backport from the GLSL version! */ 356848b8605Smrg if (x0 >= y0) { 357848b8605Smrg if (y0 >= z0) { 358848b8605Smrg i1 = 1; 359848b8605Smrg j1 = 0; 360848b8605Smrg k1 = 0; 361848b8605Smrg i2 = 1; 362848b8605Smrg j2 = 1; 363848b8605Smrg k2 = 0; 364848b8605Smrg } /* X Y Z order */ 365848b8605Smrg else if (x0 >= z0) { 366848b8605Smrg i1 = 1; 367848b8605Smrg j1 = 0; 368848b8605Smrg k1 = 0; 369848b8605Smrg i2 = 1; 370848b8605Smrg j2 = 0; 371848b8605Smrg k2 = 1; 372848b8605Smrg } /* X Z Y order */ 373848b8605Smrg else { 374848b8605Smrg i1 = 0; 375848b8605Smrg j1 = 0; 376848b8605Smrg k1 = 1; 377848b8605Smrg i2 = 1; 378848b8605Smrg j2 = 0; 379848b8605Smrg k2 = 1; 380848b8605Smrg } /* Z X Y order */ 381848b8605Smrg } 382848b8605Smrg else { /* x0<y0 */ 383848b8605Smrg if (y0 < z0) { 384848b8605Smrg i1 = 0; 385848b8605Smrg j1 = 0; 386848b8605Smrg k1 = 1; 387848b8605Smrg i2 = 0; 388848b8605Smrg j2 = 1; 389848b8605Smrg k2 = 1; 390848b8605Smrg } /* Z Y X order */ 391848b8605Smrg else if (x0 < z0) { 392848b8605Smrg i1 = 0; 393848b8605Smrg j1 = 1; 394848b8605Smrg k1 = 0; 395848b8605Smrg i2 = 0; 396848b8605Smrg j2 = 1; 397848b8605Smrg k2 = 1; 398848b8605Smrg } /* Y Z X order */ 399848b8605Smrg else { 400848b8605Smrg i1 = 0; 401848b8605Smrg j1 = 1; 402848b8605Smrg k1 = 0; 403848b8605Smrg i2 = 1; 404848b8605Smrg j2 = 1; 405848b8605Smrg k2 = 0; 406848b8605Smrg } /* Y X Z order */ 407848b8605Smrg } 408848b8605Smrg 409848b8605Smrg /* A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in 410848b8605Smrg * (x,y,z), a step of (0,1,0) in (i,j,k) means a step of 411848b8605Smrg * (-c,1-c,-c) in (x,y,z), and a step of (0,0,1) in (i,j,k) means a 412848b8605Smrg * step of (-c,-c,1-c) in (x,y,z), where c = 1/6. 413848b8605Smrg */ 414848b8605Smrg 415848b8605Smrg x1 = x0 - i1 + G3; /* Offsets for second corner in (x,y,z) coords */ 416848b8605Smrg y1 = y0 - j1 + G3; 417848b8605Smrg z1 = z0 - k1 + G3; 418848b8605Smrg x2 = x0 - i2 + 2.0f * G3; /* Offsets for third corner in (x,y,z) coords */ 419848b8605Smrg y2 = y0 - j2 + 2.0f * G3; 420848b8605Smrg z2 = z0 - k2 + 2.0f * G3; 421848b8605Smrg x3 = x0 - 1.0f + 3.0f * G3;/* Offsets for last corner in (x,y,z) coords */ 422848b8605Smrg y3 = y0 - 1.0f + 3.0f * G3; 423848b8605Smrg z3 = z0 - 1.0f + 3.0f * G3; 424848b8605Smrg 425848b8605Smrg /* Wrap the integer indices at 256 to avoid indexing perm[] out of bounds */ 426848b8605Smrg ii = i & 0xff; 427848b8605Smrg jj = j & 0xff; 428848b8605Smrg kk = k & 0xff; 429848b8605Smrg 430848b8605Smrg /* Calculate the contribution from the four corners */ 431848b8605Smrg t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0; 432848b8605Smrg if (t0 < 0.0f) 433848b8605Smrg n0 = 0.0f; 434848b8605Smrg else { 435848b8605Smrg t0 *= t0; 436848b8605Smrg n0 = t0 * t0 * grad3(perm[ii + perm[jj + perm[kk]]], x0, y0, z0); 437848b8605Smrg } 438848b8605Smrg 439848b8605Smrg t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1; 440848b8605Smrg if (t1 < 0.0f) 441848b8605Smrg n1 = 0.0f; 442848b8605Smrg else { 443848b8605Smrg t1 *= t1; 444848b8605Smrg n1 = 445848b8605Smrg t1 * t1 * grad3(perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], x1, 446848b8605Smrg y1, z1); 447848b8605Smrg } 448848b8605Smrg 449848b8605Smrg t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2; 450848b8605Smrg if (t2 < 0.0f) 451848b8605Smrg n2 = 0.0f; 452848b8605Smrg else { 453848b8605Smrg t2 *= t2; 454848b8605Smrg n2 = 455848b8605Smrg t2 * t2 * grad3(perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], x2, 456848b8605Smrg y2, z2); 457848b8605Smrg } 458848b8605Smrg 459848b8605Smrg t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3; 460848b8605Smrg if (t3 < 0.0f) 461848b8605Smrg n3 = 0.0f; 462848b8605Smrg else { 463848b8605Smrg t3 *= t3; 464848b8605Smrg n3 = 465848b8605Smrg t3 * t3 * grad3(perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], x3, y3, 466848b8605Smrg z3); 467848b8605Smrg } 468848b8605Smrg 469848b8605Smrg /* Add contributions from each corner to get the final noise value. 470848b8605Smrg * The result is scaled to stay just inside [-1,1] 471848b8605Smrg */ 472848b8605Smrg return 32.0f * (n0 + n1 + n2 + n3); /* TODO: The scale factor is preliminary! */ 473848b8605Smrg} 474848b8605Smrg 475848b8605Smrg 476848b8605Smrg/** 4D simplex noise */ 477848b8605SmrgGLfloat 478848b8605Smrg_mesa_noise4(GLfloat x, GLfloat y, GLfloat z, GLfloat w) 479848b8605Smrg{ 480848b8605Smrg /* The skewing and unskewing factors are hairy again for the 4D case */ 481848b8605Smrg#define F4 0.309016994f /* F4 = (Math.sqrt(5.0)-1.0)/4.0 */ 482848b8605Smrg#define G4 0.138196601f /* G4 = (5.0-Math.sqrt(5.0))/20.0 */ 483848b8605Smrg 484848b8605Smrg float n0, n1, n2, n3, n4; /* Noise contributions from the five corners */ 485848b8605Smrg 486848b8605Smrg /* Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in */ 487848b8605Smrg float s = (x + y + z + w) * F4; /* Factor for 4D skewing */ 488848b8605Smrg float xs = x + s; 489848b8605Smrg float ys = y + s; 490848b8605Smrg float zs = z + s; 491848b8605Smrg float ws = w + s; 492848b8605Smrg int i = FASTFLOOR(xs); 493848b8605Smrg int j = FASTFLOOR(ys); 494848b8605Smrg int k = FASTFLOOR(zs); 495848b8605Smrg int l = FASTFLOOR(ws); 496848b8605Smrg 497848b8605Smrg float t = (i + j + k + l) * G4; /* Factor for 4D unskewing */ 498848b8605Smrg float X0 = i - t; /* Unskew the cell origin back to (x,y,z,w) space */ 499848b8605Smrg float Y0 = j - t; 500848b8605Smrg float Z0 = k - t; 501848b8605Smrg float W0 = l - t; 502848b8605Smrg 503848b8605Smrg float x0 = x - X0; /* The x,y,z,w distances from the cell origin */ 504848b8605Smrg float y0 = y - Y0; 505848b8605Smrg float z0 = z - Z0; 506848b8605Smrg float w0 = w - W0; 507848b8605Smrg 508848b8605Smrg /* For the 4D case, the simplex is a 4D shape I won't even try to describe. 509848b8605Smrg * To find out which of the 24 possible simplices we're in, we need to 510848b8605Smrg * determine the magnitude ordering of x0, y0, z0 and w0. 511848b8605Smrg * The method below is a good way of finding the ordering of x,y,z,w and 512848b8605Smrg * then find the correct traversal order for the simplex we're in. 513848b8605Smrg * First, six pair-wise comparisons are performed between each possible pair 514848b8605Smrg * of the four coordinates, and the results are used to add up binary bits 515848b8605Smrg * for an integer index. 516848b8605Smrg */ 517848b8605Smrg int c1 = (x0 > y0) ? 32 : 0; 518848b8605Smrg int c2 = (x0 > z0) ? 16 : 0; 519848b8605Smrg int c3 = (y0 > z0) ? 8 : 0; 520848b8605Smrg int c4 = (x0 > w0) ? 4 : 0; 521848b8605Smrg int c5 = (y0 > w0) ? 2 : 0; 522848b8605Smrg int c6 = (z0 > w0) ? 1 : 0; 523848b8605Smrg int c = c1 + c2 + c3 + c4 + c5 + c6; 524848b8605Smrg 525848b8605Smrg unsigned int i1, j1, k1, l1; /* The integer offsets for the second simplex corner */ 526848b8605Smrg unsigned int i2, j2, k2, l2; /* The integer offsets for the third simplex corner */ 527848b8605Smrg unsigned int i3, j3, k3, l3; /* The integer offsets for the fourth simplex corner */ 528848b8605Smrg 529848b8605Smrg float x1, y1, z1, w1, x2, y2, z2, w2, x3, y3, z3, w3, x4, y4, z4, w4; 530848b8605Smrg unsigned int ii, jj, kk, ll; 531848b8605Smrg float t0, t1, t2, t3, t4; 532848b8605Smrg 533848b8605Smrg /* 534848b8605Smrg * simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some 535848b8605Smrg * order. Many values of c will never occur, since e.g. x>y>z>w 536848b8605Smrg * makes x<z, y<w and x<w impossible. Only the 24 indices which 537848b8605Smrg * have non-zero entries make any sense. We use a thresholding to 538848b8605Smrg * set the coordinates in turn from the largest magnitude. The 539848b8605Smrg * number 3 in the "simplex" array is at the position of the 540848b8605Smrg * largest coordinate. 541848b8605Smrg */ 542848b8605Smrg i1 = simplex[c][0] >= 3 ? 1 : 0; 543848b8605Smrg j1 = simplex[c][1] >= 3 ? 1 : 0; 544848b8605Smrg k1 = simplex[c][2] >= 3 ? 1 : 0; 545848b8605Smrg l1 = simplex[c][3] >= 3 ? 1 : 0; 546848b8605Smrg /* The number 2 in the "simplex" array is at the second largest coordinate. */ 547848b8605Smrg i2 = simplex[c][0] >= 2 ? 1 : 0; 548848b8605Smrg j2 = simplex[c][1] >= 2 ? 1 : 0; 549848b8605Smrg k2 = simplex[c][2] >= 2 ? 1 : 0; 550848b8605Smrg l2 = simplex[c][3] >= 2 ? 1 : 0; 551848b8605Smrg /* The number 1 in the "simplex" array is at the second smallest coordinate. */ 552848b8605Smrg i3 = simplex[c][0] >= 1 ? 1 : 0; 553848b8605Smrg j3 = simplex[c][1] >= 1 ? 1 : 0; 554848b8605Smrg k3 = simplex[c][2] >= 1 ? 1 : 0; 555848b8605Smrg l3 = simplex[c][3] >= 1 ? 1 : 0; 556848b8605Smrg /* The fifth corner has all coordinate offsets = 1, so no need to look that up. */ 557848b8605Smrg 558848b8605Smrg x1 = x0 - i1 + G4; /* Offsets for second corner in (x,y,z,w) coords */ 559848b8605Smrg y1 = y0 - j1 + G4; 560848b8605Smrg z1 = z0 - k1 + G4; 561848b8605Smrg w1 = w0 - l1 + G4; 562848b8605Smrg x2 = x0 - i2 + 2.0f * G4; /* Offsets for third corner in (x,y,z,w) coords */ 563848b8605Smrg y2 = y0 - j2 + 2.0f * G4; 564848b8605Smrg z2 = z0 - k2 + 2.0f * G4; 565848b8605Smrg w2 = w0 - l2 + 2.0f * G4; 566848b8605Smrg x3 = x0 - i3 + 3.0f * G4; /* Offsets for fourth corner in (x,y,z,w) coords */ 567848b8605Smrg y3 = y0 - j3 + 3.0f * G4; 568848b8605Smrg z3 = z0 - k3 + 3.0f * G4; 569848b8605Smrg w3 = w0 - l3 + 3.0f * G4; 570848b8605Smrg x4 = x0 - 1.0f + 4.0f * G4; /* Offsets for last corner in (x,y,z,w) coords */ 571848b8605Smrg y4 = y0 - 1.0f + 4.0f * G4; 572848b8605Smrg z4 = z0 - 1.0f + 4.0f * G4; 573848b8605Smrg w4 = w0 - 1.0f + 4.0f * G4; 574848b8605Smrg 575848b8605Smrg /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */ 576848b8605Smrg ii = i & 0xff; 577848b8605Smrg jj = j & 0xff; 578848b8605Smrg kk = k & 0xff; 579848b8605Smrg ll = l & 0xff; 580848b8605Smrg 581848b8605Smrg /* Calculate the contribution from the five corners */ 582848b8605Smrg t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0; 583848b8605Smrg if (t0 < 0.0f) 584848b8605Smrg n0 = 0.0f; 585848b8605Smrg else { 586848b8605Smrg t0 *= t0; 587848b8605Smrg n0 = 588848b8605Smrg t0 * t0 * grad4(perm[ii + perm[jj + perm[kk + perm[ll]]]], x0, y0, 589848b8605Smrg z0, w0); 590848b8605Smrg } 591848b8605Smrg 592848b8605Smrg t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1; 593848b8605Smrg if (t1 < 0.0f) 594848b8605Smrg n1 = 0.0f; 595848b8605Smrg else { 596848b8605Smrg t1 *= t1; 597848b8605Smrg n1 = 598848b8605Smrg t1 * t1 * 599848b8605Smrg grad4(perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]], 600848b8605Smrg x1, y1, z1, w1); 601848b8605Smrg } 602848b8605Smrg 603848b8605Smrg t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2; 604848b8605Smrg if (t2 < 0.0f) 605848b8605Smrg n2 = 0.0f; 606848b8605Smrg else { 607848b8605Smrg t2 *= t2; 608848b8605Smrg n2 = 609848b8605Smrg t2 * t2 * 610848b8605Smrg grad4(perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]], 611848b8605Smrg x2, y2, z2, w2); 612848b8605Smrg } 613848b8605Smrg 614848b8605Smrg t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3; 615848b8605Smrg if (t3 < 0.0f) 616848b8605Smrg n3 = 0.0f; 617848b8605Smrg else { 618848b8605Smrg t3 *= t3; 619848b8605Smrg n3 = 620848b8605Smrg t3 * t3 * 621848b8605Smrg grad4(perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]], 622848b8605Smrg x3, y3, z3, w3); 623848b8605Smrg } 624848b8605Smrg 625848b8605Smrg t4 = 0.6f - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4; 626848b8605Smrg if (t4 < 0.0f) 627848b8605Smrg n4 = 0.0f; 628848b8605Smrg else { 629848b8605Smrg t4 *= t4; 630848b8605Smrg n4 = 631848b8605Smrg t4 * t4 * 632848b8605Smrg grad4(perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]], x4, 633848b8605Smrg y4, z4, w4); 634848b8605Smrg } 635848b8605Smrg 636848b8605Smrg /* Sum up and scale the result to cover the range [-1,1] */ 637848b8605Smrg return 27.0f * (n0 + n1 + n2 + n3 + n4); /* TODO: The scale factor is preliminary! */ 638848b8605Smrg} 639