Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package pixel
import (
"github.com/faiface/mainthread"
"github.com/faiface/pixel/pixelgl"
)
// NewGLTriangles returns OpenGL triangles implemented using pixelgl.VertexSlice. A few notes.
//
// Triangles returned from this function support TrianglesPosition, TrianglesColor and
// TrianglesTexture. If you need to support more, you can "override" Update and Append method.
//
// Draw method simply draws the underlying pixelgl.VertexSlice. It needs to be called in the main
// thread manually. Also, you need to take care of additional Target initialization or setting of
// uniform attributes.
func NewGLTriangles(shader *pixelgl.Shader, t Triangles) Triangles {
var gt *glTriangles
mainthread.Call(func() {
gt = &glTriangles{
vs: pixelgl.MakeVertexSlice(shader, 0, t.Len()),
shader: shader,
}
})
gt.Update(t)
return gt
}
type glTriangles struct {
vs *pixelgl.VertexSlice
data []float32
shader *pixelgl.Shader
}
func (gt *glTriangles) Len() int {
return len(gt.data) / gt.vs.Stride()
}
func (gt *glTriangles) Draw() {
gt.vs.Begin()
gt.vs.Draw()
gt.vs.End()
}
func (gt *glTriangles) resize(len int) {
if len > gt.Len() {
needAppend := len - gt.Len()
for i := 0; i < needAppend; i++ {
gt.data = append(gt.data,
0, 0,
1, 1, 1, 1,
-1, -1,
)
}
}
if len < gt.Len() {
gt.data = gt.data[:len]
}
}
func (gt *glTriangles) updateData(offset int, t Triangles) {
// TrianglesData short path
if t, ok := t.(*TrianglesData); ok {
for i := offset; i < offset+t.Len(); i++ {
var (
px, py = (*t)[i].Position.XY()
col = (*t)[i].Color
tx, ty = (*t)[i].Texture.XY()
)
gt.data[i*gt.vs.Stride()+0] = float32(px)
gt.data[i*gt.vs.Stride()+1] = float32(py)
gt.data[i*gt.vs.Stride()+2] = float32(col.R)
gt.data[i*gt.vs.Stride()+3] = float32(col.G)
gt.data[i*gt.vs.Stride()+4] = float32(col.B)
gt.data[i*gt.vs.Stride()+5] = float32(col.A)
gt.data[i*gt.vs.Stride()+6] = float32(tx)
gt.data[i*gt.vs.Stride()+7] = float32(ty)
}
return
}
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
if t, ok := t.(TrianglesPosition); ok {
for i := offset; i < offset+t.Len(); i++ {
px, py := t.Position(i).XY()
gt.data[i*gt.vs.Stride()+0] = float32(px)
gt.data[i*gt.vs.Stride()+1] = float32(py)
}
}
if t, ok := t.(TrianglesColor); ok {
for i := offset; i < offset+t.Len(); i++ {
col := t.Color(i)
gt.data[i*gt.vs.Stride()+2] = float32(col.R)
gt.data[i*gt.vs.Stride()+3] = float32(col.G)
gt.data[i*gt.vs.Stride()+4] = float32(col.B)
gt.data[i*gt.vs.Stride()+5] = float32(col.A)
}
}
if t, ok := t.(TrianglesTexture); ok {
for i := offset; i < offset+t.Len(); i++ {
tx, ty := t.Texture(i).XY()
gt.data[i*gt.vs.Stride()+6] = float32(tx)
gt.data[i*gt.vs.Stride()+7] = float32(ty)
}
}
}
func (gt *glTriangles) submitData() {
data := gt.data // avoid race condition
mainthread.CallNonBlock(func() {
gt.vs.Begin()
dataLen := len(data) / gt.vs.Stride()
if dataLen > gt.vs.Len() {
gt.vs.Append(make([]float32, (dataLen-gt.vs.Len())*gt.vs.Stride()))
}
if dataLen < gt.vs.Len() {
gt.vs = gt.vs.Slice(0, dataLen)
}
gt.vs.SetVertexData(gt.data)
gt.vs.End()
})
}
func (gt *glTriangles) Update(t Triangles) {
gt.resize(t.Len())
gt.updateData(0, t)
gt.submitData()
}
func (gt *glTriangles) Append(t Triangles) {
gt.resize(gt.Len() + t.Len())
gt.updateData(gt.Len()-t.Len(), t)
gt.submitData()
}
func (gt *glTriangles) Copy() Triangles {
return NewGLTriangles(gt.shader, gt)
}
func (gt *glTriangles) Position(i int) Vec {
px := gt.data[i*gt.vs.Stride()+0]
py := gt.data[i*gt.vs.Stride()+1]
return V(float64(px), float64(py))
}
func (gt *glTriangles) Color(i int) NRGBA {
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]
return NRGBA{
R: float64(r),
G: float64(g),
B: float64(b),
A: float64(a),
}
}
func (gt *glTriangles) Texture(i int) Vec {
tx := gt.data[i*gt.vs.Stride()+6]
ty := gt.data[i*gt.vs.Stride()+7]
return V(float64(tx), float64(ty))
}