aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/main.zig2
-rw-r--r--src/rtw/perlin.zig52
-rw-r--r--src/rtw/texture.zig15
3 files changed, 43 insertions, 26 deletions
diff --git a/src/main.zig b/src/main.zig
index a0b0fd2..00bc7e6 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -165,7 +165,7 @@ fn generateTwoSpheres(rng: Random, allocator: anytype) !Hittable {
fn generateTwoPerlinSpheres(rng: Random, allocator: anytype) !Hittable {
var hittable_objects = ArrayList(Hittable).init(allocator);
- const perlin = Texture.makeNoise(4.0, rng);
+ const perlin = try Texture.makeNoise(allocator, 4.0, rng);
var mat1 = try allocator.create(Material);
var mat2 = try allocator.create(Material);
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) {
diff --git a/src/rtw/texture.zig b/src/rtw/texture.zig
index 537ce47..ebbf4cb 100644
--- a/src/rtw/texture.zig
+++ b/src/rtw/texture.zig
@@ -29,8 +29,8 @@ pub const Texture = union(TextureTag) {
) };
}
- pub fn makeNoise(scale: f64, rng: Random) Texture {
- return .{ .noise = .{ .perlin = Perlin.init(rng), .scale = scale } };
+ pub fn makeNoise(allocator: std.mem.Allocator, scale: f64, rng: Random) !Texture {
+ return .{ .noise = try NoiseTexture.init(allocator, rng, scale) };
}
pub fn value(tx: Texture, u: f64, v: f64, p: Vec3) Color {
@@ -85,6 +85,17 @@ pub const NoiseTexture = struct {
perlin: Perlin,
scale: f64,
+ fn init(allocator: std.mem.Allocator, rng: Random, scale: f64) !NoiseTexture {
+ return .{
+ .perlin = try Perlin.init(allocator, rng),
+ .scale = scale,
+ };
+ }
+
+ fn deinit(tx: NoiseTexture) void {
+ tx.perlin.deinit();
+ }
+
fn value(tx: NoiseTexture, u: f64, v: f64, p: Vec3) Color {
_ = u;
_ = v;