forked from faiface/glhf
/
main.go
190 lines (153 loc) · 4.13 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
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
package main
import (
"image"
"image/draw"
_ "image/png"
"os"
"github.com/faiface/glhf"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/magiccap/MagicCap/mainthread"
)
func loadImage(path string) (*image.NRGBA, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
bounds := img.Bounds()
nrgba := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
draw.Draw(nrgba, nrgba.Bounds(), img, bounds.Min, draw.Src)
return nrgba, nil
}
func run() {
var win *glfw.Window
defer func() {
mainthread.ExecMainThread(func() {
glfw.Terminate()
})
}()
mainthread.ExecMainThread(func() {
glfw.Init()
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Resizable, glfw.False)
var err error
win, err = glfw.CreateWindow(560, 697, "GLHF Rocks!", nil, nil)
if err != nil {
panic(err)
}
win.MakeContextCurrent()
glhf.Init()
})
var (
// Here we define a vertex format of our vertex slice. It's actually a basic slice
// literal.
//
// The vertex format consists of names and types of the attributes. The name is the
// name that the attribute is referenced by inside a shader.
vertexFormat = glhf.AttrFormat{
{Name: "position", Type: glhf.Vec2},
{Name: "texture", Type: glhf.Vec2},
}
// Here we declare some variables for later use.
shader *glhf.Shader
texture *glhf.Texture
slice *glhf.VertexSlice
)
// Here we load an image from a file. The loadImage function is not within the library, it
// just loads and returns a image.NRGBA.
gopherImage, err := loadImage("celebrate.png")
if err != nil {
panic(err)
}
// Every OpenGL call needs to be done inside the main thread.
mainthread.ExecMainThread(func() {
var err error
// Here we create a shader. The second argument is the format of the uniform
// attributes. Since our shader has no uniform attributes, the format is empty.
shader, err = glhf.NewShader(vertexFormat, glhf.AttrFormat{}, vertexShader, fragmentShader)
// If the shader compilation did not go successfully, an error with a full
// description is returned.
if err != nil {
panic(err)
}
// We create a texture from the loaded image.
texture = glhf.NewTexture(
gopherImage.Bounds().Dx(),
gopherImage.Bounds().Dy(),
true,
gopherImage.Pix,
)
// And finally, we make a vertex slice, which is basically a dynamically sized
// vertex array. The length of the slice is 6 and the capacity is the same.
//
// The slice inherits the vertex format of the supplied shader. Also, it should
// only be used with that shader.
slice = glhf.MakeVertexSlice(shader, 6, 6)
// Before we use a slice, we need to Begin it. The same holds for all objects in
// GLHF.
slice.Begin()
// We assign data to the vertex slice. The values are in the order as in the vertex
// format of the slice (shader). Each two floats correspond to an attribute of type
// glhf.Vec2.
slice.SetVertexData([]float32{
-1, -1, 0, 1,
+1, -1, 1, 1,
+1, +1, 1, 0,
-1, -1, 0, 1,
+1, +1, 1, 0,
-1, +1, 0, 0,
})
// When we're done with the slice, we End it.
slice.End()
})
shouldQuit := false
for !shouldQuit {
mainthread.ExecMainThread(func() {
if win.ShouldClose() {
shouldQuit = true
}
// Clear the window.
glhf.Clear(1, 1, 1, 1)
// Here we Begin/End all necessary objects and finally draw the vertex
// slice.
shader.Begin()
texture.Begin()
slice.Begin()
slice.Draw()
slice.End()
texture.End()
shader.End()
win.SwapBuffers()
glfw.PollEvents()
})
}
}
func main() {
//mainthread.Run(run)
// TODO: MagicCap initialisation.
}
var vertexShader = `
#version 330 core
in vec2 position;
in vec2 texture;
out vec2 Texture;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
Texture = texture;
}
`
var fragmentShader = `
#version 330 core
in vec2 Texture;
out vec4 color;
uniform sampler2D tex;
void main() {
color = texture(tex, Texture);
}
`