-
Notifications
You must be signed in to change notification settings - Fork 68
/
gfx.go
221 lines (192 loc) · 5.87 KB
/
gfx.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
package video
import (
"image"
"image/draw"
"os"
"github.com/go-gl/gl/v2.1/gl"
)
// Color is an RGBA type that we use in the menu
type Color struct {
R, G, B, A float32
}
// Alpha sets the alpha channel of a color
func (color Color) Alpha(alpha float32) Color {
color.A = alpha
return color
}
// XYWHTo4points converts coordinates from (x, y, width, height) to (x1, y1, x2, y2, x3, y3, x4, y4)
func XYWHTo4points(x, y, w, h, fbh float32) (x1, y1, x2, y2, x3, y3, x4, y4 float32) {
x1 = x
x2 = x
x3 = x + w
x4 = x + w
y1 = fbh - (y + h)
y2 = fbh - y
y3 = fbh - (y + h)
y4 = fbh - y
return
}
func rotateUV(va []float32, rot uint) []float32 {
switch rot {
case 1: // 90 degrees
va[2] = 0
va[3] = 0
va[6] = 1
va[7] = 0
va[10] = 0
va[11] = 1
va[14] = 1
va[15] = 1
case 2: // 180 degrees
va[2] = 1
va[3] = 0
va[6] = 1
va[7] = 1
va[10] = 0
va[11] = 0
va[14] = 0
va[15] = 1
case 3: // 270 degrees
va[2] = 1
va[3] = 1
va[6] = 0
va[7] = 1
va[10] = 1
va[11] = 0
va[14] = 0
va[15] = 0
}
return va
}
// DrawImage draws an image with x, y, w, h
func (video *Video) DrawImage(image uint32, x, y, w, h float32, scale float32, c Color) {
va := video.vertexArray(x, y, w, h, scale)
gl.UseProgram(video.demulProgram)
gl.Uniform4f(gl.GetUniformLocation(video.demulProgram, gl.Str("color\x00")), c.R, c.G, c.B, c.A)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
bindVertexArray(video.vao)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, image)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(va)*4, gl.Ptr(va), gl.STATIC_DRAW)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)
bindVertexArray(0)
gl.BindTexture(gl.TEXTURE_2D, 0)
gl.UseProgram(0)
gl.Disable(gl.BLEND)
}
// ScissorStart starts a GL scissor box, don't forget to call ScissorEnd
func (video *Video) ScissorStart(x, y, w, h int32) {
gl.Enable(gl.SCISSOR_TEST)
gl.Scissor(x, y, w, h)
}
// ScissorEnd closes a GL scissor box
func (video *Video) ScissorEnd() {
gl.Disable(gl.SCISSOR_TEST)
}
func (video *Video) vertexArray(x, y, w, h, scale float32) []float32 {
fbw, fbh := video.Window.GetFramebufferSize()
ffbw := float32(fbw)
ffbh := float32(fbh)
w *= scale
h *= scale
x1, y1, x2, y2, x3, y3, x4, y4 := XYWHTo4points(x, y, w, h, ffbh)
return []float32{
// X, Y, U, V
x1/ffbw*2 - 1, y1/ffbh*2 - 1, 0, 1, // left-bottom
x2/ffbw*2 - 1, y2/ffbh*2 - 1, 0, 0, // left-top
x3/ffbw*2 - 1, y3/ffbh*2 - 1, 1, 1, // right-bottom
x4/ffbw*2 - 1, y4/ffbh*2 - 1, 1, 0, // right-top
}
}
// DrawBorder draws a colored rectangle border
func (video *Video) DrawBorder(x, y, w, h, borderWidth float32, c Color) {
va := video.vertexArray(x, y, w, h, 1.0)
gl.UseProgram(video.borderProgram)
gl.Uniform1f(gl.GetUniformLocation(video.borderProgram, gl.Str("border_width\x00")), borderWidth)
gl.Uniform4f(gl.GetUniformLocation(video.borderProgram, gl.Str("color\x00")), c.R, c.G, c.B, c.A)
gl.Uniform2f(gl.GetUniformLocation(video.borderProgram, gl.Str("size\x00")), w, h)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
bindVertexArray(video.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(va)*4, gl.Ptr(va), gl.STATIC_DRAW)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)
bindVertexArray(0)
gl.UseProgram(0)
gl.Disable(gl.BLEND)
}
// DrawRect draws a rectangle and supports rounded corners
func (video *Video) DrawRect(x, y, w, h, r float32, c Color) {
va := video.vertexArray(x, y, w, h, 1.0)
gl.UseProgram(video.roundedProgram)
gl.Uniform4f(gl.GetUniformLocation(video.roundedProgram, gl.Str("color\x00")), c.R, c.G, c.B, c.A)
gl.Uniform1f(gl.GetUniformLocation(video.roundedProgram, gl.Str("radius\x00")), r)
gl.Uniform2f(gl.GetUniformLocation(video.roundedProgram, gl.Str("size\x00")), w, h)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
bindVertexArray(video.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(va)*4, gl.Ptr(va), gl.STATIC_DRAW)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)
bindVertexArray(0)
gl.UseProgram(0)
gl.Disable(gl.BLEND)
}
// DrawCircle draws a circle
func (video *Video) DrawCircle(x, y, r float32, c Color) {
va := video.vertexArray(x-r, y-r, r*2, r*2, 1.0)
gl.UseProgram(video.circleProgram)
gl.Uniform4f(gl.GetUniformLocation(video.circleProgram, gl.Str("color\x00")), c.R, c.G, c.B, c.A)
gl.Uniform1f(gl.GetUniformLocation(video.circleProgram, gl.Str("radius\x00")), r)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
bindVertexArray(video.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(va)*4, gl.Ptr(va), gl.STATIC_DRAW)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)
bindVertexArray(0)
gl.UseProgram(0)
gl.Disable(gl.BLEND)
}
func textureLoad(rgba *image.RGBA) uint32 {
var texture uint32
gl.GenTextures(1, &texture)
gl.ActiveTexture(gl.TEXTURE1)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(rgba.Rect.Size().X),
int32(rgba.Rect.Size().Y),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix))
gl.GenerateMipmap(gl.TEXTURE_2D)
return texture
}
// NewImage opens an image file, upload it the the GPU and returns the texture id
func NewImage(file string) uint32 {
imgFile, err := os.Open(file)
if err != nil {
return 0
}
img, _, err := image.Decode(imgFile)
if err != nil {
return 0
}
rgba := image.NewRGBA(img.Bounds())
if rgba.Stride != rgba.Rect.Size().X*4 {
return 0
}
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
return textureLoad(rgba)
}