-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
362 lines (326 loc) · 9.9 KB
/
draw.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
// Copyright 2019 The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// based on golang.org/x/exp/shiny:
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package glos
import (
"image"
"image/color"
"image/draw"
"log"
"github.com/goki/gi/mat32"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/gpu"
)
func (app *appImpl) initDrawProgs() error {
if app.progInit {
return nil
}
p := theGPU.NewProgram("draw")
_, err := p.AddShader(gpu.VertexShader, "draw-vert",
`
uniform mat3 mvp;
uniform mat3 uvp;
in vec2 pos;
out vec2 uv;
void main() {
vec3 p = vec3(pos, 1);
gl_Position = vec4(mvp * p, 1);
uv = (uvp * vec3(pos, 1)).xy;
}
`+"\x00")
if err != nil {
return err
}
_, err = p.AddShader(gpu.FragmentShader, "draw-frag",
`
precision mediump float;
uniform sampler2D tex;
in vec2 uv;
out vec4 outputColor;
void main() {
outputColor = texture(tex, uv);
}
`+"\x00")
if err != nil {
return err
}
p.AddUniform("mvp", gpu.Mat3fUniType, false, 0)
p.AddUniform("uvp", gpu.Mat3fUniType, false, 0)
p.AddUniform("tex", gpu.IUniType, false, 0)
p.AddInput("pos", gpu.Vec2fVecType, gpu.VertexPosition)
p.SetFragDataVar("outputColor")
err = p.Compile(false) // showSrc debugging
if err != nil {
return err
}
app.drawProg = p
p = theGPU.NewProgram("fill")
_, err = p.AddShader(gpu.VertexShader, "fill-vert",
`
uniform mat3 mvp;
in vec2 pos;
void main() {
vec3 p = vec3(pos, 1);
gl_Position = vec4(mvp * p, 1);
}
`+"\x00")
if err != nil {
return err
}
_, err = p.AddShader(gpu.FragmentShader, "fill-frag",
`
precision mediump float;
uniform vec4 color;
out vec4 outputColor;
void main() {
outputColor = color;
}
`+"\x00")
if err != nil {
return err
}
p.AddUniform("mvp", gpu.Mat3fUniType, false, 0)
p.AddUniform("color", gpu.Vec4fUniType, false, 0)
p.AddInput("pos", gpu.Vec2fVecType, gpu.VertexPosition)
p.SetFragDataVar("outputColor")
err = p.Compile(false) // showSrc debugging
if err != nil {
return err
}
app.fillProg = p
if err != nil {
return err
}
app.progInit = true
return nil
}
// drawQuadsBuff returns a gpu.BufferMgr for the quads verticies for drawing on window
func (app *appImpl) drawQuadsBuff() gpu.BufferMgr {
pv := app.drawProg.InputByName("pos")
b := theGPU.NewBufferMgr()
vb := b.AddVectorsBuffer(gpu.StaticDraw)
vb.AddVectors(pv, false)
vb.SetLen(4)
vb.SetAllData(quadCoords)
b.Activate()
b.TransferAll()
return b
}
// fillQuadsBuff returns a gpu.BufferMgr for the quads verticies for filling window
func (app *appImpl) fillQuadsBuff() gpu.BufferMgr {
pv := app.fillProg.InputByName("pos")
b := theGPU.NewBufferMgr()
vb := b.AddVectorsBuffer(gpu.StaticDraw)
vb.AddVectors(pv, false)
vb.SetLen(4)
vb.SetAllData(quadCoords)
b.Activate()
b.TransferAll()
return b
}
// draw draws to current render target (could be window or framebuffer / texture)
// proper context must have already been established outside this call!
// dstBotZero is true if destination has Y=0 at bottom
func (app *appImpl) draw(dstSz image.Point, src2dst mat32.Mat3, src oswin.Texture, sr image.Rectangle, op draw.Op, opts *oswin.DrawOptions, qbuff gpu.BufferMgr, dstBotZero bool) {
tx := src.(*textureImpl)
sr = sr.Intersect(tx.Bounds())
if sr.Empty() {
return
}
srcBotZero := src.BotZero()
if opts != nil && opts.FlipY {
srcBotZero = !srcBotZero
}
gpu.Draw.Op(op)
gpu.Draw.DepthTest(false)
gpu.Draw.CullFace(false, true, dstBotZero) // cull back face -- dstBotZero = CCW, !dstBotZero = CW
gpu.Draw.StencilTest(false)
gpu.Draw.Multisample(false)
app.drawProg.Activate()
// Start with src-space left, top, right and bottom.
srcL := float32(sr.Min.X)
srcT := float32(sr.Min.Y)
srcR := float32(sr.Max.X)
srcB := float32(sr.Max.Y)
// Transform to dst-space via the src2dst matrix, then to a MVP matrix.
matMVP := calcMVP(dstSz.X, dstSz.Y,
src2dst[0]*srcL+src2dst[3]*srcT+src2dst[6],
src2dst[1]*srcL+src2dst[4]*srcT+src2dst[7],
src2dst[0]*srcR+src2dst[3]*srcT+src2dst[6],
src2dst[1]*srcR+src2dst[4]*srcT+src2dst[7],
src2dst[0]*srcL+src2dst[3]*srcB+src2dst[6],
src2dst[1]*srcL+src2dst[4]*srcB+src2dst[7],
dstBotZero,
)
// fmt.Printf("trgTex: %v matMVP: %v\n", trgTex, matMVP)
err := app.drawProg.UniformByName("mvp").SetValue(matMVP)
if err != nil {
log.Println(err)
}
// OpenGL's fragment shaders' UV coordinates run from (0,0)-(1,1),
// unlike vertex shaders' XY coordinates running from (-1,+1)-(+1,-1).
//
// We are drawing a rectangle PQRS, defined by two of its
// corners, onto the entire texture. The two quads may actually
// be equal, but in the general case, PQRS can be smaller.
//
// (0,0) +---------------+ (1,0)
// | P +-----+ Q |
// | | | |
// | S +-----+ R |
// (0,1) +---------------+ (1,1)
//
// The PQRS quad is always axis-aligned. First of all, convert
// from pixel space to texture space.
tw := float32(tx.size.X)
th := float32(tx.size.Y)
px := float32(sr.Min.X-0) / tw
py := float32(sr.Min.Y-0) / th
qx := float32(sr.Max.X-0) / tw
sy := float32(sr.Max.Y-0) / th
// Due to axis alignment, qy = py and sx = px.
//
// The simultaneous equations are:
// 0 + 0 + a02 = px
// 0 + 0 + a12 = py
// a00 + 0 + a02 = qx
// a10 + 0 + a12 = qy = py
// 0 + a01 + a02 = sx = px
// 0 + a11 + a12 = sy
var matUVP mat32.Mat3
if srcBotZero {
matUVP = mat32.Mat3{
qx - px, 0, 0,
0, py - sy, 0, // py - sy
px, sy, 1}
} else {
matUVP = mat32.Mat3{
qx - px, 0, 0,
0, sy - py, 0, // sy - py
px, py, 1,
}
}
err = app.drawProg.UniformByName("uvp").SetValue(matUVP)
if err != nil {
log.Println(err)
}
// fmt.Printf("matUVP: %v\n", matUVP)
tx.Activate(0)
err = app.drawProg.UniformByName("tex").SetValue(int32(0))
if err != nil {
log.Println(err)
}
qbuff.Activate()
gpu.Draw.TriangleStrips(0, 4)
}
// fill fills to current render target (could be window or framebuffer / texture)
// proper context must have already been established outside this call!
// dstBotZero is true if flipping Y axis
func (app *appImpl) fill(mvp mat32.Mat3, src color.Color, op draw.Op, qbuff gpu.BufferMgr, dstBotZero bool) {
gpu.Draw.Op(op)
gpu.Draw.CullFace(false, true, dstBotZero) // dstBotZero = CCW, else CW
gpu.Draw.DepthTest(false)
gpu.Draw.StencilTest(false)
gpu.Draw.Multisample(false)
app.fillProg.Activate()
app.fillProg.UniformByName("mvp").SetValue(mvp)
r, g, b, a := src.RGBA()
clvec4 := mat32.NewVec4(
float32(r)/65535,
float32(g)/65535,
float32(b)/65535,
float32(a)/65535,
)
app.fillProg.UniformByName("color").SetValue(clvec4)
qbuff.Activate()
gpu.Draw.TriangleStrips(0, 4)
}
// fillRect fills given rectangle, where dstSz is overall size of the destination (e.g., window)
// dstBotZero is true if destination has Y=0 at bottom
func (app *appImpl) fillRect(dstSz image.Point, dr image.Rectangle, src color.Color, op draw.Op, qbuff gpu.BufferMgr, dstBotZero bool) {
minX := float32(dr.Min.X)
minY := float32(dr.Min.Y)
maxX := float32(dr.Max.X)
maxY := float32(dr.Max.Y)
mvp := calcMVP(dstSz.X, dstSz.Y,
minX, minY,
maxX, minY,
minX, maxY, dstBotZero,
)
app.fill(mvp, src, op, qbuff, dstBotZero)
}
// drawUniform does a fill-like uniform color fill but with an arbitrary src2dst transform
// dstBotZero is true if destination has Y=0 at bottom
func (app *appImpl) drawUniform(dstSz image.Point, src2dst mat32.Mat3, src color.Color, sr image.Rectangle, op draw.Op, opts *oswin.DrawOptions, qbuff gpu.BufferMgr, dstBotZero bool) {
minX := float32(sr.Min.X)
minY := float32(sr.Min.Y)
maxX := float32(sr.Max.X)
maxY := float32(sr.Max.Y)
// Transform to dst-space via the src2dst matrix, then to a MVP matrix.
mvp := calcMVP(dstSz.X, dstSz.Y,
src2dst[0]*minX+src2dst[3]*minY+src2dst[6],
src2dst[1]*minX+src2dst[4]*minY+src2dst[7],
src2dst[0]*maxX+src2dst[3]*minY+src2dst[6],
src2dst[1]*maxX+src2dst[4]*minY+src2dst[7],
src2dst[0]*minX+src2dst[3]*maxY+src2dst[6],
src2dst[1]*minX+src2dst[4]*maxY+src2dst[7],
dstBotZero,
)
app.fill(mvp, src, op, qbuff, dstBotZero)
}
// calcMVP returns the Model View Projection matrix that maps the quadCoords
// unit square, (0, 0) to (1, 1), to a quad QV, such that QV in vertex shader
// space corresponds to the quad QP in pixel space, where QP is defined by
// three of its four corners - the arguments to this function. The three
// corners are nominally the top-left, top-right and bottom-left, but there is
// no constraint that e.g. tlx < trx.
//
// In pixel space, the window ranges from (0, 0) to (widthPx, heightPx). The
// Y-axis points downwards (unless flipped).
//
// In vertex shader space, the window ranges from (-1, +1) to (+1, -1), which
// is a 2-unit by 2-unit square. The Y-axis points upwards.
//
// if dstBotZero is true, then the y=0 is at bottom in dest, else top
//
func calcMVP(widthPx, heightPx int, tlx, tly, trx, try, blx, bly float32, dstBotZero bool) mat32.Mat3 {
// Convert from pixel coords to vertex shader coords.
invHalfWidth := 2 / float32(widthPx)
invHalfHeight := 2 / float32(heightPx)
if dstBotZero {
tlx = tlx*invHalfWidth - 1
tly = 1 - tly*invHalfHeight // 1 - min
trx = trx*invHalfWidth - 1
try = 1 - try*invHalfHeight // 1 - min
blx = blx*invHalfWidth - 1
bly = 1 - bly*invHalfHeight // 1 - (min + max)
} else {
tlx = tlx*invHalfWidth - 1
tly = tly*invHalfHeight - 1
trx = trx*invHalfWidth - 1
try = try*invHalfHeight - 1
blx = blx*invHalfWidth - 1
bly = bly*invHalfHeight - 1
}
// The resultant affine matrix:
// - maps (0, 0) to (tlx, tly).
// - maps (1, 0) to (trx, try).
// - maps (0, 1) to (blx, bly).
return mat32.Mat3{
trx - tlx, try - tly, 0,
blx - tlx, bly - tly, 0,
tlx, tly, 1,
}
}
// Note: arranged in CCW order for dstBotZero = true
// if !dstBotZero then need to reverse culling!
var quadCoords = mat32.ArrayF32{
0, 0, // top left
0, 1, // bottom left
1, 0, // top right
1, 1, // bottom right
}