/
config.go
103 lines (97 loc) · 3.38 KB
/
config.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
package raytracer
import (
"encoding/json"
"io/ioutil"
"log"
)
// Config keeps Raytracer Configuration.
type Config struct {
AmbientColorSharingRatio float64 `json:"ambient_color_ratio"`
AmbientRadius float64 `json:"ambient_occlusion_radius"`
AntialiasSamples int `json:"antialias_samples"`
CausticsSamplerLimit int `json:"caustics_samples"`
EdgeDetechThreshold float64 `json:"edge_detect_threshold"`
EnvironmentMap string `json:"environment_map"`
Exposure float64 `json:"exposure"`
Height int `json:"height"`
LightSampleCount int `json:"light_sample_count"`
MaxReflectionDepth int `json:"max_reflection_depth"`
OcclusionRate float64 `json:"occlusion_rate"`
PhotonSpacing float64 `json:"photon_spacing"`
RayCorrection float64 `json:"ray_correction"`
RenderAmbientColors bool `json:"render_ambient_color"`
RenderBumpMap bool `json:"render_bump_map"`
RenderCaustics bool `json:"render_caustics"`
RenderColors bool `json:"render_colors"`
RenderLights bool `json:"render_lights"`
RenderOcclusion bool `json:"render_occlusion"`
RenderReflections bool `json:"render_reflections"`
RenderRefractions bool `json:"render_refractions"`
SamplerLimit int `json:"sampler_limit"`
TransparentColor Vector `json:"transparent_color"`
Width int `json:"width"`
Percentage int
}
// DEFAULT configuration parameters.
// These are likely incorrect :D.
var DEFAULT = Config{
// Default Config Settings
AmbientColorSharingRatio: 0.5,
AmbientRadius: 2.1,
AntialiasSamples: 8,
CausticsSamplerLimit: 10000,
EnvironmentMap: "",
EdgeDetechThreshold: 0.7,
Exposure: 0.2,
Height: 900,
LightSampleCount: 16,
MaxReflectionDepth: 3,
OcclusionRate: 0.2,
Percentage: 100,
PhotonSpacing: 0.005,
RayCorrection: 0.002,
RenderAmbientColors: true,
RenderBumpMap: true,
RenderCaustics: false,
RenderColors: true,
RenderLights: true,
RenderOcclusion: true,
RenderReflections: true,
RenderRefractions: true,
SamplerLimit: 16,
TransparentColor: Vector{0, 0, 0, 0},
Width: 1600,
}
// GlobalConfig is reachable from all app context
// I know that Globals are evil but believe me it's better to have it in global
// in this application.
var GlobalConfig = Config{}
// LoadConfig file for the render.
func loadConfig(jsonFile string) error {
var config Config
log.Printf("Loading configuration from %s", jsonFile)
file, err := ioutil.ReadFile(jsonFile)
if err != nil {
log.Printf("Error while reading file: %s", err.Error())
return nil
}
log.Printf("Unmarshal JSON\n")
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatalf("Error unmarshalling %s", err.Error())
return err
}
GlobalConfig = config
return nil
}
// CreateConfig file.
func CreateConfig(jsonfile string) error {
file, _ := json.MarshalIndent(DEFAULT, "", " ")
ferr := ioutil.WriteFile(jsonfile, file, 0600)
if ferr != nil {
log.Fatal(ferr.Error())
return ferr
}
log.Printf("Created config.json")
return nil
}