aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/rtw/perlin.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/rtw/perlin.zig')
-rw-r--r--src/rtw/perlin.zig52
1 files changed, 29 insertions, 23 deletions
diff --git a/src/rtw/perlin.zig b/src/rtw/perlin.zig
index 4b51390..b00291f 100644
--- a/src/rtw/perlin.zig
+++ b/src/rtw/perlin.zig
@@ -1,3 +1,6 @@
+const std = @import("std");
+const ArrayList = std.ArrayList;
+
const rand = @import("rand.zig");
const Random = rand.Random;
const randomReal01 = rand.randomReal01;
@@ -8,31 +11,40 @@ const Vec3 = @import("vec.zig").Vec3;
pub const Perlin = struct {
const POINT_COUNT = 256;
- // TODO: allocate them in heap.
- randomVec: [POINT_COUNT]Vec3,
- permX: [POINT_COUNT]usize,
- permY: [POINT_COUNT]usize,
- permZ: [POINT_COUNT]usize,
+ randomVec: ArrayList(Vec3),
+ permX: ArrayList(usize),
+ permY: ArrayList(usize),
+ permZ: ArrayList(usize),
- pub fn init(rng: Random) Perlin {
+ pub fn init(allocator: std.mem.Allocator, rng: Random) !Perlin {
var perlin = Perlin{
- .randomVec = undefined,
- .permX = undefined,
- .permY = undefined,
- .permZ = undefined,
+ .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) {
- perlin.randomVec[i] = Vec3.random(rng, -1, 1).normalized();
+ 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);
}
- perlinGeneratePerm(rng, &perlin.permX);
- perlinGeneratePerm(rng, &perlin.permY);
- perlinGeneratePerm(rng, &perlin.permZ);
+ 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);
@@ -56,7 +68,9 @@ pub const Perlin = struct {
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[perlin.permX[ix] ^ perlin.permY[iy] ^ perlin.permZ[iz]];
+ c[di][dj][dk] = perlin.randomVec.items[
+ perlin.permX.items[ix] ^ perlin.permY.items[iy] ^ perlin.permZ.items[iz]
+ ];
}
}
}
@@ -64,14 +78,6 @@ pub const Perlin = struct {
return perlinInterp(c, u_, v_, w_);
}
- fn perlinGeneratePerm(rng: Random, p: []usize) void {
- var i: usize = 0;
- while (i < POINT_COUNT) : (i += 1) {
- p[i] = i;
- }
- permute(rng, p, POINT_COUNT);
- }
-
fn permute(rng: Random, p: []usize, n: usize) void {
var i = n - 1;
while (i > 0) : (i -= 1) {