/
main.go
53 lines (45 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"flag"
"fmt"
"image/png"
"log"
"os"
"time"
"github.com/benfrisbie/raytracer/camera"
"github.com/benfrisbie/raytracer/scene"
)
func main() {
var width int
var height int
var msaa int
var fov float64
var output string
flag.IntVar(&width, "width", 1920, "width of the image")
flag.IntVar(&height, "height", 1080, "height of the image")
flag.Float64Var(&fov, "fov", 90, "field of view of the camera")
flag.IntVar(&msaa, "msaa", 4, "multisample anti-alias sets how many samples/rays per pixel should be used. increasing this improves quality around edges at the expense of performance")
flag.StringVar(&output, "output", "test.png", "name of the output png file")
flag.Parse()
// Setup camera
fmt.Println("Setting up camera...")
cam := camera.NewCamera(width, height, msaa, fov)
// Setup scene
fmt.Println("Setting up scene...")
sce := scene.NewScene()
// Have the camera render the scene
fmt.Println(fmt.Sprintf("Rendering image to '%v'...", output))
startTime := time.Now()
data := cam.RenderSceneToImage(sce)
// Open the output file and write data to it
img, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer img.Close()
png.Encode(img, data)
// Print off render time
renderTime := time.Since(startTime).Seconds()
fmt.Println("Render complete")
fmt.Println(fmt.Sprintf("Elapsed time: %v seconds", renderTime))
}