/
gome.go
58 lines (49 loc) · 1.83 KB
/
gome.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 gome
import (
"image/color"
"log"
"github.com/go-gl/mathgl/mgl32"
"github.com/goxjs/gl"
"github.com/omustardo/gome/asset"
"github.com/omustardo/gome/input/keyboard"
"github.com/omustardo/gome/input/mouse"
"github.com/omustardo/gome/model/mesh"
"github.com/omustardo/gome/shader"
"github.com/omustardo/gome/util/fps"
"github.com/omustardo/gome/view"
"os"
)
// Init should be called near the start of the main method.
// It sets up the glfw window, shaders, input, and basic meshes among other things.
// It returns a function which should be deferred, to cleanly close everything at the end.
func Initialize(windowTitle string, windowWidth, windowHeight int, baseDir string) func() {
// log print with .go file and line number.
log.SetFlags(log.Lshortfile)
// avoid defaulting to stderr.
log.SetOutput(os.Stdout)
asset.SetBaseDir(baseDir)
// Initialize gl constants and the glfw window. Note that this must be done before all other gl usage.
if view.Window != nil {
log.Fatal("GLFW Window already initialized")
}
if err := view.Initialize(windowWidth, windowHeight, windowTitle); err != nil {
log.Fatal(err)
}
// Initialize Shaders
if err := shader.Initialize(); err != nil {
log.Fatal(err)
}
if err := gl.GetError(); err != 0 {
log.Fatalf("gl error: %v", err)
}
shader.Model.SetAmbientLight(&color.NRGBA{60, 60, 60, 0}) // 3D objects don't look 3D in max lighting, so tone it down as a default.
shader.Model.SetDiffuse(mgl32.Vec3{0, 0, 1}, &color.NRGBA{150, 150, 150, 255})
// Initialize singletons.
mouse.Initialize(view.Window)
keyboard.Initialize(view.Window)
fps.Initialize()
// Load standard meshes (cubes, rectangles, etc). These depend on OpenGL buffers, which depend on having an OpenGL
// context. They must be called sometime after glfw is initialized to work.
mesh.Initialize()
return view.Terminate
}