/* renderman/fromscratch/quadratics_params.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_params 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.6, "green colour channel") blue := flag.Float64("blue", 0.0, "blue colour channel") constant := flag.Bool("constant",false,"render with constant surfaces") 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 } color := [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(800.0 * s) height := int(400.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_params.go") d("##Version %q\n",Version) d("##Scene quadrics_params.rib\n") d("version 3.04\n") d("Format %d %d 1\n", width, height) d("Display %q %q %q\n",exr("sphere_params"),"openexr","rgba") 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") if ! *constant { 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") } /* ri.h : RiSphere(float radius, float zmin,float zmax, float tmax, ...); */ radius := 1.0 zmin := -1.0 zmax := 1.0 tmax := 360.0 dzmax := 1.0 / (14.0 + 1) dtmax := 360.0 / 56.0 array := func(r ...float64) string { return format.Floatv(r) } d("WorldBegin\n") d("\t\tTranslate 0 4 20\n") if ! *constant { d("\t\tReadArchive %q\n","lighting") } for y := -12.0; y < 8.0; y += 4.0 { for x := -20.0; x < 20.0; x += 3.0 { if y > -8.0 { tmax -= dtmax zmin = -1.0 zmax = 1.0 } else { zmin += dzmax zmax -= dzmax } d("\t\tAttributeBegin\n") d("\t\t\tAttribute %q %s\n","identifier",p("name","sphere")) d("\t\t\tTranslate %s\n",array(x,y,0.0)) if *constant { d("\t\t\tBxdf %q %q %s\n","PxrDiffuse","surface",p("color diffuseColor",color)) } else { d("\t\t\tBxdf %q %q %s\n","PxrConstant","surface",p("color emitColor",color)) } d("\t\t\tSphere %s\n",array(radius,zmin,zmax,tmax)) d("\t\tAttributeEnd\n") } } d("WorldEnd\n") }