@@ -83,6 +83,8 @@ void initProgram()
8383 GL . Enable ( EnableCap . Multisample ) ;
8484
8585 lastMousePos = new Vector2 ( Mouse . X , Mouse . Y ) ;
86+ CursorVisible = false ;
87+ mainCamera . MouseSensitivity = 0.0025f ;
8688
8789 GL . GenBuffers ( 1 , out ibo_elements ) ;
8890
@@ -258,6 +260,9 @@ protected override void OnRenderFrame(FrameEventArgs e)
258260 /// <param name="camera">Camera the video is from the perspective of</param>
259261 private void RenderScene ( Camera camera )
260262 {
263+ if ( ! resourcesLoaded || time < float . Epsilon )
264+ return ;
265+
261266 GL . UseProgram ( shaders [ activeShader ] . ProgramID ) ;
262267
263268 int indiceat = 0 ;
@@ -458,6 +463,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
458463 if ( ! resourcesLoaded )
459464 return ;
460465
466+ ProcessInput ( ) ;
467+
461468 List < Vector3 > verts = new List < Vector3 > ( ) ;
462469 List < int > inds = new List < int > ( ) ;
463470 List < Vector3 > colors = new List < Vector3 > ( ) ;
@@ -529,62 +536,90 @@ protected override void OnUpdateFrame(FrameEventArgs e)
529536 }
530537
531538 view = mainCamera . GetViewMatrix ( ) ;
532-
533- // Mouse movement
539+ }
540+
541+ /// <summary>
542+ /// Handles keyboard and mouse input
543+ /// </summary>
544+ private void ProcessInput ( )
545+ {
546+ /* We'll use the escape key to make the program easy to exit from.
547+ * Otherwise it's annoying since the mouse cursor is trapped inside.*/
548+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . Escape ) )
549+ {
550+ Exit ( ) ;
551+ }
552+
553+ /** Let's start by adding WASD input (feel free to change the keys if you want,
554+ * hopefully a later tutorial will have a proper input manager) for translating the camera. */
555+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . W ) )
556+ {
557+ mainCamera . Move ( 0f , 0.1f , 0f ) ;
558+ }
559+
560+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . S ) )
561+ {
562+ mainCamera . Move ( 0f , - 0.1f , 0f ) ;
563+ }
564+
565+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . A ) )
566+ {
567+ mainCamera . Move ( - 0.1f , 0f , 0f ) ;
568+ }
569+
570+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . D ) )
571+ {
572+ mainCamera . Move ( 0.1f , 0f , 0f ) ;
573+ }
574+
575+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . Q ) )
576+ {
577+ mainCamera . Move ( 0f , 0f , 0.1f ) ;
578+ }
579+
580+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . E ) )
581+ {
582+ mainCamera . Move ( 0f , 0f , - 0.1f ) ;
583+ }
584+
585+ // Move screen camera
586+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . U ) )
587+ {
588+ screenCamera . Move ( 0f , 0f , 0.1f ) ;
589+ }
590+
591+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . J ) )
592+ {
593+ screenCamera . Move ( 0f , 0f , - 0.1f ) ;
594+ }
595+
596+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . H ) )
597+ {
598+ screenCamera . Move ( - 0.1f , 0f , 0f ) ;
599+ }
600+
601+ if ( Keyboard . GetState ( ) . IsKeyDown ( Key . K ) )
602+ {
603+ screenCamera . Move ( 0.1f , 0f , 0f ) ;
604+ }
605+
534606 if ( Focused )
535607 {
536608 Vector2 delta = lastMousePos - new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
537609 lastMousePos += delta ;
538610
539611 mainCamera . AddRotation ( delta . X , delta . Y ) ;
540- ResetCursor ( ) ;
541-
542- if ( Keyboard [ Key . Escape ] )
543- {
544- Exit ( ) ;
545- }
612+ lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
546613 }
547-
548614 }
549615
616+
550617 protected override void OnKeyPress ( KeyPressEventArgs e )
551618 {
552619 base . OnKeyPress ( e ) ;
553620
554621 switch ( e . KeyChar )
555622 {
556- // Move main camera
557- case 'w' :
558- mainCamera . Move ( 0f , 0.1f , 0f ) ;
559- break ;
560- case 'a' :
561- mainCamera . Move ( - 0.1f , 0f , 0f ) ;
562- break ;
563- case 's' :
564- mainCamera . Move ( 0f , - 0.1f , 0f ) ;
565- break ;
566- case 'd' :
567- mainCamera . Move ( 0.1f , 0f , 0f ) ;
568- break ;
569- case 'q' :
570- mainCamera . Move ( 0f , 0f , 0.1f ) ;
571- break ;
572- case 'e' :
573- mainCamera . Move ( 0f , 0f , - 0.1f ) ;
574- break ;
575- // Move screen camera
576- case 'u' :
577- screenCamera . Move ( 0f , 0f , 0.1f ) ;
578- break ;
579- case 'j' :
580- screenCamera . Move ( 0f , 0f , - 0.1f ) ;
581- break ;
582- case 'h' :
583- screenCamera . Move ( - 0.1f , 0f , 0f ) ;
584- break ;
585- case 'k' :
586- screenCamera . Move ( 0.1f , 0f , 0f ) ;
587- break ;
588623 // Change shader of screen
589624 case 'v' :
590625 tvScreen . Shader = ( tvScreen . Shader == "screen" ) ? "lit_advanced" : "screen" ;
@@ -596,23 +631,10 @@ protected override void OnKeyPress(KeyPressEventArgs e)
596631 }
597632 }
598633
599- /// <summary>
600- /// Moves the cursor to the center of the window
601- /// </summary>
602- void ResetCursor ( )
603- {
604- OpenTK . Input . Mouse . SetPosition ( Bounds . Left + Bounds . Width / 2 , Bounds . Top + Bounds . Height / 2 ) ;
605- lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
606- }
607-
608634 protected override void OnFocusedChanged ( EventArgs e )
609635 {
610636 base . OnFocusedChanged ( e ) ;
611-
612- if ( Focused )
613- {
614- ResetCursor ( ) ;
615- }
637+ lastMousePos = new Vector2 ( OpenTK . Input . Mouse . GetState ( ) . X , OpenTK . Input . Mouse . GetState ( ) . Y ) ;
616638 }
617639
618640 /// <summary>
0 commit comments