Chapter 11 done

This commit is contained in:
Luxdragon 2024-03-05 00:05:12 +01:00
parent d167c2074e
commit 6de86045ba
2 changed files with 81245 additions and 81218 deletions

162426
image.ppm

File diff suppressed because it is too large Load diff

37
main.go
View file

@ -119,7 +119,8 @@ func q_rsqrt(v Vec3) float32 {
}
func Unit_vector(v Vec3) Vec3 {
new_v := v.Mult(q_rsqrt(v))
//new_v := v.Mult(q_rsqrt(v))
new_v := v.Mult(1.0/float32(math.Sqrt(float64(v.Length_squared()))))
return new_v
}
@ -395,6 +396,13 @@ func NewMaterial(m int, a Vec3, f float32, i float32) Material {
}
func Reflectance(cosine float32, ref_idx float32) float32 {
// Use Schlick's approximation for reflectance
r0 := (1.0-ref_idx) / (1.0+ref_idx)
r0 = r0*r0
return r0 + (1.0-r0)*float32(math.Pow(float64(1 - cosine), 5))
}
func (mat Material) Scatter(r_in *Ray, rec *Hit_record, attenuation *Vec3, scattered *Ray) bool {
if (mat.material == 0) { //Lambertian
scatter_direction := rec.normal.Add(RandomUnitVector())
@ -421,8 +429,22 @@ func (mat Material) Scatter(r_in *Ray, rec *Hit_record, attenuation *Vec3, scatt
refraction_ration = mat.ir
}
unit_direction := Unit_vector(r_in.Direction())
refracted := Refract(unit_direction, rec.normal, refraction_ration)
*scattered = *NewRay(rec.p, refracted)
cos_theta := float32(math.Min(float64(Dot(unit_direction.Neg(), rec.normal)), 1.0))
sin_theta := float32(math.Sqrt(float64(1.0 - cos_theta*cos_theta)))
var cannot_refract bool
var direction Vec3
if (refraction_ration * sin_theta > 1.0) {
cannot_refract = true
}
if (cannot_refract || Reflectance(cos_theta, refraction_ration) > RandomDouble()) {
direction = Reflect(unit_direction, rec.normal)
} else {
direction = Refract(unit_direction, rec.normal, refraction_ration)
}
*scattered = *NewRay(rec.p, direction)
}
return true
}
@ -531,9 +553,9 @@ func (cam *Camera) Pixel_sample_square() Vec3 {
func main() {
// Materials
material_ground := NewMaterial(0, NewColor(0.8, 0.8, 0.0), 0.0, 0.0)
material_center := NewMaterial(2, NewColor(0.7, 0.3, 0.3), 0.0, 1.5)
material_center := NewMaterial(0, NewColor(0.1, 0.2, 0.5), 0.0, 1.5)
material_left := NewMaterial(2, NewColor(0.8, 0.8, 0.8), 0.3, 1.5)
material_right := NewMaterial(1, NewColor(0.8, 0.6, 0.2), 1.0, 0.0)
material_right := NewMaterial(1, NewColor(0.8, 0.6, 0.2), 0.0, 0.0)
// World
@ -553,6 +575,11 @@ func main() {
radius: 0.5,
mat: material_left,
})
world.Add(Sphere{
center: NewVec3(-1.0, 0, -1.0),
radius: -0.4,
mat: material_left,
})
world.Add(Sphere{
center: NewVec3(1.0, 0, -1.0),
radius: 0.5,