add Lambertian material

This commit is contained in:
Fabien Freling 2021-05-11 22:15:09 +02:00
parent 3296ea3e81
commit b3c47a914f
7 changed files with 85 additions and 19 deletions

View file

@ -24,7 +24,15 @@ pub const Vec3 = packed struct {
};
}
pub fn mul(self: Vec3, scalar: f32) Vec3 {
pub fn mul(self: Vec3, v: Vec3) Vec3 {
return Vec3{
.x = self.x * v.x,
.y = self.y * v.y,
.z = self.z * v.z,
};
}
pub fn mul_s(self: Vec3, scalar: f32) Vec3 {
return Vec3{
.x = self.x * scalar,
.y = self.y * scalar,
@ -60,14 +68,17 @@ pub const Vec3 = packed struct {
};
}
// pub fn unit_vector() Vec3 {
// return
// }
};
pub fn unit(self: Vec3) Vec3 {
return self.div(self.length());
}
pub fn unitVector(vec: Vec3) Vec3 {
return vec.div(vec.length());
}
pub fn nearZero(self: Vec3) bool {
const t = 1e-8;
return math.absFloat(self.x) < t
and math.absFloat(self.y) < t
and math.absFloat(self.z) < t;
}
};
pub fn random(rng: *Random, min: f32, max: f32) Vec3 {
const range = max - min;
@ -88,7 +99,7 @@ pub fn random_in_unit_sphere(rng: *Random) Vec3 {
}
pub fn random_unit_vector(rng: *Random) Vec3 {
return unitVector(random_in_unit_sphere(rng));
return random_in_unit_sphere(rng).unit();
}
pub fn random_in_hemisphere(rng: *Random, normal: Vec3) Vec3 {
@ -97,7 +108,7 @@ pub fn random_in_hemisphere(rng: *Random, normal: Vec3) Vec3 {
// In the same hemisphere as the normal
return in_unit_sphere;
} else {
return in_unit_sphere.mul(-1);
return in_unit_sphere.mul_s(-1);
}
}