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 (
"fmt"
"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
@ -29,11 +27,10 @@ var _ BasicTarget = (*Batch)(nil)
//
// Note, that if the container does not support TrianglesColor, color masking will not work.
func NewBatch(container Triangles, pic Picture) *Batch {
return &Batch{
cont: Drawer{Triangles: container, Picture: pic},
mat: IM,
col: NRGBA{1, 1, 1, 1},
}
b := &Batch{cont: Drawer{Triangles: container, Picture: pic}}
b.SetMatrix(IM)
b.SetColorMask(NRGBA{1, 1, 1, 1})
return b
}
// 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.
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles {
bt := &batchTriangles{
Triangles: t.Copy(),
orig: MakeTrianglesData(t.Len()),
trans: MakeTrianglesData(t.Len()),
tri: t.Copy(),
tmp: MakeTrianglesData(t.Len()),
dst: b,
}
bt.orig.Update(t)
bt.trans.Update(bt.orig)
return bt
}
@ -91,7 +85,7 @@ func (b *Batch) MakePicture(p Picture) TargetPicture {
panic(fmt.Errorf("(%T).MakePicture: Picture is not a slice of Batch's Picture", b))
}
bp := &batchPicture{
Picture: p,
pic: p,
dst: b,
}
bp.orig = bp
@ -99,30 +93,54 @@ func (b *Batch) MakePicture(p Picture) TargetPicture {
}
type batchTriangles struct {
Triangles
orig, trans *TrianglesData
tri Triangles
tmp *TrianglesData
dst *Batch
}
func (bt *batchTriangles) draw(bp *batchPicture) {
for i := range *bt.trans {
transPos := mgl64.Mat3(bt.dst.mat).Mul3x1(mgl64.Vec3{
(*bt.orig)[i].Position.X(),
(*bt.orig)[i].Position.Y(),
1,
})
(*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
func (bt *batchTriangles) Len() int {
return bt.tri.Len()
}
bt.Triangles.Update(bt.trans)
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) {
bt.tmp.Update(bt.tri)
for i := range *bt.tmp {
(*bt.tmp)[i].Position = bt.dst.mat.Project((*bt.tmp)[i].Position)
(*bt.tmp)[i].Color = bt.dst.col.Mul((*bt.tmp)[i].Color)
}
cont := bt.dst.cont.Triangles
cont.SetLen(cont.Len() + bt.Triangles.Len())
cont.Slice(cont.Len()-bt.Triangles.Len(), cont.Len()).Update(bt.Triangles)
cont.SetLen(cont.Len() + bt.tri.Len())
added := cont.Slice(cont.Len()-bt.tri.Len(), cont.Len())
added.Update(bt.tri)
added.Update(bt.tmp)
bt.dst.cont.Dirty()
}
@ -131,15 +149,18 @@ func (bt *batchTriangles) Draw() {
}
type batchPicture struct {
Picture
pic Picture
orig *batchPicture
dst *Batch
}
func (bp *batchPicture) Bounds() Rect {
return bp.pic.Bounds()
}
func (bp *batchPicture) Slice(r Rect) Picture {
return &batchPicture{
Picture: bp.Picture.Slice(r),
pic: bp.pic.Slice(r),
orig: bp.orig,
dst: bp.dst,
}