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 {
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
xOfs int16
|
|
|
|
zOfs int16
|
|
|
|
yBuffer []int32
|
|
|
|
cBuffer []int32
|
|
|
|
filled []byte
|
|
|
|
minYs []int16
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:58:12 +02:00
|
|
|
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)
|
|
|
|
bx := block.Coord.X - r.xOfs
|
|
|
|
bz := block.Coord.Z - r.zOfs
|
|
|
|
dimIndex := (bx << 4) + bz
|
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-10 18:58:12 +02:00
|
|
|
if r.filled[dimIndex] == 0xff && r.minYs[dimIndex] > block.Coord.Y {
|
2014-09-10 01:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = db
|
|
|
|
|
2014-09-10 18:58:12 +02:00
|
|
|
ay := int32(block.Coord.Y) << 4
|
2014-09-10 01:21:55 +02:00
|
|
|
|
|
|
|
_ = ay
|
|
|
|
|
|
|
|
for x := 0; x < 16; x++ {
|
|
|
|
for z := 0; z < 16; z++ {
|
|
|
|
for y := 15; y >= 0; y-- {
|
|
|
|
_, _, _ = x, y, z
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := 0; z < ph; z++ {
|
|
|
|
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
|
|
|
|
}
|