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 (
|
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 {
|
2014-09-11 02:28:09 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
xOfs int16
|
|
|
|
zOfs int16
|
|
|
|
yBuffer []int32
|
|
|
|
cBuffer []int32
|
|
|
|
filled int
|
|
|
|
Rejected int
|
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)
|
|
|
|
|
|
|
|
for i := 0; i < pixSize; i++ {
|
|
|
|
yBuffer[i] = math.MinInt32
|
|
|
|
cBuffer[i] = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer = &Renderer{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
xOfs: xOfs,
|
|
|
|
zOfs: zOfs,
|
|
|
|
yBuffer: yBuffer,
|
2014-09-10 23:49:27 +02:00
|
|
|
cBuffer: cBuffer}
|
2014-09-10 01:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-11 02:28:09 +02:00
|
|
|
//func (r *Renderer) minY(x, z int16) (minY int32) {
|
|
|
|
func (r *Renderer) minY(ofs, w int) (minY int32) {
|
2014-09-10 23:49:27 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-11 02:28:09 +02:00
|
|
|
func (r *Renderer) IsFilled() bool {
|
|
|
|
return r.filled == r.width<<4*r.height<<4
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:49:27 +02:00
|
|
|
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.
|
2014-09-11 02:28:09 +02:00
|
|
|
w := r.width << 4
|
|
|
|
ofs := int(bz)*w<<4 + int(bx)<<4
|
2014-09-10 23:49:27 +02:00
|
|
|
blockY := int32(block.Coord.Y) << 4
|
2014-09-11 02:28:09 +02:00
|
|
|
if blockY < r.minY(ofs, w) {
|
|
|
|
r.Rejected++
|
2014-09-10 01:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:49:27 +02:00
|
|
|
// Decoding is pretty expensive so 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-10 23:49:27 +02:00
|
|
|
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 {
|
2014-09-11 02:28:09 +02:00
|
|
|
if r.cBuffer[ofs] == -1 {
|
|
|
|
r.filled++
|
|
|
|
}
|
2014-09-10 23:49:27 +02:00
|
|
|
r.cBuffer[ofs] = c
|
|
|
|
r.yBuffer[ofs] = blockY + int32(y)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
2014-09-10 23:49:27 +02:00
|
|
|
ofs++
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
2014-09-10 23:49:27 +02:00
|
|
|
ofs += w - 16
|
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))
|
2014-09-10 23:49:27 +02:00
|
|
|
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
|
|
|
|
}
|