@@ -107,6 +107,8 @@ public Game()
107107 void initProgram ( )
108108 {
109109 lastMousePos = new Vector2 ( Mouse . X , Mouse . Y ) ;
110+ CursorVisible = false ;
111+ cam . MouseSensitivity = 0.0025f ;
110112
111113 objects . Add ( new Cube ( ) ) ;
112114 objects . Add ( new Cube ( ) ) ;
@@ -212,6 +214,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
212214 {
213215 base . OnUpdateFrame ( e ) ;
214216
217+ ProcessInput ( ) ;
218+
215219 List < Vector3 > verts = new List < Vector3 > ( ) ;
216220 List < int > inds = new List < int > ( ) ;
217221 List < Vector3 > colors = new List < Vector3 > ( ) ;
@@ -250,7 +254,7 @@ protected override void OnUpdateFrame(FrameEventArgs e)
250254 foreach ( Volume v in objects )
251255 {
252256 v . CalculateModelMatrix ( ) ;
253- v . ViewProjectionMatrix = cam . GetViewMatrix ( ) * Matrix4 . CreatePerspectiveFieldOfView ( 1.3f , ClientSize . Width / ( float ) ClientSize . Height , 1.0f , 40.0f ) ;
257+ v . ViewProjectionMatrix = cam . GetViewMatrix ( ) * Matrix4 . CreatePerspectiveFieldOfView ( 1.3f , ClientSize . Width / ( float ) ClientSize . Height , 1.0f , 40.0f ) ;
254258 v . ModelViewProjectionMatrix = v . ModelMatrix * v . ViewProjectionMatrix ;
255259 }
256260
@@ -260,69 +264,67 @@ protected override void OnUpdateFrame(FrameEventArgs e)
260264
261265 GL . BindBuffer ( BufferTarget . ElementArrayBuffer , ibo_elements ) ;
262266 GL . BufferData ( BufferTarget . ElementArrayBuffer , ( IntPtr ) ( indicedata . Length * sizeof ( int ) ) , indicedata , BufferUsageHint . StaticDraw ) ;
263-
264- if ( Focused )
265- {
266- Vector2 delta = lastMousePos - new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
267- lastMousePos += delta ;
268-
269- cam . AddRotation ( delta . X , delta . Y ) ;
270- ResetCursor ( ) ;
271- }
272267 }
273268
274- protected override void OnKeyPress ( KeyPressEventArgs e )
269+ /// <summary>
270+ /// Handles keyboard and mouse input
271+ /// </summary>
272+ private void ProcessInput ( )
275273 {
276- base . OnKeyPress ( e ) ;
277-
278- if ( e . KeyChar == 27 )
274+ /* We'll use the escape key to make the program easy to exit from.
275+ * Otherwise it's annoying since the mouse cursor is trapped inside.*/
276+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . Escape ) )
279277 {
280278 Exit ( ) ;
281279 }
282280
283281 /** Let's start by adding WASD input (feel free to change the keys if you want,
284282 * hopefully a later tutorial will have a proper input manager) for translating the camera. */
285- switch ( e . KeyChar )
283+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . W ) )
286284 {
287- case 'w' :
288- cam . Move ( 0f , 0.1f , 0f ) ;
289- break ;
290- case 'a' :
291- cam . Move ( - 0.1f , 0f , 0f ) ;
292- break ;
293- case 's' :
294- cam . Move ( 0f , - 0.1f , 0f ) ;
295- break ;
296- case 'd' :
297- cam . Move ( 0.1f , 0f , 0f ) ;
298- break ;
299- case 'q' :
300- cam . Move ( 0f , 0f , 0.1f ) ;
301- break ;
302- case 'e' :
303- cam . Move ( 0f , 0f , - 0.1f ) ;
304- break ;
285+ cam . Move ( 0f , 0.1f , 0f ) ;
305286 }
306- }
307287
308- /// <summary>
309- /// Centers the mouse cursor in the window
310- /// </summary>
311- void ResetCursor ( )
312- {
313- OpenTK . Input . Mouse . SetPosition ( Bounds . Left + Bounds . Width / 2 , Bounds . Top + Bounds . Height / 2 ) ;
314- lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
315- }
288+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . S ) )
289+ {
290+ cam . Move ( 0f , - 0.1f , 0f ) ;
291+ }
316292
317- protected override void OnFocusedChanged ( EventArgs e )
318- {
319- base . OnFocusedChanged ( e ) ;
293+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . A ) )
294+ {
295+ cam . Move ( - 0.1f , 0f , 0f ) ;
296+ }
297+
298+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . D ) )
299+ {
300+ cam . Move ( 0.1f , 0f , 0f ) ;
301+ }
302+
303+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . Q ) )
304+ {
305+ cam . Move ( 0f , 0f , 0.1f ) ;
306+ }
307+
308+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . E ) )
309+ {
310+ cam . Move ( 0f , 0f , - 0.1f ) ;
311+ }
320312
321313 if ( Focused )
322314 {
323- ResetCursor ( ) ;
315+ Vector2 delta = lastMousePos - new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
316+ lastMousePos += delta ;
317+
318+ cam . AddRotation ( delta . X , delta . Y ) ;
319+ lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
324320 }
325321 }
326322
323+ protected override void OnFocusedChanged ( EventArgs e )
324+ {
325+ base . OnFocusedChanged ( e ) ;
326+ lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
327+ }
328+
327329 }
328330}
0 commit comments