-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (136 loc) · 3.65 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/ironsmile/raytracer/engine"
"github.com/ironsmile/raytracer/film"
"github.com/ironsmile/raytracer/sampler"
"github.com/ironsmile/raytracer/scene"
)
func init() {
// This is needed to arrange that main() runs on main thread.
// GLFW requires this.
runtime.LockOSThread()
}
var (
cpuprofile = flag.String("cpuprofile", "",
"write cpu profile to file")
memprofile = flag.String("memprofile", "",
"write memory profile to this file")
filename = flag.String("filename", "",
"output the image to a PNG file instead of showing it to the screen")
interactive = flag.Bool("interactive", false,
"starts the renderer in interactive mode")
vsync = flag.Bool("vsync", true,
"control vsync for interactive renderer")
showFPS = flag.Bool("show-fps", true,
"continuously print the OpenGL FPS stats in the console")
fullscreen = flag.Bool("fullscreen", false,
"run fullscreen in native resolution")
renderWidth = flag.Int("w", 1024,
"image or window width in pixels")
renderHeight = flag.Int("h", 768,
"image or window height in pixels")
fpsCap = flag.Uint("fps-cap", 30,
"maximum number of frames per second. Zero means no FPS cap.")
showBBoxes = flag.Bool("show-bboxes", false,
"show bounding boxes around objects")
withVulkan = flag.Bool("vulkan", false,
"use Vulkan instead of OpenGL")
sceneName = flag.String("scene", "teapot",
"scene to render. Possible values: teapot, car")
debugMode = flag.Bool("D", false,
"debug mode, will print diagnostics information")
)
func main() {
flag.Parse()
if *sceneName != "car" && *sceneName != "teapot" {
log.Fatalf("scenem ust be either `car` or `teapot`")
}
go func() {
log.Println(http.ListenAndServe("localhost:6464", nil))
}()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *filename != "" {
infileRenderer()
} else if *withVulkan {
vulkanWindowRenderer()
} else {
openglWindowRenderer()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}
}
func infileRenderer() {
output := film.NewImage(*filename)
if err := output.Init(*renderWidth, *renderHeight); err != nil {
log.Fatalf("%s\n", err)
}
smpl := sampler.NewSimple(output.Width(), output.Height(), output)
cam := scene.GetCamera(float64(output.Width()), float64(output.Height()))
tracer := engine.New(smpl)
tracer.SetTarget(output, cam)
tracer.Scene.InitScene(*sceneName)
tracer.ShowBBoxes = *showBBoxes
renderTimer := time.Now()
tracer.Render()
fmt.Printf("Rendering finished: %s\n", time.Since(renderTimer))
smpl.Stop()
output.Wait()
}
func openglWindowRenderer() {
args := film.GlWinArgs{
Fullscreen: *fullscreen,
VSync: *vsync,
Width: *renderWidth,
Height: *renderHeight,
Interactive: *interactive,
ShowBBoxes: *showBBoxes,
FPSCap: *fpsCap,
ShowFPS: *showFPS,
SceneName: *sceneName,
}
glWin := film.NewGlWIndow(args)
if err := glWin.Run(); err != nil {
log.Fatalf("Error running GL window: %s", err)
}
}
func vulkanWindowRenderer() {
args := film.VulkanAppArgs{
Debug: *debugMode,
Fullscreen: *fullscreen,
VSync: *vsync,
Width: *renderWidth,
Height: *renderHeight,
Interactive: *interactive,
ShowBBoxes: *showBBoxes,
FPSCap: *fpsCap,
ShowFPS: *showFPS,
SceneName: *sceneName,
}
app := film.NewVulkanWindow(args)
if err := app.Run(); err != nil {
log.Fatalf("error running: %s", err)
}
}