input

randomCoder

17-11-2006 03:11:39

Im new to mogre, and ive been using it for almost an hour now, and i like it so far :D

I have followed the examples and read the forums, and I have been attempting to get keyDown/keyUp/mouseMove/etc. handlers working. the problem is ...they do... but in a weird way...

id like to preface this with the disclaimer that i could be functionally retarded, and not know it :P

anyhow, i created my own project and ripped apart the mogreForms example. I created a seperate class for handling ogre type activities, created some uinput handlers, and registered them on the mian form that
contains the panel i want ogre displayed in.

in my ogreWindowClass, all the handlers reside, and the class contains a pointer to the form on which mogre is rendered.

oddly, the handlers are called on mouse input/key presses, etc. (the movement ocde was taken from tutorial 4 i think?), but behave strangely-- nothing happens. i can trace the code and see that control is passed to those functions...if i debug and step through, it works...if i dont step through, i get tnothing. so i output some text to a debug label on the form itself...and if i access the label on the main form with something like:


public void KeyDownHandler(object sender, KeyEventArgs e)
{
mParentForm.setDebugLabel("keypress: " + e.KeyCode+" CamPos: (" + camera.Position.x + "," + camera.Position.y + "," + camera.Position.z + ")");
switch (e.KeyCode)



it begins working. so i placed this in the frameStarted frame:

bool FrameStarted(FrameEvent evt)
{
camera.Position += camera.Orientation * mTranslation * evt.timeSinceLastFrame;
mParentForm.setDebugLabel("CamPos: (" + camera.Position.x + "," + camera.Position.y + "," + camera.Position.z + ")");



and i can see the camerapos changing. without the debug output in the keyDown handler...the camera position does not change. with the debug text, it does. makes no sense to me. theres more:

when i have debug text in all places and i can tell input is being passed to the handlers...lets say i want to mvoe the camera to the right (code from tutorial 4)... i can press the right key, and nothing happens, unless i alternate between the right key and some other random key...then the mogre panel updates.

the mouse functions do not work past hiding the cursor when the right mouse button is pressed. once again, i can trace through the code and output code in the functions, but the cmaera itself does not change.

heres a large chunk of ocde if it helps:

the form:

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();
mogreWin.setParentForm(this);
createHandlers();
}
public void createHandlers()
{
this.KeyDown += new KeyEventHandler(mogreWin.KeyDownHandler);
this.KeyUp += new KeyEventHandler(mogreWin.KeyUpHandler);
this.mogrePanel.MouseDown += new MouseEventHandler(mogreWin.MouseDownHandler);
this.mogrePanel.MouseUp += new MouseEventHandler(mogreWin.MouseUpHandler);
this.mogrePanel.MouseMove += new MouseEventHandler(mogreWin.MouseMoveHandler);
}

private void MogreForm_Paint(object sender, PaintEventArgs e)
{
mogreWin.Paint();
}

void MogreForm_Disposed(object sender, EventArgs e)
{
mogreWin.Dispose();
}

public void setDebugLabel(String text)
{
label1.Text = text;
}
}



and segments of the mogre window class
InitMogre() is from the mogreForm example

public class OgreWindow
{
public Root root;
public SceneManager sceneMgr;

protected Camera camera;
protected Viewport viewport;
protected RenderWindow window;
protected Point position;
protected IntPtr hWnd;

public MogreForm mParentForm;

//input stuffs
Vector3 mTranslation = Vector3.ZERO;
const float TRANSLATE = 10;
const float ROTATE = 0.2f;
bool mRotating = false;
Point mLastPosition;

public void setParentForm(MogreForm form)
{
mParentForm = form;
}

public OgreWindow(Point origin, IntPtr hWnd)
{
position = origin;
this.hWnd = hWnd;
}
public void initMogre(){...};

public void Paint()
{
root.RenderOneFrame();
}

public void Dispose()
{
if (root != null)
{
root.Dispose();
root = null;
}
}


public void KeyUpHandler(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.W:
case Keys.Down:
case Keys.S:
mTranslation.z = 0;
break;

case Keys.Left:
case Keys.A:
case Keys.Right:
case Keys.D:
mTranslation.x = 0;
break;

case Keys.PageUp:
case Keys.Q:
case Keys.PageDown:
case Keys.E:
mTranslation.y = 0;
break;
}
}
public void KeyDownHandler(object sender, KeyEventArgs e)
{
// mParentForm.setDebugLabel("keypress: " + e.KeyCode+" CamPos: (" + camera.Position.x + "," + camera.Position.y + "," + camera.Position.z + ")");
switch (e.KeyCode)
{
case Keys.Up:
case Keys.W:
mTranslation.z = -TRANSLATE;
break;

case Keys.Down:
case Keys.S:
mTranslation.z = TRANSLATE;
break;

case Keys.Left:
case Keys.A:
mTranslation.x = -TRANSLATE;
break;

case Keys.Right:
case Keys.D:
mTranslation.x = TRANSLATE;
break;

case Keys.PageUp:
case Keys.Q:
mTranslation.y = TRANSLATE;
break;

case Keys.PageDown:
case Keys.E:
mTranslation.y = -TRANSLATE;
break;
}
}


public void MouseMoveHandler(object sender, MouseEventArgs e)
{
if (mRotating)
{
float x = mLastPosition.X - Cursor.Position.X;
float y = mLastPosition.Y - Cursor.Position.Y;

camera.Yaw(new Degree(x * ROTATE));
camera.Pitch(new Degree(y * ROTATE));

mLastPosition = Cursor.Position;
}
}

public void MouseUpHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Cursor.Show();
mRotating = false;
}
}

public void MouseDownHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Cursor.Hide();
mRotating = true;
mLastPosition = Cursor.Position;
}
}
bool FrameStarted(FrameEvent evt)
{
camera.Position += camera.Orientation * mTranslation * evt.timeSinceLastFrame;
//mParentForm.setDebugLabel("CamPos: (" + camera.Position.x + "," + camera.Position.y + "," + camera.Position.z + ")");
//mParentForm.setDebugLabel("CamPos: (" + mTranslation.x + "," + mTranslation.y + "," + mTranslation.z + ")");
return true;
}
}




i may have explained this poorly (im a bit burnt out from work all day), so if i did something weird, feel free to belittle me :) thanks!

Clay

17-11-2006 05:45:58

The problem is you are only rendering a frame when the paint event is called. When you set the label's text, it sparks an invalidate which in turn calls the paint event, which calls the Root.RenderOneFrame method. You need to invalidate the window periodically yourself (say, 30-60 times per second) to render the scene.

To fix this, you can do one of two things:

1. Rewrite your program to pump events/render frames like the MogreFramework does. Look at the Go method in this class: OgreWindow.cs.

2. Create a Timer class. Every time the timer ticks, call Invalidate on your form. This will start a redraw and that, in turn, will call Root.RenderOneFrame.

Doing #2 will be the easiest in your case, but be careful. If your frame listener changes the label, the label change will spark a redraw...and this will start an infinite chain of redraws. That's NOT going to freeze the program (it'll make it work like #1), but the timer's interval time will no longer have any meaning.

#1 is the safest way of doing this if you are going to overlay Windows.Forms objects on top of the window that's drawing Ogre.

I need to do a better job explaining this in the tutorials. Thanks for the report. I'll make some changes after I finish with the tutorial I'm currently working on.

randomCoder

18-11-2006 15:36:00

ah, that makes sense. I haven't had a whole lot of windows coding experience (usually just console c++), so i didnt even think of invalidating the form as the cause. thanks for the help-