Giter Club home page Giter Club logo

perlin-noise's Introduction

Perlin Noise

Preview Image

About

Quick and simple implementation of Perlin Noise algorithm in 1D, 2D, and 3D in C++11 with no external dependencies. This code may be easily used by simply including the header into any project along with a DB_PERLIN_IMPL definition. All relevant code is written as a small set of pure functions that work with both floats and doubles.

I wrote this with the primary goal of having a bit of fun and learning more about the famous algorithm used everywhere in procedural generation. Ultimately, my goal was to use this implementation in several other projects (it's always good to have a noise generator lying around). I hope it could be useful to other people, too!

Usage

If you're familiar with stb libraries, there is nothing new to learn here: simply copy the entire db_perlin.hpp file somewhere in your project and provide exactly one definition of DB_PERLIN_IMPL in a source file before including the header. You may create a dedicated file to serve as the implementation:

// file: db_perlin.cpp

#define DB_PERLIN_IMPL
#include "db_perlin.hpp"

Then you may use it from other files as you normally would any header file:

// file: main.cpp

#include "db_perlin.hpp"

int main() {
    double x = /* ... */;
    double y = /* ... */;
    double z = /* ... */;
    
    double noise1D = db::perlin(x);
    double noise2D = db::perlin(x, y);
    double noise3D = db::perlin(x, y, z);
}

If you only need to use the library once you may avoid creating a dedicated implementation file and treat your program source as the implementation:

// file: main.cpp

#define DB_PERLIN_IMPL
#include "db_perlin.hpp"

int main() {
    double x = /* ... */;
    double y = /* ... */;
    double z = /* ... */;

    double noise1D = db::perlin(x);
    double noise2D = db::perlin(x, y);
    double noise3D = db::perlin(x, y, z);
}

Demo

A more detailed example is provided with this repository in examples/demo.cpp file. It is a small demonstration written with SFML that displays the 3D noise as an animated grayscale with respect to time.

perlin-noise's People

Contributors

daniilsjb avatar

Stargazers

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

Watchers

 avatar

perlin-noise's Issues

Negative input values result in invalid noise result

When using the function like:

  for (int x = -2; x < 2; x++) {

    for (int z = -2; z < 2; z++) {
      std::cout << "("<< x << ", " << z << ")" << std::endl;
      std::cout << db::perlin(x * 0.2f, z * 0.2f) << std::endl; 
    }
    std::cout << std::endl;
  }

this results in very high output noise values when there is a negative number involved:

(-2, -2)
-1.11151e+28
(-2, -1)
-1.10544e+28
(-2, 0)
1.68817e+15
(-2, 1)
1.39458e+15

(-1, -2)
-1.10803e+28
(-1, -1)
-1.10199e+28
(-1, 0)
1.68027e+15
(-1, 1)
1.38806e+15

(0, -2)
1.68158e+15
(0, -1)
1.67371e+15
(0, 0)
0
(0, 1)
-0.046336

(1, -2)
1.68227e+15
(1, -1)
1.67439e+15
(1, 0)
0.188416
(1, 1)
0.128483

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.