Using Mogre v1.6.5 on a Winform ?!? [SOLVED]


01-04-2010 11:41:02

i'm back :roll: .

I'm using:
- VB.NET 2008 Express
- Mogre v1.6.5
- MyGUI v3.0.1 (i think so :? )

Now i'm trying to create a windows forms application with a Mogre render view inside of it.
But all i can see is an empty winform. I also tried the c# tutorial and convert it to vb with the same result :cry: .
Can some one help me please ?

Here's my code:

Imports Mogre
Imports MyGUI.Managed

Public Class Main

Public MyWindow As RenderWindow
Public myCamera As Camera
Public myViewport As Viewport = Nothing

Public myKeyboard As MOIS.Keyboard
Public myMouse As MOIS.Mouse
Public myMouseState As New MOIS.MouseState_NativePtr
Public myLeftMouseDown As Boolean = False
Public myRightMouseDown As Boolean = False

Public myFrameEvent As FrameEvent

Public Quitting As Boolean = False

Public myResManager As ResourceManager
Public myScene As SceneManager
Public myRoot As Root

Public myTranslation As Vector3 = Vector3.ZERO
Public myRotating As Boolean = False
Public selObject As SceneNode

Private Sub Main_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
'myRoot.Initialise(True, "NONE")
Dim misc As New NameValuePairList
misc("externalWindowHandle") = Me.Handle.ToString() 'Me.Handle
Dim const_list As Const_NameValuePairList = misc.ReadOnlyInstance
MyWindow = myRoot.CreateRenderWindow("Simple Mogre Form Window", 800, 600, 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)
End While

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

Dim myViewport As Viewport = MyWindow.AddViewport(myCamera)

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

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

'Start rendering
End Sub

Public Function FrameStarted(ByVal e As FrameEvent) As Boolean

'Capture buffered input

'Mauszeiger nur bewegen, wenn nicht die rechte Maustaste gedrückt ist (myRotating = False)
If myRotating = False Then
myMouseState = myMouse.MouseState
myMouseState.width = CInt(MyWindow.Width)
myMouseState.height = CInt(MyWindow.Height)

End If

'Handle player/camera movement

'Kamera bewegen und Objekterkennung
If myTranslation <> Vector3.ZERO Then

myCamera.Position += myCamera.Orientation * myTranslation * e.timeSinceLastFrame

'Setup the scene query
Dim camPos As Vector3 = myCamera.Position
Dim cameraRay As Ray = New Ray(New Vector3(camPos.x, 0, camPos.z), Vector3.NEGATIVE_UNIT_Y)
Dim myRaySceneQuery As RaySceneQuery = myScene.CreateRayQuery(cameraRay)
Dim results As RaySceneQueryResult = myRaySceneQuery.Execute

If results.Count > 2 Then
Dim result As RaySceneQueryResultEntry = results.Item(0)
Dim terrainHeight As Single = result.worldFragment.singleIntersection.y
If terrainHeight + 10 > camPos.y Then
myCamera.SetPosition(camPos.x, terrainHeight + 10, camPos.z)
End If

End If
End If

'Debug Overlay
Dim myAvg As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/AverageFps")
Dim myCurr As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/CurrFps")
Dim myBest As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/BestFps")
Dim myWorst As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/WorstFps")
Dim myNumTris As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/NumTris")
Dim myNumBatches As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/NumBatches")
Dim myDebug As OverlayElement = OverlayManager.Singleton.GetOverlayElement("Core/DebugText")
'Dim selObj As OverlayElement = OverlayManager.Singleton.GetOverlayElement("")

myAvg.Caption = "Average FPS: " & Mogre.StringConverter.ToString(MyWindow.AverageFPS)
myCurr.Caption = "Current FPS: " & Mogre.StringConverter.ToString(MyWindow.LastFPS)
myBest.Caption = "Best FPS: " & Mogre.StringConverter.ToString(MyWindow.BestFPS)
myWorst.Caption = "Worst FPS: " & Mogre.StringConverter.ToString(MyWindow.WorstFPS)
myNumTris.Caption = "Triangle Count: " & Mogre.StringConverter.ToString(MyWindow.TriangleCount)
myNumBatches.Caption = "Batch Count: " & Mogre.StringConverter.ToString(MyWindow.BatchCount)
'If selObject.Name.Length > 0 Then selObj.Caption = "curObj: " & selObj.Name

Return Not Quitting
End Function

End Class

Public Class InputClass

Const TRANSLATE As Single = 150 'Geschwindigkeit mit der sich die Kamera bewegt, wenn man eine Taste drückt (WASD)
Const ROTATE As Single = 0.003 'Drehgeschwindigkeit der Kamera, wenn man sich mittels rechter Maustaste umschaut

Shared Sub Init()
Dim windowHnd As Integer
Main.MyWindow.GetCustomAttribute("WINDOW", windowHnd)
Dim myInputManager As MOIS.InputManager = MOIS.InputManager.CreateInputSystem(CUInt(windowHnd))
Main.myKeyboard = myInputManager.CreateInputObject(MOIS.Type.OISKeyboard, True)
AddHandler Main.myKeyboard.KeyPressed, AddressOf InputClass.KeyPressed
AddHandler Main.myKeyboard.KeyReleased, AddressOf InputClass.KeyReleased

