MOGRE Intermediate tutorial 4 almost working

Acheron

21-03-2011 13:01:03

Hi I'm new to MOGRE, and almost got MOGRE Intermediate tutorial 4 working, but the values from the MOIS mouse pointer seem to be incorrect. Basically you can draw the box if you click in the top left of the window. The class is derived from the one in the MOgre demo. So is there a bug in MOIS or am I doing something wrong?


using System;
using System.Collections.Generic;
using System.Text;
using MOIS;
using Mogre;

namespace Mogre.Demo.ExampleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
VolumeSelectionApplication app = new VolumeSelectionApplication();
app.Go();
}
catch (System.Runtime.InteropServices.SEHException)
{
// Check if it's an Ogre Exception
if (OgreException.IsThrown)
ExampleApplication.Example.ShowOgreException();
else
throw;
}
}
}

class VolumeSelectionApplication : Mogre.Demo.ExampleApplication.Example
{
Vector2 mStart, mStop;
PlaneBoundedVolumeListSceneQuery mVolQuery;
List<MovableObject> mSelected = new List<MovableObject>();
SelectionRectangle mRect;
bool bSelecting;


public override void CreateFrameListener()
{
base.CreateFrameListener();
Root.Singleton.FrameStarted += FrameStarted;
}

public override bool UseBufferedInput
{
get { return true; }
}

public override void CreateInput()
{
// this is completely overridden from the base class
LogManager.Singleton.LogMessage("*** Initializing OIS ***");
MOIS.ParamList paramList = new MOIS.ParamList();
paramList.Insert("w32_mouse", "DISCL_FOREGROUND");
paramList.Insert("w32_mouse", "DISCL_NONEXCLUSIVE");

IntPtr windowHnd;
window.GetCustomAttribute("WINDOW", out windowHnd);
paramList.Insert("WINDOW", windowHnd.ToString());

inputManager = MOIS.InputManager.CreateInputSystem(paramList);

//Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
inputKeyboard = (MOIS.Keyboard)inputManager.CreateInputObject(MOIS.Type.OISKeyboard, UseBufferedInput);
inputMouse = (MOIS.Mouse)inputManager.CreateInputObject(MOIS.Type.OISMouse, UseBufferedInput);
inputMouse.MousePressed += new MouseListener.MousePressedHandler(OnMousePressed);
inputMouse.MouseReleased += new MouseListener.MouseReleasedHandler(OnMouseReleased);
inputMouse.MouseMoved += new MouseListener.MouseMovedHandler(OnMouseMoved);
}

protected bool OnMousePressed(MouseEvent evt, MouseButtonID button)
{
switch (button)
{
case MOIS.MouseButtonID.MB_Left:
mStart.x = (float)evt.state.X.abs / (float)evt.state.width;
mStart.y = (float)evt.state.Y.abs / (float)evt.state.height;
mStop = mStart;

bSelecting = true;
mRect.Clear();
mRect.Visible = true;

mDebugText = evt.state.X.abs + ", " + evt.state.Y.abs;
break;
}
return true;
}

protected bool OnMouseReleased(MouseEvent evt, MouseButtonID button)
{
switch (button)
{
case MOIS.MouseButtonID.MB_Left:
performSelection(mStart, mStop);
bSelecting = false;
mRect.Visible = false;
break;
}
return true;
}

protected bool OnMouseMoved(MouseEvent evt)
{
if (bSelecting)
{
mStop.x = evt.state.X.abs / (float)evt.state.width;
mStop.y = evt.state.Y.abs / (float)evt.state.height;

mRect.setCorners(mStart, mStop);
}
return true;
}

protected override void HandleInput(FrameEvent evt)
{
// this is completely overridden from the base class, to prevent it doing the camera movement stuff
inputKeyboard.Capture();
inputMouse.Capture();
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_ESCAPE))
{
// stop rendering loop
shutDown = true;
}

// update performance stats once per second
if (statDelay < 0.0f && showDebugOverlay)
{
UpdateStats();
statDelay = 1.0f;
}
else
{
statDelay -= evt.timeSinceLastFrame;
}

// turn off debug text when delay ends
if (debugTextDelay < 0.0f)
{
debugTextDelay = 0.0f;
mDebugText = "";
}
else if (debugTextDelay > 0.0f)
{
debugTextDelay -= evt.timeSinceLastFrame;
}

}

