-
Notifications
You must be signed in to change notification settings - Fork 27
/
batch.go
256 lines (202 loc) · 5.22 KB
/
batch.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2014-2016 Joseph Hager. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package engi
import (
"log"
"math"
"github.com/ajhager/webgl"
)
const size = 10000
type Drawable interface {
Texture() *webgl.Texture
Width() float32
Height() float32
View() (float32, float32, float32, float32)
}
type Batch struct {
drawing bool
lastTexture *webgl.Texture
vertices []float32
vertexVBO *webgl.Buffer
indices []uint16
indexVBO *webgl.Buffer
index int
shader *webgl.Program
inPosition int
inColor int
inTexCoords int
ufProjection *webgl.UniformLocation
projX float32
projY float32
}
func NewBatch(width, height float32) *Batch {
batch := new(Batch)
batch.shader = LoadShader(batchVert, batchFrag)
batch.inPosition = gl.GetAttribLocation(batch.shader, "in_Position")
batch.inColor = gl.GetAttribLocation(batch.shader, "in_Color")
batch.inTexCoords = gl.GetAttribLocation(batch.shader, "in_TexCoords")
batch.ufProjection = gl.GetUniformLocation(batch.shader, "uf_Projection")
batch.vertices = make([]float32, 20*size)
batch.indices = make([]uint16, 6*size)
for i, j := 0, 0; i < size*6; i, j = i+6, j+4 {
batch.indices[i+0] = uint16(j + 0)
batch.indices[i+1] = uint16(j + 1)
batch.indices[i+2] = uint16(j + 2)
batch.indices[i+3] = uint16(j + 0)
batch.indices[i+4] = uint16(j + 2)
batch.indices[i+5] = uint16(j + 3)
}
batch.indexVBO = gl.CreateBuffer()
batch.vertexVBO = gl.CreateBuffer()
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, batch.indexVBO)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, batch.indices, gl.STATIC_DRAW)
gl.BindBuffer(gl.ARRAY_BUFFER, batch.vertexVBO)
gl.BufferData(gl.ARRAY_BUFFER, batch.vertices, gl.DYNAMIC_DRAW)
gl.EnableVertexAttribArray(batch.inPosition)
gl.EnableVertexAttribArray(batch.inTexCoords)
gl.EnableVertexAttribArray(batch.inColor)
gl.VertexAttribPointer(batch.inPosition, 2, gl.FLOAT, false, 20, 0)
gl.VertexAttribPointer(batch.inTexCoords, 2, gl.FLOAT, false, 20, 8)
gl.VertexAttribPointer(batch.inColor, 4, gl.UNSIGNED_BYTE, true, 20, 16)
batch.projX = width / 2
batch.projY = height / 2
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
return batch
}
func (b *Batch) Begin() {
if b.drawing {
log.Fatal("Batch.End() must be called first")
}
b.drawing = true
gl.UseProgram(b.shader)
}
func (b *Batch) End() {
if !b.drawing {
log.Fatal("Batch.Begin() must be called first")
}
if b.index > 0 {
b.flush()
}
b.drawing = false
b.lastTexture = nil
}
func (b *Batch) flush() {
if b.lastTexture == nil {
return
}
gl.BindTexture(gl.TEXTURE_2D, b.lastTexture)
gl.Uniform2f(b.ufProjection, b.projX, b.projY)
gl.BufferSubData(gl.ARRAY_BUFFER, 0, b.vertices)
gl.DrawElements(gl.TRIANGLES, 6*b.index, gl.UNSIGNED_SHORT, 0)
b.index = 0
}
func (b *Batch) SetProjection(width, height float32) {
b.projX = width / 2
b.projY = height / 2
}
func (b *Batch) Draw(r Drawable, x, y, originX, originY, scaleX, scaleY, rotation float32, color uint32, transparency float32) {
if !b.drawing {
log.Fatal("Batch.Begin() must be called first")
}
if r.Texture() != b.lastTexture {
if b.lastTexture != nil {
b.flush()
}
b.lastTexture = r.Texture()
}
x -= originX * r.Width()
y -= originY * r.Height()
originX = r.Width() * originX
originY = r.Height() * originY
worldOriginX := x + originX
worldOriginY := y + originY
fx := -originX
fy := -originY
fx2 := float32(r.Width()) - originX
fy2 := float32(r.Height()) - originY
if scaleX != 1 || scaleY != 1 {
fx *= scaleX
fy *= scaleY
fx2 *= scaleX
fy2 *= scaleY
}
p1x := fx
p1y := fy
p2x := fx
p2y := fy2
p3x := fx2
p3y := fy2
p4x := fx2
p4y := fy
var x1 float32
var y1 float32
var x2 float32
var y2 float32
var x3 float32
var y3 float32
var x4 float32
var y4 float32
if rotation != 0 {
rot := float64(rotation * (math.Pi / 180.0))
cos := float32(math.Cos(rot))
sin := float32(math.Sin(rot))
x1 = cos*p1x - sin*p1y
y1 = sin*p1x + cos*p1y
x2 = cos*p2x - sin*p2y
y2 = sin*p2x + cos*p2y
x3 = cos*p3x - sin*p3y
y3 = sin*p3x + cos*p3y
x4 = x1 + (x3 - x2)
y4 = y3 - (y2 - y1)
} else {
x1 = p1x
y1 = p1y
x2 = p2x
y2 = p2y
x3 = p3x
y3 = p3y
x4 = p4x
y4 = p4y
}
x1 += worldOriginX
y1 += worldOriginY
x2 += worldOriginX
y2 += worldOriginY
x3 += worldOriginX
y3 += worldOriginY
x4 += worldOriginX
y4 += worldOriginY
red := (color >> 16) & 0xFF
green := ((color >> 8) & 0xFF) << 8
blue := (color & 0xFF) << 16
alpha := uint32(transparency*255.0) << 24
tint := math.Float32frombits((alpha | blue | green | red) & 0xfeffffff)
idx := b.index * 20
u, v, u2, v2 := r.View()
b.vertices[idx+0] = x1
b.vertices[idx+1] = y1
b.vertices[idx+2] = u
b.vertices[idx+3] = v
b.vertices[idx+4] = tint
b.vertices[idx+5] = x4
b.vertices[idx+6] = y4
b.vertices[idx+7] = u2
b.vertices[idx+8] = v
b.vertices[idx+9] = tint
b.vertices[idx+10] = x3
b.vertices[idx+11] = y3
b.vertices[idx+12] = u2
b.vertices[idx+13] = v2
b.vertices[idx+14] = tint
b.vertices[idx+15] = x2
b.vertices[idx+16] = y2
b.vertices[idx+17] = u
b.vertices[idx+18] = v2
b.vertices[idx+19] = tint
b.index += 1
if b.index >= size {
b.flush()
}
}