aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/rtw/perlin.zig
blob: c60bb34afcf69833bbb9671cdad0dacd8c85a3f3 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const std = @import("std");
const ArrayList = std.ArrayList;

const rand = @import("rand.zig");
const Random = rand.Random;
const randomReal01 = rand.randomReal01;
const randomInt = rand.randomInt;
const Point3 = @import("vec.zig").Point3;
const Vec3 = @import("vec.zig").Vec3;

pub const Perlin = struct {
    const POINT_COUNT = 256;

    randomVec: ArrayList(Vec3),
    permX: ArrayList(usize),
    permY: ArrayList(usize),
    permZ: ArrayList(usize),

    pub fn init(allocator: std.mem.Allocator, rng: Random) !Perlin {
        var perlin = Perlin{
            .randomVec = try ArrayList(Vec3).initCapacity(allocator, POINT_COUNT),
            .permX = try ArrayList(usize).initCapacity(allocator, POINT_COUNT),
            .permY = try ArrayList(usize).initCapacity(allocator, POINT_COUNT),
            .permZ = try ArrayList(usize).initCapacity(allocator, POINT_COUNT),
        };

        var i: usize = 0;
        while (i < POINT_COUNT) : (i += 1) {
            try perlin.randomVec.append(Vec3.random(rng, -1, 1).normalized());
            try perlin.permX.append(i);
            try perlin.permY.append(i);
            try perlin.permZ.append(i);
        }
        permute(rng, perlin.permX.items, POINT_COUNT);
        permute(rng, perlin.permY.items, POINT_COUNT);
        permute(rng, perlin.permZ.items, POINT_COUNT);

        return perlin;
    }

    pub fn deinit(perlin: Perlin) void {
        perlin.permZ.deinit();
        perlin.permY.deinit();
        perlin.permX.deinit();
        perlin.randomVec.deinit();
    }

    pub fn noise(perlin: Perlin, p: Point3) f64 {
        const u = p.x - @floor(p.x);
        const v = p.y - @floor(p.y);
        const w = p.z - @floor(p.z);
        const u_ = u * u * (3 - 2 * u);
        const v_ = v * v * (3 - 2 * v);
        const w_ = w * w * (3 - 2 * w);

        const i = @floatToInt(i32, @floor(p.x));
        const j = @floatToInt(i32, @floor(p.y));
        const k = @floatToInt(i32, @floor(p.z));

        var c: [2][2][2]Vec3 = undefined;

        var di: usize = 0;
        while (di < 2) : (di += 1) {
            var dj: usize = 0;
            while (dj < 2) : (dj += 1) {
                var dk: usize = 0;
                while (dk < 2) : (dk += 1) {
                    const ix = @intCast(usize, i + @intCast(i32, di)) & 255;
                    const iy = @intCast(usize, j + @intCast(i32, dj)) & 255;
                    const iz = @intCast(usize, k + @intCast(i32, dk)) & 255;
                    c[di][dj][dk] = perlin.randomVec.items[
                        perlin.permX.items[ix] ^ perlin.permY.items[iy] ^ perlin.permZ.items[iz]
                    ];
                }
            }
        }

        return perlinInterp(c, u_, v_, w_);
    }

    pub fn turb(perlin: Perlin, p: Point3, depth: u8) f64 {
        var accum: f64 = 0.0;
        var p_ = p;
        var weight: f64 = 1.0;
        var i: u8 = 0;
        while (i < depth) : (i += 1) {
            accum += weight * perlin.noise(p_);
            weight *= 0.5;
            p_ = p_.mul(2.0);
        }
        return @fabs(accum);
    }

    fn permute(rng: Random, p: []usize, n: usize) void {
        var i = n - 1;
        while (i > 0) : (i -= 1) {
            const target = randomInt(usize, rng, 0, i);
            const tmp = p[i];
            p[i] = p[target];
            p[target] = tmp;
        }
    }

    fn perlinInterp(c: [2][2][2]Vec3, u: f64, v: f64, w: f64) f64 {
        var accum: f64 = 0.0;
        var i: usize = 0;
        while (i < 2) : (i += 1) {
            var j: usize = 0;
            while (j < 2) : (j += 1) {
                var k: usize = 0;
                while (k < 2) : (k += 1) {
                    const ti = @intToFloat(f64, i);
                    const tj = @intToFloat(f64, j);
                    const tk = @intToFloat(f64, k);
                    const weight = Vec3{ .x = u - ti, .y = v - tj, .z = w - tk };
                    accum +=
                        (ti * u + (1.0 - ti) * (1.0 - u)) *
                        (tj * v + (1.0 - tj) * (1.0 - v)) *
                        (tk * w + (1.0 - tk) * (1.0 - w)) *
                        Vec3.dot(c[i][j][k], weight);
                }
            }
        }
        return accum;
    }
};