bool FrameStarted(FrameEvent evt)
{
return true;
}

public override void DestroyScene()
{
sceneMgr.DestroyQuery(mVolQuery);
mRect.Dispose();
}

public override void CreateScene()
{
camera.SetPosition(-60, 100, -60);
camera.LookAt(60, 0, 60);

sceneMgr.AmbientLight = ColourValue.White;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
Entity ent = sceneMgr.CreateEntity("Robot" + (i + j * 10), "robot.mesh");
//Mogre.StringConverter.ToString(i + j * 10)
SceneNode node = sceneMgr.RootSceneNode.CreateChildSceneNode(new Vector3(i * 15, 0, j * 15));
node.AttachObject(ent);
node.SetScale(0.1f, 0.1f, 0.1f);
}
}
mRect = new SelectionRectangle("Selection SelectionRectangle");
sceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(mRect);
}

void performSelection(Vector2 first, Vector2 second)
{
}



void selectObject(MovableObject obj)
{
obj.ParentSceneNode.ShowBoundingBox = true;
mSelected.Add(obj);
}

void deselectObjects()
{
foreach (var obj in mSelected)
{
obj.ParentSceneNode.ShowBoundingBox = false;
}
mSelected.Clear();
}


static void swap(ref float x, ref float y)
{
float tmp = x;
x = y;
y = tmp;
}
}


public class SelectionRectangle : ManualObject
{
public SelectionRectangle(String name)
: base(name)
{
RenderQueueGroup = (byte)RenderQueueGroupID.RENDER_QUEUE_OVERLAY; // when using this, ensure Depth Check is Off in the material
UseIdentityProjection = true;
UseIdentityView = true;
QueryFlags = 0;
}

/**
* Sets the corners of the SelectionRectangle. Every parameter should be in the
* range [0, 1] representing a percentage of the screen the SelectionRectangle
* should take up.
*/
void setCorners(float left, float top, float right, float bottom)
{
left = left * 2 - 1;
right = right * 2 - 1;
top = 1 - top * 2;
bottom = 1 - bottom * 2;
Clear();
Begin("", RenderOperation.OperationTypes.OT_LINE_STRIP);
Position(left, top, -1);
Position(right, top, -1);
Position(right, bottom, -1);
Position(left, bottom, -1);
Position(left, top, -1);
End();
BoundingBox.SetInfinite();
}

public void setCorners(Vector2 topLeft, Vector2 bottomRight)
{
setCorners(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
}
}

smiley80

21-03-2011 13:34:55

Add the following to 'CreateInput':
MOIS.MouseState_NativePtr state = inputMouse.MouseState;
state.width = (int)window.Width;
state.height = (int)window.Height;

Acheron

21-03-2011 15:58:41

Thanks for the quick reply! I found I had to do 1 more tweak, I changed

mVolQuery.SetVolumes(volList);

to

PlaneBoundedVolumeListSceneQuery volQuery = sceneMgr.CreatePlaneBoundedVolumeQuery(volList);

and also changed it from a member variable to a local variable.

It's working fine now, here is the code for the completed tutorial:


using System;
using System.Collections.Generic;
using System.Text;
using MOIS;
using Mogre;

namespace Mogre.Demo.ExampleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
VolumeSelectionApplication app = new VolumeSelectionApplication();
app.Go();
}
catch (System.Runtime.InteropServices.SEHException)
{
// Check if it's an Ogre Exception
if (OgreException.IsThrown)
ExampleApplication.Example.ShowOgreException();
else
throw;
}
}
}

