Giter Site home page Giter Site logo

perlinnoise's Introduction

siv::PerlinNoise

noise

siv::PerlinNoise is a header-only Perlin noise library for modern C++ (C++17/20).
The implementation is based on Ken Perlin's Improved Noise.

Features

  • 1D / 2D / 3D noise
  • octave noise
  • initial seed
  • (✨ new in v3.0) produce the same output on any platform (except for floating point errors)

License

siv::PerlinNoise is distributed under the MIT license.

Usage

# include <iostream>
# include "PerlinNoise.hpp"

int main()
{
	const siv::PerlinNoise::seed_type seed = 123456u;

	const siv::PerlinNoise perlin{ seed };
	
	for (int y = 0; y < 5; ++y)
	{
		for (int x = 0; x < 5; ++x)
		{
			const double noise = perlin.octave2D_01((x * 0.01), (y * 0.01), 4);
			
			std::cout << noise << '\t';
		}

		std::cout << '\n';
	}
}

API

template <class Float> class BasicPerlinNoise

  • Typedefs
    • using PerlinNoise = BasicPerlinNoise<double>;
    • using state_type = std::array<std::uint8_t, 256>;
    • using value_type = Float;
    • using default_random_engine = std::mt19937;
    • using seed_type = typename default_random_engine::result_type;
  • Constructors
    • constexpr BasicPerlinNoise();
    • BasicPerlinNoise(seed_type seed);
    • BasicPerlinNoise(URBG&& urbg);
  • Reseed
    • void reseed(seed_type seed);
    • void reseed(URBG&& urbg);
  • Serialization
    • constexpr const state_type& serialize() const noexcept;
    • constexpr void deserialize(const state_type& state) noexcept;
  • Noise (The result is in the range [-1, 1])
    • value_type noise1D(value_type x) const noexcept;
    • value_type noise2D(value_type x, value_type y) const noexcept;
    • value_type noise3D(value_type x, value_type y, value_type z) const noexcept;
  • Noise (The result is remapped to the range [0, 1])
    • value_type noise1D_01(value_type x) const noexcept;
    • value_type noise2D_01(value_type x, value_type y) const noexcept;
    • value_type noise3D_01(value_type x, value_type y, value_type z) const noexcept;
  • Octave noise (The result can be out of the range [-1, 1])
    • value_type octave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type octave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type octave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
  • Octave noise (The result is clamped to the range [-1, 1])
    • value_type octave1D_11(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type octave2D_11(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type octave3D_11(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
  • Octave noise (The result is clamped and remapped to the range [0, 1])
    • value_type octave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type octave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type octave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
  • Octave noise (The result is normalized to the range [-1, 1])
    • value_type normalizedOctave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type normalizedOctave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type normalizedOctave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
  • Octave noise (The result is normalized and remapped to the range [0, 1])
    • value_type normalizedOctave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type normalizedOctave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;
    • value_type normalizedOctave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept;

Example

Run example.cpp with the following parameters.

frequency = 8.0
octaves = 8
seed = 12345

noise


frequency = 8.0
octaves = 8
seed = 23456

noise


frequency = 8.0
octaves = 3
seed = 23456

noise

perlinnoise's People

Contributors

cmanton avatar reputeless avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

perlinnoise's Issues

Getting errors in the header file when trying to compile

I'm trying to compile the following test program:

#include <cstdio>
#include <gd.h>
#include "PerlinNoise.hpp"

using value_t = siv::PerlinNoise::value_type;

int main(int argc, char *argv[])
{
    siv::PerlinNoise noise;
    gdImagePtr img = gdImageCreateTrueColor(512, 512);

    for (int y=0; y<512; ++y) {
        value_t yf = (value_t)y/512;
        for (int x=0; x<512; ++x) {
            value_t xf = (value_t)x/512;
            value_t z = noise.noise2D(xf, yf) * 255;
            gdImageSetPixel(img, x, y, z);
        }
    }

    std::FILE* fp = fopen("output.png", "wb");
    gdImagePng(img, fp);
    gdFree(img);
    std::fclose(fp);
}

When I compile using g++ -o main main.cpp -lgd, I get:

In file included from /usr/include/c++/5/cstdint:35:0,
                 from PerlinNoise.hpp:29,
                 from main.cpp:3:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
In file included from main.cpp:3:0:
PerlinNoise.hpp:53:9: error: expected nested-name-specifier before ‘value_type’
   using value_type = Float;
         ^
PerlinNoise.hpp:57:8: error: ‘uint8_t’ in namespace ‘std’ does not name a type
   std::uint8_t p[512];
        ^
PerlinNoise.hpp:59:3: error: expected unqualified-id before ‘[’ token
   [[nodiscard]]
   ^
PerlinNoise.hpp:65:3: error: expected unqualified-id before ‘[’ token
   [[nodiscard]]
   ^
PerlinNoise.hpp:71:3: error: expected unqualified-id before ‘[’ token
   [[nodiscard]]
   ^
PerlinNoise.hpp:80:3: error: expected unqualified-id before ‘[’ token
   [[nodiscard]]
   ^
main.cpp:25:1: error: expected ‘}’ at end of input
 }
 ^
main.cpp:25:1: error: expected unqualified-id at end of input
main.cpp:25:1: error: expected ‘}’ at end of input

When I add --std=c++11, I get even more errors, and 14 and 17 don't help either.

Please let me know if you need any more information.

Some times I only get 0's from noise2D when I initialize with std::random_device

Simplified version of the test program I wrote:

  siv::PerlinNoise perlin(std::random_device{});
  
  for (int y = 0 ; y < height ; ++y)
  {
    for (int x = 0 ; x < width ; ++x)
    {
      const double X = static_cast<double>(x) / width;
      const double Y = static_cast<double>(y) / height;
      std::cout << perlin.noise2D(X,Y) << " ";
    }
  }

Most of the times I run the program I get values like I expect, but about 10% of the runs I only get 0, or the occasional -0, as output.

I haven't tried to track down why this is happening, but is this something that is expected from the perlin noise algorithm for certain seeds or something?

Ubuntu 19.10
Linux 5.3.0-51-generic #44-Ubuntu SMP Wed Apr 22 21:09:44 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008

Error in Visual studio 2019

I've been using C++ for a couple of weeks now and wanted to make some world generation, so this seemed to be really great I created a PerlinNoise.hpp and pasted in your code, and no errors except three, all on lines like the one shown below, I'm using Visual Studio 2019 with SFML so I'm just wondering if this is only for G++?
Thanks :D

Screenshot_1

How to make infinite worlds

I noticed that all the functions require normalized coordinates from 0 to 1, but how do I normalize coordinates if I have an infinite world? This is very important and kind of counterintuitive, since Perlin noise is used for infinite world generation a lot...

Persistence and Frequency in Octaves

It seems I can't provide numbers for persistence and frequency? Meaning I'd have to by default, calculate for one octave at a time in order to achieve that. Am I missing something?

ライセンス表記がほしいです!

パーリンノイズのC++11実装を使ってみたいのですが、ライセンスが明記されていないようです。
できれば、記載していただきたいです。

Trouble using this with C++17 source

Hi there!

I am getting back into C++, so have that as a caveat. I haven't programmed in it since the 90s. What do I need to do in order to use this code with mine? I am currently getting this error:

Scanning dependencies of target gen2 [ 33%] Building CXX object CMakeFiles/gen2.dir/src/main.cpp.o In file included from /usr/include/c++/7/algorithm:62:0, from /home/psotos/Documents/workspace/gen2/src/main.cpp:4: /usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘void std::shuffle(_RAIter, _RAIter, _UGenerator&&) [with _RAIter = unsigned char*; _UGenerator = int&]’: /home/<myusername>/Documents/workspace/gen2/src/./PerlinNoise/PerlinNoise.hpp:96:16: required from ‘void siv::PerlinNoise::reseed(URNG&) [with URNG = int]’ /home/<myusername>/Documents/workspace/gen2/src/./PerlinNoise/PerlinNoise.hpp:70:4: required from ‘siv::PerlinNoise::PerlinNoise(URNG&) [with URNG = int]’ /home/<myusername>/Documents/workspace/gen2/src/main.cpp:25:36: required from here /usr/include/c++/7/bits/stl_algo.h:3813:2: error: ‘std::remove_reference<int&>::type {aka int}’ is not a class, struct, or union type __uc_type; ^~~~~~~~~ /usr/include/c++/7/bits/stl_algo.h:3841:37: error: ‘std::remove_reference<int&>::type {aka int}’ is not a class, struct, or union type const pair<__uc_type, __uc_type> __pospos = ^~~~~~~~ /usr/include/c++/7/bits/stl_algo.h:3841:37: error: ‘std::remove_reference<int&>::type {aka int}’ is not a class, struct, or union type CMakeFiles/gen2.dir/build.make:62: recipe for target 'CMakeFiles/gen2.dir/src/main.cpp.o' failed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.