package main import ( "fmt" "github.com/ungerik/go3d/vec3" "log" "strings" "time" ) // Clamp a float value to the given range func fclamp(val float32, minval int, maxval int) int { return max(minval, min(maxval, int(val))) } func main() { log.Printf("Program begin") const ( width = 1024 height = 768 colmax = 255 ) framebuffer := make([]vec3.T, width*height) now := time.Now() for y := range height { for x := range width { framebuffer[x+y*width] = vec3.T{ float32(y) / float32(height), float32(x) / float32(width), 0, } } } log.Printf("Generate frame took %dms", time.Since(now).Milliseconds()) fmt.Printf("P3\n%d %d\n%d\n# Image contents\n", width, height, colmax) var buffer strings.Builder now = time.Now() for i := range height * width { fb := framebuffer[i] buffer.WriteString(fmt.Sprintf("%d %d %d\t", fclamp(colmax*fb[0], 0, colmax), fclamp(colmax*fb[1], 0, colmax), fclamp(colmax*fb[2], 0, colmax), )) if i&63 == 63 { buffer.WriteRune('\n') } } log.Printf("Conversion took %dms", time.Since(now).Milliseconds()) fmt.Printf(buffer.String()) fmt.Printf("\n# Image end\n") }