-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer_ui.go
396 lines (321 loc) · 8.59 KB
/
renderer_ui.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package noodle
import (
"log"
"math"
)
//uiRendererVertexLength is how many bytes are in each "vertex" element.
const uiRendererVertexLength = 44
//UIRenderer renders UVTiles in a batched manner
type UIRenderer struct {
Zoom float32
scale float32
scaleInput bool
shader *Shader
inPosition WebGLAttributeLocation
inTexCoords WebGLAttributeLocation
inSliceCoords WebGLAttributeLocation
inDimension WebGLAttributeLocation
inColor WebGLAttributeLocation
uProjection WebGLUniformLocation
uSampler WebGLUniformLocation
uBorder WebGLUniformLocation
vertices []float32
indices []uint16
vertexBuffer WebGLBuffer
indexBuffer WebGLBuffer
drawing bool
index int
sprite *SliceSprite
texture *Texture
}
//NewUIRenderer creates a new sprite renderer
func NewUIRenderer() *UIRenderer {
b := &UIRenderer{}
b.Zoom = 2.0
b.scale = 50.0
b.scaleInput = false
//Prepare the shader
var shaderError error
b.shader, shaderError = LoadShaderFromURL("resources/shader/ui.vert", "resources/shader/ui.frag")
if shaderError != nil {
log.Fatalln("Failed to compile batch shader!", shaderError)
return nil
}
b.inPosition = b.shader.GetAttribLocation("position")
b.inTexCoords = b.shader.GetAttribLocation("texcoords")
b.inSliceCoords = b.shader.GetAttribLocation("slicecoords")
b.inDimension = b.shader.GetAttribLocation("dimension")
b.inColor = b.shader.GetAttribLocation("color")
b.uProjection = b.shader.GetUniformLocation("uProjection")
b.uSampler = b.shader.GetUniformLocation("uSampler")
b.uBorder = b.shader.GetUniformLocation("uBorder")
//Prepare the verticies
b.vertices = make([]float32, uiRendererVertexLength*batchMaxSize)
b.indices = make([]uint16, 6*batchMaxSize)
//Link all the indicies with the verticies, forming the quads
for i, j := 0, 0; i < batchMaxSize*6; i, j = i+6, j+4 {
b.indices[i+0] = uint16(j + 0)
b.indices[i+1] = uint16(j + 1)
b.indices[i+2] = uint16(j + 2)
b.indices[i+3] = uint16(j + 0)
b.indices[i+4] = uint16(j + 2)
b.indices[i+5] = uint16(j + 3)
}
//Create the buffers
b.indexBuffer = GL.CreateBuffer()
b.vertexBuffer = GL.CreateBuffer()
b.setupBuffers()
return b
}
func (b *UIRenderer) setupBuffers() {
GL.BindBuffer(GlElementArrayBuffer, b.indexBuffer)
GL.BufferData(GlElementArrayBuffer, b.indices, GlStaticDraw)
GL.BindBuffer(GlArrayBuffer, b.vertexBuffer)
GL.BufferData(GlArrayBuffer, b.vertices, GlDynamicDraw)
//Enable them
GL.EnableVertexAttribArray(b.inPosition)
GL.EnableVertexAttribArray(b.inTexCoords)
GL.EnableVertexAttribArray(b.inSliceCoords)
GL.EnableVertexAttribArray(b.inDimension)
GL.EnableVertexAttribArray(b.inColor)
//Bind the attributes
//these numbers represent the values in the vertices buffer we are pushing, not the shader memory
GL.VertexAttribPointer(b.inPosition, 2, GlFloat, false, uiRendererVertexLength, 0)
GL.VertexAttribPointer(b.inTexCoords, 4, GlFloat, false, uiRendererVertexLength, 8)
GL.VertexAttribPointer(b.inSliceCoords, 2, GlFloat, false, uiRendererVertexLength, 24)
GL.VertexAttribPointer(b.inDimension, 2, GlFloat, false, uiRendererVertexLength, 32)
GL.VertexAttribPointer(b.inColor, 4, GlUnsignedByte, true, uiRendererVertexLength, 40)
}
//Screen2UISpace converts the screen cooridinates to UI coords
func (b *UIRenderer) Screen2UISpace(screen Vector2) Vector2 {
point := screen.Scale(b.Zoom)
if b.scaleInput {
point = point.Scale(1.0 / b.scale)
}
return point
}
//GetScale gets the current scale
func (b *UIRenderer) GetScale() float32 {
return b.scale
}
//SetScale sets how zoomed in the rectangles appear
func (b *UIRenderer) SetScale(scale float32) {
b.scale = scale
}
//ScaleInput will scale all the rectangles, so that they are pixel perfect
func (b *UIRenderer) ScaleInput(state bool) {
b.scaleInput = state
}
//Begin starts a UIRenderer
func (b *UIRenderer) Begin() *UIRenderer {
if b.drawing {
log.Fatal("b.End() must be called first")
}
b.drawing = true
b.setupBuffers()
GL.UseProgram(b.shader.GetProgram())
GL.Enable(GlBlend)
GL.BlendFunc(GlSrcColor, GlOneMinusSrcAlpha)
//Set the projection X and Y
projX := float32(Width()) / b.Zoom
projY := float32(Height()) / b.Zoom
GL.Uniform2f(b.uProjection, projX, projY)
//Clear the cache
b.texture = nil
b.sprite = nil
return b
}
//End finalises a UIRenderer
func (b *UIRenderer) End() *UIRenderer {
if !b.drawing {
log.Fatal("b.Begin() must be called first")
}
if b.index > 0 {
b.flush()
}
b.drawing = false
return b
}
//flush pushes the texture to GL
func (b *UIRenderer) flush() {
//Bind the texture
if b.texture != b.sprite.Texture() {
b.texture = b.sprite.Texture()
}
if b.texture != nil {
b.texture.SetSampler(b.uSampler, 0)
}
//Update the border
border := b.sprite.relativeBorder()
GL.Uniform2v(b.uBorder, border)
//Draw the buffer
GL.BufferSubData(GlArrayBuffer, 0, b.vertices)
GL.DrawElements(GlTriangles, 6*b.index, GlUnsignedShort, 0)
//Draw the debug outlines. This involves unbinding the texture,
// then drawing all the triangles. If loops is enabled, then we will go in pairs.
if DebugDraw {
GL.UnbindTexture(b.texture.target)
if DebugDrawLoops {
for dloop := 0; dloop < b.index; dloop++ {
GL.DrawElements(GlLineLoop, 6, GlUnsignedShort, 4*dloop*3)
}
} else {
GL.DrawElements(GlLines, 6*b.index, GlUnsignedShort, 0)
}
}
//Reset the index
b.index = 0
}
//SetSprite sets teh current sprite
func (b *UIRenderer) SetSprite(sprite *SliceSprite) {
//Flush previous sprites
if sprite != b.sprite && b.sprite != nil {
b.flush()
}
//Setup the new sprite
b.sprite = sprite
}
//Draw a particular texture
func (b *UIRenderer) Draw(rect Rectangle, color Color) {
if !b.drawing {
log.Fatal("Batch.Begin() must be called first")
}
if b.sprite == nil {
log.Fatal("Renderer.SetSprite must be called first")
}
if b.scaleInput {
rect = rect.SetPosition(rect.Position().Divide(b.scale))
rect = rect.SetSize(rect.Size().Divide(b.scale))
}
//Prepare position and scale
pos := rect.Position()
size := rect.Size()
x := pos.X
y := pos.Y
scaleX := b.scale //scale.X
scaleY := b.scale //scale.Y
//Unsused additional details that can be used for rotations and stuff
rotation := float32(0)
worldOriginX := x
worldOriginY := y
fx := float32(0)
fy := float32(0)
fx2 := size.X
fy2 := size.Y
//Update the scale
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
//Prepare the tin
tint := color.ToTint()
//Get the min/max
min, max := b.sprite.Slice()
u := min.X
v := min.Y
u2 := max.X
v2 := max.Y
//Get the dimension
dimensions := b.sprite.dimension(rect.Size())
dx := dimensions.X
dy := dimensions.Y
//Setup the verts
idx := b.index * uiRendererVertexLength
b.vertices[idx+0] = x1
b.vertices[idx+1] = y1
b.vertices[idx+2] = u
b.vertices[idx+3] = v
b.vertices[idx+4] = u2
b.vertices[idx+5] = v2
b.vertices[idx+6] = 0
b.vertices[idx+7] = 0
b.vertices[idx+8] = dx
b.vertices[idx+9] = dy
b.vertices[idx+10] = tint
b.vertices[idx+11] = x4
b.vertices[idx+12] = y4
b.vertices[idx+13] = u
b.vertices[idx+14] = v
b.vertices[idx+15] = u2
b.vertices[idx+16] = v2
b.vertices[idx+17] = 1
b.vertices[idx+18] = 0
b.vertices[idx+19] = dx
b.vertices[idx+20] = dy
b.vertices[idx+21] = tint
b.vertices[idx+22] = x3
b.vertices[idx+23] = y3
b.vertices[idx+24] = u
b.vertices[idx+25] = v
b.vertices[idx+26] = u2
b.vertices[idx+27] = v2
b.vertices[idx+28] = 1
b.vertices[idx+29] = 1
b.vertices[idx+30] = dx
b.vertices[idx+31] = dy
b.vertices[idx+32] = tint
b.vertices[idx+33] = x2
b.vertices[idx+34] = y2
b.vertices[idx+35] = u
b.vertices[idx+36] = v
b.vertices[idx+37] = u2
b.vertices[idx+38] = v2
b.vertices[idx+39] = 0
b.vertices[idx+40] = 1
b.vertices[idx+41] = dx
b.vertices[idx+42] = dy
b.vertices[idx+43] = tint
//todo: region code hered
b.index++
if b.index >= batchMaxSize {
b.flush()
}
}