Final chapter done
This commit is contained in:
parent
1f8d6adae3
commit
81ca532446
2 changed files with 85362 additions and 85330 deletions
46
main.go
46
main.go
|
@ -124,6 +124,16 @@ func Unit_vector(v Vec3) Vec3 {
|
|||
return new_v
|
||||
}
|
||||
|
||||
func Random_in_unit_disk() Vec3 {
|
||||
for true {
|
||||
p := NewVec3(RandomDoubleInRange(-1,1), RandomDoubleInRange(-1,1), 0)
|
||||
if p.Length_squared() < 1 {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return NewVec3(0,0,0)
|
||||
}
|
||||
|
||||
func RandomInUnitSphere() Vec3 {
|
||||
for true {
|
||||
p := RandomVec3(-1, 1)
|
||||
|
@ -469,6 +479,10 @@ type Camera struct {
|
|||
u Vec3
|
||||
v Vec3
|
||||
w Vec3
|
||||
defocus_angle float32
|
||||
focus_dist float32
|
||||
defocus_disk_u Vec3
|
||||
defocus_disk_v Vec3
|
||||
}
|
||||
|
||||
func NewCamera() *Camera {
|
||||
|
@ -504,10 +518,9 @@ func (cam *Camera) Initialize() {
|
|||
|
||||
|
||||
// Viewport
|
||||
var focal_length float32 = (cam.lookfrom.Sub(cam.lookat)).Length()
|
||||
theta := Degrees_to_radians(cam.vfov)
|
||||
h := float32(math.Tan(theta/2.0))
|
||||
var viewport_height float32 = 2.0 * h * focal_length
|
||||
var viewport_height float32 = 2.0 * h * cam.focus_dist
|
||||
var viewport_width float32 = viewport_height * float32(cam.image_width)/float32(cam.image_height)
|
||||
|
||||
// Calculate the u,v,w unit basis vectors for the camera coordinate frame
|
||||
|
@ -524,8 +537,13 @@ func (cam *Camera) Initialize() {
|
|||
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.w.Mult(focal_length)).Sub(viewport_u.Div(2)).Sub(viewport_v.Div(2))
|
||||
viewport_upper_left := cam.center.Sub(cam.w.Mult(cam.focus_dist)).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))
|
||||
|
||||
// Calculate the camera defocus disk basis vectors
|
||||
defocus_radius := cam.focus_dist*float32(math.Tan(Degrees_to_radians(cam.defocus_angle / 2)))
|
||||
cam.defocus_disk_u = cam.u.Mult(defocus_radius)
|
||||
cam.defocus_disk_v = cam.v.Mult(defocus_radius)
|
||||
}
|
||||
|
||||
func (cam *Camera) Render(world *Hittable) {
|
||||
|
@ -547,13 +565,26 @@ func (cam *Camera) Render(world *Hittable) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func (cam *Camera) Defocus_disk_sample() Vec3 {
|
||||
// Returns a random point in the camera defocus disk
|
||||
p := Random_in_unit_disk()
|
||||
return cam.center.Add(cam.defocus_disk_u.Mult(p.E[0])).Add(cam.defocus_disk_v.Mult(p.E[1]))
|
||||
}
|
||||
|
||||
func (cam *Camera) GetRay(i, j int) *Ray {
|
||||
// Get a randomly sampled camera ray for the pixel at location i,j
|
||||
// Get a randomly-sampled camera ray for the pixel at location i,j originating from the camera defocus disk
|
||||
pixel_center := cam.pixel00_loc.Add(cam.pixel_delta_u.Mult(float32(i))).Add(cam.pixel_delta_v.Mult(float32(j)))
|
||||
pixel_sample := pixel_center.Add(cam.Pixel_sample_square())
|
||||
|
||||
ray_direction := pixel_sample.Sub(cam.center)
|
||||
return NewRay(cam.center, ray_direction)
|
||||
var ray_origin Vec3
|
||||
if cam.defocus_angle <= 0 {
|
||||
ray_origin = cam.center
|
||||
} else {
|
||||
ray_origin = cam.Defocus_disk_sample()
|
||||
}
|
||||
ray_direction := pixel_sample.Sub(ray_origin)
|
||||
return NewRay(ray_origin , ray_direction)
|
||||
}
|
||||
|
||||
func (cam *Camera) Pixel_sample_square() Vec3 {
|
||||
|
@ -564,7 +595,6 @@ func (cam *Camera) Pixel_sample_square() Vec3 {
|
|||
}
|
||||
|
||||
|
||||
|
||||
// =============== MAIN =====================
|
||||
func main() {
|
||||
|
||||
|
@ -613,6 +643,8 @@ func main() {
|
|||
cam.lookfrom = NewVec3(-2,2,1)
|
||||
cam.lookat = NewVec3(0,0,-1)
|
||||
cam.vup = NewVec3(0,1,0)
|
||||
cam.defocus_angle = 10.0
|
||||
cam.focus_dist = 3.4
|
||||
cam.Render(world)
|
||||
// INFO: The pixels are written out in rows.
|
||||
// Image file can be created with
|
||||
|
|
Loading…
Reference in a new issue