List of hittables added
This commit is contained in:
parent
4bf76c8764
commit
69301d65c6
2 changed files with 47262 additions and 47179 deletions
145
main.go
145
main.go
|
@ -63,6 +63,10 @@ func (v Vec3) Length() float32 {
|
|||
return float32(math.Sqrt(float64(v.E[0]*v.E[0] + v.E[1]*v.E[1] + v.E[2]*v.E[2])))
|
||||
}
|
||||
|
||||
func (v Vec3) Length_squared() float32 {
|
||||
return v.E[0]*v.E[0] + v.E[1]*v.E[1] + v.E[2]*v.E[2]
|
||||
}
|
||||
|
||||
// Vector utility functions
|
||||
|
||||
func (v Vec3) String() string {
|
||||
|
@ -107,6 +111,12 @@ func Unit_vector(v Vec3) Vec3 {
|
|||
return new_v
|
||||
}
|
||||
|
||||
const pi float32 = 3.1415926535897932385
|
||||
|
||||
func Degrees_to_radians(degrees float32) float32 {
|
||||
return (degrees * pi) / 180.0
|
||||
}
|
||||
|
||||
// ============== COLOUR CLASS ==============
|
||||
|
||||
func Write_color(v Vec3) {
|
||||
|
@ -149,32 +159,103 @@ func (r *Ray) At(t float32) Vec3 {
|
|||
}
|
||||
}
|
||||
|
||||
func Ray_color(r *Ray) Vec3 {
|
||||
var t float32 = hit_sphere(NewPoint3(0,0,-1), 0.5, r)
|
||||
if (t > 0.0) {
|
||||
N := Unit_vector(r.At(t).Sub(NewVec3(0,0,-1)))
|
||||
return NewColor(N.X()+1, N.Y()+1, N.Z()+1).Mult(0.5)
|
||||
}
|
||||
|
||||
unit_direction := Unit_vector(r.Direction())
|
||||
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))
|
||||
// =============== HIT ======================
|
||||
|
||||
type Hit_record struct {
|
||||
p Vec3
|
||||
normal Vec3
|
||||
t float32
|
||||
front_face bool
|
||||
}
|
||||
|
||||
func (rec *Hit_record) Set_face_normal(r *Ray, outward_normal Vec3) {
|
||||
if (Dot(r.Direction(), outward_normal) < 0) {
|
||||
rec.front_face = true
|
||||
}
|
||||
if (rec.front_face) {
|
||||
rec.normal = outward_normal
|
||||
} else {
|
||||
rec.normal = outward_normal.Neg()
|
||||
}
|
||||
}
|
||||
|
||||
// =============== SPHERE ===================
|
||||
|
||||
func hit_sphere(center Vec3, radius float32, r *Ray) float32 {
|
||||
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
|
||||
type Sphere struct {
|
||||
center Vec3
|
||||
radius float32
|
||||
}
|
||||
|
||||
func (s Sphere) Hit_sphere(r *Ray, ray_tmin float32, ray_tmax float32, rec *Hit_record) bool {
|
||||
oc := r.Origin().Sub(s.center)
|
||||
a := r.Direction().Length_squared()
|
||||
half_b := Dot(oc, r.Direction())
|
||||
c := oc.Length_squared() - s.radius*s.radius
|
||||
|
||||
discriminant := half_b*half_b - a*c
|
||||
if (discriminant < 0) {
|
||||
return -1.0
|
||||
} else {
|
||||
return (-b-float32(math.Sqrt(float64(discriminant)))) / (2.0*a)
|
||||
return false
|
||||
}
|
||||
sqrtd := float32(math.Sqrt(float64(discriminant)))
|
||||
|
||||
// find the nearest root that lies in the acceptable range
|
||||
root := (-half_b - sqrtd) / a
|
||||
if (root <= ray_tmin || ray_tmax<= root) {
|
||||
root = (sqrtd-half_b) / a
|
||||
if (root <= ray_tmin || ray_tmax <= root) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
rec.t = root
|
||||
rec.p = r.At(rec.t)
|
||||
outward_normal := rec.p.Sub(s.center).Div(s.radius)
|
||||
rec.Set_face_normal(r, outward_normal)
|
||||
return true
|
||||
}
|
||||
|
||||
// =============== HITTABLE ==================
|
||||
type Hittable struct {
|
||||
spheres []Sphere
|
||||
}
|
||||
|
||||
func NewHittable() *Hittable {
|
||||
return &Hittable{}
|
||||
}
|
||||
|
||||
func (hl *Hittable) Add(s Sphere) {
|
||||
hl.spheres = append(hl.spheres, s)
|
||||
}
|
||||
|
||||
func (hl *Hittable) Clear() {
|
||||
hl.spheres = []Sphere{}
|
||||
}
|
||||
|
||||
func (hl Hittable) Hit(r *Ray, ray_tmin float32, ray_tmax float32, rec *Hit_record) bool {
|
||||
var temp_rec Hit_record
|
||||
var hit_anything bool = false
|
||||
var closest_so_far float32 = ray_tmax
|
||||
|
||||
for _, sphere := range hl.spheres {
|
||||
if (sphere.Hit_sphere(r, ray_tmin, closest_so_far, &temp_rec)) {
|
||||
hit_anything = true
|
||||
closest_so_far = temp_rec.t
|
||||
*rec = temp_rec
|
||||
}
|
||||
}
|
||||
return hit_anything
|
||||
}
|
||||
|
||||
func Ray_color(r Ray, world *Hittable) Vec3 {
|
||||
var rec Hit_record
|
||||
if (world.Hit(&r, 0, float32(math.Inf(1)), &rec)) {
|
||||
return rec.normal.Add(NewColor(1,1,1)).Mult(0.5)
|
||||
}
|
||||
unit_direction := Unit_vector(r.Direction())
|
||||
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))
|
||||
|
||||
}
|
||||
|
||||
// =============== MAIN =====================
|
||||
|
@ -189,6 +270,19 @@ func main() {
|
|||
if image_height < 1 {
|
||||
image_height = 1
|
||||
}
|
||||
|
||||
// World
|
||||
|
||||
world := NewHittable()
|
||||
world.Add(Sphere{
|
||||
center: NewVec3(0, 0, -1),
|
||||
radius: 0.5,
|
||||
})
|
||||
world.Add(Sphere{
|
||||
center: NewVec3(0, -100.5, -1),
|
||||
radius: 100,
|
||||
})
|
||||
|
||||
// Camera
|
||||
var focal_length float32 = 1.0
|
||||
var viewport_height float32 = 2.0
|
||||
|
@ -212,22 +306,11 @@ func main() {
|
|||
for j := 0; j < image_height; j++ {
|
||||
fmt.Fprintf(os.Stderr, "\rScanlines remaining: %d ", image_height-j)
|
||||
for i := 0; i < image_width; i++ {
|
||||
// r := float32(i) / float32(image_width - 1)
|
||||
// g := float32(j) / float32(image_height-1)
|
||||
// b := 0
|
||||
|
||||
// ir := int(r * 256)
|
||||
// ig := int(g * 256)
|
||||
// ib := int(b * 256)
|
||||
// fmt.Println(ir, " ", ig, " ", ib)
|
||||
|
||||
//pixel_colour := NewVec3(float32(i) / float32(image_width - 1), float32(j) / float32(image_height-1), 0)
|
||||
//Write_color(pixel_colour)
|
||||
pixel_center := pixel00_loc.Add(pixel_delta_u.Mult(float32(i))).Add(pixel_delta_v.Mult(float32(j)))
|
||||
ray_direction := pixel_center.Sub(camera_center)
|
||||
r := NewRay(camera_center, ray_direction)
|
||||
|
||||
pixel_color := Ray_color(r)
|
||||
pixel_color := Ray_color(*r, world)
|
||||
Write_color(pixel_color)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue