Skip to content

Commit 97429c0

Browse files
committed
practice mode
1 parent 8433219 commit 97429c0

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module example.com/raytracing
1+
module github.com/bhaney/raytracing
22

33
go 1.16
44

raytracing.go

+27-25
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package main
22

33
import (
44
"fmt"
5-
"io"
65
"math"
76
"math/rand"
87
"os"
98

109
"github.com/bhaney/raytracing/camera"
1110
"github.com/bhaney/raytracing/env"
1211
"github.com/bhaney/raytracing/ray"
12+
"github.com/bhaney/raytracing/utils"
1313
"github.com/golang/geo/r3"
1414
)
1515

@@ -19,13 +19,13 @@ func ColorRay(r *ray.Ray, world env.Hittable, depth int) ray.Color {
1919
rec := new(env.HitRecord)
2020
// If we've exceed the ray bounce limit, no more light is gathered
2121
if depth <= 0 {
22-
return Color{0, 0, 0}
22+
return ray.Color{0, 0, 0}
2323
}
2424
if world.Hit(r, 0.001, math.Inf(1), rec) { // Remember, Go is pass-by-value!
2525
scattered := new(ray.Ray)
2626
attenuation := new(ray.Color)
2727
if rec.MatPtr.Scatter(r, scattered, rec, attenuation) {
28-
resultVec := ray.ColorRay(scattered, world, depth-1)
28+
resultVec := ColorRay(scattered, world, depth-1)
2929
return ray.Color{attenuation.X * resultVec.X, attenuation.Y * resultVec.Y, attenuation.Z * resultVec.Z}
3030
}
3131
return ray.Color{0, 0, 0}
@@ -80,7 +80,7 @@ func RandomScene() env.HittableList {
8080
world = append(world, &env.Sphere{center, 0.2, sphereMaterial})
8181
} else if chooseMat < 0.95 {
8282
// metal
83-
albedo := ray.Color(utis.RandomVectorFromRange(0.5, 1.))
83+
albedo := ray.Color(utils.RandomVectorFromRange(0.5, 1.))
8484
fuzz := utils.RandomFromRange(0., 0.5)
8585
sphereMaterial = &env.Metal{albedo, fuzz}
8686
world = append(world, &env.Sphere{center, 0.2, sphereMaterial})
@@ -94,9 +94,9 @@ func RandomScene() env.HittableList {
9494

9595
material1 := env.Dielectric{1.5}
9696
world = append(world, &env.Sphere{r3.Vector{0, 1, 0}, 1.0, &material1})
97-
material2 := env.Lambertian{Color{0.4, 0.2, 0.1}}
97+
material2 := env.Lambertian{ray.Color{0.4, 0.2, 0.1}}
9898
world = append(world, &env.Sphere{r3.Vector{-4, 1, 0}, 1.0, &material2})
99-
material3 := env.Metal{Color{0.7, 0.6, 0.5}, 0.0}
99+
material3 := env.Metal{ray.Color{0.7, 0.6, 0.5}, 0.0}
100100
world = append(world, &env.Sphere{r3.Vector{4, 1, 0}, 1.0, &material3})
101101

102102
return world
@@ -107,46 +107,48 @@ func RandomScene() env.HittableList {
107107

108108
func main() {
109109
// Image
110-
aspectRatio := 3.0 / 2.0
111-
imageWidth := 1200.
112-
imageHeight := imageWidth / aspectRatio
113-
samplesPerPixel := 500
114-
maxDepth := 50
115110
/*
116-
aspectRatio := 16.0 / 9.0
117-
imageWidth := 400.
111+
aspectRatio := 3.0 / 2.0
112+
imageWidth := 1200.
118113
imageHeight := imageWidth / aspectRatio
119-
samplesPerPixel := 100
114+
samplesPerPixel := 500
120115
maxDepth := 50
121116
*/
117+
aspectRatio := 16.0 / 9.0
118+
imageWidth := 400.
119+
imageHeight := imageWidth / aspectRatio
120+
samplesPerPixel := 100
121+
maxDepth := 50
122122

123123
// Camera
124-
vfov := 20.
125-
lookfrom := r3.Vector{13, 2, 3}
126-
lookat := r3.Vector{0, 0, 0}
127-
vup := r3.Vector{0, 1, 0}
128-
distToFocus := 10.0
129-
aperture := 0.1
130124
/*
131-
lookfrom := r3.Vector{3, 3, 2}
132-
lookat := r3.Vector{0, 0, -1}
125+
vfov := 20.
126+
lookfrom := r3.Vector{13, 2, 3}
127+
lookat := r3.Vector{0, 0, 0}
133128
vup := r3.Vector{0, 1, 0}
134-
distToFocus := lookfrom.Sub(lookat).Norm()
129+
distToFocus := 10.0
135130
aperture := 0.1
136131
*/
132+
vfov := 20.
133+
lookfrom := r3.Vector{3, 3, 2}
134+
lookat := r3.Vector{0, 0, -1}
135+
vup := r3.Vector{0, 1, 0}
136+
distToFocus := lookfrom.Sub(lookat).Norm()
137+
aperture := 0.1
137138

138139
cam := camera.NewCamera(aspectRatio, vfov, aperture, distToFocus, lookfrom, lookat, vup)
139140

140141
// World
141-
world := RandomScene()
142+
//world := RandomScene()
143+
world := PracticeScene()
142144
// Render
143145

144146
fmt.Printf("P3\n %d %d\n255\n", int(imageWidth), int(imageHeight))
145147

146148
for j := int(imageHeight) - 1; j >= 0; j-- {
147149
fmt.Fprintf(os.Stderr, "\rScanlines remaining: %d \n", j)
148150
for i := int(imageWidth) - 1; i >= 0; i-- {
149-
c := Color{0, 0, 0}
151+
c := ray.Color{0, 0, 0}
150152
for s := 0; s < samplesPerPixel; s++ {
151153
u := (float64(i) + rand.Float64()) / float64(imageWidth-1)
152154
v := (float64(j) + rand.Float64()) / float64(imageHeight-1)

0 commit comments

Comments
 (0)