Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reflective #26

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@
"z": 2
},
"radius": 0.5,
"material": 0
"material": 1
}
},
{
"sphere": {
"origin": {
"x": -0.2,
"y": 0.5,
"z": 2
},
"radius": 0.3,
"material": 1
}
}
],
Expand All @@ -43,7 +54,17 @@
"r": 200,
"g": 0,
"b": 200
}
},
"reflective": 0.5
},
{
"specular": 0,
"color": {
"r": 200,
"g": 200,
"b": 200
},
"reflective": 1
}
]
}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
nativeBuildInputs = [
zigPackage
pkgs.linuxPackages_latest.perf
pkgs.ffmpeg
];
};
packages.default = pkgs.stdenv.mkDerivation {
Expand Down
2 changes: 2 additions & 0 deletions src/Material.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ pub const Material = struct {
const Self = @This();
color: Color,
specular: f32,
reflective: f32,

pub fn nil() Self {
return Material{
.color = .{ .r = 0.0, .g = 0.0, .b = 0.0 },
.specular = 0.0,
.reflective = 0.0,
};
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/Vec3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ pub const Vec3 = struct {
pub fn sum(self: *const Self) f32 {
return self.x + self.y + self.z;
}

pub fn reflect(self: *const Self, normal: Self) Vec3 {
return normal.mulf32(self.dot(normal) * 2).subVec3(self.*);
}
};

test "subVec3" {
Expand Down
70 changes: 47 additions & 23 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn compute_lighting(intersection: Vec3, normal: Vec3, scene: *Scene.Scene, r
.point_light => |item| {
const L = intersection.to(item.position).normalized();
t_max = intersection.to(item.position).length();
const closest_hit = find_closest_intersection(scene, Ray{ .direction = L, .origin = intersection }, 0, t_max);
const closest_hit = find_closest_intersection(scene, Ray{ .direction = L, .origin = intersection.addVec3(normal.mulf32(0.0001)) }, 0.0001, t_max);
if (closest_hit.hit) {
continue;
}
Expand All @@ -38,7 +38,7 @@ pub fn compute_lighting(intersection: Vec3, normal: Vec3, scene: *Scene.Scene, r
lighting.g += item.color.g * em;
lighting.r += item.color.r * em;
if (material.specular != -1) {
const R = normal.mulf32(n_dot_l * 2.0).subVec3(L);
const R = L.reflect(normal);
const V = ray.direction.inv().normalized();
const r_dot_v = R.dot(V);
if (r_dot_v > 0) {
Expand Down Expand Up @@ -147,38 +147,60 @@ fn find_closest_intersection(scene: *Scene.Scene, ray: Ray, t_min: f32, t_max: f
return closest_hit;
}

fn get_pixel_color(x: usize, y: usize, scene: *Scene.Scene, height: u32, width: u32) qoi.Color {
const scaled_x: f32 = @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width));
const scaled_y: f32 = @as(f32, @floatFromInt((height - 1) - y)) / @as(f32, @floatFromInt(height));
const ray: Ray = scene.camera.createRay(scaled_x, scaled_y);

fn get_pixel_color(ray: Ray, scene: *Scene.Scene, height: u32, width: u32, recursion_depth: usize) qoi.Color {
const closest_hit = find_closest_intersection(scene, ray, std.math.floatMin(f32), std.math.floatMax(f32));

if (closest_hit.hit) {
const norm = closest_hit.normal.normalized();
const inter = closest_hit.intersection_point;
const material = closest_hit.material;
const light_color = compute_lighting(inter, norm, scene, ray, material);
if (!closest_hit.hit) {
return .{
.r = @as(u8, @intFromFloat(material.color.r * light_color.r / 255)),
.g = @as(u8, @intFromFloat(material.color.g * light_color.g / 255)),
.b = @as(u8, @intFromFloat(material.color.b * light_color.b / 255)),
.r = 0,
.g = 0,
.b = 0,
.a = 255,
};
}
const norm = closest_hit.normal.normalized();
const inter = closest_hit.intersection_point;
const material = closest_hit.material;
const light_color = compute_lighting(inter, norm, scene, ray, material);
const color = .{
.r = @as(u8, @intFromFloat(material.color.r * light_color.r / 255)),
.g = @as(u8, @intFromFloat(material.color.g * light_color.g / 255)),
.b = @as(u8, @intFromFloat(material.color.b * light_color.b / 255)),
.a = 255,
};
const reflective = closest_hit.material.reflective;
if (recursion_depth <= 0 or reflective <= 0) {
return color;
}

const R = ray.direction.inv().reflect(norm);
const new_origin = closest_hit.intersection_point.addVec3(norm.mulf32(0.0001));
const reflected_color = get_pixel_color(
Ray{
.direction = R,
.origin = new_origin,
},
scene,
height,
width,
recursion_depth - 1,
);
return .{
.r = 0,
.g = 0,
.b = 0,
.r = @as(u8, @intFromFloat(@as(f32, @floatFromInt(color.r)) * (1 - reflective) + @as(f32, @floatFromInt(reflected_color.r)) * reflective)),
.g = @as(u8, @intFromFloat(@as(f32, @floatFromInt(color.g)) * (1 - reflective) + @as(f32, @floatFromInt(reflected_color.g)) * reflective)),
.b = @as(u8, @intFromFloat(@as(f32, @floatFromInt(color.b)) * (1 - reflective) + @as(f32, @floatFromInt(reflected_color.b)) * reflective)),
.a = 255,
};
}

fn calculate_image(pixels: []qoi.Color, scene: *Scene.Scene, height: u32, width: u32) !void {
const recursion_depth = 5;
for (0..height) |y| {
for (0..width) |x| {
pixels[x + y * width] = get_pixel_color(x, y, scene, height, width);
const scaled_x: f32 = @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width));
const scaled_y: f32 = @as(f32, @floatFromInt((height - 1) - y)) / @as(f32, @floatFromInt(height));
const ray: Ray = scene.camera.createRay(scaled_x, scaled_y);
pixels[x + y * width] = get_pixel_color(ray, scene, height, width, recursion_depth);
}
}
}
Expand All @@ -193,12 +215,12 @@ pub fn main() !void {

const light = Light{
.color = .{ .b = 255, .g = 255, .r = 255 },
.intensity = 0.6,
.intensity = 1,
.position = .{ .x = 1, .y = -0.1, .z = 3 },
};
const ambiant_light: AmbientLight = .{
.color = .{ .b = 255, .g = 255, .r = 255 },
.intensity = 0.2,
.intensity = 0.3,
};

var scene = Scene.Scene.init(allocator, config.camera);
Expand All @@ -218,6 +240,7 @@ pub fn main() !void {
.material = .{
.specular = 100,
.color = .{ .b = 255, .g = 0, .r = 0 },
.reflective = 0.5,
},
.transform = .{ .rotation = .{
.x = -0.3,
Expand All @@ -239,6 +262,7 @@ pub fn main() !void {
.material = .{
.specular = 100,
.color = .{ .b = 0, .g = 255, .r = 0 },
.reflective = 0.5,
},
.transform = .{ .translation = .{
.x = 0,
Expand All @@ -249,8 +273,8 @@ pub fn main() !void {
try scene.lights.append(.{ .point_light = light });
try scene.lights.append(.{ .ambient_light = ambiant_light });

const height: u32 = 1000;
const width: u32 = 1000;
const height: u32 = 1920;
const width: u32 = 1920;

var image = qoi.Image{
.width = width,
Expand Down