Skip to content

Commit

Permalink
Merge pull request #13 from Miou-zora/normal_go_brrrrrrrr
Browse files Browse the repository at this point in the history
Add proper diffuse lighting
  • Loading branch information
Miou-zora authored Apr 21, 2024
2 parents 25bded1 + 9c0b77b commit 315aede
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
16 changes: 14 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const std = @import("std");
const Vec3 = @import("Vec3.zig").Vec3;
const Pt3 = @import("Pt3.zig").Pt3;
const Ray = @import("Ray.zig").Ray;
const Sphere = @import("Sphere.zig").Sphere;
const Camera = @import("Camera.zig").Camera;
const qoi = @import("qoi.zig");
const Light = @import("Light.zig").Light;

fn compute_lighting(intersection: Vec3, normal: Vec3, light: Pt3, ambient: f32) f32 {
const L = intersection.to(light);
const n_dot_l = normal.dot(L);
// TODO: Check if adding the ambient is not just better
return std.math.clamp(1.0 * n_dot_l / (normal.length() * L.length()), ambient, 1.0);
}

pub fn main() !void {
const camera = Camera{
.origin = Vec3.nil(),
Expand Down Expand Up @@ -62,7 +70,8 @@ pub fn main() !void {
record.intersection_point.y = record.intersection_point.y + record.normal.y * 0.001;
record.intersection_point.z = record.intersection_point.z + record.normal.z * 0.001;
if (record.hit) {
const obstacle = sphere.hits(Ray{ .direction = record.intersection_point.to(light.position), .origin = record.intersection_point });
const vec_to_light = record.intersection_point.to(light.position);
const obstacle = sphere.hits(Ray{ .direction = vec_to_light, .origin = record.intersection_point });
if (obstacle.hit) {
image.pixels[index] = .{
.r = @as(u8, @intFromFloat(255.0 * ambiant_color_intensity)),
Expand All @@ -71,8 +80,11 @@ pub fn main() !void {
.a = 255,
};
} else {
const norm = record.normal;
const inter = record.intersection_point;
const light_color = 255.0 * compute_lighting(inter, norm, light.position, ambiant_color_intensity);
image.pixels[index] = .{
.r = 255,
.r = @as(u8, @intFromFloat(light_color)),
.g = 0,
.b = 0,
.a = 255,
Expand Down
31 changes: 0 additions & 31 deletions src/qoi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -495,34 +495,3 @@ test "random encode/decode" {
try std.testing.expectEqualSlices(Color, &input_buffer, image.pixels);
}
}

test "input fuzzer. plz do not crash" {
var rng_engine = std.rand.DefaultPrng.init(0x1337);
const rng = rng_engine.random();

var rounds: usize = 32;
while (rounds > 0) {
rounds -= 1;
var input_buffer: [1 << 20]u8 = undefined; // perform on a 1 MB buffer
rng.bytes(&input_buffer);

if ((rounds % 4) != 0) { // 25% is fully random 75% has a correct looking header
std.mem.copy(u8, &input_buffer, &(Header{
.width = rng.int(u16),
.height = rng.int(u16),
.format = rng.enumValue(Format),
.colorspace = rng.enumValue(Colorspace),
}).encode());
}

var stream = std.io.fixedBufferStream(&input_buffer);

var image_or_err = decodeStream(std.testing.allocator, stream.reader());
if (image_or_err) |*image| {
defer image.deinit(std.testing.allocator);
} else |err| {
// error is also okay, just no crashes plz
err catch {};
}
}
}

0 comments on commit 315aede

Please sign in to comment.