Chapter 5 done :D

This commit is contained in:
Luxdragon 2024-02-10 18:34:15 +01:00
parent 64bfcf5158
commit 263fe3b81c
2 changed files with 87437 additions and 87417 deletions

174826
image.ppm

File diff suppressed because it is too large Load diff

28
main.go
View file

@ -94,8 +94,8 @@ func Cross(v1 Vec3, v2 Vec3) Vec3 {
}
func Unit_vector(v Vec3) Vec3 {
v.Div(v.Length())
return v
new_v := v.Div(v.Length())
return new_v
}
// ============== COLOUR CLASS ==============
@ -141,11 +141,31 @@ func (r *Ray) At(t float32) Vec3 {
}
func Ray_color(r *Ray) Vec3 {
if (hit_sphere(NewPoint3(0,0,-1), 0.5, r)) {
return NewColor(1, 0, 0)
}
unit_direction := Unit_vector(r.Direction())
var a float32 = 0.5*unit_direction.Y() + 0.5
a := (unit_direction.Y() + 1.0)*0.5
return NewColor(1.0,1.0,1.0).Mult(float32(1.0-a)).Add(NewColor(0.5,0.7,1.0).Mult(a))
}
// =============== SPHERE ===================
func hit_sphere(center Vec3, radius float32, r *Ray) bool {
oc := r.Origin().Sub(center)
a := Dot(r.Direction(), r.Direction())
b := Dot(oc, r.Direction()) * 2.0
c := Dot(oc, oc) - radius*radius
discriminant := b*b - 4*a*c
if discriminant >= 0 {
return true
} else {
return false
}
}
// =============== MAIN =====================
func main() {
@ -161,7 +181,7 @@ func main() {
// Camera
var focal_length float32 = 1.0
var viewport_height float32 = 2.0
var viewport_width float32 = viewport_height * float32(image_width/image_height)
var viewport_width float32 = viewport_height * float32(image_width)/float32(image_height)
camera_center := NewVec3(0,0,0)
// Calculate the vectors across the horizontal and down the vertical viewport edges