-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtutorial.go
332 lines (282 loc) · 8.09 KB
/
tutorial.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
package main
import (
"fmt"
_ "image/png"
"log"
"os"
"runtime"
"strconv"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/raedatoui/glfont"
"github.com/raedatoui/glutils"
"github.com/raedatoui/learn-opengl-golang/sections"
"github.com/raedatoui/learn-opengl-golang/sections/getstarted"
"github.com/raedatoui/learn-opengl-golang/sections/lighting"
"github.com/raedatoui/learn-opengl-golang/sections/modelloading"
)
var (
currentSlide sections.Slide
slides []sections.Slide
covers map[int]sections.Slide
slideIndex = 0
window *glfw.Window
font *glfont.Font
keys map[glfw.Key]bool
wireframe int32
)
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()
}
func init() {
dir, err := glutils.ImportPathToDir("github.com/raedatoui/learn-opengl-golang")
if err != nil {
log.Fatalln("Unable to find Go package in your GOPATH, it's needed to load assets:", err)
}
if err := os.Chdir(dir); err != nil {
log.Panicln("os.Chdir:", err)
}
}
func keyCallBack(w *glfw.Window, k glfw.Key, s int, a glfw.Action, mk glfw.ModifierKey) {
if a == glfw.Press {
if k == glfw.KeyEscape {
window.SetShouldClose(true)
}
if k == glfw.KeySpace {
gl.GetIntegerv(gl.POLYGON_MODE, &wireframe)
switch wireframe {
case gl.FILL:
gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
case gl.LINE:
gl.PolygonMode(gl.FRONT_AND_BACK, gl.POINT)
gl.PointSize(20.0)
case gl.POINT:
gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
}
}
if k >= glfw.Key0 && k <= glfw.Key9 {
c := int(k) - 48
if c < len(covers) {
slideIndex = sections.SlidePosition(slides, covers[c])
currentSlide.Close()
currentSlide = slides[slideIndex]
if err := currentSlide.InitGL(); err != nil {
log.Fatalf("slide failed %v: ", err)
}
return
}
}
newIndex := slideIndex
if s == 124 {
newIndex = slideIndex + 1
if newIndex > len(slides)-1 {
newIndex = len(slides) - 1
}
}
if s == 123 {
newIndex = slideIndex - 1
if newIndex < 0 {
newIndex = 0
}
}
if newIndex != slideIndex {
slideIndex = newIndex
currentSlide.Close()
currentSlide = slides[newIndex]
if err := currentSlide.InitGL(); err != nil {
log.Fatalf("slide failed %v: ", err)
}
return
}
keys[k] = true
} else if a == glfw.Release {
keys[k] = false
}
if currentSlide != nil {
currentSlide.HandleKeyboard(k, s, a, mk, keys)
}
}
func mouseCallback(w *glfw.Window, xpos float64, ypos float64) {
if currentSlide != nil {
currentSlide.HandleMousePosition(xpos, ypos)
}
}
func scrollCallback(w *glfw.Window, xoff float64, yoff float64) {
if currentSlide != nil {
currentSlide.HandleScroll(xoff, yoff)
}
}
func resizeCallback(w *glfw.Window, width int, height int) {
sections.WIDTH = float64(width)
sections.HEIGHT = float64(height)
sections.Ratio = float32(sections.WIDTH / sections.HEIGHT)
font.Resize(sections.WIDTH, sections.HEIGHT)
gl.Viewport(0, 0, int32(width), int32(height))
}
func fileDropCallback(w *glfw.Window, names []string) {
if currentSlide != nil {
currentSlide.HandleFiles(names)
}
}
func setup() (*glfw.Window, error) {
glfw.WindowHint(glfw.Resizable, glfw.True)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, gl.TRUE)
window, err := glfw.CreateWindow(int(sections.WIDTH), int(sections.HEIGHT), "learnopengl.com in Golang", nil, nil)
if err != nil {
return nil, err
}
window.MakeContextCurrent()
// Initialize Glow - this is the equivalent of glew
if err := gl.Init(); err != nil {
return nil, err
}
// Resize Callback
window.SetFramebufferSizeCallback(resizeCallback)
//Keyboard Callback
window.SetKeyCallback(keyCallBack)
window.SetCursorPosCallback(mouseCallback)
window.SetScrollCallback(scrollCallback)
// File Drag n Drop
window.SetDropCallback(fileDropCallback)
version := gl.GoStr(gl.GetString(gl.VERSION))
glsl := gl.GoStr(gl.GetString(gl.SHADING_LANGUAGE_VERSION))
fmt.Println("OpenGL version", version, glsl)
width, height := window.GetFramebufferSize()
gl.Viewport(0, 0, int32(width), int32(height))
return window, nil
}
func setupSlides() []sections.Slide {
// make a slice of pointers to sketch instances
return []sections.Slide{
new(sections.TitleSlide),
new(getstarted.HelloCube),
new(sections.TitleSlide),
new(getstarted.HelloWindow),
new(getstarted.HelloTriangle),
new(getstarted.TriangleEx1),
new(getstarted.TriangleEx2),
new(getstarted.HelloShaders),
new(getstarted.ShaderEx1),
new(getstarted.ShaderEx2),
new(getstarted.ShaderEx3),
new(getstarted.ShaderEx4),
new(getstarted.HelloTextures),
new(getstarted.TexturesEx1),
new(getstarted.TexturesEx2),
new(getstarted.TexturesEx3),
new(getstarted.TexturesEx4),
new(getstarted.HelloTransformations),
new(getstarted.TransformationEx1),
new(getstarted.TransformationEx2),
new(getstarted.HelloCoordinates),
new(getstarted.HelloCamera),
new(sections.TitleSlide),
new(lighting.LightingColors),
new(lighting.BasicSpecular),
new(lighting.Materials),
new(sections.TitleSlide),
new(modelloading.ModelLoading),
new(sections.TitleSlide),
new(sections.TitleSlide),
new(sections.TitleSlide),
new(sections.TitleSlide),
}
}
func main() {
// init GLFW
if err := glfw.Init(); err != nil {
log.Fatalln("failed to initialize glfw:", err)
}
defer glfw.Terminate()
keys = make(map[glfw.Key]bool)
// create window
w, err := setup()
if err != nil {
log.Fatalf("cant create window %v", err)
}
window = w
//load font (fontfile, font scale, window width, window height
f, err := glfont.LoadFont("_assets/fonts/huge_agb_v5.ttf", int32(52), sections.WIDTH, sections.HEIGHT)
if err != nil {
log.Fatalf("LoadFont: %v", err)
}
font = f
c := glutils.White.To32()
font.SetColor(c.R, c.G, c.B, 1.0)
slides = setupSlides()
l := len(slides)
covers = make(map[int]sections.Slide)
titles := []string{
"LearnOpenGL in Go\n \n" +
"0. Test installation using go-gl\n" +
"1. Getting Started\n" +
"2. Lighting\n" +
"3. Model Loading\n" +
"4. Advanced OpenGL\n" +
"5. Advanced Lighting\n" +
"6. PBR\n" +
"7. In Practice",
"Section 1: Getting Started",
"Section 2: Lighting",
"Section 3: Model Loading",
"Section 4: Advanced OpenGL",
"Section 5: Advanced Lighting",
"Section 6: PBR",
"Section 7: In Practice",
}
count := 0
titleSlideType := §ions.TitleSlide{}
for x, slide := range slides {
c := glutils.StepColor(glutils.Magenta, glutils.Black, l, x)
if glutils.IsType(slide, titleSlideType) {
covers[count] = slide
if err := slide.Init(f, c, titles[count]); err != nil {
log.Fatalf("Failed setting up sketch: %v", err)
}
count += 1
} else {
if err := slide.Init(f, c); err != nil {
log.Fatalf("Failed setting up sketch: %v", err)
}
}
}
currentSlide = slides[0]
if err := currentSlide.InitGL(); err != nil {
log.Fatalf("Failed initializing GL for slide: %v", err)
}
// TODO: do we always need to enabled the depth test?
gl.Enable(gl.DEPTH_TEST)
//gl.DepthFunc(gl.LESS)
// TODO: how to effectively blend shapes that use a frag shader with
// TODO: transparency
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
var maxAttrib int32
gl.GetIntegerv(gl.MAX_VERTEX_ATTRIBS, &maxAttrib)
glutils.InitFPS()
// loop
for !window.ShouldClose() {
// Update
currentSlide.Update()
//Render
currentSlide.Draw()
if currentSlide.DrawText() {
font.Printf(30, 30, 0.5, currentSlide.GetHeader())
if currentSlide.GetSubHeader() != "" {
font.Printf(30, 50, 0.3, currentSlide.GetSubHeader())
}
}
font.Printf(30, float32(sections.HEIGHT)-20, 0.2, currentSlide.GetColorHex())
fps := "FPS: " + strconv.FormatFloat(glutils.CalcFPS(1.0), 'f', 2, 64)
font.Printf(float32(sections.WIDTH)-80, float32(sections.HEIGHT)-20, 0.25, fps)
window.SwapBuffers()
// Poll Events
glfw.PollEvents()
}
currentSlide.Close()
}