class VolumeSelectionApplication : Mogre.Demo.ExampleApplication.Example
{
Vector2 mStart, mStop;
List<MovableObject> mSelected = new List<MovableObject>();
SelectionRectangle mRect;
bool bSelecting;


public override void CreateFrameListener()
{
base.CreateFrameListener();
Root.Singleton.FrameStarted += FrameStarted;
}

public override bool UseBufferedInput
{
get { return true; }
}

public override void CreateInput()
{
// this is completely overridden from the base class
LogManager.Singleton.LogMessage("*** Initializing OIS ***");
MOIS.ParamList paramList = new MOIS.ParamList();
paramList.Insert("w32_mouse", "DISCL_FOREGROUND");
paramList.Insert("w32_mouse", "DISCL_NONEXCLUSIVE");

IntPtr windowHnd;
window.GetCustomAttribute("WINDOW", out windowHnd);
paramList.Insert("WINDOW", windowHnd.ToString());

inputManager = MOIS.InputManager.CreateInputSystem(paramList);

//Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
inputKeyboard = (MOIS.Keyboard)inputManager.CreateInputObject(MOIS.Type.OISKeyboard, UseBufferedInput);
inputMouse = (MOIS.Mouse)inputManager.CreateInputObject(MOIS.Type.OISMouse, UseBufferedInput);

MOIS.MouseState_NativePtr state = inputMouse.MouseState;
state.width = (int)window.Width;
state.height = (int)window.Height;

inputMouse.MousePressed += new MouseListener.MousePressedHandler(OnMousePressed);
inputMouse.MouseReleased += new MouseListener.MouseReleasedHandler(OnMouseReleased);
inputMouse.MouseMoved += new MouseListener.MouseMovedHandler(OnMouseMoved);
}

protected bool OnMousePressed(MouseEvent evt, MouseButtonID button)
{
switch (button)
{
case MOIS.MouseButtonID.MB_Left:
mStart.x = (float)evt.state.X.abs / (float)evt.state.width;
mStart.y = (float)evt.state.Y.abs / (float)evt.state.height;
mStop = mStart;

bSelecting = true;
mRect.Clear();
mRect.Visible = true;

mDebugText = evt.state.X.abs + ", " + evt.state.Y.abs;
break;
}
return true;
}

protected bool OnMouseReleased(MouseEvent evt, MouseButtonID button)
{
switch (button)
{
case MOIS.MouseButtonID.MB_Left:
performSelection(mStart, mStop);
bSelecting = false;
mRect.Visible = false;
break;
}
return true;
}

protected bool OnMouseMoved(MouseEvent evt)
{
if (bSelecting)
{
mStop.x = evt.state.X.abs / (float)evt.state.width;
mStop.y = evt.state.Y.abs / (float)evt.state.height;

mDebugText = evt.state.X.abs + ", " + evt.state.Y.abs;

mRect.setCorners(mStart, mStop);
}
return true;
}

protected override void HandleInput(FrameEvent evt)
{
// this is completely overridden from the base class, to prevent it doing the camera movement stuff
inputKeyboard.Capture();
inputMouse.Capture();
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_ESCAPE))
{
// stop rendering loop
shutDown = true;
}

// update performance stats once per second
if (statDelay < 0.0f && showDebugOverlay)
{
UpdateStats();
statDelay = 1.0f;
}
else
{
statDelay -= evt.timeSinceLastFrame;
}

// turn off debug text when delay ends
if (debugTextDelay < 0.0f)
{
debugTextDelay = 0.0f;
mDebugText = "";
}
else if (debugTextDelay > 0.0f)
{
debugTextDelay -= evt.timeSinceLastFrame;
}

}

bool FrameStarted(FrameEvent evt)
{
return true;
}

public override void DestroyScene()
{
mRect.Dispose();
}

public override void CreateScene()
{
camera.SetPosition(-60, 100, -60);
camera.LookAt(60, 0, 60);

sceneMgr.AmbientLight = ColourValue.White;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
Entity ent = sceneMgr.CreateEntity("Robot" + (i + j * 10), "robot.mesh");
//Mogre.StringConverter.ToString(i + j * 10)
SceneNode node = sceneMgr.RootSceneNode.CreateChildSceneNode(new Vector3(i * 15, 0, j * 15));
node.AttachObject(ent);
node.SetScale(0.1f, 0.1f, 0.1f);
}
}
mRect = new SelectionRectangle("Selection SelectionRectangle");
sceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(mRect);
}

