@@ -10,6 +10,7 @@ import (
1010 "github.com/go-gl/mathgl/mgl32"
1111 "github.com/jackmott/gogl"
1212 "github.com/veandco/go-sdl2/sdl"
13+ "time"
1314)
1415
1516const winWidth = 1280
@@ -27,13 +28,15 @@ func main() {
2728 sdl .GLSetAttribute (sdl .GL_CONTEXT_MINOR_VERSION , 3 )
2829
2930 window , err := sdl .CreateWindow ("Hello Triangle" , 200 , 200 , winWidth , winHeight , sdl .WINDOW_OPENGL )
31+ sdl .SetRelativeMouseMode (true )
3032 if err != nil {
3133 panic (err )
3234 }
3335 window .GLCreateContext ()
3436 defer window .Destroy ()
3537
3638 gl .Init ()
39+ gl .Enable (gl .DEPTH_TEST )
3740 gogl .MglTest ()
3841
3942 fmt .Println ("OpenGL Version" , gogl .GetVersion ())
@@ -43,7 +46,7 @@ func main() {
4346 panic (err )
4447 }
4548
46- texture := gogl .LoadTextureAlpha ("assets/tex .png" )
49+ texture := gogl .LoadTextureAlpha ("assets/metalbox_full .png" )
4750
4851 vertices := []float32 {
4952 - 0.5 , - 0.5 , - 0.5 , 0.0 , 0.0 ,
@@ -105,37 +108,45 @@ func main() {
105108
106109 keyboardState := sdl .GetKeyboardState ()
107110
108- var x float32
109- var z float32
111+ position := mgl32. Vec3 { 0.0 , 0.0 , 0.0 }
112+ worldUp := mgl32. Vec3 { 0.0 , 1.0 , 0.0 }
110113
114+ camera := gogl .NewCamera (position , worldUp , - 90.0 , 0.0 , 0.02 , 0.1 )
115+ var elapsedTime float32
116+ prevMouseX , prevMouseY , _ := sdl .GetMouseState ()
111117 for {
118+ frameStart := time .Now ()
112119 for event := sdl .PollEvent (); event != nil ; event = sdl .PollEvent () {
113120 switch event .(type ) {
114121 case * sdl.QuitEvent :
115122 return
116123 }
117124 }
118125
119- gl .ClearColor (0.0 , 0.0 , 0.0 , 0.0 )
120- gl .Clear (gl .COLOR_BUFFER_BIT )
121-
126+ dir := gogl .Nowhere
122127 if keyboardState [sdl .SCANCODE_LEFT ] != 0 {
123- x = x - .1
128+ dir = gogl . Left
124129 }
125130 if keyboardState [sdl .SCANCODE_RIGHT ] != 0 {
126- x = x + .1
131+ dir = gogl . Right
127132 }
128133 if keyboardState [sdl .SCANCODE_UP ] != 0 {
129- z = z + .1
134+ dir = gogl . Forward
130135 }
131136 if keyboardState [sdl .SCANCODE_DOWN ] != 0 {
132- z = z - .1
137+ dir = gogl . Backward
133138 }
134139
140+ mouseX , mouseY , _ := sdl .GetMouseState ()
141+ camera .UpdateCamera (dir , elapsedTime , float32 (mouseX - prevMouseX ), float32 (mouseY - prevMouseY ))
142+ prevMouseX = mouseX
143+ prevMouseY = mouseY
144+ gl .ClearColor (0.0 , 0.0 , 0.0 , 0.0 )
145+ gl .Clear (gl .COLOR_BUFFER_BIT | gl .DEPTH_BUFFER_BIT )
146+
135147 shaderProgram .Use ()
136148 projectionMatrix := mgl32 .Perspective (mgl32 .DegToRad (45.0 ), float32 (winWidth )/ float32 (winHeight ), 0.1 , 100.0 )
137- viewMatrix := mgl32 .Ident4 ()
138- viewMatrix = mgl32 .Translate3D (x , 0.0 , z )
149+ viewMatrix := camera .GetViewMatrix ()
139150 shaderProgram .SetMat4 ("projection" , projectionMatrix )
140151 shaderProgram .SetMat4 ("view" , viewMatrix )
141152 gogl .BindTexture (texture )
@@ -152,5 +163,6 @@ func main() {
152163
153164 window .GLSwap ()
154165 shaderProgram .CheckShaderForChanges ()
166+ elapsedTime = float32 (time .Since (frameStart ).Seconds () * 1000 )
155167 }
156168}
0 commit comments