3dtrial/renderer2/generation.go

57 lines
1.8 KiB
Go
Raw Normal View History

2024-08-02 20:50:14 +00:00
package main
import (
"image"
"image/color"
"renderer1/hrend"
)
func Checkerboard(cols []color.Color, size int) image.Image {
result := image.NewRGBA(image.Rect(0, 0, size, size))
for y := range size {
for x := range size {
result.Set(x, y, cols[(x+y)%len(cols)])
}
}
return result
}
func FlatTerrain(size int) *hrend.ObjModel {
result := hrend.ObjModel{
Vertices: make([]hrend.Vec3f, 0),
VTexture: make([]hrend.Vec3f, 4),
Faces: make([]hrend.Facef, 0),
}
// For the simple square terrain, there aren't a lot of texture coords...
result.VTexture[0] = hrend.Vec3f{X: 0, Y: 0, Z: 0}
result.VTexture[1] = hrend.Vec3f{X: 1, Y: 0, Z: 0}
result.VTexture[2] = hrend.Vec3f{X: 0, Y: 1, Z: 0}
result.VTexture[3] = hrend.Vec3f{X: 1, Y: 1, Z: 0}
// Generate all the simple vertices along the plane at y=0
for z := -size; z <= size; z++ {
for x := -size; x <= size; x++ {
result.Vertices = append(result.Vertices, hrend.Vec3f{X: float32(x), Y: 0, Z: float32(z)})
}
}
// Faces are slightly different; we generate two for every "cell" inside the vertices
for z := 0; z < size*2; z++ {
for x := 0; x < size*2; x++ {
topleft := x + z*size*2
topright := x + 1 + z*size*2
bottomleft := x + (z+1)*size*2
bottomright := x + 1 + (z+1)*size*2
// remember to wind counter-clockwise
result.Faces = append(result.Faces, hrend.Facef{
hrend.Vertex{Pos: result.Vertices[topleft], Tex: result.VTexture[0]},
hrend.Vertex{Pos: result.Vertices[bottomleft], Tex: result.VTexture[2]},
hrend.Vertex{Pos: result.Vertices[topright], Tex: result.VTexture[1]},
}, hrend.Facef{
hrend.Vertex{Pos: result.Vertices[topright], Tex: result.VTexture[1]},
hrend.Vertex{Pos: result.Vertices[bottomleft], Tex: result.VTexture[2]},
hrend.Vertex{Pos: result.Vertices[bottomright], Tex: result.VTexture[3]},
})
}
}
return &result
}