Skip to content

Commit

Permalink
Add basic light
Browse files Browse the repository at this point in the history
  • Loading branch information
Miou-zora committed Apr 19, 2024
1 parent ca3313e commit 2aa6ef5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/Camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ pub const Camera = struct {
origin: Pt3,
screen: Rect3,

pub fn createRay(self: *const Self, u: f32, v: f32) Ray {
pub fn createRay(self: *const Self, x: f32, y: f32) Ray {
return Ray{
.origin = self.origin,
.direction = self.screen.pointAt(u, v).subVec3(self.origin),
.direction = self.screen.pointAt(x, y).subVec3(self.origin),
};
}
};
Expand Down
11 changes: 11 additions & 0 deletions src/ColorRGB.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const std = @import("std");

pub const ColorRGB = struct {
red: f16,
green: f16,
blue: f16,
};

test {
std.testing.refAllDecls(@This());
}
13 changes: 13 additions & 0 deletions src/Light.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Pt3 = @import("Pt3.zig").Pt3;
const ColorRGB = @import("ColorRGB.zig").ColorRGB;
const std = @import("std");

pub const Light = struct {
intensity: u8,
color: ColorRGB,
position: Pt3,
};

test {
std.testing.refAllDecls(@This());
}
4 changes: 2 additions & 2 deletions src/Rect3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub const Rect3 = struct {
top: Vec3,
left: Vec3,

pub fn pointAt(self: *const Self, u: f32, v: f32) Pt3 {
return self.origin.addVec3(self.top.mulf32(u)).addVec3(self.left.mulf32(v));
pub fn pointAt(self: *const Self, x: f32, y: f32) Pt3 {
return self.origin.addVec3(self.top.mulf32(y)).addVec3(self.left.mulf32(x));
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/Sphere.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ pub const Sphere = struct {
if (distance < 0) {
return HitRecord{ .hit = false, .normal = Vec3.nil(), .intersection_point = Vec3.nil() };
} else {
return HitRecord{ .hit = true, .normal = self.center.subVec3(intersection_point), .intersection_point = intersection_point };
return HitRecord{ .hit = true, .normal = intersection_point.subVec3(self.center), .intersection_point = intersection_point };
}
} else {
const t1 = (-b + std.math.sqrt(delta)) / (2 * a);
const t2 = (-b - std.math.sqrt(delta)) / (2 * a);
if (t1 < 0 and t2 < 0) {
return HitRecord{ .hit = false, .normal = Vec3.nil(), .intersection_point = Vec3.nil() };
}
const t = if (std.math.absInt(t1) < std.math.absInt(t2)) t1 else t2;
const t = if (@fabs(t1) < @fabs(t2)) t1 else t2;
const intersection_point = ray.origin.addVec3(ray.direction.mulf32(t));
const distance = ray.origin.distance(intersection_point);
if (distance < 0) {
return HitRecord{ .hit = false, .normal = Vec3.nil(), .intersection_point = Vec3.nil() };
} else {
return HitRecord{ .hit = true, .normal = self.center.subVec3(intersection_point), .intersection_point = intersection_point };
return HitRecord{ .hit = true, .normal = intersection_point.subVec3(self.center), .intersection_point = intersection_point };
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/Vec3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ pub const Vec3 = struct {
}

pub fn to(self: *const Self, other: Self) Vec3 {
return other.subVec3(self);
return other.subVec3(self.*);
}
// static Maths::Vector GetReflectedRay(const Maths::Vector &normal,
// const Maths::Vector &incident)
// {
// return (((normal * incident) * normal) * 2 - incident);
// }

pub fn
pub fn angleBetween(self: *const Self, other: Self) f32 {
return std.math.acos(self.dot(other) / (self.length() * other.length()));
}

pub fn length(self: *const Self) f32 {
return std.math.sqrt(std.math.pow(f32, self.x, 2) + std.math.pow(f32, self.y, 2) + std.math.pow(f32, self.z, 2));
}

pub fn dot(self: *const Self, other: Self) f32 {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
};

test "subVec3" {
Expand Down
44 changes: 20 additions & 24 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@ const Ray = @import("Ray.zig").Ray;
const Sphere = @import("Sphere.zig").Sphere;
const Camera = @import("Camera.zig").Camera;
const qoi = @import("qoi.zig");

const ColorRGB = struct {
red: f16,
green: f16,
blue: f16,
};

const Pt3 = @import("Pt3.zig");
const Light = struct {
intensity: u8,
color: ColorRGB,
position: Pt3,
};
const Light = @import("Light.zig").Light;

pub fn main() !void {
const camera = Camera{
Expand Down Expand Up @@ -46,11 +34,11 @@ pub fn main() !void {
const light = Light{
.color = .{ .blue = 255, .green = 255, .red = 255 },
.intensity = 1,
.position = .{ .x = 2, .y = 0, .z = 2 },
.position = .{ .x = 2, .y = 3, .z = 2 },
};
const ambiant_color_intensity = 0.1;
const height = 1080;
const width = 1920;
const height = 1000;
const width = 1000;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();

Expand All @@ -67,26 +55,34 @@ pub fn main() !void {
for (0..height) |y| {
for (0..width) |x| {
const scaled_x: f32 = @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width));
const scaled_y: f32 = @as(f32, @floatFromInt(y)) / @as(f32, @floatFromInt(height));
const scaled_y: f32 = @as(f32, @floatFromInt((height - 1) - y)) / @as(f32, @floatFromInt(height));
const ray: Ray = camera.createRay(scaled_x, scaled_y);
const record = sphere.hits(ray);
var record = sphere.hits(ray);
record.intersection_point.x = record.intersection_point.x + record.normal.x * 0.001;
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(sphere.center), .origin = record.intersection_point });
const obstacle = sphere.hits(Ray{ .direction = record.intersection_point.to(light.position), .origin = record.intersection_point });
if (obstacle.hit) {
image.pixels[index] = .{
.r = 255 * ambiant_color_intensity,
.r = @as(u8, @intFromFloat(255.0 * ambiant_color_intensity)),
.g = 0,
.b = 0,
.a = 255,
};
} else {

image.pixels[index] = .{
.r = 255,
.g = 0,
.b = 0,
.a = 255,
};
}
} else {
image.pixels[index] = .{
.r = 255,
.g = 255,
.b = 255,
.r = 0,
.g = 0,
.b = 0,
.a = 255,
};
}
Expand Down

0 comments on commit 2aa6ef5

Please sign in to comment.