fix updating Batch's TargetTriangles
This commit is contained in:
parent
bec6bdca6a
commit
0b5c3f2171
85
batch.go
85
batch.go
|
@ -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,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))
|
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
|
||||||
|
@ -99,30 +93,54 @@ func (b *Batch) MakePicture(p Picture) TargetPicture {
|
||||||
}
|
}
|
||||||
|
|
||||||
type batchTriangles struct {
|
type batchTriangles struct {
|
||||||
Triangles
|
tri Triangles
|
||||||
orig, trans *TrianglesData
|
tmp *TrianglesData
|
||||||
|
|
||||||
dst *Batch
|
dst *Batch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bt *batchTriangles) draw(bp *batchPicture) {
|
func (bt *batchTriangles) Len() int {
|
||||||
for i := range *bt.trans {
|
return bt.tri.Len()
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := 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,15 +149,18 @@ 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,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue