/* renderman/fromscratch/quadratics.go * mae 012018 */ package main import ( "flag" "fmt" "os" "strings" "reflect" "gitlab.com/mae.earth/renderman/fromscratch/library/format" ) const Version string = "renderman/fromscratch/quadratics program 1" func main() { scale := flag.Float64("scale", 1.0, "`scale of output image") maxsamples := flag.Int("maxsamples", 512, "maxsamples for raytrace hider") minsamples := flag.Int("minsamples", 16, "minsamples for raytrace hider") red := flag.Float64("red", 1.0, "red colour channel") green := flag.Float64("green", 0.0, "green colour channel") blue := flag.Float64("blue", 0.0, "blue colour channel") help := flag.Bool("help",false,"print the help") flag.Parse() if *help { fmt.Printf("%s\n\nhelp -\n",Version) flag.PrintDefaults() os.Exit(0) } within := func(d float64) float64 { if d < 0.000000 { return 0.0 } if d > 1.000000 { return 1.0 } return d } diffuseColor := [3]float64{within(*red),within(*green),within(*blue)} s := *scale if s < 0.1 { s = 0.1 } if s > 5.0 { s = 5.0 } width := int(200.0 * s) height := int(200.0 * s) /* process the parameterlist using reflection */ p := func(params ...interface{}) string { if len(params) == 0 { return "" } out := make([]string,len(params)) for i,param := range params { if param == nil { out = append(out,"") continue } switch reflect.TypeOf(param).String() { case "string": str,_ := param.(string) out[i] = fmt.Sprintf("%q",str) break case "float64": f,_ := param.(float64) /* make use of the RIB's floating point notation to compact output values */ out[i] = fmt.Sprintf("[%s]",format.Float64(f)) break case "int": d,_ := param.(int) out[i] = fmt.Sprintf("[%d]",d) break case "[3]float64": fs,_ := param.([3]float64) out[i] = fmt.Sprintf("[%s]",format.Float64(fs)) break default: out[i] = fmt.Sprintf("",reflect.TypeOf(param)) break } } return strings.Join(out," ") } /* strip the first word and use as the label */ label := func(str string) string { parts := strings.Split(str," ") return strings.ToLower(parts[0]) } exr := func(str string) string { return label(str) + ".exr" } d := fmt.Printf d("##RenderMan\n") d("##Machine Generated By %q\n", "quadratics.go") d("##Scene quadrics.rib\n") d("version 3.04\n") d("Format %d %d 1\n", width, height) d("Projection %q %s\n", "perspective", p("float fov",60.0)) d("Hider %q %s\n","raytrace",p("int maxsamples",*maxsamples,"int minsamples",*minsamples,"int incremental",100)) d("Integrator %q %q\n", "PxrPathTracer", "example") d("PixelFilter %q 4 4\n","gaussian") d("ArchiveBegin %q\n","lighting") d("\tAttributeBegin\n") d("\t\tAttribute %q %s\n","identifier",p("name","fill_light")) d("\t\tTranslate 0 15 0\n") d("\t\tScale 3 3 3\n") d("\t\tLight %q %q %s\n","PxrSphereLight","fill_light",p("float intensity",8.0,"float temperature",7500.0)) d("\tAttributeEnd\n") d("\tAttributeBegin\n") d("\t\tAttribute %q %s\n","identifier",p("name","key_light")) d("\t\tTranslate 0 0 -5\n") d("\t\tScale 3 3 3\n") d("\t\tLight %q %q %s\n","PxrSphereLight","key_light",p("float intensity",8.0,"float temperature",3000.0)) d("\tAttributeEnd\n") d("ArchiveEnd\n") for i,geometry := range []string{"Sphere 1 -1 1 360","Cone 3 1 360", "Cylinder 1 -1 1 360","Disk 1 1 360","Torus 1 0.6 0 360 360"} { d("FrameBegin %d\n",i + 1) d("\tDisplay %q %q %q\n",exr(geometry),"openexr","rgba") d("\tWorldBegin\n") d("\t\tTranslate 0 0 3.5\n") d("\t\tReadArchive %q\n","lighting") d("\t\tAttributeBegin\n") d("\t\t\tAttribute %q %s\n","identifier",p("name",label(geometry))) d("\t\t\tBxdf %q %q %s\n","PxrDiffuse","surface",p("color diffuseColor",diffuseColor)) d("\t\t\t%s\n",geometry) d("\t\tAttributeEnd\n") d("\tWorldEnd\n") d("FrameEnd\n") } }