diff --git a/.gitignore b/.gitignore index d880db8..4caeb20 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.gif *.jpg *.jpeg +a.out diff --git a/go1/main.go b/go1/main.go index 37b3610..3c9a6b5 100644 --- a/go1/main.go +++ b/go1/main.go @@ -3,12 +3,56 @@ package main import ( "fmt" "github.com/ungerik/go3d/vec3" + "log" + "strings" + "time" ) -func main() { - fmt.Printf("Hello world") - thing := vec3.T{ - 0, 0, 0, - } - fmt.Printf("Thing: %v", thing) +// 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") }