Problem: Null Rev - 1st Proj. with MogreNewt VB --SOLVED--


14-05-2009 20:57:13

Hey Guys,

this is my first try on Mogre n Newton,
Engine is running no problem, Newtons running... (hit F3)

I'am writing on simple basic prog like tutorila + sample1 mogrenewt in vb...
to get in touch with the engine...
BUT i'm getting an strange System.NullReferenceException
when I hit the space -> shoot a cube...
with the line
box1 = mySceneManager.CreateEntity("Entity" & (System.Math.Max(System.Threading.Interlocked.Increment(mEntityCount), mEntityCount - 1)), "box.mesh")

Don't know whats missing ...

Would be great, someone knows...
Heres the code:

Imports Mogre
Imports MogreNewt

Module Module1

Dim mySceneManager As SceneManager

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

Const NEWTON_FRAMERATE As Integer = 60
Private m_update As Single = 1.0F / CSng(NEWTON_FRAMERATE)
Private m_elapsed As Single = 0
Private mEntityCount As Integer = 0
Private m_World As World
Private timer As Single = 0

Sub Main()

'Initialization of Ogre Root and RenderWindow
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, "OGRE Render Window")

'Create Ogre SceneManager
Dim mySceneManager As SceneManager = myRoot.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE)

'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(500, 100, 500)
myCamera.SetPosition(50.0F, 20.0F, 20.0F)
myCamera.LookAt(New Vector3(-100, -50, -100))
myCamera.NearClipDistance = 5

' Newton initialization
m_World = New World()

Dim myViewport As Viewport = MyWindow.AddViewport(myCamera)
myViewport.BackgroundColour = ColourValue.Black
myCamera.AspectRatio = myViewport.ActualWidth / myViewport.ActualHeight

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

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

''Load Terrain

' floor object
Dim floor As Entity
Dim floornode As SceneNode
floor = mySceneManager.CreateEntity("Floor", "simple_terrain.mesh")
floornode = mySceneManager.RootSceneNode.CreateChildSceneNode("FloorNode")

floor.CastShadows = False

mySceneManager.SetSkyDome(True, "Examples/CloudySky", 5, 8)

'Shadows on
mySceneManager.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE

'make a light
Dim mylight As Light
mylight = mySceneManager.CreateLight("Light1")
mylight.Type = Light.LightTypes.LT_POINT
mylight.SetPosition(0.0F, 100.0F, 100.0F)

'Dim MyFadeColor As ColourValue = New ColourValue(0.9, 0.9, 0.9)
'myViewport.BackgroundColour = MyFadeColor
'mySceneManager.SetFog(FogMode.FOG_EXP, MyFadeColor, 0.005)

' using the new "SceneParser" TreeCollision primitive. this will automatically parse an entire tree of
' SceneNodes (parsing all children), and add collision for all meshes in the tree.
Dim stat_col As New MogreNewt.CollisionPrimitives.TreeCollisionSceneParser(m_World)
stat_col.ParseScene(floornode, True)
Dim bod As New MogreNewt.Body(m_World, stat_col)

Dim myPanelOverlay As Overlay = OverlayManager.Singleton.GetByName("Core/DebugOverlay")

'Create the Frame Liseners
AddHandler myRoot.FrameStarted, AddressOf FrameStarted
AddHandler myRoot.FrameStarted, AddressOf Newton_FrameStarted
AddHandler myRoot.FrameStarted, AddressOf Shoot_FrameStarted

'Start rendering

Catch ex As System.Runtime.InteropServices.SEHException
If OgreException.IsThrown Then
MsgBox(OgreException.LastException.FullDescription, MsgBoxStyle.Critical, _
"An Ogre exception has occured!")
MsgBox(ex.ToString, "An error has occured")
End If
End Try

End Sub

Public Function FrameStarted(ByVal e As FrameEvent) As Boolean

'Capture buffered input

'Handle player/camera movement

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

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")

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)

Return Not Quitting
End Function

Private Function MakeSimpleBox(ByVal size As Vector3, ByVal pos As Vector3, ByVal orient As Quaternion) As Body
' base mass on the size of the object.
Dim mass As Single = size.x * size.y * size.z * 2.5F

' calculate the inertia based on box formula and mass
Dim inertia As Vector3 = MogreNewt.MomentOfInertia.CalcBoxSolid(mass, size)

Dim box1 As Entity
Dim box1node As SceneNode

box1 = mySceneManager.CreateEntity("Entity" & (System.Math.Max(System.Threading.Interlocked.Increment(mEntityCount), mEntityCount - 1)), "box.mesh")
box1node = mySceneManager.RootSceneNode.CreateChildSceneNode()

box1.NormaliseNormals = True

Dim col As MogreNewt.Collision = New MogreNewt.CollisionPrimitives.Box(m_World, size)
Dim bod As New MogreNewt.Body(m_World, col)

bod.SetMassMatrix(mass, inertia)
bod.IsGravityEnabled = True


bod.SetPositionOrientation(pos, orient)

Return bod
End Function

Private Function Shoot_FrameStarted(ByVal evt As FrameEvent) As Boolean
' in this frame listener we allow the user to "shoot" boxes
' by pressing the space bar.

If myKeyboard.IsKeyDown(MOIS.KeyCode.KC_SPACE) Then
If timer <= 0.0F Then
' now "shoot" an object!

' we get the position and direction from the camera...
Dim dir As Vector3, vec As Vector3
Dim camorient As Quaternion = myCamera.Orientation
vec = New Vector3(0, 0, -1)

dir = camorient * vec

' then make the visual object (again a cylinder)
Dim pos As Vector3 = myCamera.Position

Dim body As Body = MakeSimpleBox(New Vector3(2, 2, 2), pos, camorient)

' set the initial orientation and velocity!
body.Velocity = dir * 30.0F

timer = 0.2F
End If
End If

timer -= evt.timeSinceLastFrame

Return True
End Function

' The basic OgreNewt framelistener with time slicing
Private Function Newton_FrameStarted(ByVal evt As FrameEvent) As Boolean
m_elapsed += evt.timeSinceLastFrame

If (m_elapsed > m_update) AndAlso (m_elapsed < (1.0F)) Then
While m_elapsed > m_update
m_elapsed -= m_update
End While
If m_elapsed < (m_update) Then
' not enough time has passed this loop, so ignore for now.
' reset the elapsed time so we don't become "eternally behind".
m_elapsed = 0.0F
End If
End If

' For the debug lines
If myKeyboard.IsKeyDown(MOIS.KeyCode.KC_F3) Then
End If

Return True
End Function

Public Class InputClass

Const TRANSLATE As Single = 200
Const ROTATE As Single = 0.003

Shared Function KeyPressed(ByVal e As MOIS.KeyEvent) As Boolean
Select Case e.key

Quitting = True
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
'Tyipcally you either process unbuffered keyboard input (as in ProcessKeyboard)
'or you process buffered Keypress

Return Nothing
End Function

Shared Sub ProcessKeyboard()

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

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

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

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

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

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

If myKeyboard.IsKeyDown(MOIS.KeyCode.KC_E) Or _
myKeyboard.IsKeyDown(MOIS.KeyCode.KC_PGDOWN) Then
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 myRotating Then
myCamera.Yaw(e.state.X.rel * -ROTATE)
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_Right) Then
myRotating = True
End If
End Function

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
myRotating = False
End If
End Function
End Class

End Module



14-05-2009 22:32:31

Dim mySceneManager As SceneManager = myRoot.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE)
should be:
mySceneManager = myRoot.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE)