Mogre Basic Tutorial VB 4        

Beginner Tutorial 4: Frame Listeners and Windows.Forms Input

Original version by Clay Culver<BR>

Ported to VB.NET by Aeauseth

Any problems you encounter while working with this tutorial should be posted to the Mogre Forum.

Prerequisites

This tutorial assumes you have knowledge of VB.NET programming and are able to setup and compile an Ogre application (if you have trouble setting up your application, see this guide for help). This tutorial builds on the previous tutorials, so be sure that you have already worked through them.

Introduction

In this tutorial we will be introducing one of the most useful Ogre constructs: the FrameListener. By the end of this tutorial you will understand FrameListeners, how to use FrameListeners to do things that require updates every frame, and how to use the MOIS input system.

We will not be adding any code during this tutorial.

Getting Started

As with the previous tutorials, we will be using a pre-constructed code base as our starting point. Create a Windows Application project and replace the code in program.cs with the following:

 <font color='blue'>Imports</font> Mogre
 
 <font color='blue'>Module</font> Module1
 
     <font color='blue'>Public</font> myKeyboard <font color='blue'>As</font> MOIS.Keyboard
     <font color='blue'>Public</font> myMouse <font color='blue'>As</font> MOIS.Mouse
     <font color='blue'>Public</font> myCamera <font color='blue'>As</font> Camera
     <font color='blue'>Public</font> MyWindow <font color='blue'>As</font> RenderWindow
     <font color='blue'>Public</font> myTranslation <font color='blue'>As</font> Vector3 = Vector3.ZERO
     <font color='blue'>Public</font> Quitting <font color='blue'>As</font> <font color='blue'>Boolean</font> = <font color='blue'>False
 </font>    <font color='blue'>Public</font> myRotating <font color='blue'>As</font> <font color='blue'>Boolean</font> = <font color='blue'>False
 
 </font>    <font color='blue'>Sub</font> Main()
         <font color='blue'>Try
 
 </font>            <font color='green'>'Initialization of Ogre Root and RenderWindow
 </font>            <font color='blue'>Dim</font> myRoot <font color='blue'>As</font> Root = <font color='blue'>New</font> Root(<font color='darkred'>"Plugins.cfg"</font>, <font color='darkred'>"ogre.cfg"</font>, <font color='darkred'>"ogre.log"</font>)
 
             <font color='green'>'Show Ogre Rendering Subsystem setup dialog box
 </font>            <font color='blue'>If</font> <font color='blue'>Not</font> myRoot.RestoreConfig <font color='blue'>Then
 </font>                <font color='blue'>If</font> <font color='blue'>Not</font> myRoot.ShowConfigDialog <font color='blue'>Then
 </font>                    <font color='blue'>Exit</font> <font color='blue'>Sub
 </font>                <font color='blue'>End</font> <font color='blue'>If
 </font>            <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='green'>'Create an Ogre render window
 </font>            MyWindow = myRoot.Initialise(<font color='blue'>True</font>, <font color='darkred'>"OGRE Render Window"</font>)
             <font color='blue'>AddHandler</font> myRoot.FrameStarted, <font color='blue'>AddressOf</font> FrameStarted
 
             <font color='green'>'Create Ogre -SceneManager
 </font>            <font color='blue'>Dim</font> mySceneManager <font color='blue'>As</font> -SceneManager = myRoot.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE)
 
             <font color='green'>'Read Resources
 </font>            <font color='blue'>Dim</font> cf <font color='blue'>As</font> <font color='blue'>New</font> ConfigFile
             cf.Load(<font color='darkred'>"resources.cfg"</font>, vbTab + <font color='darkred'>":="</font>, <font color='blue'>True</font>)
             <font color='blue'>Dim</font> seci <font color='blue'>As</font> ConfigFile.SectionIterator = cf.GetSectionIterator
             <font color='blue'>Dim</font> secName <font color='blue'>As</font> <font color='blue'>String</font>, typeName <font color='blue'>As</font> <font color='blue'>String</font>, archName <font color='blue'>As</font> <font color='blue'>String
 </font>            <font color='blue'>While</font> (seci.MoveNext())
                 secName = seci.CurrentKey
                 <font color='blue'>Dim</font> settings <font color='blue'>As</font> ConfigFile.SettingsMultiMap = seci.Current
                 <font color='blue'>For</font> <font color='blue'>Each</font> pair <font color='blue'>As</font> KeyValuePair(<font color='blue'>Of</font> <font color='blue'>String</font>, <font color='blue'>String</font>) <font color='blue'>In</font> settings
                     typeName = pair.Key
                     archName = pair.Value
                     ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName)
                 <font color='blue'>Next
 </font>            <font color='blue'>End</font> <font color='blue'>While
 </font>            ResourceGroupManager.Singleton.InitialiseAllResourceGroups()
 
             <font color='green'>'Ninja
 </font>            <font color='blue'>Dim</font> myNinja <font color='blue'>As</font> Entity = mySceneManager.CreateEntity(<font color='darkred'>"Ninja"</font>, <font color='darkred'>"ninja.mesh"</font>)
             mySceneManager.RootSceneNode.CreateChildSceneNode.AttachObject(myNinja)
 
             <font color='green'>'Lights
 </font>            <font color='blue'>Dim</font> mylight <font color='blue'>As</font> Light = mySceneManager.CreateLight(<font color='darkred'>"Light"</font>)
             mylight.Type = Light.LightTypes.LT_POINT
             mylight.Position = <font color='blue'>New</font> Vector3(250, 150, 250)
             mylight.DiffuseColour = ColourValue.White
             mylight.SpecularColour = ColourValue.White
 
             <font color='green'>'Create Camera
 </font>            myCamera = mySceneManager.CreateCamera(<font color='darkred'>"Camera"</font>)
             myCamera.SetPosition(0, 200, 400)
             myCamera.LookAt(myNinja.BoundingBox.Center)
             myCamera.NearClipDistance = 5
 
             <font color='green'>'Viewport
 </font>            <font color='blue'>Dim</font> myViewport <font color='blue'>As</font> Viewport = MyWindow.AddViewport(myCamera)
             myViewport.BackgroundColour = ColourValue.Black
             myCamera.AspectRatio = myViewport.ActualWidth / myViewport.ActualHeight
 
             <font color='green'>'Keyboard
 </font>            <font color='blue'>Dim</font> windowHnd <font color='blue'>As</font> <font color='blue'>Integer
 </font>            MyWindow.GetCustomAttribute(<font color='darkred'>"WINDOW"</font>, windowHnd)
             <font color='blue'>Dim</font> myInputManager <font color='blue'>As</font> MOIS.InputManager = MOIS.InputManager.CreateInputSystem(windowHnd)
             myKeyboard = myInputManager.CreateInputObject(MOIS.Type.OISKeyboard, <font color='blue'>True</font>)
             <font color='blue'>AddHandler</font> myKeyboard.KeyPressed, <font color='blue'>AddressOf</font> InputClass.KeyPressed
             <font color='blue'>AddHandler</font> myKeyboard.KeyReleased, <font color='blue'>AddressOf</font> InputClass.KeyReleased
 
             <font color='green'>'Mouse
 </font>            myMouse = myInputManager.CreateInputObject(MOIS.Type.OISMouse, <font color='blue'>True</font>)
             <font color='blue'>AddHandler</font> myMouse.MouseMoved, <font color='blue'>AddressOf</font> InputClass.MouseMovedListener
             <font color='blue'>AddHandler</font> myMouse.MousePressed, <font color='blue'>AddressOf</font> InputClass.MousePressedListener
             <font color='blue'>AddHandler</font> myMouse.MouseReleased, <font color='blue'>AddressOf</font> InputClass.MouseReleasedListener
 
             <font color='green'>'Start rendering
 </font>            myRoot.StartRendering()
 
         <font color='blue'>Catch</font> ex <font color='blue'>As</font> System.Runtime.InteropServices.SEHException
             <font color='blue'>If</font> OgreException.IsThrown <font color='blue'>Then
 </font>                MsgBox(OgreException.LastException.FullDescription, MsgBoxStyle.Critical, _
                      <font color='darkred'>"An Ogre exception has occured!"</font>)
             <font color='blue'>Else
 </font>                MsgBox(ex.ToString, <font color='darkred'>"An error has occured"</font>)
             <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Try
 
 </font>    <font color='blue'>End</font> <font color='blue'>Sub
 
 </font>    <font color='blue'>Public</font> <font color='blue'>Function</font> FrameStarted(<font color='blue'>ByVal</font> e <font color='blue'>As</font> FrameEvent) <font color='blue'>As</font> <font color='blue'>Boolean
 
 </font>        <font color='green'>'Capture buffered input
 </font>        myKeyboard.Capture()
         myMouse.Capture()
 
         <font color='green'>'Handle player/camera movement
 </font>        InputClass.ProcessKeyboard()
 
         myCamera.Position += myCamera.Orientation * myTranslation * e.timeSinceLastFrame
 
         <font color='blue'>Return</font> <font color='blue'>Not</font> Quitting
     <font color='blue'>End</font> <font color='blue'>Function
 
 </font>    <font color='blue'>Public</font> <font color='blue'>Class</font> InputClass
 
         <font color='blue'>Const</font> TRANSLATE <font color='blue'>As</font> <font color='blue'>Single</font> = 200
         <font color='blue'>Const</font> ROTATE <font color='blue'>As</font> <font color='blue'>Single</font> = 0.003
 
         <font color='blue'>Shared</font> <font color='blue'>Function</font> KeyPressed(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.KeyEvent) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>Select</font> <font color='blue'>Case</font> e.key
 
                 <font color='blue'>Case</font> MOIS.KeyCode.KC_ESCAPE
                     Quitting = <font color='blue'>True
 </font>            <font color='blue'>End</font> <font color='blue'>Select
 
 </font>            <font color='blue'>Return</font> <font color='blue'>Nothing
 </font>        <font color='blue'>End</font> <font color='blue'>Function
 
 </font>        <font color='blue'>Shared</font> <font color='blue'>Function</font> KeyReleased(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.KeyEvent) <font color='blue'>As</font> <font color='blue'>Boolean
 
 </font>            <font color='green'>'This function is just a placeholder
 </font>            <font color='green'>'It is unlikely you will ever use this
 </font>            <font color='green'>'Typically you either process unbuffered keyboard input (as in ProcessKeyboard)
 </font>            <font color='green'>'or you process buffered Keypress
 
 </font>            <font color='blue'>Return</font> <font color='blue'>Nothing
 </font>        <font color='blue'>End</font> <font color='blue'>Function
 
 </font>        <font color='blue'>Shared</font> <font color='blue'>Sub</font> ProcessKeyboard()
 
             <font color='green'>'This Sub is typically called via the FrameStarted event.
 
 </font>            <font color='green'>'Clear previous translation
 </font>            myTranslation.z = 0
             myTranslation.x = 0
             myTranslation.y = 0
 
             <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_UP) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_W) <font color='blue'>Then
 </font>                myTranslation.z += -TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_S) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_DOWN) <font color='blue'>Then
 </font>                myTranslation.z += TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_A) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_LEFT) <font color='blue'>Then
 </font>                myTranslation.x += -TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_D) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_RIGHT) <font color='blue'>Then
 </font>                myTranslation.x += TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_Q) <font color='blue'>Or</font> _
             myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGUP) <font color='blue'>Or</font> _
             myKeyboard.IsKeyDown(MOIS.KeyCode.KC_SPACE) <font color='blue'>Then
 </font>                myTranslation.y += TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_Z) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGDOWN) <font color='blue'>Then
 </font>                myTranslation.y += -TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Sub
 
 </font>        <font color='blue'>Shared</font> <font color='blue'>Function</font> MouseMovedListener(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.MouseEvent) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>Static</font> myLastX <font color='blue'>As</font> <font color='blue'>Integer</font> = e.state.X.abs
             <font color='blue'>Static</font> myLastY <font color='blue'>As</font> <font color='blue'>Integer</font> = e.state.Y.abs
 
             <font color='blue'>If</font> myRotating <font color='blue'>Then
 </font>                myCamera.Yaw(e.state.X.rel * -ROTATE)
                 myCamera.Pitch(e.state.Y.rel * -ROTATE)
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>        <font color='blue'>End</font> <font color='blue'>Function
 
 </font>        <font color='blue'>Shared</font> <font color='blue'>Function</font> MousePressedListener(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.MouseEvent, <font color='blue'>ByVal</font> id <font color='blue'>As</font> MOIS.MouseButtonID) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>If</font> e.state.ButtonDown(MOIS.MouseButtonID.MB_Right) <font color='blue'>Then
 </font>                myRotating = <font color='blue'>True
 </font>            <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Function
 
 </font>        <font color='blue'>Shared</font> <font color='blue'>Function</font> MouseReleasedListener(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.MouseEvent, <font color='blue'>ByVal</font> id <font color='blue'>As</font> MOIS.MouseButtonID) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>If</font> <font color='blue'>Not</font> e.state.ButtonDown(MOIS.MouseButtonID.MB_Right) <font color='blue'>Then
 </font>                myRotating = <font color='blue'>False
 </font>            <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Function
 
 </font>    <font color='blue'>End</font> <font color='blue'>Class
 
 
 End</font> <font color='blue'>Module</font>

Make sure you can compile and run the application before continuing. If you are having difficulty, refer to the project setup guide or post to the forums.

In this tutorial we will start with a "default" scene (see the code snippet above). The only thing interesting to note is the Camera.LookAt call. In this line of code we have set the camera to look at the center of the Ninja Entity. This works because the node we created resides at the origin (0, 0, 0). If you want to duplicate this functionality later on you have to add the node's position to the entity's bounding box's center.

FrameListeners

Introduction

In the previous tutorials we only looked at what we could do when we create the scene. In Ogre, we can register a class to receive notification before and after a frame is rendered to the screen. The root class provides the FrameStarted and FrameEnded events to handle these actions.

Ogre's main loop looks like this:

  1. The Root object fires the FrameStarted event.
  2. The Root object renders one frame.
  3. The Root object fires the FrameEnded event.


This loops until any of the event handlers return false. The return values for the event handlers basically mean "keep rendering". If you return false from either, the program will exit. The FrameEvent object contains two variables, but only the timeSinceLastFrame is useful in a FrameListener. This variable keeps track of how long it's been since the FrameStarted or FrameEnded last fired. Note that in the frameStarted method, FrameEvent.TimeSinceLastFrame will contain how long it has been since the last FrameStarted event was last fired (not the last time a FrameEnded event was fired).

One important concept to realize about Ogre's FrameListeners is that the order in which they are called is entirely up to Ogre. You cannot determine which FrameListener is called first, second, third...and so on. If you need to ensure that FrameListeners are called in a certain order, then you should register only one FrameListener and have it call all of the objects in the proper order.

You might also notice that the main loop really only does three things, and since nothing happens in between the frameEnded and frameStarted methods being called, you can use them almost interchangably. Where you decide to put all of your code is entirely up to you. You can put it all in one big FrameStarted or FrameEnded handler, or you could divide it up between the two.

Frame Listeners vs Timers

Frame listeners are one of the most useful Ogre constructs, as they allow us to incrementally update objects in the scene. You may have also noticed that a Timer (System.Windows.Forms.Timer) could do the same thing, and they allow you to control how often they are fired (as opposed to the frame listeners which fire as fast as you are rendering). In practice, these two things are virtually interchangable, but I have found that these usage guidelines work well for me:

  1. If you are updating objects which are being rendered you should use a frame listener to update it every frame. For example, if you are moving an object incrementally across the screen, you should use frame listeners.
  2. If you are performing an action which should happen often, but the result of which is not being directly rendered to the screen, you should use a Timer. For example, let's say your program is running at 600 FPS. You do not need to poll the keyboard, joystick(s), and network interfaces every frame (which would be 600 times per second) when polling it 10 times per second wouldn't be a noticable difference to the user.


You should mix and match frame listeners and timers based on your needs for the program.

Registering a FrameListener

Since the Root class is what renders frames, it also is in charge of keeping track of FrameListeners. The first thing we need to do is register a FrameStarted event handler.

      <font color='blue'>AddHandler</font> myRoot.FrameStarted, <font color='blue'>AddressOf</font> FrameStarted

Handling User Input


Background

Ogre considers itself a graphics only engine. It has intentionally left out any mouse, keyboard, or other user input from the core engine. Ogre does support addins, and there are several the provide user input. Lucky for us the MOgre SDK include MOIS. MOIS provides basic user input support which is good enough for us to use in this tutorial.

The C++ tutorial uses windows forms and associated keyboard/mouse events to handle input. I don't know how to reproduce this with VB.NET, and in my opinion wasn't important to do so. In all likely hood you are going to write a VB.NET console application with full screen graphics, not a form with an embedded MOgre window.

Overview

Since moving the camera changes what we render every frame, we will use a frame listener for this instead of a timer.

Our strategy for keyboard input is to keep track of camera movement with a single Vector3 variable. When the user presses specific keys we will add and subtract from this vector and move the camera by this amount every frame. Our strategy for handling mouse input is a bit more tricky. We want to rotate the camera only when the right mouse button is held down. To accomplish this we will use a boolean variable to keep track of the state of the right mouse button (<font color='blue'>myRotating</font>).

Key Input

The first thing we will do is create a MOIS Inputmanager for the Keyboard. We also need to add event handlers for the KeyPressed and KeyReleased.

      <font color='green'>'Keyboard</font>
      <font color='blue'>Dim</font> windowHnd <font color='blue'>As</font> <font color='blue'>Integer</font>
      MyWindow.GetCustomAttribute(<font color='darkred'>"WINDOW"</font>, windowHnd)
      <font color='blue'>Dim</font> myInputManager <font color='blue'>As</font> MOIS.InputManager = MOIS.InputManager.CreateInputSystem(windowHnd)
      myKeyboard = myInputManager.CreateInputObject(MOIS.Type.OISKeyboard, <font color='blue'>True</font>)
      <font color='blue'>AddHandler</font> myKeyboard.KeyPressed, <font color='blue'>AddressOf</font> InputClass.KeyPressed
      <font color='blue'>AddHandler</font> myKeyboard.KeyReleased, <font color='blue'>AddressOf</font> InputClass.KeyReleased

We will only be using the KeyPressed event for the ESC key, and we won't be using the KeyReleased event at all. In practice it is unlikely you will ever use the KeyReleased key, I'm not even sure why I bothered to include it in this tutorial.

      <font color='blue'>Shared</font> <font color='blue'>Function</font> KeyPressed(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.KeyEvent) <font color='blue'>As</font> <font color='blue'>Boolean</font>
           <font color='blue'>Select</font> <font color='blue'>Case</font> e.key
                <font color='blue'>Case</font> MOIS.KeyCode.KC_ESCAPE
                     Quitting = <font color='blue'>True</font>
           <font color='blue'>End</font> <font color='blue'>Select</font>
           <font color='blue'>Return</font> <font color='blue'>Nothing</font>
      <font color='blue'>End</font> <font color='blue'>Function</font>

You will notice that the event handlers for MOIS are wrapped up in a class and that the event handlers are marked as shared. I recommend moving this class to a seperate VB file for your real project, I left it here for clarity.

The ASWD keys are handled via subroutine called ProcessKeyboard, which is called via the FrameStarted event. We also need to call a Capture method in order for the Keypressed & MouseMove events to fire. This is a quirk of MOIS. Each addin will have it's own little quirks.

      <font color='blue'>Public</font> <font color='blue'>Function</font> FrameStarted(<font color='blue'>ByVal</font> e <font color='blue'>As</font> FrameEvent) <font color='blue'>As</font> <font color='blue'>Boolean
 
 </font>        <font color='green'>'Capture buffered input
 </font>        myKeyboard.Capture()
         myMouse.Capture()
 
         <font color='green'>'Handle player/camera movement
 </font>        InputClass.ProcessKeyboard()
 
         myCamera.Position += myCamera.Orientation * myTranslation * e.timeSinceLastFrame
 
         <font color='blue'>Return</font> <font color='blue'>Not</font> Quitting
     <font color='blue'>End</font> <font color='blue'>Function</font>

        <font color='blue'>Shared</font> <font color='blue'>Sub</font> ProcessKeyboard()
 
             <font color='green'>'This Sub is typically called via the FrameStarted event.
 
 </font>            <font color='green'>'Clear previous translation
 </font>            myTranslation.z = 0
             myTranslation.x = 0
             myTranslation.y = 0
 
             <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_UP) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_W) <font color='blue'>Then
 </font>                myTranslation.z += -TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_S) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_DOWN) <font color='blue'>Then
 </font>                myTranslation.z += TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_A) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_LEFT) <font color='blue'>Then
 </font>                myTranslation.x += -TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_D) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_RIGHT) <font color='blue'>Then
 </font>                myTranslation.x += TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_Q) <font color='blue'>Or</font> _
             myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGUP) <font color='blue'>Or</font> _
             myKeyboard.IsKeyDown(MOIS.KeyCode.KC_SPACE) <font color='blue'>Then
 </font>                myTranslation.y += TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>            <font color='blue'>If</font> myKeyboard.IsKeyDown(MOIS.KeyCode.KC_Z) <font color='blue'>Or</font> _
                 myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGDOWN) <font color='blue'>Then
 </font>                myTranslation.y += -TRANSLATE
             <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Sub</font>

Take note that the Camera position and orientation is changed in the FrameStarted method.

            myCamera.Position += myCamera.Orientation * myTranslation * e.timeSinceLastFrame

This moves the camera's position every frame. We multiply the translation by the camera's orientation so that when we translate, we are moving in the correct direction. That means that even when we hold down the W button we move "forward" no matter what direction the camera faces. If we did not do this, our camera would rotate independantly of its movement. (Which is currently not what we are going for.) We multiply the translation by the time since the last frame to keep the movement smooth, and independent of framerate.

Mouse Input

Now we need to add mouse-look to the program. To do this we create a MOIS InputManager for the Mouse and register handlers for the MouseMoved, MousePressed, and MouseReleased events.

              <font color='green'>'Mouse
 </font>            myMouse = myInputManager.CreateInputObject(MOIS.Type.OISMouse, <font color='blue'>True</font>)
             <font color='blue'>AddHandler</font> myMouse.MouseMoved, <font color='blue'>AddressOf</font> InputClass.MouseMovedListener
             <font color='blue'>AddHandler</font> myMouse.MousePressed, <font color='blue'>AddressOf</font> InputClass.MousePressedListener
             <font color='blue'>AddHandler</font> myMouse.MouseReleased, <font color='blue'>AddressOf</font> InputClass.MouseReleasedListener 

The MousePressed and MouseReleased simply set the <font color='blue'>myRotating</font> variable.

         <font color='blue'>Shared</font> <font color='blue'>Function</font> MousePressedListener(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.MouseEvent, _
             <font color='blue'>ByVal</font> id <font color='blue'>As</font> MOIS.MouseButtonID) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>If</font> e.state.ButtonDown(MOIS.MouseButtonID.MB_Right) <font color='blue'>Then
 </font>                myRotating = <font color='blue'>True
 </font>            <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Function</font>

         <font color='blue'>Shared</font> <font color='blue'>Function</font> MouseReleasedListener(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.MouseEvent, _
             <font color='blue'>ByVal</font> id <font color='blue'>As</font> MOIS.MouseButtonID) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>If</font> <font color='blue'>Not</font> e.state.ButtonDown(MOIS.MouseButtonID.MB_Right) <font color='blue'>Then
 </font>                myRotating = <font color='blue'>False
 </font>            <font color='blue'>End</font> <font color='blue'>If
 </font>        <font color='blue'>End</font> <font color='blue'>Function</font>

When the mouse is moved we need to handle it by rotating the camera, but only when mRotating is true.

         <font color='blue'>Shared</font> <font color='blue'>Function</font> MouseMovedListener(<font color='blue'>ByVal</font> e <font color='blue'>As</font> MOIS.MouseEvent) <font color='blue'>As</font> <font color='blue'>Boolean
 </font>            <font color='blue'>Static</font> myLastX <font color='blue'>As</font> <font color='blue'>Integer</font> = e.state.X.abs
             <font color='blue'>Static</font> myLastY <font color='blue'>As</font> <font color='blue'>Integer</font> = e.state.Y.abs
 
             <font color='blue'>If</font> myRotating <font color='blue'>Then</font>
                myCamera.Yaw(e.state.X.rel * -ROTATE)
                myCamera.Pitch(e.state.Y.rel * -ROTATE)
             <font color='blue'>End</font> <font color='blue'>If
 
 </font>        <font color='blue'>End</font> <font color='blue'>Function</font>

That's it, run your program. You can move and rotate the mouse based on user input. You might have noticed that the mouse cursor isn't visable. Unfortunatly, Ogre doesn't support mouse cursors either. To get around that we must make our own using an overlay. Overlay's are discussed in Tutorial 5.

Final Note

This tutorial shows you the basics of implementing an input system using MOIS. You may find that Microsoft DirectX or another Ogre addin is more appropriate for your project.

Proceed to Basic Tutorial 5 The Ogre Startup Sequence


Category:Tutorials
Category:MOGRE