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

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