-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
112 lines (89 loc) · 2.35 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
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"image/color"
"image/png"
"io/ioutil"
"log"
"os"
"runtime/pprof"
"sync"
"github.com/DheerendraRathor/GoTracer/models"
"github.com/DheerendraRathor/GoTracer/tracer"
"github.com/DheerendraRathor/GoTracer/utils"
"gopkg.in/cheggaaa/pb.v1"
)
var renderSpecFile string
var doCpuProfile bool
var showProgress bool
func init() {
flag.StringVar(&renderSpecFile, "spec", "./examples/fiveSpheresWithLights.json", "Name of JSON file containing rendering spec")
flag.BoolVar(&doCpuProfile, "cpu", false, "Enable CPU Profile")
flag.BoolVar(&showProgress, "progress", false, "Show progress by rendering pixel by pixel")
}
func main() {
flag.Parse()
// Profiler Code
if doCpuProfile {
f, err := os.Create("cpu.prof")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
file, e := ioutil.ReadFile(renderSpecFile)
if e != nil {
panic(fmt.Sprintf("File error: %v\n", e))
}
var env models.Specification
json.Unmarshal(file, &env)
pngImage := image.NewRGBA(image.Rectangle{
image.Point{0, 0},
image.Point{env.Image.Width, env.Image.Height},
})
if showProgress {
progress := make(chan *models.Pixel, 100)
defer close(progress)
var pbWg sync.WaitGroup
var progressBar *pb.ProgressBar
pbWg.Add(1)
go func() {
defer pbWg.Done()
total := env.Image.Width * env.Image.Height
progressBar = pb.StartNew(total)
progressBar.ShowFinalTime = true
progressBar.ShowTimeLeft = false
progressBar.ShowBar = false
for pixel := range progress {
if pixel == nil {
break
}
updateImage(pngImage, pixel)
progressBar.Increment()
}
}()
tracer.GoTrace(&env, true, progress, false, nil)
pbWg.Wait()
progressBar.Finish()
} else {
tracerOutput := tracer.GoTrace(&env, false, nil, false, nil)
for _, row := range tracerOutput.Pixels {
for _, pixel := range row {
updateImage(pngImage, pixel)
}
}
}
pngFile := utils.CreateNestedFile(env.Image.OutputFile)
defer pngFile.Close()
png.Encode(pngFile, pngImage)
}
func updateImage(image *image.RGBA, pixel *models.Pixel) {
rgbaColor := color.RGBA{pixel.Color[0], pixel.Color[1], pixel.Color[2], 255}
image.Set(pixel.I, pixel.J, rgbaColor)
}