mtsatellite/tilemapper/renderer.go

276 lines
5.5 KiB
Go
Raw Normal View History

2014-09-10 01:21:55 +02:00
// Copyright 2014 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package main
import (
"container/heap"
2014-09-10 18:58:12 +02:00
"image"
"image/color"
2014-09-10 01:21:55 +02:00
"math"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
)
type Renderer struct {
width int
height int
xOfs int16
zOfs int16
yBuffer []int32
yMin []int32
cBuffer []int32
filled int
Rejected int
2014-09-10 01:21:55 +02:00
}
type YOrder struct {
Renderer *Renderer
blocks []*common.Block
capacity int
}
func NewYOrder(renderer *Renderer, capacity int) *YOrder {
return &YOrder{
Renderer: renderer,
blocks: make([]*common.Block, 0, capacity),
capacity: capacity}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func copyData(data []byte) []byte {
l := len(data)
ndata := make([]byte, l, max(l, 8*1024))
copy(ndata, data)
return ndata
}
func (yo *YOrder) RenderBlock(block *common.Block, nameIndex map[string]int32) (err error) {
var nblock *common.Block
if len(yo.blocks) == yo.capacity {
oblock := yo.blocks[0]
if oblock.Coord.Y < block.Coord.Y {
// New one is above highest old. Directly render new.
err = yo.Renderer.RenderBlock(block, nameIndex)
return
}
// Render old one. Store copy of new in heap.
heap.Pop(yo)
err = yo.Renderer.RenderBlock(oblock, nameIndex)
l := len(block.Data)
if cap(oblock.Data) < l {
oblock.Data = make([]byte, l, max(l, 8*1024))
} else {
oblock.Data = oblock.Data[0:l]
}
copy(oblock.Data, block.Data)
oblock.Coord = block.Coord
nblock = oblock
} else {
nblock = &common.Block{Coord: block.Coord, Data: copyData(block.Data)}
}
heap.Push(yo, nblock)
return
}
func (yo *YOrder) Drain(nameIndex map[string]int32) (err error) {
for len(yo.blocks) > 0 {
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*common.Block), nameIndex); err != nil {
return
}
}
return
}
func (yo *YOrder) Len() int {
return len(yo.blocks)
}
func (yo *YOrder) Swap(i, j int) {
yo.blocks[i], yo.blocks[j] = yo.blocks[j], yo.blocks[i]
}
func (yo *YOrder) Less(i, j int) bool {
// Reverse order intented.
return yo.blocks[i].Coord.Y > yo.blocks[j].Coord.Y
}
func (yo *YOrder) Push(x interface{}) {
yo.blocks = append(yo.blocks, x.(*common.Block))
}
func (yo *YOrder) Pop() (x interface{}) {
l := len(yo.blocks)
x = yo.blocks[l-1]
yo.blocks = yo.blocks[0 : l-1]
return x
}
2014-09-10 01:21:55 +02:00
func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) {
dim := width * height
pixSize := dim * 16 * 16
yBuffer := make([]int32, pixSize)
cBuffer := make([]int32, pixSize)
yMin := make([]int32, dim)
2014-09-10 01:21:55 +02:00
for i := 0; i < pixSize; i++ {
yBuffer[i] = math.MinInt32
cBuffer[i] = -1
}
for i := 0; i < dim; i++ {
yMin[i] = math.MinInt32
}
2014-09-10 01:21:55 +02:00
renderer = &Renderer{
width: width,
height: height,
xOfs: xOfs,
zOfs: zOfs,
yBuffer: yBuffer,
2014-09-11 22:38:01 +02:00
cBuffer: cBuffer,
yMin: yMin}
2014-09-10 01:21:55 +02:00
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) {
2014-09-10 18:58:12 +02:00
bx := block.Coord.X - r.xOfs
bz := block.Coord.Z - r.zOfs
2014-09-10 01:21:55 +02:00
// We do not need to render the block if the whole 16x16 area
// is already filled and the block is strictly below.
blockY := int32(block.Coord.Y) << 4
pos := int(bz)*r.width + int(bx)
if blockY < r.yMin[pos] {
r.Rejected++
2014-09-10 01:21:55 +02:00
return
}
// Decoding is pretty expensive so do it that late.
2014-09-10 01:21:55 +02:00
var db *DecodedBlock
2014-09-10 18:58:12 +02:00
if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil {
2014-09-10 01:21:55 +02:00
return
}
2014-09-11 22:38:01 +02:00
w := r.width << 4
ofs := int(bz)*w<<4 + int(bx)<<4
yMin := int32(math.MaxInt32)
for z := 0; z < 16; z++ {
for x := 0; x < 16; x++ {
currentY := r.yBuffer[ofs]
if 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
currentY = blockY + int32(y)
r.yBuffer[ofs] = currentY
break
}
}
2014-09-10 01:21:55 +02:00
}
if currentY < yMin {
yMin = currentY
}
ofs++
2014-09-10 01:21:55 +02:00
}
ofs += w - 16
2014-09-10 01:21:55 +02:00
}
r.yMin[pos] = yMin
2014-09-10 01:21:55 +02:00
return
}
2014-09-10 18:58:12 +02:00
func (r *Renderer) CreateImage(colors []color.RGBA, background color.RGBA) *image.RGBA {
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 := ph - 1; z >= 0; z-- {
2014-09-10 18:58:12 +02:00
for x := 0; x < pw; x++ {
colIdx := r.cBuffer[ofs]
if colIdx >= 0 && colIdx < numCols {
image.Set(x, z, colors[colIdx])
} else {
image.Set(x, z, background)
}
ofs++
}
}
return image
}
2014-09-11 17:35:03 +02:00
func safeColor(x int32) uint8 {
2014-09-12 10:45:36 +02:00
switch {
case x < 0:
return 0
case x > 255:
return 255
default:
return uint8(x)
2014-09-11 17:35:03 +02:00
}
}
func (r *Renderer) CreateShadedImage(
xOfs, zOfs, width, height int,
colors []color.RGBA, background color.RGBA) *image.RGBA {
image := image.NewRGBA(image.Rect(0, 0, width, height))
pw := r.width << 4
ofs, numCols := zOfs*pw+xOfs, int32(len(colors))
stride := pw - width
for z := height - 1; z >= 0; z-- {
for x := 0; x < width; x++ {
2014-09-11 17:35:03 +02:00
colIdx := r.cBuffer[ofs]
if colIdx < 0 || colIdx >= numCols {
image.Set(x, z, background)
} else {
var y, y1, y2 int32
y = r.yBuffer[ofs]
if x == 0 {
y1 = y
} else {
y1 = r.yBuffer[ofs-1]
}
if z == 0 {
y2 = y
} else {
y2 = r.yBuffer[ofs+pw]
}
d := ((y - y1) + (y - y2)) * 12
if d > 36 {
d = 36
}
col := colors[colIdx]
image.Set(x, z, color.RGBA{
R: safeColor(int32(col.R) + d),
G: safeColor(int32(col.G) + d),
B: safeColor(int32(col.B) + d),
A: 0xff})
}
ofs++
}
ofs += stride
2014-09-11 17:35:03 +02:00
}
return image
}