Made tilerender finally work. Needs performance tweaks.

This commit is contained in:
Sascha L. Teichmann 2014-09-10 23:49:27 +02:00
parent 4fd0722ade
commit ab767a97bb
4 changed files with 65 additions and 46 deletions

View File

@ -25,9 +25,9 @@ const (
type DecodedBlock struct { type DecodedBlock struct {
Version byte Version byte
MapContent []byte MapContent []byte
AirId int AirId int32
IgnoreId int IgnoreId int32
IndexMap map[int]int IndexMap map[int32]int32
} }
// The content of the map and the meta data are compressed with zlib. // The content of the map and the meta data are compressed with zlib.
@ -41,7 +41,7 @@ type PosBuf struct {
Pos int Pos int
} }
func NewDecodedBlock(data []byte, nameIndex map[string]int) (db *DecodedBlock, err error) { func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock, err error) {
version := data[0] version := data[0]
contentWidth := int(data[2]) contentWidth := int(data[2])
@ -113,14 +113,14 @@ func NewDecodedBlock(data []byte, nameIndex map[string]int) (db *DecodedBlock, e
} }
offset += 4 offset += 4
airId, ignoreId := -1, -1 airId, ignoreId := int32(-1), int32(-1)
indexMap := make(map[int]int) indexMap := make(map[int32]int32)
if version >= 22 { if version >= 22 {
offset++ offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:])) numMappings := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2 offset += 2
for i := 0; i < numMappings; i++ { for i := 0; i < numMappings; i++ {
nodeId := int(binary.BigEndian.Uint16(data[offset:])) nodeId := int32(binary.BigEndian.Uint16(data[offset:]))
offset += 2 offset += 2
nameLen := int(binary.BigEndian.Uint16(data[offset:])) nameLen := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2 offset += 2
@ -151,18 +151,18 @@ func NewDecodedBlock(data []byte, nameIndex map[string]int) (db *DecodedBlock, e
return return
} }
func (db *DecodedBlock) Content(x, y, z int) (content int, found bool) { func (db *DecodedBlock) Content(x, y, z int) (content int32, found bool) {
pos := z<<8 + y<<4 + x pos := z<<8 + y<<4 + x
switch { switch {
case db.Version >= 24: case db.Version >= 24:
pos <<= 1 pos <<= 1
content = int(db.MapContent[pos])<<8 | int(db.MapContent[pos+1]) content = int32(db.MapContent[pos])<<8 | int32(db.MapContent[pos+1])
case db.Version >= 20: case db.Version >= 20:
if db.MapContent[pos] <= 0x80 { if db.MapContent[pos] <= 0x80 {
content = int(db.MapContent[pos]) content = int32(db.MapContent[pos])
} else { } else {
content = int(db.MapContent[pos])<<4 | int(db.MapContent[pos+0x2000])>>4 content = int32(db.MapContent[pos])<<4 | int32(db.MapContent[pos+0x2000])>>4
} }
default: default:
return return

View File

@ -14,7 +14,7 @@ import (
type Colors struct { type Colors struct {
Colors []color.RGBA Colors []color.RGBA
NameIndex map[string]int NameIndex map[string]int32
} }
func ParseColors(filename string) (colors *Colors, err error) { func ParseColors(filename string) (colors *Colors, err error) {
@ -25,7 +25,7 @@ func ParseColors(filename string) (colors *Colors, err error) {
} }
defer file.Close() defer file.Close()
nameIndex := make(map[string]int) nameIndex := make(map[string]int32)
cols := make([]color.RGBA, 0, 2200) cols := make([]color.RGBA, 0, 2200)
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
@ -38,7 +38,7 @@ func ParseColors(filename string) (colors *Colors, err error) {
var name string var name string
if n, _ := fmt.Sscanf( if n, _ := fmt.Sscanf(
line, "%s %d %d %d %d", &name, &c.R, &c.G, &c.B, &c.A); n > 0 { line, "%s %d %d %d %d", &name, &c.R, &c.G, &c.B, &c.A); n > 0 {
idx := len(cols) idx := int32(len(cols))
cols = append(cols, c) cols = append(cols, c)
nameIndex[name] = idx nameIndex[name] = idx
} }

View File

@ -81,4 +81,6 @@ func main() {
if err = SaveAsPNG(outfile, image); err != nil { if err = SaveAsPNG(outfile, image); err != nil {
log.Fatalf("writing image failed: %s", err) log.Fatalf("writing image failed: %s", err)
} }
fmt.Printf("Rejected blocks: %d\n", renderer.RejectedBlocks)
} }

View File

@ -19,8 +19,9 @@ type Renderer struct {
zOfs int16 zOfs int16
yBuffer []int32 yBuffer []int32
cBuffer []int32 cBuffer []int32
filled []byte filled []uint16
minYs []int16 minYs []int16
RejectedBlocks int
} }
func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) { func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) {
@ -28,60 +29,76 @@ func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) {
pixSize := dim * 16 * 16 pixSize := dim * 16 * 16
yBuffer := make([]int32, pixSize) yBuffer := make([]int32, pixSize)
cBuffer := make([]int32, pixSize) cBuffer := make([]int32, pixSize)
filled := make([]byte, dim)
minYs := make([]int16, dim)
for i := 0; i < pixSize; i++ { for i := 0; i < pixSize; i++ {
yBuffer[i] = math.MinInt32 yBuffer[i] = math.MinInt32
cBuffer[i] = -1 cBuffer[i] = -1
} }
for i := 0; i < dim; i++ {
minYs[i] = math.MinInt16
}
renderer = &Renderer{ renderer = &Renderer{
width: width, width: width,
height: height, height: height,
xOfs: xOfs, xOfs: xOfs,
zOfs: zOfs, zOfs: zOfs,
yBuffer: yBuffer, yBuffer: yBuffer,
cBuffer: cBuffer, cBuffer: cBuffer}
filled: filled,
minYs: minYs}
return return
} }
func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int) (err error) { func (r *Renderer) minY(x, z int16) (minY int32) {
// fmt.Printf("%d %d\n", r.xOfs, r.zOfs) x -= r.xOfs
// fmt.Printf("%d %d\n", block.Coord.X, block.Coord.Z) z -= r.zOfs
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++ {
y := r.yBuffer[ofs]
if y < minY {
minY = y
}
}
ofs += w - 16
}
return
}
func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int32) (err error) {
bx := block.Coord.X - r.xOfs bx := block.Coord.X - r.xOfs
bz := block.Coord.Z - r.zOfs bz := block.Coord.Z - r.zOfs
dimIndex := (bx << 4) + bz
// We do not need to render the block if the whole 16x16 area // We do not need to render the block if the whole 16x16 area
// is already filled and the block is strictly below. // is already filled and the block is strictly below.
if r.filled[dimIndex] == 0xff && r.minYs[dimIndex] > block.Coord.Y { blockY := int32(block.Coord.Y) << 4
if blockY < r.minY(block.Coord.X, block.Coord.Z) {
r.RejectedBlocks++
return return
} }
// Decoding is pretty expensive so it that late.
var db *DecodedBlock var db *DecodedBlock
if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil { if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil {
return return
} }
_ = db w := r.width << 4
ay := int32(block.Coord.Y) << 4 ofs := int(bz)*w<<4 + int(bx)<<4
_ = ay
for x := 0; x < 16; x++ {
for z := 0; z < 16; z++ { 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-- { for y := 15; y >= 0; y-- {
_, _, _ = x, y, z if c, ok := db.Content(x, y, z); ok {
r.cBuffer[ofs] = c
r.yBuffer[ofs] = blockY + int32(y)
break
} }
} }
} }
ofs++
}
ofs += w - 16
}
return return
} }
@ -90,7 +107,7 @@ func (r *Renderer) CreateImage(colors []color.RGBA, background color.RGBA) *imag
pw, ph := r.width<<4, r.height<<4 pw, ph := r.width<<4, r.height<<4
image := image.NewRGBA(image.Rect(0, 0, pw, ph)) image := image.NewRGBA(image.Rect(0, 0, pw, ph))
ofs, numCols := 0, int32(len(colors)) ofs, numCols := 0, int32(len(colors))
for z := 0; z < ph; z++ { for z := ph - 1; z >= 0; z-- {
for x := 0; x < pw; x++ { for x := 0; x < pw; x++ {
colIdx := r.cBuffer[ofs] colIdx := r.cBuffer[ofs]
if colIdx >= 0 && colIdx < numCols { if colIdx >= 0 && colIdx < numCols {