MOGRE MousePickingExample         Get XZ coordinates by mouse click

Here is an example where the Mogre RenderWindow is embedded in a Windows Form, an Ogre head is displayed, and your task is to try to click it so that you can be rewarded with a "You hit the head" message box. Great fun for all the family!

This is using the Mogre ray queries that test intersection against bounding boxes. This means that the mouse picking is not accurate, polygon-level picking.

MogreForm.cs

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 using Mogre;
 
 namespace Mogre.Demo.MogreForm
 {
     public partial class MogreForm : Form
     {
         protected OgreWindow mogreWin;
 
         public MogreForm()
         {
             InitializeComponent();
             this.Disposed += new EventHandler(MogreForm_Disposed);
 
             mogreWin = new OgreWindow(new Point(100, 30), mogrePanel.Handle);
             mogreWin.InitMogre();
         }
 
         private void MogreForm_Paint(object sender, PaintEventArgs e)
         {
             mogreWin.Paint();
         }
 
         void MogreForm_Disposed(object sender, EventArgs e)
         {
             mogreWin.Dispose();
         }
 
         private void mogrePanel_MouseClick(object sender, MouseEventArgs e)
         {
             if (mogreWin.ClickedOnHead(e.X, e.Y))
                 MessageBox.Show("You hit the head");
             else
                 MessageBox.Show("Missed");
         }
     }
 
     public class OgreWindow
     {
         public bool ClickedOnHead(int mx, int my)
         {
             //normalise mouse coordinates to [0,1]
             //we could have used the panel's width/height in pixels instead of viewport's width/height
             float scrx = (float)mx / viewport.ActualWidth;
             float scry = (float)my / viewport.ActualHeight;
 
             Ray ray = camera.GetCameraToViewportRay(scrx, scry);
             RaySceneQuery query = sceneMgr.CreateRayQuery(ray);
             RaySceneQueryResult results = query.Execute();
 
             foreach (RaySceneQueryResultEntry entry in results)
             {
                 // Do stuff with the objects that intersect the ray
             }
 
             //if we hit the head then there is 1 result
             return results.Count > 0;
         }
 
         public Root root;
         public SceneManager sceneMgr;
        
         protected Camera camera;
         protected Viewport viewport;
         protected RenderWindow window;
         protected Point position;
         protected IntPtr hWnd;
 
         public OgreWindow(Point origin, IntPtr hWnd)
         {
             position = origin;
             this.hWnd = hWnd;
         }
 
         public void InitMogre()
         {
 
             //-----------------------------------------------------
             // 1 enter ogre
             //-----------------------------------------------------
             root = new Root();
 
             //-----------------------------------------------------
             // 2 configure resource paths
             //-----------------------------------------------------
             ConfigFile cf = new ConfigFile();
             cf.Load("resources.cfg", "\t:=", true);
 
             // Go through all sections & settings in the file
             ConfigFile.SectionIterator seci = cf.GetSectionIterator();
 
             String secName, typeName, archName;
 
             // Normally we would use the foreach syntax, which enumerates the values, but in this case we need CurrentKey too;
             while (seci.MoveNext())
             {
                 secName = seci.CurrentKey;
                 ConfigFile.SettingsMultiMap settings = seci.Current;
                 foreach (KeyValuePair<string, string> pair in settings)
                 {
                     typeName = pair.Key;
                     archName = pair.Value;
                     ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
                 }
             }
 
             //-----------------------------------------------------
             // 3 Configures the application and creates the window
             //-----------------------------------------------------
             bool foundit = false;
             foreach (RenderSystem rs in root.GetAvailableRenderers())
             {
                 root.RenderSystem = rs;
                 String rname = root.RenderSystem.Name;
                 if (rname == "Direct3D9 Rendering Subsystem")
                 {
                     foundit = true;
                     break;
                 }
             }
 
             if (!foundit)
                 return; //we didn't find it... Raise exception?
 
             //we found it, we might as well use it!
             root.RenderSystem.SetConfigOption("Full Screen", "No");
             root.RenderSystem.SetConfigOption("Video Mode", "640 x 480 @ 32-bit colour");
 
             root.Initialise(false);
             NameValuePairList misc = new NameValuePairList();
             misc["externalWindowHandle"] = hWnd.ToString();
             window = root.CreateRenderWindow("Simple Mogre Form Window", 0, 0, false, misc);
             ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
 
             //-----------------------------------------------------
             // 4 Create the SceneManager
             //
             //      ST_GENERIC = octree
             //      ST_EXTERIOR_CLOSE = simple terrain
             //      ST_EXTERIOR_FAR = nature terrain (depreciated)
             //      ST_EXTERIOR_REAL_FAR = paging landscape
             //      ST_INTERIOR = Quake3 BSP
             //-----------------------------------------------------
             sceneMgr = root.CreateSceneManager(SceneType.ST_GENERIC, "SceneMgr");
             sceneMgr.AmbientLight = new ColourValue(0.5f, 0.5f, 0.5f);
 
             //-----------------------------------------------------
             // 5 Create the camera
             //-----------------------------------------------------
             camera = sceneMgr.CreateCamera("SimpleCamera");
             camera.Position = new Vector3(0f,0f,100f);
             // Look back along -Z
             camera.LookAt(new Vector3(0f,0f,-300f));
             camera.NearClipDistance = 5;
 
             viewport = window.AddViewport(camera);
             viewport.BackgroundColour = new ColourValue(0.0f, 0.0f, 0.0f, 1.0f);
 
 
             Entity ent = sceneMgr.CreateEntity("ogre", "ogrehead.mesh");
             SceneNode node = sceneMgr.RootSceneNode.CreateChildSceneNode("ogreNode");
             node.AttachObject(ent);
         }
 
         public void Paint()
         {
             root.RenderOneFrame();
         }
 
         public void Dispose()
         {
             if (root != null)
             {
                 root.Dispose();
                 root = null;
             }
         }
     }
 
 }



MogreForm.Designer.cs

namespace Mogre.Demo.MogreForm
 {
     partial class MogreForm
     {
         /// <summary>
         /// Required designer variable.
         /// </summary>
         private System.ComponentModel.IContainer components = null;
 
         /// <summary>
         /// Clean up any resources being used.
         /// </summary>
         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
         protected override void Dispose(bool disposing)
         {
             if (disposing && (components != null))
             {
                 components.Dispose();
             }
             base.Dispose(disposing);
         }
 
         #region Windows Form Designer generated code
 
         /// <summary>
         /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
         /// </summary>
         private void InitializeComponent()
         {
             this.mogrePanel = new System.Windows.Forms.Panel();
             this.SuspendLayout();
             //
             // mogrePanel
             //
             this.mogrePanel.Location = new System.Drawing.Point(150, 68);
             this.mogrePanel.Name = "mogrePanel";
             this.mogrePanel.Size = new System.Drawing.Size(483, 375);
             this.mogrePanel.TabIndex = 0;
             this.mogrePanel.MouseClick += new System.Windows.Forms.MouseEventHandler(this.mogrePanel_MouseClick);
             //
             // MogreForm
             //
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(810, 544);
             this.Controls.Add(this.mogrePanel);
             this.Name = "MogreForm";
             this.Text = "Simple Mogre Form";
             this.Paint += new System.Windows.Forms.PaintEventHandler(this.MogreForm_Paint);
             this.ResumeLayout(false);
 
         }
 
         #endregion
 
         private System.Windows.Forms.Panel mogrePanel;
 
     }
 }



Alias: MOGRE_MousePickingExample