mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-17 15:48:17 +01:00
180 lines
3.6 KiB
Go
180 lines
3.6 KiB
Go
// 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 (
|
|
"image"
|
|
"image/color"
|
|
"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
|
|
}
|
|
|
|
const (
|
|
invalid = math.MinInt32 + 1
|
|
)
|
|
|
|
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)
|
|
|
|
for i := 0; i < pixSize; i++ {
|
|
yBuffer[i] = math.MinInt32
|
|
cBuffer[i] = -1
|
|
}
|
|
|
|
for i := 0; i < dim; i++ {
|
|
yMin[i] = math.MinInt32
|
|
}
|
|
|
|
renderer = &Renderer{
|
|
width: width,
|
|
height: height,
|
|
xOfs: xOfs,
|
|
zOfs: zOfs,
|
|
yBuffer: yBuffer,
|
|
cBuffer: cBuffer,
|
|
yMin: yMin}
|
|
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) {
|
|
|
|
bx := block.Coord.X - r.xOfs
|
|
bz := block.Coord.Z - r.zOfs
|
|
|
|
// 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)
|
|
//fmt.Printf("pos: %d %d\n", pos, len(r.yMin))
|
|
if blockY < r.yMin[pos] {
|
|
r.Rejected++
|
|
return
|
|
}
|
|
|
|
// Decoding is pretty expensive so do it that late.
|
|
var db *DecodedBlock
|
|
if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil {
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
if currentY < yMin {
|
|
yMin = currentY
|
|
}
|
|
ofs++
|
|
}
|
|
ofs += w - 16
|
|
}
|
|
r.yMin[pos] = yMin
|
|
|
|
return
|
|
}
|
|
|
|
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-- {
|
|
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
|
|
}
|
|
|
|
func safeColor(x int32) uint8 {
|
|
if x < 0 {
|
|
x = 0
|
|
} else if x > 255 {
|
|
x = 255
|
|
}
|
|
return uint8(x)
|
|
}
|
|
|
|
func (r *Renderer) CreateShadedImage(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-- {
|
|
for x := 0; x < pw; x++ {
|
|
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++
|
|
}
|
|
}
|
|
return image
|
|
}
|