aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Lehoux <martin@lehoux.net>2020-04-05 23:34:05 -0700
committerMartin Lehoux <martin@lehoux.net>2020-04-05 23:34:05 -0700
commit4c823f57959a5d821a9f00d8e8c7f57c08ac2ded (patch)
tree483ba7af5cbcd051cf9835e7cb4821e6d8cbd2f4
parent09126c915e0f410fddfee14239589fad250ea712 (diff)
feat - Camera VFOV
-rw-r--r--camera.go14
-rw-r--r--main.go37
-rw-r--r--utils.go6
3 files changed, 41 insertions, 16 deletions
diff --git a/camera.go b/camera.go
index 6cc3391..2992b02 100644
--- a/camera.go
+++ b/camera.go
@@ -1,11 +1,25 @@
package main
+import "math"
+
// Camera handle view position and screen
type Camera struct {
lowerLeftCorner Vec3D
origin, horizontal, vertical Vec3D
}
+// NewCamera creates a new camera
+func NewCamera(vfov float64, aspect float64) Camera {
+ origin := Vec3D{0.0, 0.0, 0.0}
+ theta := DegreesToRadians(vfov)
+ halfHeight := math.Tan(theta / 2.0)
+ halfWidth := aspect * halfHeight
+ lowerLeftCorner := Vec3D{-halfWidth, -halfHeight, -1.0}
+ horizontal := Vec3D{2.0 * halfWidth, 0.0, 0.0}
+ vertical := Vec3D{0.0, 2.0 * halfHeight, 0.0}
+ return Camera{lowerLeftCorner, origin, horizontal, vertical}
+}
+
// GetRay computes Ray given screen params
func (camera Camera) GetRay(u float64, v float64) Ray {
return Ray{
diff --git a/main.go b/main.go
index 7042d21..957fc92 100644
--- a/main.go
+++ b/main.go
@@ -2,6 +2,7 @@
package main
import (
+ "math"
"math/rand"
"github.com/schollz/progressbar"
@@ -26,24 +27,28 @@ const (
func main() {
// Scene
- lowerLeftCorner := Vec3D{-2.0, -1.0, -1.0}
- origin := Vec3D{0.0, 0.0, 0.0}
- horizontal := Vec3D{4.0, 0.0, 0.0}
- vertical := Vec3D{0.0, 2.0, 0.0}
- camera := Camera{lowerLeftCorner, origin, horizontal, vertical}
+ camera := NewCamera(90.0, float64(Width)/float64(Height))
- matRed := Lambertian{Color{0.7, 0.3, 0.3}}
- matGreen := Lambertian{Color{0.8, 0.8, 0.0}}
- metalGrey := Metal{Color{0.8, 0.8, 0.8}, 0.3}
+ r := math.Cos(math.Pi / 4)
+ blue := Lambertian{Color{0.0, 0.0, 1.0}}
+ red := Lambertian{Color{1.0, 0.0, 0.0}}
+ World := HitableList{
+ Sphere{Vec3D{-r, 0.0, -1.0}, r, blue},
+ Sphere{Vec3D{r, 0.0, -1.0}, r, red},
+ }
+
+ // matRed := Lambertian{Color{0.7, 0.3, 0.3}}
+ // matGreen := Lambertian{Color{0.8, 0.8, 0.0}}
+ // metalGrey := Metal{Color{0.8, 0.8, 0.8}, 0.3}
// metalBrown := Metal{Color{0.8, 0.6, 0.2}, 1.0}
- glass := Dielectric{1.5}
+ // glass := Dielectric{1.5}
- WORLD := HitableList{
- Sphere{Vec3D{0.0, 0.0, -1.0}, 0.5, glass},
- Sphere{Vec3D{-1.0, 0.0, -1.0}, 0.5, metalGrey},
- Sphere{Vec3D{1.0, 0.0, -1.0}, 0.5, matRed},
- Sphere{Vec3D{0.0, -100.5, -1.0}, 100.0, matGreen},
- }
+ // World := HitableList{
+ // Sphere{Vec3D{0.0, 0.0, -1.0}, 0.5, glass},
+ // Sphere{Vec3D{-1.0, 0.0, -1.0}, 0.5, metalGrey},
+ // Sphere{Vec3D{1.0, 0.0, -1.0}, 0.5, matRed},
+ // Sphere{Vec3D{0.0, -100.5, -1.0}, 100.0, matGreen},
+ // }
// Program
// start := AddVec3D(Origin, Vec3D{-1.0, 0.0, 0.0})
@@ -61,7 +66,7 @@ func main() {
u := (float64(y) + rand.Float64()) / float64(Width)
v := (float64(x) + rand.Float64()) / float64(Height)
ray := camera.GetRay(u, v)
- colorList[it] = RayColor(ray, WORLD, 0)
+ colorList[it] = RayColor(ray, World, 0)
}
color := ColorMeanSquare(colorList)
frame[Height-1-x][y] = color.ToPixel()
diff --git a/utils.go b/utils.go
index ca40e87..a0d0e7c 100644
--- a/utils.go
+++ b/utils.go
@@ -1,5 +1,7 @@
package main
+import "math"
+
// FMin return mins of floats
func FMin(a float64, b float64) float64 {
if a >= b {
@@ -7,3 +9,7 @@ func FMin(a float64, b float64) float64 {
}
return b
}
+
+func DegreesToRadians(degrees float64) float64 {
+ return degrees * 2.0 * math.Pi / 360.0
+}