1 /*
2   Copyright (c) 2010-2011, Intel Corporation
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8 
9     * Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11 
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15 
16     * Neither the name of Intel Corporation nor the names of its
17       contributors may be used to endorse or promote products derived from
18       this software without specific prior written permission.
19 
20 
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #include <math.h>
35 
36 #define NOISE_PERM_SIZE 256
37 
38 static int NoisePerm[2 * NOISE_PERM_SIZE] = {
39     151, 160, 137, 91,  90,  15,  131, 13,  201, 95,  96,  53,  194, 233, 7,   225, 140, 36,  103, 30,  69,  142, 8,
40     99,  37,  240, 21,  10,  23,  190, 6,   148, 247, 120, 234, 75,  0,   26,  197, 62,  94,  252, 219, 203, 117, 35,
41     11,  32,  57,  177, 33,  88,  237, 149, 56,  87,  174, 20,  125, 136, 171, 168, 68,  175, 74,  165, 71,  134, 139,
42     48,  27,  166, 77,  146, 158, 231, 83,  111, 229, 122, 60,  211, 133, 230, 220, 105, 92,  41,  55,  46,  245, 40,
43     244, 102, 143, 54,  65,  25,  63,  161, 1,   216, 80,  73,  209, 76,  132, 187, 208, 89,  18,  169, 200, 196, 135,
44     130, 116, 188, 159, 86,  164, 100, 109, 198, 173, 186, 3,   64,  52,  217, 226, 250, 124, 123, 5,   202, 38,  147,
45     118, 126, 255, 82,  85,  212, 207, 206, 59,  227, 47,  16,  58,  17,  182, 189, 28,  42,  223, 183, 170, 213, 119,
46     248, 152, 2,   44,  154, 163, 70,  221, 153, 101, 155, 167, 43,  172, 9,   129, 22,  39,  253, 19,  98,  108, 110,
47     79,  113, 224, 232, 178, 185, 112, 104, 218, 246, 97,  228, 251, 34,  242, 193, 238, 210, 144, 12,  191, 179, 162,
48     241, 81,  51,  145, 235, 249, 14,  239, 107, 49,  192, 214, 31,  181, 199, 106, 157, 184, 84,  204, 176, 115, 121,
49     50,  45,  127, 4,   150, 254, 138, 236, 205, 93,  222, 114, 67,  29,  24,  72,  243, 141, 128, 195, 78,  66,  215,
50     61,  156, 180, 151, 160, 137, 91,  90,  15,  131, 13,  201, 95,  96,  53,  194, 233, 7,   225, 140, 36,  103, 30,
51     69,  142, 8,   99,  37,  240, 21,  10,  23,  190, 6,   148, 247, 120, 234, 75,  0,   26,  197, 62,  94,  252, 219,
52     203, 117, 35,  11,  32,  57,  177, 33,  88,  237, 149, 56,  87,  174, 20,  125, 136, 171, 168, 68,  175, 74,  165,
53     71,  134, 139, 48,  27,  166, 77,  146, 158, 231, 83,  111, 229, 122, 60,  211, 133, 230, 220, 105, 92,  41,  55,
54     46,  245, 40,  244, 102, 143, 54,  65,  25,  63,  161, 1,   216, 80,  73,  209, 76,  132, 187, 208, 89,  18,  169,
55     200, 196, 135, 130, 116, 188, 159, 86,  164, 100, 109, 198, 173, 186, 3,   64,  52,  217, 226, 250, 124, 123, 5,
56     202, 38,  147, 118, 126, 255, 82,  85,  212, 207, 206, 59,  227, 47,  16,  58,  17,  182, 189, 28,  42,  223, 183,
57     170, 213, 119, 248, 152, 2,   44,  154, 163, 70,  221, 153, 101, 155, 167, 43,  172, 9,   129, 22,  39,  253, 19,
58     98,  108, 110, 79,  113, 224, 232, 178, 185, 112, 104, 218, 246, 97,  228, 251, 34,  242, 193, 238, 210, 144, 12,
59     191, 179, 162, 241, 81,  51,  145, 235, 249, 14,  239, 107, 49,  192, 214, 31,  181, 199, 106, 157, 184, 84,  204,
60     176, 115, 121, 50,  45,  127, 4,   150, 254, 138, 236, 205, 93,  222, 114, 67,  29,  24,  72,  243, 141, 128, 195,
61     78,  66,  215, 61,  156, 180};
62 
Clamp(float v,float low,float high)63 inline float Clamp(float v, float low, float high) { return v < low ? low : ((v > high) ? high : v); }
64 
SmoothStep(float low,float high,float value)65 inline float SmoothStep(float low, float high, float value) {
66     float v = Clamp((value - low) / (high - low), 0.f, 1.f);
67     return v * v * (-2.f * v + 3.f);
68 }
69 
Floor2Int(float val)70 inline int Floor2Int(float val) { return (int)floorf(val); }
71 
Grad(int x,int y,int z,float dx,float dy,float dz)72 inline float Grad(int x, int y, int z, float dx, float dy, float dz) {
73     int h = NoisePerm[NoisePerm[NoisePerm[x] + y] + z];
74     h &= 15;
75     float u = h < 8 || h == 12 || h == 13 ? dx : dy;
76     float v = h < 4 || h == 12 || h == 13 ? dy : dz;
77     return ((h & 1) ? -u : u) + ((h & 2) ? -v : v);
78 }
79 
NoiseWeight(float t)80 inline float NoiseWeight(float t) {
81     float t3 = t * t * t;
82     float t4 = t3 * t;
83     return 6.f * t4 * t - 15.f * t4 + 10.f * t3;
84 }
85 
Lerp(float t,float low,float high)86 inline float Lerp(float t, float low, float high) { return (1.f - t) * low + t * high; }
87 
Noise(float x,float y,float z)88 static float Noise(float x, float y, float z) {
89     // Compute noise cell coordinates and offsets
90     int ix = Floor2Int(x), iy = Floor2Int(y), iz = Floor2Int(z);
91     float dx = x - ix, dy = y - iy, dz = z - iz;
92 
93     // Compute gradient weights
94     ix &= (NOISE_PERM_SIZE - 1);
95     iy &= (NOISE_PERM_SIZE - 1);
96     iz &= (NOISE_PERM_SIZE - 1);
97     float w000 = Grad(ix, iy, iz, dx, dy, dz);
98     float w100 = Grad(ix + 1, iy, iz, dx - 1, dy, dz);
99     float w010 = Grad(ix, iy + 1, iz, dx, dy - 1, dz);
100     float w110 = Grad(ix + 1, iy + 1, iz, dx - 1, dy - 1, dz);
101     float w001 = Grad(ix, iy, iz + 1, dx, dy, dz - 1);
102     float w101 = Grad(ix + 1, iy, iz + 1, dx - 1, dy, dz - 1);
103     float w011 = Grad(ix, iy + 1, iz + 1, dx, dy - 1, dz - 1);
104     float w111 = Grad(ix + 1, iy + 1, iz + 1, dx - 1, dy - 1, dz - 1);
105 
106     // Compute trilinear interpolation of weights
107     float wx = NoiseWeight(dx), wy = NoiseWeight(dy), wz = NoiseWeight(dz);
108     float x00 = Lerp(wx, w000, w100);
109     float x10 = Lerp(wx, w010, w110);
110     float x01 = Lerp(wx, w001, w101);
111     float x11 = Lerp(wx, w011, w111);
112     float y0 = Lerp(wy, x00, x10);
113     float y1 = Lerp(wy, x01, x11);
114     return Lerp(wz, y0, y1);
115 }
116 
Turbulence(float x,float y,float z,int octaves)117 static float Turbulence(float x, float y, float z, int octaves) {
118     float omega = 0.6;
119 
120     float sum = 0., lambda = 1., o = 1.;
121     for (int i = 0; i < octaves; ++i) {
122         sum += fabsf(o * Noise(lambda * x, lambda * y, lambda * z));
123         lambda *= 1.99f;
124         o *= omega;
125     }
126     return sum * 0.5f;
127 }
128 
noise_serial(float x0,float y0,float x1,float y1,int width,int height,float output[])129 void noise_serial(float x0, float y0, float x1, float y1, int width, int height, float output[]) {
130     float dx = (x1 - x0) / width;
131     float dy = (y1 - y0) / height;
132 
133     for (int j = 0; j < height; j++) {
134         for (int i = 0; i < width; ++i) {
135             float x = x0 + i * dx;
136             float y = y0 + j * dy;
137 
138             int index = (j * width + i);
139             output[index] = Turbulence(x, y, 0.6f, 8);
140         }
141     }
142 }
143