/
hello_window_clear.go
71 lines (58 loc) · 1.88 KB
/
hello_window_clear.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
package main
import (
"log"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
func main() {
// glfw: initialize
if err := glfw.Init(); err != nil {
log.Fatal(err)
}
// glfw: terminate, clearing all previously allocated GLFW resources.
defer glfw.Terminate()
// glfw: configure
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
// glfw window creation
window, err := glfw.CreateWindow(800, 600, "LearnOpenGL", nil, nil)
if err != nil {
log.Fatal(err)
}
defer window.Destroy()
window.MakeContextCurrent()
window.SetFramebufferSizeCallback(frameBufferSizeCallback)
// gl: initializes the OpenGL bindings by loading the function pointers
// (for each OpenGL function) from the active OpenGL context.
if err := gl.Init(); err != nil {
log.Fatal(err)
}
// render loop
for !window.ShouldClose() {
// input
processInput(window)
// render
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
window.SwapBuffers()
glfw.PollEvents()
}
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
func processInput(w *glfw.Window) {
if w.GetKey(glfw.KeyEscape) == glfw.Press {
w.SetShouldClose(true)
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
func frameBufferSizeCallback(w *glfw.Window, width int, height int) {
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
gl.Viewport(0, 0, int32(width), int32(height))
// log.Printf("frameBufferSizeCallback (%d, %d)", width, height)
}