From cf9cbe70874801c177ff70dd2c6328888ca25b83 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Tue, 28 Oct 2014 11:53:18 +0100 Subject: [PATCH] Fixed hill shading for transparent structures a bit. Still not optimal. --- common/renderer.go | 30 ++++++++++++++++++++++++++---- common/spans.go | 6 ++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/common/renderer.go b/common/renderer.go index 3ca50a3..d5046f3 100644 --- a/common/renderer.go +++ b/common/renderer.go @@ -55,6 +55,13 @@ func max(a, b int) int { return b } +func max32(a, b int32) int32 { + if a > b { + return a + } + return b +} + func copyData(data []byte) []byte { l := len(data) ndata := make([]byte, l, max(l, 8*1024)) @@ -429,17 +436,32 @@ func (r *Renderer) CreateShadedImage( pix[iofs+2] = background.B pix[iofs+3] = 0xff } else { - var y, y1, y2 int32 - y = r.yBuffer[ofs] + y := r.yBuffer[ofs] + t := r.tBuffer[ofs] + + opaque := t == nil || t.Top() < y + + var y1, y2 int32 + if x == 0 { y1 = y } else { y1 = r.yBuffer[ofs-1] + if opaque { + if s := r.tBuffer[ofs-1]; s != nil { + y1 = max32(y1, s.Top()) + } + } } if z == 0 { y2 = y } else { y2 = r.yBuffer[ofs+pw] + if opaque { + if s := r.tBuffer[ofs+pw]; s != nil { + y1 = max32(y1, s.Top()) + } + } } d := ((y - y1) + (y - y2)) * 12 if d > 36 { @@ -451,8 +473,8 @@ func (r *Renderer) CreateShadedImage( G: safeColor(int32(col.G) + d), B: safeColor(int32(col.B) + d), A: 0xff} - if r.tBuffer[ofs] != nil { - col = cols.BlendColors(r.tBuffer[ofs], col, y) + if !opaque { + col = cols.BlendColors(t, col, y) } pix[iofs] = col.R pix[iofs+1] = col.G diff --git a/common/spans.go b/common/spans.go index 00b1738..7623a86 100644 --- a/common/spans.go +++ b/common/spans.go @@ -136,6 +136,12 @@ func (s *Span) Len() int { return n } +func (s *Span) Top() int32 { + for ; s.Next != nil; s = s.Next { + } + return s.To +} + func (s *Span) String() string { var buf bytes.Buffer first := true