aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--camera.go64
-rw-r--r--rtiaw.go45
3 files changed, 69 insertions, 46 deletions
diff --git a/Makefile b/Makefile
index 7bcf3da..7a62fa2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,9 @@
-rtiaw: $(wildcard *.go)
- go build
-
out.ppm : rtiaw
./$< > $@
+rtiaw: $(wildcard *.go)
+ go build
+
.phony: view clean
view: out.ppm
diff --git a/camera.go b/camera.go
new file mode 100644
index 0000000..e460cc0
--- /dev/null
+++ b/camera.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "fmt"
+ "os"
+)
+
+type Camera struct {
+ ImageWidth uint
+ ImageHeight uint
+ Centre Vec3
+ PDU Vec3
+ PDV Vec3
+ Pixel00 Vec3
+}
+
+func NewCamera(w uint, ar float64) (cam Camera) {
+ cam.ImageWidth = w;
+ cam.ImageHeight = uint(float64(cam.ImageWidth) / ar);
+ if cam.ImageHeight < 1 {
+ cam.ImageHeight = 1;
+ }
+
+ focal_length := 1.0;
+ viewport_height := 2.0;
+ viewport_width := viewport_height *
+ float64(cam.ImageWidth) /
+ float64(cam.ImageHeight);
+ cam.Centre = Splat(0);
+
+ viewport_u := Vec3{viewport_width, 0, 0};
+ viewport_v := Vec3{0, -viewport_height, 0};
+
+ cam.PDU = viewport_u.Div(Splat(float64(cam.ImageWidth)));
+ cam.PDV = viewport_v.Div(Splat(float64(cam.ImageHeight)));
+
+ viewport_upperleft := cam.Centre.
+ Sub(Vec3{0, 0, focal_length}).
+ Sub(viewport_u.Div(Splat(2))).
+ Sub(viewport_v.Div(Splat(2)));
+ cam.Pixel00 = viewport_upperleft.
+ Add(cam.PDU.Add(cam.PDV).Div(Splat(2)));
+
+ return;
+}
+
+func (cam Camera) Render(world Hittable) {
+ fmt.Printf("P3\n%d %d\n255\n", cam.ImageWidth, cam.ImageHeight);
+ for row := uint(0); row < cam.ImageHeight; row++ {
+ fmt.Fprintf(os.Stderr,
+ "Scanlines remaining: %3d...",
+ (cam.ImageHeight - row));
+ for col := uint(0); col < cam.ImageWidth; col++ {
+ pixel_centre := cam.Pixel00.
+ Add(cam.PDU.Mul(Splat(float64(col)))).
+ Add(cam.PDV.Mul(Splat(float64(row))));
+ ray_direction := pixel_centre.Sub(cam.Centre);
+ ray := Ray{cam.Centre, ray_direction}
+ pixel_colour := ray.Colour(world);
+ fmt.Printf("%s ", pixel_colour.ToPPM());
+ }
+ fmt.Printf("\n");
+ }
+}
diff --git a/rtiaw.go b/rtiaw.go
index 9f900b6..03cee09 100644
--- a/rtiaw.go
+++ b/rtiaw.go
@@ -9,53 +9,12 @@ import (
func main() {
start := time.Now();
- image_width := 400;
- aspect := 16.0 / 9.0;
-
var world Hittables;
world.Add(Sphere{Vec3{0, 0 , -1}, 0.5});
world.Add(Sphere{Vec3{0, -100.5, -1}, 100 });
- image_height := int(float64(image_width) / aspect);
- if image_height < 1 {
- image_height = 1;
- }
-
- focal_length := 1.0;
- viewport_height := 2.0;
- viewport_width := viewport_height *
- (float64(image_width) / float64(image_height));
- camera_centre := Splat(0);
-
- viewport_u := Vec3{viewport_width, 0, 0};
- viewport_v := Vec3{0, -viewport_height, 0};
-
- pd_u := viewport_u.Div(Splat(float64(image_width)));
- pd_v := viewport_v.Div(Splat(float64(image_height)));
-
- viewport_upperleft := camera_centre.
- Sub(Vec3{0, 0, focal_length}).
- Sub(viewport_u.Div(Splat(2))).
- Sub(viewport_v.Div(Splat(2)));
- pixel00_loc := viewport_upperleft.
- Add(pd_u.Add(pd_v).Div(Splat(2)));
-
- fmt.Printf("P3\n%d %d\n255\n", image_width, image_height);
- for row := 0; row < image_height; row++ {
- fmt.Fprintf(os.Stderr,
- "Scanlines remaining: %3d...",
- (image_height - row));
- for col := 0; col < image_width; col++ {
- pixel_centre := pixel00_loc.
- Add(pd_u.Mul(Splat(float64(col)))).
- Add(pd_v.Mul(Splat(float64(row))));
- ray_direction := pixel_centre.Sub(camera_centre);
- ray := Ray{camera_centre, ray_direction}
- pixel_colour := ray.Colour(world);
- fmt.Printf("%s ", pixel_colour.ToPPM());
- }
- fmt.Printf("\n");
- }
+ cam := NewCamera(400, 16.0/9.0);
+ cam.Render(world);
dur := time.Since(start);
fmt.Fprintf(os.Stderr,