/
goicmpgl.go
633 lines (543 loc) · 16.8 KB
/
goicmpgl.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package main
// speed-up compilation:
// go install -a github.com/go-gl/gl/v4.1-core/gl
// go install -a github.com/go-gl/glfw3/v3.2/glfw
import (
"fmt"
"image"
"image/color"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"runtime"
"strings"
"github.com/aubonbeurre/glplus"
glfw "github.com/go-gl/glfw3/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
"github.com/jessevdk/go-flags"
)
var (
// vertex shader
vertShader = `#version 330
in vec4 position;
in vec2 uvs;
out vec4 out_pos;
out vec2 out_uvs;
uniform mat3 ModelviewMatrix;
void main()
{
gl_Position = vec4(ModelviewMatrix * vec3(position.xy, 1.0), 0.0).xywz;
out_uvs = uvs;
}`
// fragment shader
fragShaderTex0 = `#version 330
in vec4 out_pos;
in vec2 out_uvs;
out vec4 colourOut;
uniform sampler2D tex1;
uniform float blend;
void main()
{
vec4 col0 = texture(tex1, out_uvs);
//col0 = vec4(col0.r * col0.a, col0.g * col0.a, col0.b * col0.a, col0.a);
col0 = col0 * blend;
colourOut = col0;
}`
sFragmentShaderGrid = `#version 330
uniform vec4 color1;
uniform vec4 color2;
uniform vec3 grid;
in vec4 out_pos;
in vec2 out_uvs;
out vec4 colourOut;
void main(void)
{
// grid
float blocksize = grid.z;
int x = int(out_uvs.x * grid.x / blocksize);
int y = int(out_uvs.y * grid.y / blocksize);
int evenodd = (x + y) & 1;
colourOut = color1 * evenodd + (1 - evenodd) * color2;
}`
gOffX float32 // texture coordinates
gOffY float32
gMouseX float32 // framebuffer coordinates
gMouseY float32
gMouseDown = false
gHelp = true
gZoom float32 = 1
gBlend float32 = 1
gRetinaScale float32 = 1
gImage1 image.Image
gImage2 image.Image
gDiffFlag = false
sProgram2Src = `#version 330
in vec4 out_pos;
in vec2 out_uvs;
out vec4 colourOut;
uniform sampler2D decalA;
uniform sampler2D decalB;
uniform float diffBlend;
void main()
{
vec4 srcColorA = texture(decalA, out_uvs);
vec4 srcColorB = texture(decalB, out_uvs);
vec4 diff = abs(srcColorA - srcColorB);
if(diff != vec4(0.0,0.0,0.0,0.0)) diff = vec4(1.0,0.0,0.0,1.0);
diff = srcColorA * 0.1 + diff * 0.9;
colourOut = srcColorA * (1.0 - diffBlend) + diff * diffBlend;
}
`
sProgram3Src = `#version 330
in vec4 out_pos;
in vec2 out_uvs;
out vec4 colourOut;
uniform sampler2D decalA;
uniform sampler2D decalB;
uniform float diffBlend;
void main()
{
vec4 srcColorA = texture(decalA, out_uvs);
vec4 srcColorB = texture(decalB, out_uvs);
vec4 diff = abs(srcColorA - srcColorB);
if(diff != vec4(0.0,0.0,0.0,0.0)) diff = vec4(1.0,0.0,0.0,1.0);
diff = srcColorB * 0.1 + diff * 0.9;
colourOut = diff * (1.0 - diffBlend) + srcColorB * diffBlend;
}
`
sProgram4Src = `#version 330
in vec4 out_pos;
in vec2 out_uvs;
out vec4 colourOut;
uniform sampler2D decalA;
uniform sampler2D decalB;
uniform float diffBlend;
void main()
{
vec4 srcColorA = texture(decalA, out_uvs);
vec4 srcColorB = texture(decalB, out_uvs);
vec4 diff = abs(srcColorA - srcColorB);
vec4 diff1 = diff;
if(diff1 != vec4(0.0,0.0,0.0,0.0)) diff1 = vec4(1.0,0.0,0.0,1.0);
diff1 = srcColorA * 0.1 + diff1 * 0.9;
float d = distance(srcColorA, srcColorB);
vec4 diff2 = vec4(1.0,1.0-d,1.0-d,1.0);
colourOut = diff1 * (1.0 - diffBlend) + diff2 * diffBlend;
}
`
sProgram5Src = `#version 330
in vec4 out_pos;
in vec2 out_uvs;
out vec4 colourOut;
uniform sampler2D decalA;
uniform sampler2D decalB;
uniform float diffBlend;
void main()
{
vec4 srcColorA = texture(decalA, out_uvs);
vec4 srcColorB = texture(decalB, out_uvs);
vec4 diff = abs(srcColorA - srcColorB);
vec4 diff1 = diff;
if(diff1 != vec4(0.0,0.0,0.0,0.0)) diff1 = vec4(1.0,0.0,0.0,1.0);
diff1 = srcColorB * 0.1 + diff1 * 0.9;
float d = distance(srcColorA, srcColorB);
vec4 diff2 = vec4(1.0,1.0-d,1.0-d,1.0);
colourOut = diff2 * (1.0 - diffBlend) + diff1 * diffBlend;
}
`
)
var gOpts struct {
// Slice of bool will append 'true' each time the option
// is encountered (can be set multiple times, like -vvv)
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
}
// handle GLFW errors by printing them out
func errorCallback(err glfw.ErrorCode, desc string) {
fmt.Printf("%v: %v\n", err, desc)
}
// key events are a way to get input from GLFW.
// here we check for the escape key being pressed. if it is pressed,
// request that the window be closed
func keyCallback(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.KeyEscape && action == glfw.Press {
w.SetShouldClose(true)
} else if key == glfw.KeyZ && action == glfw.Press {
gZoom = 1
gOffX = 0
gOffY = 0
gBlend = 1
} else if key == glfw.KeyUp && (action == glfw.Press || action == glfw.Repeat) {
gBlend += 0.05
if gBlend > 1 {
gBlend = 1
}
} else if key == glfw.KeyDown && (action == glfw.Press || action == glfw.Repeat) {
gBlend -= 0.05
if gBlend < 0 {
gBlend = 0
}
} else if key == glfw.KeyLeftBracket && (action == glfw.Press || action == glfw.Repeat) {
gZoom /= 2
} else if key == glfw.KeyRightBracket && (action == glfw.Press || action == glfw.Repeat) {
gZoom *= 2
} else if key == glfw.Key1 && (action == glfw.Press || action == glfw.Repeat) {
gBlend = 0
} else if key == glfw.Key2 && (action == glfw.Press || action == glfw.Repeat) {
gBlend = 1
} else if key == glfw.Key3 && (action == glfw.Press || action == glfw.Repeat) {
gBlend = 0.5
} else if key == glfw.KeyH && (action == glfw.Press || action == glfw.Repeat) {
gHelp = !gHelp
}
}
func winToFb(xpos float64, ypos float64) (x float32, y float32) {
return float32(xpos) * gRetinaScale, float32(ypos) * gRetinaScale
}
func fbToTex(xpos float32, ypos float32) (x float32, y float32) {
return xpos/gZoom - gOffX, ypos/gZoom - gOffY
}
func texToFb(xpos float32, ypos float32) (x float32, y float32) {
return (xpos + gOffX) * gZoom, (ypos + gOffY) * gZoom
}
func fbColor(xtex float32, ytex float32) (col color.Color) {
var xtexi, ytexi = int(math.Floor(float64(xtex) + .5)), int(math.Floor(float64(ytex) + .5))
if gDiffFlag {
if gBlend == 0 {
col = gImage1.At(xtexi, ytexi)
} else {
col = gImage2.At(xtexi, ytexi)
}
} else {
col = gImage1.At(xtexi, ytexi)
}
return col
}
func mouseDownCallback(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
//var xpos, ypos float64 = w.GetCursorPosition()
//gMouseX, gMouseY = winToFb(xpos, ypos)
if action == glfw.Press {
gMouseDown = true
var xtex, ytex float32 = fbToTex(gMouseX, gMouseY)
var col = fbColor(xtex, ytex)
var r, g, b, a uint32 = col.RGBA()
fmt.Printf("X,Y: %.2f %.2f RGBA: 0x%x 0x%x 0x%x 0x%x\n", xtex, ytex, r, g, b, a)
} else {
gMouseDown = false
}
}
func mouseMoveCallback(w *glfw.Window, xpos float64, ypos float64) {
var x, y float32 = winToFb(xpos, ypos)
if gMouseDown {
gOffX += (x - gMouseX) / gZoom
gOffY += (y - gMouseY) / gZoom
}
gMouseX = x
gMouseY = y
//var xtex, ytex float32 = fbToTex(x, y)
//fmt.Printf("%.2f %.2f\n", xtex, ytex)
}
func mouseWheelCallback(w *glfw.Window, xoff float64, yoff float64) {
//fmt.Printf("x=%.2f y=%.2f\n", xoff, yoff)
if yoff < 0 {
gOffX += gMouseX / gZoom
gOffY += gMouseY / gZoom
gZoom /= 2
} else if yoff > 0 {
gZoom *= 2
gOffX -= gMouseX / gZoom
gOffY -= gMouseY / gZoom
}
}
func downloadImage(url string) (path string, err error) {
var f *os.File
if f, err = ioutil.TempFile("", ""); err != nil {
return "", err
}
var resp *http.Response
if resp, err = http.Get(url); err != nil {
return "", err
}
defer resp.Body.Close()
if _, err = io.Copy(f, resp.Body); err != nil {
return "", err
}
f.Close()
return f.Name(), nil
}
func main() {
var parser = flags.NewParser(&gOpts, flags.Default)
var err error
var args []string
if args, err = parser.Parse(); err != nil {
os.Exit(1)
}
if len(args) < 1 || len(args) > 2 {
panic(fmt.Errorf("Too many or not enough arguments"))
}
gDiffFlag = len(args) == 2
// make sure that we display any errors that are encountered
//glfw.SetErrorCallback(errorCallback)
// the GLFW library has to be initialized before any of the methods
// can be invoked
if err = glfw.Init(); err != nil {
panic(err)
}
// to be tidy, make sure glfw.Terminate() is called at the end of
// the program to clean things up by using `defer`
defer glfw.Terminate()
// hints are the way you configure the features requested for the
// window and are required to be set before calling glfw.CreateWindow().
// desired number of samples to use for mulitsampling
//glfw.WindowHint(glfw.Samples, 4)
// request a OpenGL 4.1 core context
if runtime.GOOS == "darwin" {
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
} else {
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
}
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
// do the actual window creation
var window *glfw.Window
window, err = glfw.CreateWindow(1024, 768, "goicmpgl", nil, nil)
if err != nil {
// we legitimately cannot recover from a failure to create
// the window in this sample, so just bail out
panic(err)
}
// set the callback function to get all of the key input from the user
window.SetKeyCallback(keyCallback)
window.SetMouseButtonCallback(mouseDownCallback)
window.SetScrollCallback(mouseWheelCallback)
window.SetCursorPosCallback(mouseMoveCallback)
// GLFW3 can work with more than one window, so make sure we set our
// new window as the current context to operate on
window.MakeContextCurrent()
// disable v-sync for max FPS if the driver allows it
//glfw.SwapInterval(0)
// make sure that GLEW initializes all of the GL functions
glplus.Gl = glplus.NewContext()
Gl := glplus.Gl
var attribs = []string{
"position",
"uvs",
}
// compile our shaders
var progTex0 *glplus.GPProgram
if progTex0, err = glplus.LoadShaderProgram(vertShader, fragShaderTex0, attribs); err != nil {
panic(err)
}
defer progTex0.DeleteProgram()
var progGrid *glplus.GPProgram
if progGrid, err = glplus.LoadShaderProgram(vertShader, sFragmentShaderGrid, attribs); err != nil {
panic(err)
}
defer progGrid.DeleteProgram()
var diffProg2 *glplus.GPProgram
if diffProg2, err = glplus.LoadShaderProgram(vertShader, sProgram2Src, attribs); err != nil {
panic(err)
}
defer diffProg2.DeleteProgram()
var diffProg3 *glplus.GPProgram
if diffProg3, err = glplus.LoadShaderProgram(vertShader, sProgram3Src, attribs); err != nil {
panic(err)
}
defer diffProg3.DeleteProgram()
var diffProg4 *glplus.GPProgram
if diffProg4, err = glplus.LoadShaderProgram(vertShader, sProgram4Src, attribs); err != nil {
panic(err)
}
defer diffProg4.DeleteProgram()
var diffProg5 *glplus.GPProgram
if diffProg5, err = glplus.LoadShaderProgram(vertShader, sProgram5Src, attribs); err != nil {
panic(err)
}
defer diffProg5.DeleteProgram()
var image1path = args[0]
if strings.HasPrefix(image1path, "http") {
if image1path, err = downloadImage(image1path); err != nil {
panic(err)
}
}
var texture *glplus.GPTexture
if texture, gImage1, err = glplus.LoadTexture(image1path, false, false); err != nil {
panic(err)
}
defer texture.DeleteTexture()
var texture2 *glplus.GPTexture
if gDiffFlag {
var image2path = args[1]
if strings.HasPrefix(image2path, "http") {
if image2path, err = downloadImage(image2path); err != nil {
panic(err)
}
}
if texture2, gImage2, err = glplus.LoadTexture(image2path, false, false); err != nil {
panic(err)
}
defer texture2.DeleteTexture()
if texture.Size.X != texture2.Size.X || texture.Size.Y != texture2.Size.Y {
fmt.Println("WARNING: image dimensions differ!")
} else {
fmt.Printf("image dimensions: %dx%d\n", texture.Size.X, texture.Size.Y)
}
} else {
fmt.Printf("image dimensions: %dx%d\n", texture.Size.X, texture.Size.Y)
}
var fontReader *os.File
var font *glplus.Font
if fontReader, err = glplus.FreeSerif(); err != nil {
panic(err)
}
defer fontReader.Close()
if font, err = glplus.NewFont(fontReader); err != nil {
panic(err)
}
defer font.DeleteFont()
var help1 = font.NewString("1: show only A")
defer help1.DeleteString()
var help2 = font.NewString("2: show only B")
defer help2.DeleteString()
var help3 = font.NewString("3: show diff A&B")
defer help3.DeleteString()
var helph = font.NewString("h: toggle this help")
defer helph.DeleteString()
var helparrows = font.NewString("<up>,<down>: go from A to B")
defer helparrows.DeleteString()
var helpzoom = font.NewString("[]: zoom in/out (also mouse wheel)")
defer helpzoom.DeleteString()
var helpclear = font.NewString("Z: reset zoom/view")
defer helpclear.DeleteString()
var helpescape = font.NewString("ESC: quit")
defer helpescape.DeleteString()
var vbo *glplus.VBO
vbo = glplus.NewVBOQuad(progGrid, 0, 0, float32(texture.Size.X), float32(texture.Size.Y))
defer vbo.DeleteVBO()
var cnt float32
// while there's no request to close the window
for !window.ShouldClose() {
cnt++
// get the texture of the window because it may have changed since creation
width, height := window.GetFramebufferSize()
wwidth, _ := window.GetSize()
gRetinaScale = float32(width) / float32(wwidth)
//fmt.Printf("x=%d y=%d wx=%d wy=%d\n", width, height, wwidth, wheight)
if cnt >= float32(width) {
cnt = 0
}
var matrix = mgl32.Translate2D(-1, 1)
matrix = matrix.Mul3(mgl32.Scale2D(2.0/float32(width), -2.0/float32(height)))
// clear it all out
Gl.Viewport(0, 0, width, height)
Gl.ClearColor(0.0, 0.0, 0.0, 1.0)
Gl.Clear(Gl.COLOR_BUFFER_BIT | Gl.DEPTH_BUFFER_BIT)
Gl.Enable(Gl.BLEND)
Gl.BlendFunc(Gl.ONE, Gl.ONE_MINUS_SRC_ALPHA)
Gl.BlendEquation(Gl.FUNC_ADD)
var matrix3 = matrix.Mul3(mgl32.Scale2D(gZoom, gZoom))
matrix3 = matrix3.Mul3(mgl32.Translate2D(gOffX, gOffY))
// draw the grid
if true {
vbo.Bind(progGrid)
progGrid.UseProgram()
color1 := [4]float32{.4, .4, .4, 1}
color2 := [4]float32{.9, .9, .9, 1}
grid := [3]float32{float32(texture.Size.X), float32(texture.Size.Y), 8 / gZoom}
//fmt.Printf("%.2f %.2f %.2f %.2f\n", grid[0], grid[1], grid[2], grid[3])
progGrid.ProgramUniformMatrix3fv("ModelviewMatrix", matrix3)
progGrid.ProgramUniform4fv("color1", color1)
progGrid.ProgramUniform4fv("color2", color2)
progGrid.ProgramUniform3fv("grid", grid)
if err = progGrid.ValidateProgram(); err != nil {
panic(err)
}
vbo.Draw()
vbo.Unbind(progGrid)
progGrid.UnuseProgram()
}
// draw the texture
if !gDiffFlag {
vbo.Bind(progTex0)
progTex0.UseProgram()
texture.BindTexture(0)
progTex0.ProgramUniformMatrix3fv("ModelviewMatrix", matrix3)
progTex0.ProgramUniform1i("tex1", 0)
progTex0.ProgramUniform1f("blend", gBlend)
if err = progTex0.ValidateProgram(); err != nil {
panic(err)
}
vbo.Draw()
vbo.Unbind(progTex0)
progTex0.UnuseProgram()
texture.UnbindTexture(0)
} else {
var diffBlend = gBlend
var diffProg *glplus.GPProgram
if diffBlend < 0.25 {
diffBlend *= 4
diffProg = diffProg2
} else if diffBlend < 0.5 { // 0.25 -> 0.5
diffBlend = 4*diffBlend - 1
diffProg = diffProg4
} else if diffBlend < 0.75 { // 0.5 -> 0.75
diffBlend = 4*diffBlend - 2
diffProg = diffProg5
} else { // 0.75 -> 1.0=
diffBlend = 4*diffBlend - 3
diffProg = diffProg3
}
vbo.Bind(diffProg)
diffProg.UseProgram()
texture.BindTexture(0)
texture2.BindTexture(1)
diffProg.ProgramUniformMatrix3fv("ModelviewMatrix", matrix3)
diffProg.ProgramUniform1i("decalA", 0)
diffProg.ProgramUniform1i("decalB", 1)
diffProg.ProgramUniform1f("diffBlend", diffBlend)
if err = diffProg.ValidateProgram(); err != nil {
panic(err)
}
vbo.Draw()
vbo.Unbind(diffProg)
diffProg.UnuseProgram()
texture.UnbindTexture(0)
texture2.UnbindTexture(1)
}
// font
if gHelp {
color := [...]float32{0, 0, 1, 1}
bg := [...]float32{0.5, 0.5, 0.5, 0.5}
var line float32
if err = helph.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128); err != nil {
panic(err)
}
line++
help1.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
line++
help2.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
line++
help3.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
line++
helparrows.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
line++
helpzoom.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
line++
helpclear.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
line++
helpescape.Draw(font, color, bg, matrix, 0.5, 20, 100+line*128)
}
// swapping OpenGL buffers and polling events has been decoupled
// in GLFW3, so make sure to invoke both here
window.SwapBuffers()
glfw.PollEvents()
}
}
func init() {
// This is needed to arrange that main() runs on main thread.
// See documentation for functions that are only allowed to be called from the main thread.
runtime.LockOSThread()
}