MOGRE Form Problems in VB.NET

jimanjr

04-07-2008 08:30:23

Hi!

I'm trying to use MOGRE with VB.net to make a little preview window in my editor.

The problem is I can't seem to be able to create a resizeable form.

Here's my code:


Imports Mogre

Public Class Form2

Public myKeyboard As MOIS.Keyboard
Public myMouse As MOIS.Mouse
Public myCamera As Camera
Public MyWindow As RenderWindow
Public myTranslation As Vector3 = Vector3.ZERO
Public Quitting As Boolean = False
Public myRotating As Boolean = False


Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim myRoot As Root = New Root("Plugins.cfg", "ogre.cfg", "ogre.log")

'Show Ogre Rendering Subsystem setup dialog box
If Not myRoot.RestoreConfig Then
If Not myRoot.ShowConfigDialog Then
Exit Sub
End If
End If

'Create an Ogre render window
'MyWindow = myRoot.Initialise(True, "Application")
Dim misc As New NameValuePairList
misc("externalWindowHandle") = Me.Handle.ToString()
Dim const_list As Const_NameValuePairList = misc.ReadOnlyInstance
MyWindow = myRoot.CreateRenderWindow("Simple Mogre Form Window", 0, 0, False, const_list)
'AddHandler myRoot.FrameStarted, AddressOf FrameStarted

'Create Ogre SceneManager & Set AmbientLight to bright white
Dim mySceneManager As SceneManager = myRoot.CreateSceneManager(SceneType.ST_GENERIC)
mySceneManager.AmbientLight = New ColourValue(1, 1, 1)

'Read Resources
Dim cf As New ConfigFile
cf.Load("resources.cfg", vbTab + ":=", True)
Dim seci As ConfigFile.SectionIterator = cf.GetSectionIterator
Dim secName As String, typeName As String, archName As String
While (seci.MoveNext())
secName = seci.CurrentKey
Dim settings As ConfigFile.SettingsMultiMap = seci.Current
For Each pair As KeyValuePair(Of String, String) In settings
typeName = pair.Key
archName = pair.Value
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName)
Next
End While
ResourceGroupManager.Singleton.InitialiseAllResourceGroups()

'Create Camera
myCamera = mySceneManager.CreateCamera("Camera")
myCamera.SetPosition(0, 0, 500)
myCamera.LookAt(0, 0, 0)
myCamera.NearClipDistance = 5
myCamera.FarClipDistance = 5000

'Viewport
Dim myViewport As Viewport = MyWindow.AddViewport(myCamera)

'Keyboard
Dim windowHnd As Integer
MyWindow.GetCustomAttribute("WINDOW", windowHnd)
Dim myInputManager As MOIS.InputManager = MOIS.InputManager.CreateInputSystem(windowHnd)
myKeyboard = myInputManager.CreateInputObject(MOIS.Type.OISKeyboard, True)

'Mouse
myMouse = myInputManager.CreateInputObject(MOIS.Type.OISMouse, True)

'Start rendering
myRoot.StartRendering()


End Sub
End Class


It crashes when I try to create a window:


An unhandled exception of type 'System.AccessViolationException' occurred in Mogre.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


If I use MyWindow = myRoot.Initialise(True, "Application") everything works, but the MOGRE window takes total control of my application and becomes the only window.

Can anyone give me a hand on this one?

P.S. I know I'm doing something wrong. I'm new to MOGRE, and I couldn't find an example with Forms for VB.NET, so bare with me :)

raygeee

05-07-2008 14:57:30

Where exactly does the exception occur? In which line?

How do you create your Form2 instance? With the built-in Windows.Forms-Application-Framework? Dou you use Application.Run(new Form2()) or the Visual Studio Project settings? Then there's your problem.

The problem are the different threads. I always get that exception if I try to set Form properties from a different thread than the one that created the Form. Mogre runs in a different thread (with StartRendering()) than the creation of your Form.

One other hint: Please don't ever run so much vital code in your Form's Load-Event. Try to seperate it and get back control of your threads.

jimanjr

07-07-2008 07:54:31

Hm... I managed to get it running. I converted the code from the C# tutorial, and everything worked fine.

I'm using Form2 (i know I should name it to something more meaningful :P ) as a preview window. I'm not actually creating it anywhere. I just do "Form2.Show()" on a button click event.

I know I should have the whole MOGRE loading code somewhere else, but I'm not sure how to do that. Right now, when the window is shown, it loads everything and when you close it, it disposes of everything. It's a bit slow (about 2 seconds 'till the window starts), but it's not that bad for an editor.

Still, I'd like to know how to separate that code from the form_load. I think the problem lies in the fact that everything gets destroyed when you close the form.

Awaiting suggestions :)

feanor91

21-07-2008 19:42:01

Hi...

Hm... I managed to get it running. I converted the code from the C# tutorial, and everything worked fine.

I'm using Form2 (i know I should name it to something more meaningful :P ) as a preview window. I'm not actually creating it anywhere. I just do "Form2.Show()" on a button click event.

I know I should have the whole MOGRE loading code somewhere else, but I'm not sure how to do that. Right now, when the window is shown, it loads everything and when you close it, it disposes of everything. It's a bit slow (about 2 seconds 'till the window starts), but it's not that bad for an editor.

Still, I'd like to know how to separate that code from the form_load. I think the problem lies in the fact that everything gets destroyed when you close the form.

Awaiting suggestions :)


I want to achieve the same thing. Are you successfull? Me I manage to show OgreRender in Picturebox; but, by now, I can't attach MOIS event to move my cam, I get an error on creating keyboard :

myKeyboard = myInputManager.CreateInputObject(MOIS.Type.OISKeyboard, True)

feanor91

26-07-2008 11:27:30

I found why it doesn't work....

See this

http://www.ogre3d.org/phpBB2addons/viewtopic.php?t=7697