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

<HR>
Creative Commons Copyright -- Some rights reserved.


THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.

BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.

1. Definitions

  • "Collective Work" means a work, such as a periodical issue, anthology or encyclopedia, in which the Work in its entirety in unmodified form, along with a number of other contributions, constituting separate and independent works in themselves, are assembled into a collective whole. A work that constitutes a Collective Work will not be considered a Derivative Work (as defined below) for the purposes of this License.
  • "Derivative Work" means a work based upon the Work or upon the Work and other pre-existing works, such as a translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art reproduction, abridgment, condensation, or any other form in which the Work may be recast, transformed, or adapted, except that a work that constitutes a Collective Work will not be considered a Derivative Work for the purpose of this License. For the avoidance of doubt, where the Work is a musical composition or sound recording, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered a Derivative Work for the purpose of this License.
  • "Licensor" means the individual or entity that offers the Work under the terms of this License.
  • "Original Author" means the individual or entity who created the Work.
  • "Work" means the copyrightable work of authorship offered under the terms of this License.
  • "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation.
  • "License Elements" means the following high-level license attributes as selected by Licensor and indicated in the title of this License: Attribution, ShareAlike.

2. Fair Use Rights

Nothing in this license is intended to reduce, limit, or restrict any rights arising from fair use, first sale or other limitations on the exclusive rights of the copyright owner under copyright law or other applicable laws.

3. License Grant

Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below:

  • to reproduce the Work, to incorporate the Work into one or more Collective Works, and to reproduce the Work as incorporated in the Collective Works;
  • to create and reproduce Derivative Works;
  • to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission the Work including as incorporated in Collective Works;
  • to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission Derivative Works.
  • For the avoidance of doubt, where the work is a musical composition:
    • Performance Royalties Under Blanket Licenses. Licensor waives the exclusive right to collect, whether individually or via a performance rights society (e.g. ASCAP, BMI, SESAC), royalties for the public performance or public digital performance (e.g. webcast) of the Work.
    • Mechanical Rights and Statutory Royalties. Licensor waives the exclusive right to collect, whether individually or via a music rights society or designated agent (e.g. Harry Fox Agency), royalties for any phonorecord You create from the Work ("cover version") and distribute, subject to the compulsory license created by 17 USC Section 115 of the US Copyright Act (or the equivalent in other jurisdictions).
    • Webcasting Rights and Statutory Royalties. For the avoidance of doubt, where the Work is a sound recording, Licensor waives the exclusive right to collect, whether individually or via a performance-rights society (e.g. SoundExchange), royalties for the public digital performance (e.g. webcast) of the Work, subject to the compulsory license created by 17 USC Section 114 of the US Copyright Act (or the equivalent in other jurisdictions).


The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. All rights not expressly granted by Licensor are hereby reserved.

4. Restrictions

The license granted in Section 3 above is expressly made subject to and limited by the following restrictions:

  • You may distribute, publicly display, publicly perform, or publicly digitally perform the Work only under the terms of this License, and You must include a copy of, or the Uniform Resource Identifier for, this License with every copy or phonorecord of the Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Work that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Work itself to be made subject to the terms of this License. If You create a Collective Work, upon notice from any Licensor You must, to the extent practicable, remove from the Collective Work any credit as required by clause 4(c), as requested. If You create a Derivative Work, upon notice from any Licensor You must, to the extent practicable, remove from the Derivative Work any credit as required by clause 4(c), as requested.
  • You may distribute, publicly display, publicly perform, or publicly digitally perform a Derivative Work only under the terms of this License, a later version of this License with the same License Elements as this License, or a Creative Commons iCommons license that contains the same License Elements as this License (e.g. Attribution-ShareAlike 2.5 Japan). You must include a copy of, or the Uniform Resource Identifier for, this License or other license specified in the previous sentence with every copy or phonorecord of each Derivative Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Derivative Works that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder, and You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Derivative Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Derivative Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Derivative Work itself to be made subject to the terms of this License.
  • If you distribute, publicly display, publicly perform, or publicly digitally perform the Work or any Derivative Works or Collective Works, You must keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or (ii) if the Original Author and/or Licensor designate another party or parties (e.g. a sponsor institute, publishing entity, journal) for attribution in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; the title of the Work if supplied; to the extent reasonably practicable, the Uniform Resource Identifier, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and in the case of a Derivative Work, a credit identifying the use of the Work in the Derivative Work (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). Such credit may be implemented in any reasonable manner; provided, however, that in the case of a Derivative Work or Collective Work, at a minimum such credit will appear where any other comparable authorship credit appears and in a manner at least as prominent as such other comparable authorship credit.

5. Representations, Warranties and Disclaimer

UNLESS OTHERWISE AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE MATERIALS, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.

6. Limitation on Liability.

EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

7. Termination

  • This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Derivative Works or Collective Works from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
  • Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above.

8. Miscellaneous

  • Each time You distribute or publicly digitally perform the Work or a Collective Work, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License.
  • Each time You distribute or publicly digitally perform a Derivative Work, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License.
  • If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
  • No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent.
  • This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You.