-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (103 loc) · 2.73 KB
/
main.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
package main
import (
_ "embed"
"fmt"
"os"
"runtime"
"unsafe"
"github.com/fzipp/geom"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
const (
resW = 800
resH = 600
)
type Rect struct {
geom.Vec2
geom.Size
}
var (
//go:embed shader/vertex.glsl
vertexShader string
//go:embed shader/fragment.glsl
fragmentShader string
)
func main() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
win, err := initGraphics(resW, resH)
defer glfw.Terminate()
check(err)
vertexShader, fragmentShader, err := loadShaders(vertexShader, fragmentShader)
check(err)
gl.ClearColor(0.0, 0.0, 0.4, 0.0)
var vertices quadArray
quadCoords(Rect{geom.V2(100, 100), geom.Size{W: 600, H: 400}}, &vertices)
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, unsafe.Pointer(&vertices), gl.STATIC_DRAW)
program, err := loadProgram(vertexShader, fragmentShader)
check(err)
gl.UseProgram(program)
// Init and load PMV matrices
var pmatrix, mvmatrix geom.Mat4
pmatrix.Ortho(0, resW, resH, 0, -1.0, 1.0)
loadMatrix(&pmatrix, program, "pmatrix")
mvmatrix.ID()
loadMatrix(&mvmatrix, program, "mvmatrix")
attr := uint32(gl.GetAttribLocation(program, gl.Str("vertex\x00")))
for !win.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.EnableVertexAttribArray(attr)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.VertexAttribPointer(attr, dimensions, gl.FLOAT, false, 0, nil)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, quadVertexCount)
gl.DisableVertexAttribArray(attr)
win.SwapBuffers()
glfw.PollEvents()
}
}
func initGraphics(width, height int) (*glfw.Window, error) {
if err := glfw.Init(); err != nil {
return nil, fmt.Errorf("could not initialize GLFW: %w", err)
}
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
win, err := glfw.CreateWindow(width, height, "OpenGL Demo", nil, nil)
if err != nil {
return nil, fmt.Errorf("could not create windoe: %w", err)
}
win.MakeContextCurrent()
if err := gl.Init(); err != nil {
return nil, fmt.Errorf("could not initialize OpenGL bindings: %w", err)
}
return win, nil
}
const (
quadVertexCount = 4
dimensions = 2
)
type quadArray [quadVertexCount * dimensions]float32
func quadCoords(r Rect, coords *quadArray) {
coords[0] = r.X
coords[1] = r.Y
coords[2] = r.X + r.W
coords[3] = r.Y
coords[4] = r.X
coords[5] = r.Y + r.H
coords[6] = coords[2]
coords[7] = coords[5]
}
func check(err error) {
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}