/
lib.go
58 lines (47 loc) · 1.48 KB
/
lib.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
package graphics143
import (
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
// Converts Pixels to Opengl Coordinates: X Axis
func XtoFloat(x, windowWidth int) float32 {
return float32(2.0)*(float32(x)/float32(windowWidth)) - float32(1.0)
}
// Converts Pixels to Opengl Coordinates: Y Axis
func YtoFloat(y, windowHeight int) float32 {
return float32(1.0) - (float32(2.0) * float32(y) / float32(windowHeight))
}
// NewWindow begins with initializing glfw and gl libraries.
func NewWindow(width, height int, title string, resizable bool) *glfw.Window {
if err := glfw.Init(); err != nil {
panic(err)
}
if resizable {
glfw.WindowHint(glfw.Resizable, glfw.True)
} else {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Samples, 4)
window, err := glfw.CreateWindow(width, height, title, nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
primaryMon := glfw.GetPrimaryMonitor()
_, _, monWidth, monHeight := primaryMon.GetWorkarea()
windowX := (monWidth - width) / 2
windowY := (monHeight - height) / 2
window.SetPos(windowX, windowY)
// OpenGL state
if err := gl.Init(); err != nil {
panic(err)
}
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.Enable(gl.MULTISAMPLE)
return window
}