1
0
forked from MTSR/mapserver

working renderer

This commit is contained in:
NatureFreshMilk 2019-01-11 13:30:51 +01:00
parent f6547e1d69
commit 2c2e20404e
2 changed files with 89 additions and 7 deletions

View File

@ -5,7 +5,9 @@ import (
"mapserver/coords" "mapserver/coords"
"mapserver/mapblockaccessor" "mapserver/mapblockaccessor"
"image" "image"
"image/color" "image/draw"
"errors"
"fmt"
) )
type MapBlockRenderer struct { type MapBlockRenderer struct {
@ -20,14 +22,89 @@ func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *co
const ( const (
IMG_SCALE = 16 IMG_SCALE = 16
IMG_SIZE = IMG_SCALE * 16 IMG_SIZE = IMG_SCALE * 16
EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK = 16*16
) )
func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) *image.RGBA { func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) (*image.NRGBA, error) {
if pos1.X != pos2.X {
return nil, errors.New("X does not line up")
}
if pos1.Z != pos2.Z {
return nil, errors.New("Z does not line up")
}
upLeft := image.Point{0, 0} upLeft := image.Point{0, 0}
lowRight := image.Point{IMG_SIZE, IMG_SIZE} lowRight := image.Point{IMG_SIZE, IMG_SIZE}
img := image.NewNRGBA(image.Rectangle{upLeft, lowRight})
img := image.NewRGBA(image.Rectangle{upLeft, lowRight}) maxY := pos1.Y
cyan := color.RGBA{100, 200, 200, 0xff} minY := pos2.Y
img.Set(10, 10, cyan)
return img if minY > maxY {
maxY, minY = minY, maxY
}
foundBlocks := 0
xzOccupationMap := make([][]bool, 16)
for x := range xzOccupationMap {
xzOccupationMap[x] = make([]bool, 16)
}
for mapBlockY := maxY; mapBlockY >= minY; mapBlockY-- {
currentPos := coords.NewMapBlockCoords(pos1.X, mapBlockY, pos1.Z)
mb, err := r.accessor.GetMapBlock(currentPos)
if err != nil {
return nil, err
}
if mb == nil {
continue
}
for x := 0; x < 16; x++ {
for z := 0; z < 16; z++ {
for y := 15; y >= 0; y-- {
if xzOccupationMap[x][z] {
continue
}
nodeName := mb.GetNodeName(x,y,z)
fmt.Println(x,y,z, nodeName)//XXX
if nodeName == "" {
continue
}
c := r.colors.GetColor(nodeName)
if c == nil {
continue
}
rect := image.Rect(
x*IMG_SCALE, z*IMG_SCALE,
(x*IMG_SCALE)+IMG_SCALE, (z*IMG_SCALE)+IMG_SCALE,
)
foundBlocks++
xzOccupationMap[x][z] = true
draw.Draw(img, rect, &image.Uniform{c}, image.ZP, draw.Src)
if foundBlocks == EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK {
break
}
}
if foundBlocks == EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK {
break
}
}
}
if foundBlocks == EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK {
break
}
}
return img, nil
} }

View File

@ -32,10 +32,15 @@ func TestSimpleRender(t *testing.T) {
cache := mapblockaccessor.NewMapBlockAccessor(a) cache := mapblockaccessor.NewMapBlockAccessor(a)
c := colormapping.NewColorMapping() c := colormapping.NewColorMapping()
err = c.LoadVFSColors(false, "/colors.txt")
if err != nil {
t.Fatal(err)
}
r := NewMapBlockRenderer(cache, c) r := NewMapBlockRenderer(cache, c)
img := r.Render(coords.NewMapBlockCoords(0, 10, 0), coords.NewMapBlockCoords(0, -1, 0)) img, _ := r.Render(coords.NewMapBlockCoords(0, 10, 0), coords.NewMapBlockCoords(0, -1, 0))
f, _ := os.Create("image.png") f, _ := os.Create("image.png")
png.Encode(f, img) png.Encode(f, img)
f.Close()
} }