2017-01-12 06:47:44 -06:00
|
|
|
package pixel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Batch is a Target that allows for efficient drawing of many objects with the same Picture (but
|
|
|
|
// different slices of the same Picture are allowed).
|
|
|
|
//
|
|
|
|
// To put an object into a Batch, just draw it onto it:
|
|
|
|
// object.Draw(batch)
|
|
|
|
type Batch struct {
|
2017-01-14 09:07:51 -06:00
|
|
|
cont TrianglesDrawer
|
|
|
|
fixpic *Picture
|
2017-01-12 06:47:44 -06:00
|
|
|
|
|
|
|
pic *Picture
|
|
|
|
mat mgl32.Mat3
|
|
|
|
col NRGBA
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBatch creates an empty Batch with the specified Picture and container.
|
|
|
|
//
|
|
|
|
// The container is where objects get accumulated. Batch will support precisely those vertex
|
|
|
|
// properties, that the supplied container supports.
|
|
|
|
//
|
|
|
|
// Note, that if the container does not support TrianglesColor, color masking will not work.
|
|
|
|
func NewBatch(pic *Picture, container Triangles) *Batch {
|
|
|
|
return &Batch{
|
2017-01-14 09:07:51 -06:00
|
|
|
cont: TrianglesDrawer{Triangles: container},
|
|
|
|
fixpic: pic,
|
2017-01-12 06:47:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes all objects from the Batch.
|
|
|
|
func (b *Batch) Clear() {
|
|
|
|
b.cont.Update(&TrianglesData{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws all objects that are currently in the Batch onto another Target.
|
|
|
|
func (b *Batch) Draw(t Target) {
|
2017-01-14 09:07:51 -06:00
|
|
|
t.SetPicture(b.fixpic)
|
2017-01-12 06:47:44 -06:00
|
|
|
b.cont.Draw(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeTriangles returns a specialized copy of the provided Triangles, that draws onto this Batch.
|
|
|
|
func (b *Batch) MakeTriangles(t Triangles) Triangles {
|
|
|
|
return &batchTriangles{
|
|
|
|
Triangles: t.Copy(),
|
|
|
|
trans: t.Copy(),
|
|
|
|
batch: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 11:55:17 -06:00
|
|
|
// SetPicture sets the current Picture that will be used with the following draws. The original
|
|
|
|
// Picture of this Picture (the one from which p was obtained by slicing) must be same as the
|
|
|
|
// original Picture of the Batch's Picture.
|
2017-01-12 06:47:44 -06:00
|
|
|
func (b *Batch) SetPicture(p *Picture) {
|
2017-01-25 11:55:17 -06:00
|
|
|
if p != nil && p.Texture() != b.fixpic.Texture() {
|
2017-01-14 09:07:51 -06:00
|
|
|
panic("batch: attempted to draw with a different underlying Picture")
|
2017-01-12 07:50:28 -06:00
|
|
|
}
|
2017-01-14 09:07:51 -06:00
|
|
|
b.pic = p
|
2017-01-12 06:47:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTransform sets transforms used in the following draws onto the Batch.
|
|
|
|
func (b *Batch) SetTransform(t ...Transform) {
|
|
|
|
b.mat = transformToMat(t...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMaskColor sets a mask color used in the following draws onto the Batch.
|
|
|
|
func (b *Batch) SetMaskColor(c color.Color) {
|
|
|
|
if c == nil {
|
|
|
|
b.col = NRGBA{1, 1, 1, 1}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.col = NRGBAModel.Convert(c).(NRGBA)
|
|
|
|
}
|
|
|
|
|
|
|
|
type batchTriangles struct {
|
|
|
|
Triangles
|
|
|
|
trans Triangles
|
2017-01-12 12:30:36 -06:00
|
|
|
data TrianglesData
|
2017-01-12 06:47:44 -06:00
|
|
|
|
|
|
|
batch *Batch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *batchTriangles) Draw() {
|
|
|
|
// need to apply transforms and mask color and picture bounds
|
2017-01-12 12:30:36 -06:00
|
|
|
bt.data.Update(bt.Triangles)
|
|
|
|
for i := range bt.data {
|
2017-01-12 06:47:44 -06:00
|
|
|
transPos := bt.batch.mat.Mul3x1(mgl32.Vec3{
|
2017-01-12 12:30:36 -06:00
|
|
|
float32(bt.data[i].Position.X()),
|
|
|
|
float32(bt.data[i].Position.Y()),
|
2017-01-12 06:47:44 -06:00
|
|
|
1,
|
|
|
|
})
|
2017-01-12 12:30:36 -06:00
|
|
|
bt.data[i].Position = V(float64(transPos.X()), float64(transPos.Y()))
|
|
|
|
bt.data[i].Color = bt.data[i].Color.Mul(bt.batch.col)
|
2017-01-14 09:07:51 -06:00
|
|
|
if bt.batch.pic != nil && bt.data[i].Texture != V(-1, -1) {
|
|
|
|
bt.data[i].Texture = pictureBounds(bt.batch.pic, bt.data[i].Texture)
|
|
|
|
}
|
2017-01-12 06:47:44 -06:00
|
|
|
}
|
2017-01-12 12:30:36 -06:00
|
|
|
bt.trans.Update(&bt.data)
|
2017-01-12 06:47:44 -06:00
|
|
|
bt.batch.cont.Append(bt.trans)
|
|
|
|
}
|