Embed Mogre into usercontrols


11-04-2007 16:29:15

I want to embed Mogre into one of user control, something like a panel, by following Tutorial 6's instructions. But nothing happened. In another attempt I fully folowed the tutorial and there's no problem. After some study, I changed the code like this:
// Create Render Window
mRoot.Initialise(false, "Main Ogre Window");
NameValuePairList misc = new NameValuePairList();
misc["parentWindowHandle"] = this.dockPanel.Handle.ToString();
misc["outerDimensions"] = "true";
mWindow = mRoot.CreateRenderWindow("MainRenderWindow",(uint)this.dockPanel.Width, (uint)this.dockPanel.Height, false, misc);

"dockPanel" is the name of control. Things seem get better, because the full window become black. Yet nothing more, the ninja model cannt be found anywhere, neither do any exception(I have put all the code under a try block).
So, is there anybody knows what's going on?


11-04-2007 22:09:59

I'm not sure I understand, is tutorial 6 working or not?


12-04-2007 02:00:14

tutorial 6 works pretty well

He Who Is

12-04-2007 15:08:53

I think what he's saying is that he's using Tutorial 6 to test his technique of placing Mogre in a Control.

I would suggest simply creating a class that extends RenderTarget, and which draws to the specified Control.

Alternatively, you could use a PictureBox as your Control, create a normal RenderWindow, and hide it. Then you would draw the RenderWindow to a Bitmap (Form.drawToBitmap(...)), and display the bitmap in the Picturebox. (Remember to remove all borders and padding from the Form, and set it to exactly the same size as the Control you'll draw it to.

The second one seems more complicated, but I really have no idea how complicated the first one is.


12-04-2007 23:49:35

Hi Mars,
I was struggling with the same issue for some time there and have only just managed to get around it.

I created a class for the ogre portion as in tutorial 6 and adjusted the constructor so that it could be assigned to any control on the form.

public OgreWindow(Point origin, IntPtr hWnd)
position = origin;
this.hWnd = hWnd;

because of this the create Render window was simplified
protected virtual void CreateRenderWindow(String title, IntPtr handle)
fRoot.Initialise(false, title);
if (handle != IntPtr.Zero)
NameValuePairList misc = new NameValuePairList();
misc["externalWindowHandle"] = handle.ToString();
fWindow = fRoot.CreateRenderWindow("RALDSmainRenderWindow", 800, 600, false, misc);

I'd suggest looking at the MogreFramework files to get a better understanding for it though.
Hope this helps

Edit: Oh my mistake you must add a paint event to the control and get it to call mogres paint handler... i.e.
private void MogreForm_Paint(object sender, PaintEventArgs e)


13-04-2007 04:17:14

thx all
I had resolved the problem by myself.The truth is,as you had noticed it, I forgot add a Paint event to my app, so the window appears entirely black.
Another question:the app repaint itself without stop, so it dont allow me to draw a static scene and repaint whenever change occured. when required(i.e. user press a button),it can enable danemic realtime render thereafter. is there some way to achieve that?


13-04-2007 04:21:52


I havent really followed the topic here. But do you, at any point, call the StartRendering() method ?

Cause if you do, it will keep rendering and use all your CPU resource. It is cool for a game, but not for an app. The solution is to control the update process yourself by calling Update() or your RenderWindow(s).

Or better yet! There is a method called RenderOneFrame() in the Mogre.Root class that does just that.

Have fun!


13-04-2007 04:41:03

hi, thanks for your suggestion
actually I did use RenderOneFrame(), like this:
Paint += new PaintEventHandler(MogreForm_Paint);
private void MogreForm_Paint(object sender, PaintEventArgs e)
private void Render()

So the true question is: when and how paint event works?I didnt make any change to my app window, but the paint is being called on and on. Is there some better choice?


13-04-2007 04:46:15

Its been a while since the last time i played with that stuff. You should really look in the MSDN documentation, there a zillions of examples of that. Stuff like "Overriding the OnPaint Method", and so on.

Have fun!



13-04-2007 04:49:38

I'll have a try there


29-08-2008 14:32:16

Hello! I'm trying to integrate Mogre with DockPanel sourceforge.net/projects/dockpanelsuite/. Following tutorials 5 and 6, everything has worked for me. I'm creating a second form with a panel stretched all over the form and passing its handle to the CreateRenderWindow method. I can post here some code, if you'd like.

However, I'm expiriencing problems when I start using the docking possibilities of DockPanel. The window gets lost its context and crashes ogre because of "Direct3D Device Lost". I have an event handler for this, but no idea what to write there. It should be something like "detatch the window from the rendering context and reinitialize it with a new one". Anyone can help, please?


02-09-2008 07:12:51


Perhaps this will help :

Public Sub ReSize()

'Pour redimensionner le vieport de manière correcte quand on redimmensionne la picturebox (du à un resize de la form ou un déplacement des splitter)
'to resize vieport when render control is resized

If Not _init Then
'on récupère le viewport / getting vieport
Dim myViewport As Mogre.Viewport = _RenderWindow.GetViewport(0)

'on récupère la couleur du vieport car elle est perdu à sa destruction / geting vieport color before destruction
Dim CouleurVieport As Mogre.ColourValue = myViewport.BackgroundColour

'On le supprime de la fenêtre de rendu / removing viewport from render window

'Et on efface la fenêtre de rendu / destroying render window
_RenderWindow = Nothing

'On recrè la fenêtre, qui prendra comme dimension la nouvelle taille du contrôle / recreation of render window with it's new size
Dim misc As NameValuePairList = New NameValuePairList
misc("externalWindowHandle") = _RenderWindowHandle.ToString
Dim const_list As Const_NameValuePairList = misc.ReadOnlyInstance
_RenderWindow = _OgreRacine.CreateRenderWindow("OgreWieport", 0, 0, False, const_list)

'On recrè un viewport avec les nouveaux paramètres / creation of new vieport
myViewport = _RenderWindow.AddViewport(_OgreCamera)
'on réaffecte la couleur précédemment sauvegardée / aplying save color
myViewport.BackgroundColour = CouleurVieport
'on fixe l'aspect ratio de la caméra / camera new aspect ratio
_OgreCamera.AspectRatio = myViewport.ActualWidth / myViewport.ActualHeight

Catch ex As Exception
'en cas d'erreur..../ if error
If OgreException.IsThrown Then
MsgBox(OgreException.LastException.FullDescription, MsgBoxStyle.Critical, _
"Exeption OGRE!")

Catch ex2 As Exception

End Try
MsgBox(ex.ToString, "Erreur")
End If
End Try
End If
End Sub

I use this to resize ogre window. Perhaps, your ploblem is linked.


08-10-2008 23:32:03

Hi Guys,

cause i also need the possibility to resize the render control in my app, i have done that in this way:

i have a class MainRender, with a mehod NotifyResize(float Width, float Height).

void OnMyControlResize(object sender, EventArgs e)

///MainRender class
public void NotifyResize(float Width, float Height)
this.Camera.AspectRatio = Width / Height;

Thats it 8)


09-10-2008 06:09:15


It"s more simple, I will try this.

Edit :

Try it, adopt it...2 lines in place of many more, that is I called optimisation. Thanks a lot.