summaryrefslogtreecommitdiffstats
path: root/Projects/Asteroids/src/noise.h
blob: 250ae5ca07556ee57580cc436e617e0938a4ddfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2014 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.  
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.

#pragma once

#include "simplexnoise1234.h"

// Very simple multi-octave simplex noise helper
// Returns noise in the range [0, 1] vs. the usual [-1, 1]
template <size_t N = 4>
class NoiseOctaves
{
private:
    float mWeights[N];
    float mWeightNorm;
    
public:
    NoiseOctaves(float persistence = 0.5f)
    {
        float weightSum = 0.0f;
        for (size_t i = 0; i < N; ++i) {
            mWeights[i] = persistence;
            weightSum += persistence;
            persistence *= persistence;
        }
        mWeightNorm = 0.5f / weightSum; // Will normalize to [-0.5, 0.5]
    }

    // Returns [0, 1]
    float operator()(float x, float y, float z) const
    {
        float r = 0.0f;
        for (size_t i = 0; i < N; ++i) {
            r += mWeights[i] * snoise3(x, y, z);
            x *= 2.0f; y *= 2.0f; z *= 2.0f;
        }
        return r * mWeightNorm + 0.5f;
    }

    // Returns [0, 1]
    float operator()(float x, float y, float z, float w) const
    {
        float r = 0.0f;
        for (size_t i = 0; i < N; ++i) {
            r += mWeights[i] * snoise4(x, y, z, w);
            x *= 2.0f; y *= 2.0f; z *= 2.0f; w *= 2.0f;
        }
        return r * mWeightNorm + 0.5f;
    }
};