void performSelection(Vector2 first, Vector2 second)
{
float left = first.x, right = second.x,
top = first.y, bottom = second.y;

if (left > right) swap(ref left, ref right);
if (top > bottom) swap(ref top, ref bottom);

if ((right - left) * (bottom - top) < 0.0001) return;

Ray topLeft = camera.GetCameraToViewportRay(left, top);
Ray topRight = camera.GetCameraToViewportRay(right, top);
Ray bottomLeft = camera.GetCameraToViewportRay(left, bottom);
Ray bottomRight = camera.GetCameraToViewportRay(right, bottom);

PlaneBoundedVolume vol = new PlaneBoundedVolume();
vol.planes.Add(new Plane(topLeft.GetPoint(3), topRight.GetPoint(3), bottomRight.GetPoint(3))); // front plane
vol.planes.Add(new Plane(topLeft.Origin, topLeft.GetPoint(100), topRight.GetPoint(100))); // top plane
vol.planes.Add(new Plane(topLeft.Origin, bottomLeft.GetPoint(100), topLeft.GetPoint(100))); // left plane
vol.planes.Add(new Plane(bottomLeft.Origin, bottomRight.GetPoint(100), bottomLeft.GetPoint(100)));// bottom plane
vol.planes.Add(new Plane(topRight.Origin, topRight.GetPoint(100), bottomRight.GetPoint(100))); // right plane

PlaneBoundedVolumeList volList = new PlaneBoundedVolumeList();
volList.Add(vol);
PlaneBoundedVolumeListSceneQuery volQuery = sceneMgr.CreatePlaneBoundedVolumeQuery(volList);
SceneQueryResult result = volQuery.Execute();

foreach (var obj in result.movables) selectObject(obj);

sceneMgr.DestroyQuery(volQuery);
}

void selectObject(MovableObject obj)
{
obj.ParentSceneNode.ShowBoundingBox = true;
mSelected.Add(obj);
}

void deselectObjects()
{
foreach (var obj in mSelected)
{
obj.ParentSceneNode.ShowBoundingBox = false;
}
mSelected.Clear();
}


static void swap(ref float x, ref float y)
{
float tmp = x;
x = y;
y = tmp;
}
}


public class SelectionRectangle : ManualObject
{
public SelectionRectangle(String name)
: base(name)
{
RenderQueueGroup = (byte)RenderQueueGroupID.RENDER_QUEUE_OVERLAY; // when using this, ensure Depth Check is Off in the material
UseIdentityProjection = true;
UseIdentityView = true;
QueryFlags = 0;
}

/**
* Sets the corners of the SelectionRectangle. Every parameter should be in the
* range [0, 1] representing a percentage of the screen the SelectionRectangle
* should take up.
*/
void setCorners(float left, float top, float right, float bottom)
{
left = left * 2 - 1;
right = right * 2 - 1;
top = 1 - top * 2;
bottom = 1 - bottom * 2;
Clear();
Begin("", RenderOperation.OperationTypes.OT_LINE_STRIP);
Position(left, top, -1);
Position(right, top, -1);
Position(right, bottom, -1);
Position(left, bottom, -1);
Position(left, top, -1);
End();
BoundingBox.SetInfinite();
}

public void setCorners(Vector2 topLeft, Vector2 bottomRight)
{
setCorners(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
}
}

questore

17-06-2012 12:12:11

Hi guys!
The mogre code is working for me too except:
When I reposition the camera to look at diferent direction, the selection working, but no selection line is drawn in screen..any idea why it is not working?
Example looking at this camera angle draws no line but select objects on screen nicely for me:

mCamera.Position = new Vector3(500, 500, 500);
mCamera.LookAt(new Vector3(-1000, 200, -50));
mCamera.NearClipDistance = 0.1f;
mCamera.FarClipDistance = 50000;

Lotus

07-12-2012 23:36:02

Hi!

I'm new to Mogre and going through the VB.Net tutorials (yes, I realize I am in the minority). I am working with Mogre 1.7.3 and all of the Beginner tutorials worked perfectly - thanks for porting them to VB.Net. I am now going though Intermediate tutorials and realize the same level of attention has not been given to the VB.Net side of these ones. That's OK. I have worked with C# enough to get by and translate to VB.Net easily enough. I have gotten to the Mogre Intermediate tutorial 4 and almost got it working in VB. There is the problem of not having a cursor, but other than that everything else works. There is only one line giving me trouble:

mVolQuery.SetVolumes(volList);

Normally this would be easy enough in VB.Net - it should just be:

mVolQuery.SetVolumes(volList)

Trouble is that I am getting a type conversion error that I don't know how to get around. It says "Error Value of type 'Mogre.PlaneBoundedVolumeList' cannot be converted to 'Mogre.Const_PlaneBoundedVolumeList'."

Anyone know how to get around this?

Thanks!