-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (128 loc) · 5.28 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
. "github.com/tiegz/raytracer-go/examples"
)
var examples map[string]func(bool, int) = map[string]func(bool, int){
// Examples from the Raytracer Challenge
"World": func(printProgress bool, jobs int) { RunDrawWorld(printProgress, jobs) },
"WorldWithPlane": func(printProgress bool, jobs int) { RunDrawWorldWithPlane(printProgress, jobs) },
"WorldWithPatterns": func(printProgress bool, jobs int) { RunDrawWorldWithPatterns(printProgress, jobs) },
"WorldWithMultiplePatterns": func(printProgress bool, jobs int) { RunDrawWorldWithMultiplePatterns(printProgress, jobs) },
"WorldWithCube": func(printProgress bool, jobs int) { RunDrawWorldWithCube(printProgress, jobs) },
"WorldWithTable": func(printProgress bool, jobs int) { RunDrawWorldWithTable(printProgress, jobs) },
"WorldWithCylinderAndCone": func(printProgress bool, jobs int) { RunDrawWorldWithCylinderAndCone(printProgress, jobs) },
"WorldWithHexagonGroup": func(printProgress bool, jobs int) { RunDrawWorldWithHexagonGroup(printProgress, jobs) },
"WorldWithTriangles": func(printProgress bool, jobs int) { RunDrawWorldWithTriangles(printProgress, jobs) },
"WorldWithTeapot": func(printProgress bool, jobs int) { RunDrawWorldWithTeapot(printProgress, jobs) },
"WorldWithDice": func(printProgress bool, jobs int) { RunDrawWorldWithDice(printProgress, jobs) },
"WorldWithCubeOfSpheres": func(printProgress bool, jobs int) { RunDrawWorldWithCubeOfSpheres(printProgress, jobs) },
"WorldWithSphereAndAreaLight": func(printProgress bool, jobs int) { RunDrawWorldWithSphereAndAreaLight(printProgress, jobs) },
"WorldWithSnowman": func(printProgress bool, jobs int) { RunDrawWorldWithSnowman(printProgress, jobs) },
"WorldWithUVPattern": func(printProgress bool, jobs int) { RunDrawWorldWithUVPattern(printProgress, jobs) },
"UVAlignCheck": func(printProgress bool, jobs int) { RunDrawUVAlignCheck(printProgress, jobs) },
"UVAlignCheckCubes": func(printProgress bool, jobs int) { RunDrawUVAlignCheckCubes(printProgress, jobs) },
"UVImage": func(printProgress bool, jobs int) { RunDrawUVImage(printProgress, jobs) },
"Skybox": func(printProgress bool, jobs int) { RunDrawSkybox(printProgress, jobs) },
"Cover": func(printProgress bool, jobs int) { RunDrawCover(printProgress, jobs) },
// Other examples
"Animation": func(printProgress bool, jobs int) { RunAnimation(printProgress, jobs) },
}
func main() {
// Root command
cmd := flag.NewFlagSet("raytracer", flag.ExitOnError)
// --> example sub-command
exampleCmd := flag.NewFlagSet("example", flag.ExitOnError)
exampleNamePtr := exampleCmd.String("name", "", "Example name.")
exampleListPtr := exampleCmd.Bool("list", false, "List examples.")
examplePrintProgressPtr := exampleCmd.Bool("progress", true, "Write progress to stdout.")
exampleJobsPtr := exampleCmd.Int("jobs", 1, "Run n jobs in parallel.")
// --> version sub-command
versionCmd := flag.NewFlagSet("version", flag.ExitOnError)
// --> help sub-command
helpCmd := flag.NewFlagSet("help", flag.ExitOnError)
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
switch os.Args[1] {
case "example":
exampleCmd.Parse(os.Args[2:])
case "version":
versionCmd.Parse(os.Args[2:])
case "help":
helpCmd.Parse(os.Args[2:])
default:
cmd.Parse(os.Args[1:])
printUsage()
os.Exit(1)
}
if exampleCmd.Parsed() {
if *exampleListPtr == true {
fmt.Println("Listing examples: ")
for k, _ := range examples {
fmt.Printf("\t%s\n", k)
}
} else if len(*exampleNamePtr) > 0 {
name := *exampleNamePtr
fmt.Printf("Rendering example: %s\n", name)
if f, ok := examples[name]; ok {
f(*examplePrintProgressPtr, *exampleJobsPtr)
} else {
fmt.Printf("Example %s not found!\nRun 'raytracer-go example -list' to see examples.\n", name)
}
} else {
printUsageForSubcommand("example", cmd, exampleCmd)
}
}
if versionCmd.Parsed() {
branch, err := exec.Command("git", "describe", "--tags").Output()
if err != nil {
fmt.Printf("Error: %s\n", err)
}
fmt.Printf("raytracer-go version: %s\n", strings.TrimSpace(string(branch)))
}
if helpCmd.Parsed() {
if len(os.Args) < 3 {
printUsage()
os.Exit(1)
}
switch os.Args[2] {
case "example":
printUsageForSubcommand("example", cmd, exampleCmd)
default:
printUsage()
}
}
}
func printUsage() {
fmt.Println("raytracer-go is a tool for rendering 3d scenes using raytracing.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println()
fmt.Println("\traytracer <command> [arguments]")
fmt.Println()
fmt.Println("The commands are:")
fmt.Println()
fmt.Println("\texample\t\trender an example scene")
fmt.Println("\tversion\t\tprint raytracer-go version")
fmt.Println("\thelp \t\tshow usage for a command (eg 'help example')")
os.Exit(1)
}
func printUsageForSubcommand(name string, cmd, subCmd *flag.FlagSet) {
fmt.Println("raytracer-go is a tool for rendering 3d scenes using raytracing.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println()
fmt.Printf("\traytracer %s [arguments]", name)
fmt.Println()
fmt.Println()
fmt.Println("Flags:")
fmt.Println()
subCmd.PrintDefaults()
os.Exit(1)
}