From 072eba3e5a58ef08f015c57972df46aee38ca3a5 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Thu, 11 Sep 2014 02:28:09 +0200 Subject: [PATCH] Better early stopping when the tile is filled. --- tilemapper/main.go | 9 ++++++--- tilemapper/renderer.go | 42 +++++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/tilemapper/main.go b/tilemapper/main.go index 041f18b..ae5a26d 100644 --- a/tilemapper/main.go +++ b/tilemapper/main.go @@ -72,8 +72,8 @@ func main() { c1 := common.Coord{X: q1x, Z: q1z} c2 := common.Coord{X: q2x, Z: q2z} - for c2.Y = q2y; c2.Y > q1y; c2.Y -= 16 { - c1.Y = c2.Y - 15 + for c2.Y = q2y; c2.Y > q1y; c2.Y -= 8 { + c1.Y = c2.Y - 7 if c1.Y < q1y { c1.Y = q1y } @@ -81,6 +81,9 @@ func main() { if err = client.QueryCuboid(cuboid, drawBlock); err != nil { log.Fatalf("query failed: %s", err) } + if renderer.IsFilled() { + break + } } image := renderer.CreateImage( @@ -90,6 +93,6 @@ func main() { log.Fatalf("writing image failed: %s", err) } - fmt.Printf("Rejected blocks: %d\n", renderer.RejectedBlocks) fmt.Printf("num blocks: %d\n", numBlocks) + fmt.Printf("rejected blocks: %d\n", renderer.Rejected) } diff --git a/tilemapper/renderer.go b/tilemapper/renderer.go index 3bf8bb0..2e207fb 100644 --- a/tilemapper/renderer.go +++ b/tilemapper/renderer.go @@ -13,15 +13,14 @@ import ( ) type Renderer struct { - width int - height int - xOfs int16 - zOfs int16 - yBuffer []int32 - cBuffer []int32 - filled []uint16 - minYs []int16 - RejectedBlocks int + width int + height int + xOfs int16 + zOfs int16 + yBuffer []int32 + cBuffer []int32 + filled int + Rejected int } func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) { @@ -45,11 +44,10 @@ func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) { return } -func (r *Renderer) minY(x, z int16) (minY int32) { - x -= r.xOfs - z -= r.zOfs - w := r.width << 4 - ofs := int(z)*w<<4 + int(x)<<4 +//func (r *Renderer) minY(x, z int16) (minY int32) { +func (r *Renderer) minY(ofs, w int) (minY int32) { + //w := r.width << 4 + //ofs := int(z)*w<<4 + int(x)<<4 minY = int32(math.MaxInt32) for yEnd := ofs + w<<4; ofs < yEnd; { for xEnd := ofs + 16; ofs < xEnd; ofs++ { @@ -63,15 +61,21 @@ func (r *Renderer) minY(x, z int16) (minY int32) { return } +func (r *Renderer) IsFilled() bool { + return r.filled == r.width<<4*r.height<<4 +} + func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int32) (err error) { bx := block.Coord.X - r.xOfs bz := block.Coord.Z - r.zOfs // We do not need to render the block if the whole 16x16 area // is already filled and the block is strictly below. + w := r.width << 4 + ofs := int(bz)*w<<4 + int(bx)<<4 blockY := int32(block.Coord.Y) << 4 - if blockY < r.minY(block.Coord.X, block.Coord.Z) { - r.RejectedBlocks++ + if blockY < r.minY(ofs, w) { + r.Rejected++ return } @@ -81,14 +85,14 @@ func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int32) return } - w := r.width << 4 - - ofs := int(bz)*w<<4 + int(bx)<<4 for z := 0; z < 16; z++ { for x := 0; x < 16; x++ { if currentY := r.yBuffer[ofs]; currentY < blockY { for y := 15; y >= 0; y-- { if c, ok := db.Content(x, y, z); ok { + if r.cBuffer[ofs] == -1 { + r.filled++ + } r.cBuffer[ofs] = c r.yBuffer[ofs] = blockY + int32(y) break