Working frame generation
This commit is contained in:
parent
8a06d84ca2
commit
f18eb93c32
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@
|
||||
*.gif
|
||||
*.jpg
|
||||
*.jpeg
|
||||
a.out
|
||||
|
52
go1/main.go
52
go1/main.go
@ -3,12 +3,56 @@ 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() {
|
||||
fmt.Printf("Hello world")
|
||||
thing := vec3.T{
|
||||
0, 0, 0,
|
||||
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,
|
||||
}
|
||||
fmt.Printf("Thing: %v", thing)
|
||||
}
|
||||
}
|
||||
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user