-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (89 loc) · 3.03 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
package main
import (
"time"
"fmt"
"os"
"github.com/g3n/engine/app"
"github.com/g3n/engine/camera"
"github.com/g3n/engine/core"
"github.com/g3n/engine/gls"
"github.com/g3n/engine/gui"
"github.com/g3n/engine/light"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/renderer"
"github.com/g3n/engine/util/helper"
"github.com/g3n/engine/window"
"github.com/davidreis97/GoFlightSim/graphics/terrain"
"github.com/davidreis97/GoFlightSim/graphics/airplane"
"github.com/davidreis97/GoFlightSim/graphics/skybox"
"github.com/davidreis97/GoFlightSim/controller"
"github.com/davidreis97/GoFlightSim/physics"
)
func main() {
fmt.Println("start")
// Create application and scene
app := app.App()
scene := core.NewNode()
// Set the scene to be managed by the gui manager
gui.Manager().Set(scene)
// Create perspective camera
cam := camera.New(1)
cam.SetFar(1000000)
scene.Add(cam)
// Set up callback to update viewport and camera aspect ratio when the window is resized
onResize := func(evname string, ev interface{}) {
// Get framebuffer size and update viewport accordingly
width, height := app.GetSize()
app.Gls().Viewport(0, 0, int32(width), int32(height))
// Update the camera's aspect ratio
cam.SetAspect(float32(width) / float32(height))
}
app.Subscribe(window.OnWindowSize, onResize)
onResize("", nil)
//Initialize and add to scene airplane mesh
airplaneMesh, err := airplane.Init()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
scene.Add(airplaneMesh)
// Create and add lights to the scene
scene.Add(light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.5))
// Create and add an axis helper to the scene
scene.Add(helper.NewAxes(0.5))
// Set background color to gray
app.Gls().ClearColor(0.5, 0.5, 0.5, 1.0)
//Initialize keyboard controller
keyboard := controller.InitKeyboard(app.IWindow)
//Initialize plane physics
planePos := math32.Vector3{0,0,0}
plane := physics.NewPlane(math32.Vector3{0,50,0})
plane.Transform.Scale(&math32.Vector3{0.1,0.1,0.1})
airplaneMesh.SetMatrix(plane.Transform)
//Camera offset used to make camera follow plane
cameraOffsetTranslate := math32.NewMatrix4().MakeTranslation(0, 10, 30)
cameraOffsetRotate := math32.NewMatrix4().MakeRotationX(-0.2)
cameraOffset := cameraOffsetRotate.Multiply(cameraOffsetTranslate)
//Initialize terrain generator
terrain.Init(app.Renderer())
//Generate and add skybox
skybox := skybox.Generate()
scene.Add(skybox)
// Run the application
app.Run(func(renderer *renderer.Renderer, deltaTime time.Duration) {
//Render stuff
app.Gls().Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
renderer.Render(scene, cam)
//Process input and physics step
input := keyboard.ProcessInput()
plane.Step(deltaTime,input)
airplaneMesh.SetMatrix(plane.Transform)
//Generate terrain around plane
planePos.SetFromMatrixPosition(plane.Transform)
terrain.RenderTerrain(planePos.X,planePos.Z,scene)
//Camera follow plane
cameraMat := plane.Transform.Clone()
cameraMat.Multiply(cameraOffset)
cam.SetMatrix(cameraMat)
})
}