This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/simplexnoise.h

172 lines
6.9 KiB
C
Raw Normal View History

2013-04-11 18:44:00 +00:00
/* Copyright (c) 2007-2012 Eliot Eshelman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SIMPLEX_H_
#define SIMPLEX_H_
#include <stdint.h>
/* 2D, 3D and 4D Simplex Noise functions return 'random' values in (-1, 1).
This algorithm was originally designed by Ken Perlin, but my code has been
adapted from the implementation written by Stefan Gustavson (stegu@itn.liu.se)
Raw Simplex noise functions return the value generated by Ken's algorithm.
Scaled Raw Simplex noise functions adjust the range of values returned from the
traditional (-1, 1) to whichever bounds are passed to the function.
Multi-Octave Simplex noise functions compine multiple noise values to create a
more complex result. Each successive layer of noise is adjusted and scaled.
Scaled Multi-Octave Simplex noise functions scale the values returned from the
traditional (-1,1) range to whichever range is passed to the function.
In many cases, you may think you only need a 1D noise function, but in practice
2D is almost always better. For instance, if you're using the current frame
number as the parameter for the noise, all objects will end up with the same
noise value at each frame. By adding a second parameter on the second
dimension, you can ensure that each gets a unique noise value and they don't
all look identical.
*/
//from http://www.6by9.net/simplex-noise-for-c-and-python/
// Multi-octave Simplex noise
// For each octave, a higher frequency/lower amplitude function will be added to the original.
// The higher the persistence [0-1], the more of each succeeding octave will be added.
float octave_noise_2d(const float octaves,
const float persistence,
const float scale,
const float x,
const float y);
float octave_noise_3d(const float octaves,
const float persistence,
const float scale,
const float x,
const float y,
const float z);
float octave_noise_4d(const float octaves,
const float persistence,
const float scale,
const float x,
const float y,
const float z,
const float w);
// Scaled Multi-octave Simplex noise
// The result will be between the two parameters passed.
float scaled_octave_noise_2d( const float octaves,
const float persistence,
const float scale,
const float loBound,
const float hiBound,
const float x,
const float y);
float scaled_octave_noise_3d( const float octaves,
const float persistence,
const float scale,
const float loBound,
const float hiBound,
const float x,
const float y,
const float z);
float scaled_octave_noise_4d( const float octaves,
const float persistence,
const float scale,
const float loBound,
const float hiBound,
const float x,
const float y,
const float z,
const float w);
// Scaled Raw Simplex noise
// The result will be between the two parameters passed.
float scaled_raw_noise_2d( const float loBound,
const float hiBound,
const float x,
const float y);
float scaled_raw_noise_3d( const float loBound,
const float hiBound,
const float x,
const float y,
const float z);
float scaled_raw_noise_4d( const float loBound,
const float hiBound,
const float x,
const float y,
const float z,
const float w);
// Raw Simplex noise - a single noise value.
float raw_noise_2d(const float x, const float y);
float raw_noise_3d(const float x, const float y, const float z);
float raw_noise_4d(const float x, const float y, const float, const float w);
int fastfloor(const float x);
float dot(const int8_t* g, const float x, const float y);
float dot(const int8_t* g, const float x, const float y, const float z);
float dot(const int8_t* g, const float x, const float y, const float z, const float w);
// The gradients are the midpoints of the vertices of a cube.
static const int8_t grad3[12][3] = {
{1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0},
{1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1},
{0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1}
};
// The gradients are the midpoints of the vertices of a hypercube.
static const int8_t grad4[32][4]= {
{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
{0,-1,1,1}, {0,-1,1,-1}, {0,-1,-1,1}, {0,-1,-1,-1},
{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
{-1,0,1,1}, {-1,0,1,-1}, {-1,0,-1,1}, {-1,0,-1,-1},
{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
{-1,1,0,1}, {-1,1,0,-1}, {-1,-1,0,1}, {-1,-1,0,-1},
{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
{-1,1,1,0}, {-1,1,-1,0}, {-1,-1,1,0}, {-1,-1,-1,0}
};
// Permutation table. The same list is repeated twice.
// removed const
extern uint8_t perm[512];
// A lookup table to traverse the simplex around a given point in 4D.
static const uint8_t simplex[64][4] = {
{0,1,2,3},{0,1,3,2},{0,0,0,0},{0,2,3,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,2,3,0},
{0,2,1,3},{0,0,0,0},{0,3,1,2},{0,3,2,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,3,2,0},
{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
{1,2,0,3},{0,0,0,0},{1,3,0,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,3,0,1},{2,3,1,0},
{1,0,2,3},{1,0,3,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,0,3,1},{0,0,0,0},{2,1,3,0},
{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
{2,0,1,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0},
{2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0}
};
#endif /*SIMPLEX_H_*/