2024-01-25 19:41:11 +00:00
|
|
|
package main
|
|
|
|
|
2024-01-29 16:25:29 +00:00
|
|
|
import "fmt" // for IO and standard library
|
|
|
|
import "os" // for handling the progress bar
|
|
|
|
import "math" // for maths
|
2024-02-12 12:05:10 +00:00
|
|
|
import "unsafe"
|
2024-01-25 19:41:11 +00:00
|
|
|
|
2024-01-28 13:32:04 +00:00
|
|
|
// ================ VEC3 CLASS =====================
|
|
|
|
|
|
|
|
type Vec3 struct {
|
|
|
|
E [3]float32
|
|
|
|
}
|
|
|
|
|
|
|
|
//Basic vector functions
|
|
|
|
|
|
|
|
func NewVec3(e0, e1, e2 float32) Vec3 {
|
|
|
|
return Vec3{E: [3]float32{e0, e1, e2}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vec3) X() float32 {
|
|
|
|
return v.E[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// can be executed as v.X() in main
|
|
|
|
|
|
|
|
func (v Vec3) Y() float32 {
|
|
|
|
return v.E[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vec3) Z() float32 {
|
|
|
|
return v.E[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vec3) Neg() Vec3 {
|
|
|
|
return NewVec3(-v.E[0], -v.E[1], -v.E[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vec3) Get(i int) float32 {
|
|
|
|
return v.E[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Vec3) Set(i int, val float32) {
|
|
|
|
v.E[i] = val
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (v Vec3) Add(v2 Vec3) Vec3 {
|
|
|
|
return NewVec3(v.E[0]+v2.E[0], v.E[1]+v2.E[1], v.E[2]+v2.E[2])
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (v Vec3) Mult(t float32) Vec3 {
|
|
|
|
return NewVec3(v.E[0]*t, v.E[1]*t, v.E[2]*t)
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (v Vec3) Div(t float32) Vec3 {
|
2024-01-28 13:32:04 +00:00
|
|
|
if t != 0 {
|
2024-02-03 21:48:01 +00:00
|
|
|
return NewVec3(v.E[0] / t, v.E[1] / t, v.E[2] / t)
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
2024-02-03 21:48:01 +00:00
|
|
|
return v
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
|
2024-01-28 13:32:04 +00:00
|
|
|
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])))
|
|
|
|
}
|
|
|
|
|
2024-02-27 13:01:12 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2024-01-28 13:32:04 +00:00
|
|
|
// Vector utility functions
|
|
|
|
|
|
|
|
func (v Vec3) String() string {
|
|
|
|
return fmt.Sprintf("%v %v %v", v.E[0], v.E[1], v.E[2])
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (v Vec3) Sub(v2 Vec3) Vec3 {
|
|
|
|
return NewVec3(v.E[0]-v2.E[0], v.E[1]-v2.E[1], v.E[2]-v2.E[2])
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (v Vec3) MultVec(v2 Vec3) Vec3 {
|
|
|
|
return NewVec3(v.E[0]*v2.E[0], v.E[1]*v2.E[1], v.E[2]*v2.E[2])
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (v Vec3) DivVec(v2 Vec3) Vec3 {
|
|
|
|
if v2.E[0] != 0 && v2.E[1] != 0 && v2.E[2] != 0 {
|
|
|
|
return NewVec3(v.E[0]/v2.E[0], v.E[1]/v2.E[1], v.E[2]/v2.E[2])
|
|
|
|
}
|
|
|
|
return v
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Dot(v1 Vec3, v2 Vec3) float32 {
|
|
|
|
return (v1.E[0]*v2.E[0] + v1.E[1]*v2.E[1] + v1.E[2]*v2.E[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
func Cross(v1 Vec3, v2 Vec3) Vec3 {
|
|
|
|
return NewVec3(v1.E[1]*v2.E[2] - v1.E[2]*v2.E[1],
|
|
|
|
v1.E[2]*v2.E[0] - v1.E[0]*v2.E[2],
|
|
|
|
v1.E[0]*v2.E[1] - v1.E[1]*v2.E[0])
|
|
|
|
}
|
|
|
|
|
2024-02-12 12:05:10 +00:00
|
|
|
func q_rsqrt(v Vec3) float32 {
|
|
|
|
var x float32 = v.E[0]*v.E[0] + v.E[1]*v.E[1] + v.E[2]*v.E[2]
|
|
|
|
i := *(*int32)(unsafe.Pointer(&x)) // evil floating point bit level hacking
|
|
|
|
i = 0x5f3759df - (i >> 1) // what the fuck?
|
|
|
|
y := *(*float32)(unsafe.Pointer(&i))
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2024-01-28 13:32:04 +00:00
|
|
|
func Unit_vector(v Vec3) Vec3 {
|
2024-02-12 12:05:10 +00:00
|
|
|
new_v := v.Mult(q_rsqrt(v))
|
2024-02-10 17:34:15 +00:00
|
|
|
return new_v
|
2024-01-28 13:32:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 13:01:12 +00:00
|
|
|
const pi float32 = 3.1415926535897932385
|
|
|
|
|
|
|
|
func Degrees_to_radians(degrees float32) float32 {
|
|
|
|
return (degrees * pi) / 180.0
|
|
|
|
}
|
|
|
|
|
2024-01-28 13:32:04 +00:00
|
|
|
// ============== COLOUR CLASS ==============
|
|
|
|
|
|
|
|
func Write_color(v Vec3) {
|
|
|
|
fmt.Println(int(255.999*v.E[0]), int(255.999*v.E[1]), int(255.999*v.E[2]))
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func NewColor(e0, e1, e2 float32) Vec3 {
|
|
|
|
return Vec3{E: [3]float32{e0, e1, e2}}
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:25:29 +00:00
|
|
|
// ============== RAY CLASS =================
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func NewPoint3(e0, e1, e2 float32) Vec3 {
|
|
|
|
return Vec3{E: [3]float32{e0, e1, e2}}
|
|
|
|
}
|
2024-01-29 16:25:29 +00:00
|
|
|
type Ray struct {
|
2024-02-03 21:48:01 +00:00
|
|
|
Orig Vec3
|
2024-01-29 16:25:29 +00:00
|
|
|
Dir Vec3
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func NewRay(orig Vec3, dir Vec3) *Ray {
|
2024-01-29 16:25:29 +00:00
|
|
|
return &Ray{Orig: orig, Dir: dir}
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (r *Ray) Origin() Vec3 {
|
2024-01-29 16:25:29 +00:00
|
|
|
return r.Orig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Ray) Direction() Vec3 {
|
|
|
|
return r.Dir
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:48:01 +00:00
|
|
|
func (r *Ray) At(t float32) Vec3 {
|
|
|
|
return Vec3{
|
|
|
|
E: [3]float32{
|
|
|
|
r.Orig.X() + t*r.Dir.X(),
|
|
|
|
r.Orig.Y() + t*r.Dir.Y(),
|
|
|
|
r.Orig.Z() + t*r.Dir.Z(),
|
|
|
|
},
|
2024-01-29 16:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:01:28 +00:00
|
|
|
// =============== INTERVAL =================
|
|
|
|
|
|
|
|
type Interval struct {
|
|
|
|
min, max float32
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInterval(borders ...float32) *Interval {
|
|
|
|
if (len(borders) == 2) {
|
|
|
|
return &Interval{
|
|
|
|
min: borders[0],
|
|
|
|
max: borders[1],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return &Interval{
|
|
|
|
min: float32(math.Inf(1)),
|
|
|
|
max: float32(math.Inf(-1)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interval) Contains(x float32) bool {
|
|
|
|
return i.min <= x && x <= i.max
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interval) Surrounds(x float32) bool {
|
|
|
|
return i.min < x && x < i.max
|
|
|
|
}
|
|
|
|
|
|
|
|
var Empty *Interval = NewInterval()
|
|
|
|
var Universe *Interval = NewInterval(float32(math.Inf(-1)), float32(math.Inf(1)))
|
|
|
|
|
2024-02-10 17:34:15 +00:00
|
|
|
|
2024-02-27 13:01:12 +00:00
|
|
|
// =============== 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()
|
|
|
|
}
|
2024-02-03 21:48:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-10 17:34:15 +00:00
|
|
|
// =============== SPHERE ===================
|
|
|
|
|
2024-02-27 13:01:12 +00:00
|
|
|
type Sphere struct {
|
|
|
|
center Vec3
|
|
|
|
radius float32
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:01:28 +00:00
|
|
|
func (s Sphere) Hit_sphere(r *Ray, ray_t *Interval, rec *Hit_record) bool {
|
2024-02-27 13:01:12 +00:00
|
|
|
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
|
2024-02-12 12:05:10 +00:00
|
|
|
if (discriminant < 0) {
|
2024-02-27 13:01:12 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
sqrtd := float32(math.Sqrt(float64(discriminant)))
|
|
|
|
|
|
|
|
// find the nearest root that lies in the acceptable range
|
|
|
|
root := (-half_b - sqrtd) / a
|
2024-02-27 18:01:28 +00:00
|
|
|
if (!ray_t.Surrounds(root)) {
|
2024-02-27 13:01:12 +00:00
|
|
|
root = (sqrtd-half_b) / a
|
2024-02-27 18:01:28 +00:00
|
|
|
if (!ray_t.Surrounds(root)) {
|
2024-02-27 13:01:12 +00:00
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:01:28 +00:00
|
|
|
func (hl Hittable) Hit(r *Ray, ray_t Interval, rec *Hit_record) bool {
|
2024-02-27 13:01:12 +00:00
|
|
|
var temp_rec Hit_record
|
|
|
|
var hit_anything bool = false
|
2024-02-27 18:01:28 +00:00
|
|
|
var closest_so_far float32 = ray_t.max
|
2024-02-27 13:01:12 +00:00
|
|
|
|
|
|
|
for _, sphere := range hl.spheres {
|
2024-02-27 18:01:28 +00:00
|
|
|
if (sphere.Hit_sphere(r, NewInterval(ray_t.min, closest_so_far), &temp_rec)) {
|
2024-02-27 13:01:12 +00:00
|
|
|
hit_anything = true
|
|
|
|
closest_so_far = temp_rec.t
|
|
|
|
*rec = temp_rec
|
|
|
|
}
|
2024-02-10 17:34:15 +00:00
|
|
|
}
|
2024-02-27 13:01:12 +00:00
|
|
|
return hit_anything
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:04:26 +00:00
|
|
|
// =============== CAMERA ===================
|
|
|
|
|
|
|
|
type Camera struct {
|
|
|
|
aspect_ratio float32
|
|
|
|
image_width int
|
|
|
|
image_height int
|
|
|
|
center Vec3
|
|
|
|
pixel00_loc Vec3
|
|
|
|
pixel_delta_u Vec3
|
|
|
|
pixel_delta_v Vec3
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCamera() *Camera {
|
|
|
|
return &Camera{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Ray_color(r Ray, world *Hittable) Vec3 {
|
2024-02-27 13:01:12 +00:00
|
|
|
var rec Hit_record
|
2024-02-27 18:01:28 +00:00
|
|
|
if (world.Hit(&r, *NewInterval(0, float32(math.Inf(1))), &rec)) {
|
2024-02-27 13:01:12 +00:00
|
|
|
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))
|
2024-02-27 19:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cam *Camera) Initialize() {
|
|
|
|
// Calculate the image height
|
|
|
|
cam.image_height = int(float32(cam.image_width) / cam.aspect_ratio)
|
|
|
|
if cam.image_height < 1 {
|
|
|
|
cam.image_height = 1
|
|
|
|
}
|
|
|
|
// Viewport
|
|
|
|
var focal_length float32 = 1.0
|
|
|
|
var viewport_height float32 = 2.0
|
|
|
|
var viewport_width float32 = viewport_height * float32(cam.image_width)/float32(cam.image_height)
|
|
|
|
cam.center = NewVec3(0,0,0)
|
|
|
|
|
|
|
|
// Calculate the vectors across the horizontal and down the vertical viewport edges
|
|
|
|
viewport_u := NewVec3(viewport_width, 0, 0)
|
|
|
|
viewport_v := NewVec3(0, -viewport_height, 0)
|
2024-02-27 13:01:12 +00:00
|
|
|
|
2024-02-27 19:04:26 +00:00
|
|
|
//Calculate the horizontal and vertical delta vectors from pixel to pixel
|
|
|
|
cam.pixel_delta_u = viewport_u.Div(float32(cam.image_width))
|
|
|
|
cam.pixel_delta_v = viewport_v.Div(float32(cam.image_height))
|
|
|
|
|
|
|
|
// Calculate the location of the upper left pixel
|
|
|
|
viewport_upper_left := cam.center.Sub(cam.center).Sub(NewVec3(0, 0, focal_length)).Sub(viewport_u.Div(2)).Sub(viewport_v.Div(2))
|
|
|
|
cam.pixel00_loc = viewport_upper_left.Add((cam.pixel_delta_u.Add(cam.pixel_delta_v)).Div(2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cam *Camera) Render(world *Hittable) {
|
|
|
|
cam.Initialize()
|
|
|
|
|
|
|
|
// Rendering
|
|
|
|
fmt.Println("P3")
|
|
|
|
fmt.Println(cam.image_width, " ", cam.image_height, "\n255")
|
|
|
|
for j := 0; j < cam.image_height; j++ {
|
|
|
|
fmt.Fprintf(os.Stderr, "\rScanlines remaining: %d ", cam.image_height-j)
|
|
|
|
for i := 0; i < cam.image_width; i++ {
|
|
|
|
pixel_center := cam.pixel00_loc.Add(cam.pixel_delta_u.Mult(float32(i))).Add(cam.pixel_delta_v.Mult(float32(j)))
|
|
|
|
ray_direction := pixel_center.Sub(cam.center)
|
|
|
|
r := NewRay(cam.center, ray_direction)
|
|
|
|
|
|
|
|
pixel_color := Ray_color(*r, world)
|
|
|
|
Write_color(pixel_color)
|
|
|
|
}
|
|
|
|
}
|
2024-02-10 17:34:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 19:04:26 +00:00
|
|
|
|
|
|
|
|
2024-01-28 13:32:04 +00:00
|
|
|
// =============== MAIN =====================
|
2024-01-25 19:41:11 +00:00
|
|
|
func main() {
|
2024-02-27 19:04:26 +00:00
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
// Image
|
|
|
|
|
|
|
|
cam := NewCamera()
|
|
|
|
cam.aspect_ratio = 16.0 / 9.0
|
|
|
|
cam.image_width = 400
|
2024-02-27 13:01:12 +00:00
|
|
|
|
2024-02-27 19:04:26 +00:00
|
|
|
cam.Render(world)
|
|
|
|
// INFO: The pixels are written out in rows.
|
|
|
|
// Image file can be created with
|
|
|
|
// go run main.go > image.ppm
|
2024-01-25 19:41:11 +00:00
|
|
|
}
|