2017-02-22 13:44:38 -06:00
|
|
|
package pixelgl
|
2017-01-20 11:40:48 -06:00
|
|
|
|
|
|
|
import (
|
2017-01-28 13:01:59 -06:00
|
|
|
"fmt"
|
|
|
|
|
2017-02-11 07:09:47 -06:00
|
|
|
"github.com/faiface/glhf"
|
2017-01-20 11:40:48 -06:00
|
|
|
"github.com/faiface/mainthread"
|
2017-02-22 13:44:38 -06:00
|
|
|
"github.com/faiface/pixel"
|
2017-01-20 11:40:48 -06:00
|
|
|
)
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// GLTriangles are OpenGL triangles implemented using glhf.VertexSlice.
|
2017-01-20 11:40:48 -06:00
|
|
|
//
|
|
|
|
// Triangles returned from this function support TrianglesPosition, TrianglesColor and
|
2017-03-15 16:55:43 -05:00
|
|
|
// TrianglesPicture. If you need to support more, you can "override" SetLen and Update methods.
|
2017-02-24 12:30:06 -06:00
|
|
|
type GLTriangles struct {
|
2017-04-12 04:25:11 -05:00
|
|
|
vs *glhf.VertexSlice
|
|
|
|
data []float32
|
2020-06-21 22:25:45 -05:00
|
|
|
shader *GLShader
|
2020-06-21 21:51:08 -05:00
|
|
|
clip pixel.Rect
|
2017-02-24 12:30:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ pixel.TrianglesPosition = (*GLTriangles)(nil)
|
|
|
|
_ pixel.TrianglesColor = (*GLTriangles)(nil)
|
|
|
|
_ pixel.TrianglesPicture = (*GLTriangles)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewGLTriangles returns GLTriangles initialized with the data from the supplied Triangles.
|
2017-01-20 11:40:48 -06:00
|
|
|
//
|
2017-02-24 12:30:06 -06:00
|
|
|
// Only draw the Triangles using the provided Shader.
|
2020-06-21 22:25:45 -05:00
|
|
|
func NewGLTriangles(shader *GLShader, t pixel.Triangles) *GLTriangles {
|
2017-03-05 17:28:52 -06:00
|
|
|
var gt *GLTriangles
|
2017-01-20 11:40:48 -06:00
|
|
|
mainthread.Call(func() {
|
2017-03-05 17:28:52 -06:00
|
|
|
gt = &GLTriangles{
|
2020-06-21 22:25:45 -05:00
|
|
|
vs: glhf.MakeVertexSlice(shader.s, 0, t.Len()),
|
2017-01-20 11:40:48 -06:00
|
|
|
shader: shader,
|
|
|
|
}
|
|
|
|
})
|
2017-01-28 13:01:59 -06:00
|
|
|
gt.SetLen(t.Len())
|
2017-01-20 11:40:48 -06:00
|
|
|
gt.Update(t)
|
|
|
|
return gt
|
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// VertexSlice returns the VertexSlice of this GLTriangles.
|
|
|
|
//
|
|
|
|
// You can use it to draw them.
|
|
|
|
func (gt *GLTriangles) VertexSlice() *glhf.VertexSlice {
|
|
|
|
return gt.vs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shader returns the GLTriangles's associated shader.
|
2020-06-21 22:25:45 -05:00
|
|
|
func (gt *GLTriangles) Shader() *GLShader {
|
2017-02-24 12:30:06 -06:00
|
|
|
return gt.shader
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// Len returns the number of vertices.
|
|
|
|
func (gt *GLTriangles) Len() int {
|
2017-01-20 11:40:48 -06:00
|
|
|
return len(gt.data) / gt.vs.Stride()
|
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// SetLen efficiently resizes GLTriangles to len.
|
2017-03-15 16:55:43 -05:00
|
|
|
//
|
|
|
|
// Time complexity is amortized O(1).
|
2017-06-09 18:10:59 -05:00
|
|
|
func (gt *GLTriangles) SetLen(length int) {
|
|
|
|
switch {
|
|
|
|
case length > gt.Len():
|
|
|
|
needAppend := length - gt.Len()
|
2017-01-20 11:40:48 -06:00
|
|
|
for i := 0; i < needAppend; i++ {
|
|
|
|
gt.data = append(gt.data,
|
|
|
|
0, 0,
|
|
|
|
1, 1, 1, 1,
|
2017-03-05 17:28:52 -06:00
|
|
|
0, 0,
|
|
|
|
0,
|
2017-01-20 11:40:48 -06:00
|
|
|
)
|
|
|
|
}
|
2017-06-09 18:10:59 -05:00
|
|
|
case length < gt.Len():
|
|
|
|
gt.data = gt.data[:length*gt.vs.Stride()]
|
|
|
|
default:
|
|
|
|
return
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
2019-04-15 05:20:40 -05:00
|
|
|
mainthread.Call(func() {
|
2017-06-09 18:10:59 -05:00
|
|
|
gt.vs.Begin()
|
|
|
|
gt.vs.SetLen(length)
|
|
|
|
gt.vs.End()
|
|
|
|
})
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// Slice returns a sub-Triangles of this GLTriangles in range [i, j).
|
|
|
|
func (gt *GLTriangles) Slice(i, j int) pixel.Triangles {
|
2017-03-05 17:28:52 -06:00
|
|
|
return &GLTriangles{
|
2017-01-28 13:01:59 -06:00
|
|
|
vs: gt.vs.Slice(i, j),
|
|
|
|
data: gt.data[i*gt.vs.Stride() : j*gt.vs.Stride()],
|
|
|
|
shader: gt.shader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
func (gt *GLTriangles) updateData(t pixel.Triangles) {
|
2017-01-28 13:01:59 -06:00
|
|
|
// glTriangles short path
|
2017-03-05 17:28:52 -06:00
|
|
|
if t, ok := t.(*GLTriangles); ok {
|
2017-01-28 13:01:59 -06:00
|
|
|
copy(gt.data, t.data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:51:27 -06:00
|
|
|
// TrianglesData short path
|
2017-06-09 19:19:32 -05:00
|
|
|
stride := gt.vs.Stride()
|
|
|
|
length := gt.Len()
|
2017-02-22 13:44:38 -06:00
|
|
|
if t, ok := t.(*pixel.TrianglesData); ok {
|
2017-06-09 19:19:32 -05:00
|
|
|
for i := 0; i < length; i++ {
|
2017-01-25 16:51:27 -06:00
|
|
|
var (
|
|
|
|
px, py = (*t)[i].Position.XY()
|
|
|
|
col = (*t)[i].Color
|
2017-02-23 06:07:40 -06:00
|
|
|
tx, ty = (*t)[i].Picture.XY()
|
2017-03-05 17:28:52 -06:00
|
|
|
in = (*t)[i].Intensity
|
2017-01-25 16:51:27 -06:00
|
|
|
)
|
2017-06-09 19:19:32 -05:00
|
|
|
d := gt.data[i*stride : i*stride+9]
|
2017-06-09 00:07:08 -05:00
|
|
|
d[0] = float32(px)
|
|
|
|
d[1] = float32(py)
|
|
|
|
d[2] = float32(col.R)
|
|
|
|
d[3] = float32(col.G)
|
|
|
|
d[4] = float32(col.B)
|
|
|
|
d[5] = float32(col.A)
|
|
|
|
d[6] = float32(tx)
|
|
|
|
d[7] = float32(ty)
|
|
|
|
d[8] = float32(in)
|
2017-01-25 16:51:27 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-22 13:44:38 -06:00
|
|
|
if t, ok := t.(pixel.TrianglesPosition); ok {
|
2017-06-09 19:19:32 -05:00
|
|
|
for i := 0; i < length; i++ {
|
2017-01-20 11:40:48 -06:00
|
|
|
px, py := t.Position(i).XY()
|
2017-06-09 19:19:32 -05:00
|
|
|
gt.data[i*stride+0] = float32(px)
|
|
|
|
gt.data[i*stride+1] = float32(py)
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 13:44:38 -06:00
|
|
|
if t, ok := t.(pixel.TrianglesColor); ok {
|
2017-06-09 19:19:32 -05:00
|
|
|
for i := 0; i < length; i++ {
|
2017-01-20 11:40:48 -06:00
|
|
|
col := t.Color(i)
|
2017-06-09 19:19:32 -05:00
|
|
|
gt.data[i*stride+2] = float32(col.R)
|
|
|
|
gt.data[i*stride+3] = float32(col.G)
|
|
|
|
gt.data[i*stride+4] = float32(col.B)
|
|
|
|
gt.data[i*stride+5] = float32(col.A)
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 06:07:40 -06:00
|
|
|
if t, ok := t.(pixel.TrianglesPicture); ok {
|
2017-06-09 19:19:32 -05:00
|
|
|
for i := 0; i < length; i++ {
|
2017-03-05 17:28:52 -06:00
|
|
|
pic, intensity := t.Picture(i)
|
2017-06-09 19:19:32 -05:00
|
|
|
gt.data[i*stride+6] = float32(pic.X)
|
|
|
|
gt.data[i*stride+7] = float32(pic.Y)
|
|
|
|
gt.data[i*stride+8] = float32(intensity)
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 18:10:59 -05:00
|
|
|
// Update copies vertex properties from the supplied Triangles into this GLTriangles.
|
|
|
|
//
|
|
|
|
// The two Triangles (gt and t) must be of the same len.
|
|
|
|
func (gt *GLTriangles) Update(t pixel.Triangles) {
|
|
|
|
if gt.Len() != t.Len() {
|
|
|
|
panic(fmt.Errorf("(%T).Update: invalid triangles len", gt))
|
|
|
|
}
|
|
|
|
gt.updateData(t)
|
|
|
|
|
2017-04-11 08:02:58 -05:00
|
|
|
// this code is supposed to copy the vertex data and CallNonBlock the update if
|
|
|
|
// the data is small enough, otherwise it'll block and not copy the data
|
|
|
|
if len(gt.data) < 256 { // arbitrary heurestic constant
|
|
|
|
data := append([]float32{}, gt.data...)
|
|
|
|
mainthread.CallNonBlock(func() {
|
|
|
|
gt.vs.Begin()
|
|
|
|
gt.vs.SetVertexData(data)
|
|
|
|
gt.vs.End()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
mainthread.Call(func() {
|
|
|
|
gt.vs.Begin()
|
|
|
|
gt.vs.SetVertexData(gt.data)
|
|
|
|
gt.vs.End()
|
|
|
|
})
|
|
|
|
}
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// Copy returns an independent copy of this GLTriangles.
|
|
|
|
//
|
2017-03-15 16:55:43 -05:00
|
|
|
// The returned Triangles are *GLTriangles as the underlying type.
|
2017-02-24 12:30:06 -06:00
|
|
|
func (gt *GLTriangles) Copy() pixel.Triangles {
|
2017-01-20 11:40:48 -06:00
|
|
|
return NewGLTriangles(gt.shader, gt)
|
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// Position returns the Position property of the i-th vertex.
|
|
|
|
func (gt *GLTriangles) Position(i int) pixel.Vec {
|
2017-01-20 11:40:48 -06:00
|
|
|
px := gt.data[i*gt.vs.Stride()+0]
|
|
|
|
py := gt.data[i*gt.vs.Stride()+1]
|
2017-02-22 13:44:38 -06:00
|
|
|
return pixel.V(float64(px), float64(py))
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// Color returns the Color property of the i-th vertex.
|
2017-04-09 15:00:26 -05:00
|
|
|
func (gt *GLTriangles) Color(i int) pixel.RGBA {
|
2017-01-20 11:40:48 -06:00
|
|
|
r := gt.data[i*gt.vs.Stride()+2]
|
|
|
|
g := gt.data[i*gt.vs.Stride()+3]
|
|
|
|
b := gt.data[i*gt.vs.Stride()+4]
|
|
|
|
a := gt.data[i*gt.vs.Stride()+5]
|
2017-04-09 15:00:26 -05:00
|
|
|
return pixel.RGBA{
|
2017-01-20 11:40:48 -06:00
|
|
|
R: float64(r),
|
|
|
|
G: float64(g),
|
|
|
|
B: float64(b),
|
|
|
|
A: float64(a),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 12:30:06 -06:00
|
|
|
// Picture returns the Picture property of the i-th vertex.
|
2017-03-05 17:28:52 -06:00
|
|
|
func (gt *GLTriangles) Picture(i int) (pic pixel.Vec, intensity float64) {
|
2017-01-20 11:40:48 -06:00
|
|
|
tx := gt.data[i*gt.vs.Stride()+6]
|
|
|
|
ty := gt.data[i*gt.vs.Stride()+7]
|
2017-03-05 17:28:52 -06:00
|
|
|
intensity = float64(gt.data[i*gt.vs.Stride()+8])
|
|
|
|
return pixel.V(float64(tx), float64(ty)), intensity
|
2017-01-20 11:40:48 -06:00
|
|
|
}
|
2020-06-21 21:51:08 -05:00
|
|
|
|
|
|
|
// SetClipRect sets the rectangle to scissor the triangles by
|
|
|
|
func (gt *GLTriangles) SetClipRect(r pixel.Rect) {
|
|
|
|
gt.clip = r.Norm()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClipRect gets the clipping rectangle and returns true if that
|
|
|
|
// rectangle is not the Zero Rectangle
|
|
|
|
func (gt *GLTriangles) ClipRect() (pixel.Rect, bool) {
|
|
|
|
return gt.clip, gt.clip.Area() != 0
|
|
|
|
}
|