Skip to content

Commit

Permalink
Update equation
Browse files Browse the repository at this point in the history
  • Loading branch information
Miou-zora committed May 9, 2024
1 parent 15ea32b commit d2c0b7a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 65 deletions.
Binary file added Zaytracer
Binary file not shown.
31 changes: 31 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@
]
}
},
{
"sphere": {
"origin": {
"x": 1,
"y": -0.5,
"z": 5
},
"radius": 0.5,
"material": 1
}
},
{
"sphere": {
"origin": {
"x": 1,
"y": 0.5,
"z": 5
},
"radius": 0.3,
"material": 1,
"transforms": [
{
"scale": {
"x": 0.5,
"y": 2,
"z": 0.9
}
}
]
}
},
{
"plane": {
"origin": {
Expand Down
Binary file added perf.data
Binary file not shown.
43 changes: 12 additions & 31 deletions src/Cylinder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,19 @@ pub const Cylinder = struct {
const delta = b * b - 4.0 * a * c;
if (delta < 0.0 or a == 0.0) {
return HitRecord.nil();
} else if (delta == 0) {
const t = -b / (2.0 * a);
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
if (t < 0.0) {
return HitRecord.nil();
} else {
const normal = intersection_point - self.origin;
return HitRecord{
.hit = true,
.normal = zmath.f32x4(normal[0], normal[1], 0, 0),
.intersection_point = intersection_point,
.t = 0,
.material = self.material,
};
}
} else {
const t1 = (-b + @sqrt(delta)) / (2.0 * a);
const t2 = (-b - @sqrt(delta)) / (2.0 * a);
if (t1 < 0 and t2 < 0) {
return HitRecord.nil();
}
const t = if (t1 < t2 and t1 > 0) t1 else t2;
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
const normal = intersection_point - self.origin;
return HitRecord{
.hit = true,
.normal = zmath.f32x4(normal[0], normal[1], 0, 0),
.intersection_point = intersection_point,
.t = 0,
.material = self.material,
};
}
const t = (-b - @sqrt(delta)) / (2.0 * a);
if (t < 0)
return HitRecord.nil();
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
const normal = intersection_point - self.origin;
return HitRecord{
.hit = true,
.normal = zmath.f32x4(normal[0], normal[1], 0, 0),
.intersection_point = intersection_point,
.t = 0,
.material = self.material,
};
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/HitRecord.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const HitRecord = struct {
t: f32,
material: Material,

pub fn nil() Self {
pub inline fn nil() Self {
return Self{
.hit = false,
.normal = zmath.f32x4s(0),
Expand Down
44 changes: 11 additions & 33 deletions src/Sphere.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,18 @@ pub const Sphere = struct {
const delta: f32 = b * b - 4 * a * c;
if (delta < 0) {
return HitRecord.nil();
} else if (delta == 0) {
const t = -b / (2 * a);
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
if (t < 0) {
return HitRecord.nil();
} else {
return HitRecord{
.hit = true,
.normal = intersection_point - self.origin,
.intersection_point = intersection_point,
.t = 0,
.material = self.material,
};
}
} else {
const t1 = (-b + @sqrt(delta)) / (2 * a);
const t2 = (-b - @sqrt(delta)) / (2 * a);
if (t1 < 0 and t2 < 0) {
return HitRecord.nil();
}
const t = if (t1 < t2 and t1 > 0) t1 else t2;
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
if (t < 0) {
return HitRecord.nil();
} else {
return HitRecord{
.hit = true,
.normal = intersection_point - self.origin,
.intersection_point = intersection_point,
.t = 0,
.material = self.material,
};
}
}
const t = (-b - @sqrt(delta)) / (2 * a);
if (t < 0)
return HitRecord.nil();
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
return HitRecord{
.hit = true,
.normal = intersection_point - self.origin,
.intersection_point = intersection_point,
.t = 0,
.material = self.material,
};
}
};

Expand Down

0 comments on commit d2c0b7a

Please sign in to comment.