fix updating Batch's TargetTriangles

This commit is contained in:
faiface 2017-03-19 19:49:08 +01:00
parent bec6bdca6a
commit 0b5c3f2171
1 changed files with 57 additions and 36 deletions

View File

@ -3,8 +3,6 @@ package pixel
import ( import (
"fmt" "fmt"
"image/color" "image/color"
"github.com/go-gl/mathgl/mgl64"
) )
// Batch is a Target that allows for efficient drawing of many objects with the same Picture (but // Batch is a Target that allows for efficient drawing of many objects with the same Picture (but
@ -29,11 +27,10 @@ var _ BasicTarget = (*Batch)(nil)
// //
// Note, that if the container does not support TrianglesColor, color masking will not work. // Note, that if the container does not support TrianglesColor, color masking will not work.
func NewBatch(container Triangles, pic Picture) *Batch { func NewBatch(container Triangles, pic Picture) *Batch {
return &Batch{ b := &Batch{cont: Drawer{Triangles: container, Picture: pic}}
cont: Drawer{Triangles: container, Picture: pic}, b.SetMatrix(IM)
mat: IM, b.SetColorMask(NRGBA{1, 1, 1, 1})
col: NRGBA{1, 1, 1, 1}, return b
}
} }
// Dirty notifies Batch about an external modification of it's container. If you retain access to // Dirty notifies Batch about an external modification of it's container. If you retain access to
@ -75,13 +72,10 @@ func (b *Batch) SetColorMask(c color.Color) {
// MakeTriangles returns a specialized copy of the provided Triangles that draws onto this Batch. // MakeTriangles returns a specialized copy of the provided Triangles that draws onto this Batch.
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles { func (b *Batch) MakeTriangles(t Triangles) TargetTriangles {
bt := &batchTriangles{ bt := &batchTriangles{
Triangles: t.Copy(), tri: t.Copy(),
orig: MakeTrianglesData(t.Len()), tmp: MakeTrianglesData(t.Len()),
trans: MakeTrianglesData(t.Len()), dst: b,
dst: b,
} }
bt.orig.Update(t)
bt.trans.Update(bt.orig)
return bt return bt
} }
@ -91,38 +85,62 @@ func (b *Batch) MakePicture(p Picture) TargetPicture {
panic(fmt.Errorf("(%T).MakePicture: Picture is not a slice of Batch's Picture", b)) panic(fmt.Errorf("(%T).MakePicture: Picture is not a slice of Batch's Picture", b))
} }
bp := &batchPicture{ bp := &batchPicture{
Picture: p, pic: p,
dst: b, dst: b,
} }
bp.orig = bp bp.orig = bp
return bp return bp
} }
type batchTriangles struct { type batchTriangles struct {
Triangles tri Triangles
orig, trans *TrianglesData tmp *TrianglesData
dst *Batch dst *Batch
} }
func (bt *batchTriangles) Len() int {
return bt.tri.Len()
}
func (bt *batchTriangles) SetLen(len int) {
bt.tri.SetLen(len)
bt.tmp.SetLen(len)
}
func (bt *batchTriangles) Slice(i, j int) Triangles {
return &batchTriangles{
tri: bt.tri.Slice(i, j),
tmp: bt.tmp.Slice(i, j).(*TrianglesData),
dst: bt.dst,
}
}
func (bt *batchTriangles) Update(t Triangles) {
bt.tri.Update(t)
}
func (bt *batchTriangles) Copy() Triangles {
return &batchTriangles{
tri: bt.tri.Copy(),
tmp: bt.tmp.Copy().(*TrianglesData),
dst: bt.dst,
}
}
func (bt *batchTriangles) draw(bp *batchPicture) { func (bt *batchTriangles) draw(bp *batchPicture) {
for i := range *bt.trans { bt.tmp.Update(bt.tri)
transPos := mgl64.Mat3(bt.dst.mat).Mul3x1(mgl64.Vec3{
(*bt.orig)[i].Position.X(), for i := range *bt.tmp {
(*bt.orig)[i].Position.Y(), (*bt.tmp)[i].Position = bt.dst.mat.Project((*bt.tmp)[i].Position)
1, (*bt.tmp)[i].Color = bt.dst.col.Mul((*bt.tmp)[i].Color)
})
(*bt.trans)[i].Position = V(float64(transPos.X()), float64(transPos.Y()))
(*bt.trans)[i].Color = (*bt.orig)[i].Color.Mul(bt.dst.col)
(*bt.trans)[i].Picture = (*bt.orig)[i].Picture
(*bt.trans)[i].Intensity = (*bt.orig)[i].Intensity
} }
bt.Triangles.Update(bt.trans)
cont := bt.dst.cont.Triangles cont := bt.dst.cont.Triangles
cont.SetLen(cont.Len() + bt.Triangles.Len()) cont.SetLen(cont.Len() + bt.tri.Len())
cont.Slice(cont.Len()-bt.Triangles.Len(), cont.Len()).Update(bt.Triangles) added := cont.Slice(cont.Len()-bt.tri.Len(), cont.Len())
added.Update(bt.tri)
added.Update(bt.tmp)
bt.dst.cont.Dirty() bt.dst.cont.Dirty()
} }
@ -131,17 +149,20 @@ func (bt *batchTriangles) Draw() {
} }
type batchPicture struct { type batchPicture struct {
Picture pic Picture
orig *batchPicture orig *batchPicture
dst *Batch dst *Batch
} }
func (bp *batchPicture) Bounds() Rect {
return bp.pic.Bounds()
}
func (bp *batchPicture) Slice(r Rect) Picture { func (bp *batchPicture) Slice(r Rect) Picture {
return &batchPicture{ return &batchPicture{
Picture: bp.Picture.Slice(r), pic: bp.pic.Slice(r),
orig: bp.orig, orig: bp.orig,
dst: bp.dst, dst: bp.dst,
} }
} }