package main import ( "image" "image/color" "renderer2/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)}) } } width := size + size + 1 // Faces are slightly different; we generate two for every "cell" inside the vertices for z := 0; z < width-1; z++ { for x := 0; x < width-1; x++ { topleft := x + z*width topright := x + 1 + z*width bottomleft := x + (z+1)*width bottomright := x + 1 + (z+1)*width // 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 }