Working frame generation

This commit is contained in:
Carlos Sanchez 2024-07-16 18:06:28 -04:00
parent 8a06d84ca2
commit f18eb93c32
2 changed files with 51 additions and 6 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
*.gif
*.jpg
*.jpeg
a.out

View File

@ -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")
}