func main() { log.SetFlags(0) args := os.Args[1:] if len(args) < 3 || len(args)%2 != 1 { log.Fatalln("Usage: combine path1.png weight1 path2.png weight2 ... output.png") } var images []image.Image var weights []int64 var totalWeight int64 for i := 0; i < len(args)-1; i += 2 { path := args[i] weight, err := strconv.ParseInt(args[i+1], 0, 0) if err != nil { log.Fatalf("Invalid weight: %s\n", args[i+1]) } fmt.Printf("Loading %s...\n", path) im, err := pt.LoadPNG(path) if err != nil { log.Fatalf("Error loading %s: %v", path, err) } images = append(images, im) weights = append(weights, weight) totalWeight += weight } for i := 1; i < len(images); i++ { s1 := images[0].Bounds().Max s2 := images[i].Bounds().Max if s1 != s2 { log.Fatalf("Images must all have the same dimensions") } } fmt.Println("Combining images...") size := images[0].Bounds().Max w, h := size.X, size.Y result := image.NewNRGBA(image.Rect(0, 0, w, h)) for y := 0; y < h; y++ { for x := 0; x < w; x++ { fr, fg, fb := 0.0, 0.0, 0.0 for i, im := range images { weight := float64(weights[i]) r, g, b, _ := im.At(x, y).RGBA() fr += weight * float64(r) / 65535 fg += weight * float64(g) / 65535 fb += weight * float64(b) / 65535 } fr /= float64(totalWeight) fg /= float64(totalWeight) fb /= float64(totalWeight) r := uint8(math.Min(255, fr*255)) g := uint8(math.Min(255, fg*255)) b := uint8(math.Min(255, fb*255)) result.SetNRGBA(x, y, color.NRGBA{r, g, b, 255}) } } path := args[len(args)-1] if err := pt.SavePNG(path, result); err != nil { log.Fatalf("Error saving %s: %v", path, err) } }
func main() { scene := pt.Scene{} scene.Add(pt.NewSphere(pt.Vector{-2, 5, -3}, 0.5, pt.LightMaterial(pt.Color{1, 1, 1}, 1, pt.NoAttenuation))) scene.Add(pt.NewSphere(pt.Vector{5, 5, -3}, 0.5, pt.LightMaterial(pt.Color{1, 1, 1}, 1, pt.NoAttenuation))) scene.Add(pt.NewCube(pt.Vector{-30, -1, -30}, pt.Vector{30, 0, 30}, pt.SpecularMaterial(pt.HexColor(0xFCFAE1), 2))) mesh, err := pt.LoadOBJ("examples/teapot.obj", pt.SpecularMaterial(pt.HexColor(0xB9121B), 2)) if err != nil { log.Fatalln("LoadOBJ error:", err) } scene.Add(mesh) camera := pt.LookAt(pt.Vector{2, 5, -6}, pt.Vector{0.5, 1, 0}, pt.Vector{0, 1, 0}, 45) im := pt.Render(&scene, &camera, 2560/4, 1440/4, 4, 16, 4) if err := pt.SavePNG("out.png", im); err != nil { log.Fatalln("SavePNG error:", err) } }
func main() { scene := pt.Scene{} material := pt.DiffuseMaterial(pt.HexColor(0x334D5C)) scene.Add(pt.NewSphere(pt.Vector{0.5, 1, 3}, 0.5, pt.LightMaterial(pt.Color{1, 1, 1}, 1, pt.NoAttenuation))) scene.Add(pt.NewSphere(pt.Vector{1.5, 1, 3}, 0.5, pt.LightMaterial(pt.Color{1, 1, 1}, 1, pt.NoAttenuation))) scene.Add(pt.NewCube(pt.Vector{-5, -5, -2}, pt.Vector{5, 5, -1}, material)) mesh, err := pt.LoadOBJ("examples/suzanne.obj", pt.SpecularMaterial(pt.HexColor(0xEFC94C), 2)) if err != nil { log.Fatalln("LoadOBJ error:", err) } scene.Add(mesh) camera := pt.LookAt(pt.Vector{1, -0.45, 4}, pt.Vector{1, -0.6, 0.4}, pt.Vector{0, 1, 0}, 45) im := pt.Render(&scene, &camera, 2560/4, 1440/4, 4, 16, 4) if err := pt.SavePNG("out.png", im); err != nil { log.Fatalln("SavePNG error:", err) } }
func main() { scene := pt.Scene{} material := pt.GlossyMaterial(pt.HexColor(0xEFC94C), 3, pt.Radians(30)) whiteMat := pt.GlossyMaterial(pt.Color{1, 1, 1}, 3, pt.Radians(30)) for x := 0; x < 10; x++ { for z := 0; z < 10; z++ { center := pt.Vector{float64(x) - 4.5, 0, float64(z) - 4.5} scene.Add(pt.NewSphere(center, 0.4, material)) } } scene.Add(pt.NewCube(pt.Vector{-100, -1, -100}, pt.Vector{100, 0, 100}, whiteMat)) scene.Add(pt.NewSphere(pt.Vector{-1, 3, -1}, 0.5, pt.LightMaterial(pt.Color{1, 1, 1}, 1, pt.NoAttenuation))) camera := pt.LookAt(pt.Vector{0, 4, -8}, pt.Vector{0, 0, -2}, pt.Vector{0, 1, 0}, 45) im := pt.Render(&scene, &camera, 2560/4, 1440/4, 4, 16, 8) if err := pt.SavePNG("out.png", im); err != nil { log.Fatalln("SavePNG error:", err) } }