Main.myMouse = myInputManager.CreateInputObject(MOIS.Type.OISMouse, True)
AddHandler Main.myMouse.MouseMoved, AddressOf InputClass.MouseMovedListener
AddHandler Main.myMouse.MousePressed, AddressOf InputClass.MousePressedListener
AddHandler Main.myMouse.MouseReleased, AddressOf InputClass.MouseReleasedListener

SetMouseLocation(New Vector2(Main.MyWindow.Width / 2, Main.MyWindow.Height / 2))
End Sub

Shared Sub SetMouseLocation(ByVal loc As Vector2)
Dim axis As MOIS.Axis_NativePtr = Main.myMouse.MouseState.X
axis.abs = loc.x
axis = Main.myMouse.MouseState.Y
axis.abs = loc.y
End Sub

Shared Function KeyPressed(ByVal e As MOIS.KeyEvent) As Boolean
Select Case e.key
Main.Quitting = True
Case MOIS.KeyCode.KC_B
If Main.myScene.ShowBoundingBoxes Then
Main.myScene.ShowBoundingBoxes = False
Main.myScene.ShowBoundingBoxes = True
End If
End Select

Return Nothing
End Function

Shared Function KeyReleased(ByVal e As MOIS.KeyEvent) As Boolean

'This function is just a placeholder
'It is unlikely you will ever use this
'Typically you either process unbuffered keyboard input (as in ProcessKeyboard)
'or you process buffered Keypress

Return Nothing
End Function

Shared Sub ProcessKeyboard()

'This Sub is typically called via the FrameStarted event.

'Clear previous translation
Main.myTranslation.z = 0
Main.myTranslation.x = 0
Main.myTranslation.y = 0

If Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_UP) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_W) Then
Main.myTranslation.z += -TRANSLATE
End If

If Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_S) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_DOWN) Then
Main.myTranslation.z += TRANSLATE
End If

If Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_A) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_LEFT) Then
Main.myTranslation.x += -TRANSLATE
End If

If Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_D) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_RIGHT) Then
Main.myTranslation.x += TRANSLATE
End If

If Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_Q) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGUP) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_SPACE) Then
Main.myTranslation.y += TRANSLATE
End If

If Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_Z) Or Main.myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGDOWN) Then
Main.myTranslation.y += -TRANSLATE
End If

End Sub

Shared Function MouseMovedListener(ByVal e As MOIS.MouseEvent) As Boolean
Static myLastX As Integer = e.state.X.abs
Static myLastY As Integer = e.state.Y.abs

If Main.myRotating Then
Main.myCamera.Yaw(e.state.X.rel * -ROTATE)
Main.myCamera.Pitch(e.state.Y.rel * -ROTATE)
End If

End Function

Shared Function MousePressedListener(ByVal e As MOIS.MouseEvent, ByVal id As MOIS.MouseButtonID) As Boolean
If e.state.ButtonDown(MOIS.MouseButtonID.MB_Left) Then
End If

If e.state.ButtonDown(MOIS.MouseButtonID.MB_Right) Then
End If
End Function

Shared Sub onLeftPressed()
Main.myLeftMouseDown = True

'Turn off bounding box
If Not Main.selObject Is Nothing Then
Main.selObject.ShowBoundingBox = False
Main.selObject = Nothing
End If

'Objekt mit dem Mauszeiger auswählen
'Dim mouseRay As Ray = myCamera.GetCameraToViewportRay((myMouseCursor.getPixelPosition.x + 15) / MyWindow.Width, (myMouseCursor.getPixelPosition.y + 15) / MyWindow.Height)
'Dim myRaySceneQuery As RaySceneQuery = myScene.CreateRayQuery(mouseRay)
'myRaySceneQuery.QueryMask = QueryFlags.BUILDING_MASK 'nur Objekte mit QueryFlags.BUILDING_MASK können selektiert werden

'Dim results As RaySceneQueryResult = myRaySceneQuery.Execute

'For Each result As RaySceneQueryResultEntry In results
' 'Movable object?
' If Not result.movable Is Nothing Then
' selObject = result.movable.ParentSceneNode
' 'Turn on bounding box
' If Not selObject Is Nothing Then
' selObject.ShowBoundingBox = True
' Console.WriteLine("Ausgewähltes Objekt: " & selObject.Name)
' Else
' Console.WriteLine("nichts gewählt")
' End If
' Exit For
' End If


End Sub

Shared Sub onRightPressed()
Main.myRightMouseDown = True
Main.myRotating = True
End Sub

Shared Function MouseReleasedListener(ByVal e As MOIS.MouseEvent, ByVal id As MOIS.MouseButtonID) As Boolean
If Not e.state.ButtonDown(MOIS.MouseButtonID.MB_Right) Then
Main.myRotating = False
End If
End Function

End Class

I will upload an example app (VB.NET) later :)