sAmurai
31-03-2011 12:14:48
Hi Mogre Community!
I am currently using MOGRE as my renderer for an advertiesment app. Here is what i want to achieve:
I want a small Windows Forms GUI as the applications main window. It has its own thread of course.
By pressing a button i want a new windows form to pop up and have run ogre in it.
From there on i want to use the main GUI to manipulate the scenery/window properties in the secondary window.
Therefore the rendering loop from ogre has to run in its own thread.
So far everything works fine. I press a button in my GUI and a new windows form pops up, ogre starts in it and shows my scenery. But as soon as i try to resize the window my program crashes. To me it looks like this must be some kind of multithreading issue, where ogre trys to update the scenery but due to the resizing operations the window is unavailable but i have locks everywhere!
Also i use ResizeBegin and ResizeEnd events to stop and resume the render loop but my program still crashes as soon as i release the mouse button after resizing the window.
I'm totally running out of ideas...
I have been following this tutorial:
http://www.ogre3d.org/tikiwiki/Mogre+Basic+Tutorial+6
And here is the important code:
Any advice?
Oh btw the program crashes with an System.Runtime.InteropServices.SEHException in the Root's RenderOneFrame method
I am currently using MOGRE as my renderer for an advertiesment app. Here is what i want to achieve:
I want a small Windows Forms GUI as the applications main window. It has its own thread of course.
By pressing a button i want a new windows form to pop up and have run ogre in it.
From there on i want to use the main GUI to manipulate the scenery/window properties in the secondary window.
Therefore the rendering loop from ogre has to run in its own thread.
So far everything works fine. I press a button in my GUI and a new windows form pops up, ogre starts in it and shows my scenery. But as soon as i try to resize the window my program crashes. To me it looks like this must be some kind of multithreading issue, where ogre trys to update the scenery but due to the resizing operations the window is unavailable but i have locks everywhere!
Also i use ResizeBegin and ResizeEnd events to stop and resume the render loop but my program still crashes as soon as i release the mouse button after resizing the window.
I'm totally running out of ideas...
I have been following this tutorial:
http://www.ogre3d.org/tikiwiki/Mogre+Basic+Tutorial+6
And here is the important code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Display
{
public partial class RenderForm : Form
{
private Renderer mRenderer;
private Thread mRenderThread;
public IRenderControl RendererProp
{
get { return mRenderer; }
}
public RenderForm()
{
InitializeComponent();
mRenderer = new Renderer();
//Disposed += new EventHandler(mRenderer.RenderFormDisposed);
FormClosing += new FormClosingEventHandler(mRenderer.RenderFormDisposed);
Resize += new EventHandler(mRenderer.RenderFormResized);
ResizeBegin += new EventHandler(RenderForm_ResizeBegin);
ResizeEnd += new EventHandler(RenderForm_ResizeEnd);
mRenderer.Initialize(Handle);
Show();
mRenderThread = new Thread(new ThreadStart(mRenderer.Go));
mRenderThread.Start();
//mRenderer.Go();
}
private void RenderForm_ResizeBegin(object sender, EventArgs args)
{
lock (mRenderer)
{
Console.WriteLine("ResizeBegin");
mRenderer.ResizePause = true;
}
}
private void RenderForm_ResizeEnd(object sender, EventArgs args)
{
lock (mRenderer)
{
Console.WriteLine("ResizeEnd");
mRenderer.ResizePause = false;
}
}
}
}
using Mogre;
using Mogre.TutorialFramework;
using System;
using System.Windows.Forms;
using System.Threading;
namespace Display
{
public class Renderer : IRenderControl
{
protected Root mRoot = null;
protected RenderWindow mRenderWindow;
protected IntPtr mWindowHandle;
protected SceneManager mSceneMgr;
protected Camera mCamera;
protected Entity mContent;
protected SceneNode mContentNode;
protected bool mFullscreen = false;
protected bool mGoRendering = true;
protected bool mResizePause = false;
protected UInt32 mRenderCounter = 0;
public bool ResizePause
{
get { return mResizePause; }
set { mResizePause = value; }
}
public void RenderFormDisposed(object sender, EventArgs evt)
{
lock (this)
{
mGoRendering = false;
mRoot.Dispose();
mRoot = null;
}
}
public void RenderFormResized(object sender, EventArgs evt)
{
//Console.WriteLine("Resize");
mRenderWindow.WindowMovedOrResized();
}
// Inherited from IRenderControl
public void Initialize(IntPtr windowHandle)
{
mWindowHandle = windowHandle;
//this.Go();
// init root object
mRoot = new Root();
// Define Resources
ConfigFile cf = new ConfigFile();
cf.Load("resources.cfg", "\t:=", true);
var section = cf.GetSectionIterator();
while (section.MoveNext())
{
foreach (var line in section.Current)
{
ResourceGroupManager.Singleton.AddResourceLocation(
line.Value, line.Key, section.CurrentKey);
}
}
// Setup RenderSystem
RenderSystem rs = mRoot.GetRenderSystemByName("Direct3D9 Rendering Subsystem");
// or use "OpenGL Rendering Subsystem"
rs.SetConfigOption("Full Screen", "No");
rs.SetConfigOption("Video Mode", "1024 x 768 @ 32-bit colour");
mRoot.RenderSystem = rs;
// Create Render Window
mRoot.Initialise(false, "Main Ogre Window");
NameValuePairList misc = new NameValuePairList();
misc["externalWindowHandle"] = mWindowHandle.ToString();
mRenderWindow = mRoot.CreateRenderWindow("Main RenderWindow", 1024, 768, false, misc);
// Init resources
TextureManager.Singleton.DefaultNumMipmaps = 5;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
// Create scene manager, camera and viewport
mSceneMgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
mCamera = mSceneMgr.CreateCamera("Camera");
mCamera.Position = new Vector3(0, 100, 200);
mCamera.LookAt(Vector3.ZERO);
mCamera.AutoAspectRatio = true;
Viewport viewport = mRenderWindow.AddViewport(mCamera);
viewport.BackgroundColour = ColourValue.White;
// create scenery
CreateScene();
}
public void Go()
{
while (mGoRendering)
{
if (!mResizePause)
{
lock (this)
{
if (mRoot != null && mRoot.RenderOneFrame())
{
Console.WriteLine("{0}", mRenderCounter);
mRenderCounter++;
Application.DoEvents();
}
}
}
}
}
// Inherited from IRenderControl
public void Translate(float x, float y, float z)
{
mContentNode.Translate(x, y, z);
}
// Inherited from IRenderControl
public void Rotate(float yaw, float pitch, float roll)
{
mContentNode.Yaw(new Degree(yaw));
mContentNode.Pitch(new Degree(pitch));
mContentNode.Roll(new Degree(roll));
}
// Inherited from IRenderControl
public void Fullscreen(bool fullscreen)
{
// this api function is buggy!
//mRenderWindow.SetFullscreen(fullscreen, 1024, 768);
}
protected void CreateScene()
{
// set lighting properties
mSceneMgr.AmbientLight = ColourValue.Black;
mSceneMgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_MODULATIVE;
// set skybox
//mSceneMgr.SetSkyDome(true, "Examples/CloudySky", 2, 8);
mContent = mSceneMgr.CreateEntity("Content", "teapot.mesh");
mContent.CastShadows = true;
mContentNode = mSceneMgr.RootSceneNode.CreateChildSceneNode("ContentNode");
mContentNode.AttachObject(mContent);
mContentNode.Rotate(Vector3.UNIT_Y, new Degree(180));
// create plane
Plane plane = new Plane(Vector3.UNIT_Y, 0);
MeshManager.Singleton.CreatePlane("ground", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane,
1500, 1500, 20, 20, true, 1, 5, 5, Vector3.UNIT_Z);
Entity ground = mSceneMgr.CreateEntity("GroundEntity", "ground");
ground.CastShadows = false;
//ground.ReceivesShadows = true;
MaterialManager.Singleton.Load("white", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
//ground.SetMaterialName("white");
ground.SetMaterial(MaterialManager.Singleton.GetByName("white"));
mSceneMgr.RootSceneNode.CreateChildSceneNode("GroundNode").AttachObject(ground);
// test
/*Entity ent = mSceneMgr.CreateEntity("head", "ogrehead.mesh");
ent.CastShadows = true;
SceneNode node = mSceneMgr.RootSceneNode.CreateChildSceneNode("headNode");
node.AttachObject(ent);*/
// create lights
Light dirLight = mSceneMgr.CreateLight("dirLight");
dirLight.Type = Light.LightTypes.LT_DIRECTIONAL;
dirLight.DiffuseColour = new ColourValue(1, 1, 1);
dirLight.SpecularColour = new ColourValue(1, 1, 1);
dirLight.Direction = new Vector3(1, -1, 1);
}
}
}
Any advice?
Oh btw the program crashes with an System.Runtime.InteropServices.SEHException in the Root's RenderOneFrame method