Compare commits
3 Commits
94b0f8355e
...
a2012e6742
| Author | SHA1 | Date | |
|---|---|---|---|
| a2012e6742 | |||
| 6d7998ad9f | |||
| 58646e4142 |
@ -8,11 +8,11 @@ pub fn write_color(c: Color, samples_per_pixel: isize) anyerror!void {
|
||||
var g = c.y();
|
||||
var b = c.z();
|
||||
|
||||
// Divide the color by the number of samples.
|
||||
var scale = 1.0 / @intToFloat(f32, samples_per_pixel);
|
||||
r *= scale;
|
||||
g *= scale;
|
||||
b *= scale;
|
||||
// Divide the color by the number of samples and gamma-correct for gamma=2.0.
|
||||
const scale = 1.0 / @intToFloat(f32, samples_per_pixel);
|
||||
r = @sqrt(scale * r);
|
||||
g = @sqrt(scale * g);
|
||||
b = @sqrt(scale * b);
|
||||
|
||||
// Write the translated [0,255] value of each color component.
|
||||
var ir = @floatToInt(u8, 256 * clamp(r, 0, 0.999));
|
||||
|
||||
@ -15,7 +15,7 @@ const Point3 = vec.Point3;
|
||||
const Ray = ray.Ray;
|
||||
const Sphere = sphere.Sphere;
|
||||
const Vec3 = vec.Vec3;
|
||||
const random_in_unit_sphere = vec.random_in_unit_sphere;
|
||||
const random_in_hemisphere = vec.random_in_hemisphere;
|
||||
const info = std.log.info;
|
||||
const write_color = color.write_color;
|
||||
|
||||
@ -23,10 +23,10 @@ fn ray_color(r: Ray, world: Hittable, depth: isize) Color {
|
||||
// If we've exceeded the ray bounce limit, no more light is gathered.
|
||||
if (depth <= 0) return Color.init(0, 0, 0);
|
||||
|
||||
var hit = world.hit(r, 0, std.math.inf(f32));
|
||||
var hit = world.hit(r, 0.0001, std.math.inf(f32));
|
||||
if (hit) |rec| {
|
||||
const target = rec.p.add(rec.normal.add(random_in_unit_sphere()));
|
||||
return ray_color(Ray.init(rec.p, target.sub(rec.p)), world, depth - 1);
|
||||
const target = rec.p.add(rec.normal.add(random_in_hemisphere(rec.normal)));
|
||||
return ray_color(Ray.init(rec.p, target.sub(rec.p)), world, depth - 1).scale(0.5);
|
||||
}
|
||||
var unit_direction = r.direction().unit();
|
||||
const t = 0.5 * (unit_direction.y() + 1);
|
||||
|
||||
@ -47,9 +47,9 @@ pub const Vec3 = struct {
|
||||
/// Return value in unit cube from -1, 1
|
||||
pub fn random() Vec3 {
|
||||
return Vec3.init(
|
||||
rand.float(f32) * 2 - 0.5,
|
||||
rand.float(f32) * 2 - 0.5,
|
||||
rand.float(f32) * 2 - 0.5,
|
||||
rand.float(f32) * 2 - 1,
|
||||
rand.float(f32) * 2 - 1,
|
||||
rand.float(f32) * 2 - 1,
|
||||
);
|
||||
}
|
||||
};
|
||||
@ -61,6 +61,17 @@ pub fn random_in_unit_sphere() Vec3 {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
pub fn random_unit_vector() Vec3 {
|
||||
return random_in_unit_sphere().unit();
|
||||
}
|
||||
pub fn random_in_hemisphere(normal: Vec3) Vec3 {
|
||||
const in_unit_sphere = random_in_unit_sphere();
|
||||
if (in_unit_sphere.dot(normal) > 0.0) { // In the same hemisphere as the normal
|
||||
return in_unit_sphere;
|
||||
} else {
|
||||
return in_unit_sphere.scale(-1);
|
||||
}
|
||||
}
|
||||
|
||||
pub const Color = Vec3;
|
||||
pub const Point3 = Vec3;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user