/* renderman/fromscratch/quadratics_ani.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_ani 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") shape := flag.String("shape","torus","`quadratic shape` to use : sphere, disk, torus, cylinder, cone") help := flag.Bool("help",false,"print the help") flag.Parse() helpfunc := func() { fmt.Printf("%s\n\nhelp -\n",Version) flag.PrintDefaults() os.Exit(0) } if *help { helpfunc() } qshape := "--" switch strings.ToLower(*shape) { case "torus": qshape = "torus" break case "sphere": qshape = "sphere" break case "disk": qshape = "disk" break case "cylinder": qshape = "cylinder" break case "cone": qshape = "cone" break } if qshape == "--" { fmt.Printf("%q -- invalid shape selection!\n",qshape) helpfunc() } diffuseColor := [3]float64{1.0,1.0,1.0} 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,frame int) string { return fmt.Sprintf("frames/ani_%s-%05d.exr",label(str),frame) } d := fmt.Printf d("##RenderMan\n") d("##Machine Generated By %q\n", "quadratics_ani.go") d("##Scene quadrics_ani.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") geometry := make(map[string]string,0) geometry["sphere"] = "Sphere 1 -1 1 360" geometry["cone"] = "Cone 1 1 360" geometry["cylinder"] = "Cylinder 1 -1 1 360" geometry["disk"] = "Disk 1 1 360" geometry["torus"] = "Torus 1 0.6 0 360 360" geo := geometry[qshape] df := 1.0 / (360.0 / 2.0) /* we use a little animation trick of missing the last frame * to maintain momentum in a looping animation */ for frame := 0; frame < (360 - 1); frame ++ { rot := 360.0 * (float64(frame) / 360.0) gb := 1.0 - (float64(frame) * df) if frame >= 180 { gb = float64(frame - 180) * df } diffuseColor[1] = gb diffuseColor[2] = gb d("FrameBegin %d\n",frame + 1) d("\tDisplay %q %q %q\n",exr(geo,frame),"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(geo))) d("\t\t\tRotate %s 0 1 0\n",format.Float64(rot)) d("\t\t\tBxdf %q %q %s\n","PxrDiffuse","surface",p("color diffuseColor",diffuseColor)) d("\t\t\t%s\n",geo) d("\t\tAttributeEnd\n") d("\tWorldEnd\n") d("FrameEnd\n") } }