Problem to create reflections more than one time

feanor91

29-08-2008 08:20:03

Hello

It's the stupid man another time....

This time I want to know how to cleanup all that mess when quiting...I explain

My soft is not all in 3D. I call my 3D window to show something, then closing it, then reopen it and so on. All work fine, exept if I put reflections on. The first time I open it, that's OK, but when I close and reopen it, the sub reflection crash on call to createplane. here's my code :


Public Sub ReflectionOn()

'Permet d'initialiser les reflections ou de les réactiver si elles avaient été suspendue
'Allow to initialise reflection or reactivat it if the have been suspended

If ReflectInit Then 'si elle ont été suspendus, alors, on les réactive / if suspended, reactivate it
mPlaneEnt.Visible = True 'on montre le plan de réflection / showing reflection plane
_mReflectSceneNode.GetAttachedObject(CType(0, UShort)).Visible = False 'on cache l'objet de base / hiding base object
ReflectOn = True 'on réfléchit / recflection on
Exit Sub
End If

ReflectionPlane.normal = _ReflectPlaneNormal 'par défaut un plan horizontal / horizontal plane
ReflectionPlane.d = 0 '?
'Creation du plan / plane creation
Mogre.MeshManager.Singleton.CreatePlane("ReflectPlane", Mogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, ReflectionPlane, 1024, 1024, 16, 16, True, 1, 1, 1, Mogre.Vector3.UNIT_Z)

'Tou ce qui suit est pour ajouter le plan à la scene / all that follow is to add plane to scene
mPlaneEnt = _OgreSceneManager.CreateEntity("ReflectPlaneEnt", "ReflectPlane")
mPlaneNode = _OgreSceneManager.RootSceneNode.CreateChildSceneNode
mPlaneNode.AttachObject(mPlaneEnt)
'Le plan pend les même taile et position que l'objet de base / plane take same size and position than base object
mPlaneNode.Position = _mReflectSceneNode.Position
mPlaneNode.Orientation = _mReflectSceneNode.Orientation
mPlaneNode.Scale(_mReflectSceneNode.GetScale)

'Creation de la texture pour le calcul des réflections / texture creation for reflection

Dim texture As Mogre.TexturePtr = Mogre.TextureManager.Singleton.CreateManual("RttTex", Mogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, Mogre.TextureType.TEX_TYPE_2D, CType(512, UInteger), CType(512, UInteger), 0, Mogre.PixelFormat.PF_R8G8B8, Mogre.TextureUsage.TU_RENDERTARGET)
rttTex = texture.GetBuffer.GetRenderTarget()

'Creation du viewport pour calculer les réflections / creation of reflection viewport
Dim v As Mogre.Viewport = rttTex.AddViewport(_OgreCamera)
v.SetClearEveryFrame(True)
v.BackgroundColour = Mogre.ColourValue.Black

'Récupération de la texture de l'objet de base / getting texture from base object
Dim OrigObjet As Mogre.Entity = _mReflectSceneNode.GetAttachedObject(CType(0, UShort))
Dim MatToBlend As Mogre.MaterialPtr = OrigObjet.GetSubEntity(CType(0, UShort)).GetMaterial().Clone("ReflectMat") 'on la clone pour ne pas alterer celle de l'objet / cloning it to not alter base object texture

'on crè une nouvelle technique dans la texture / creation of a new ehcnique in texture
Dim t As Mogre.TextureUnitState = MatToBlend.GetTechnique(CType(0, UShort)).GetPass(CType(0, UShort)).CreateTextureUnitState("RttTex")
'on récupère la première technique de la texture pour permettre sa mise à l'échelle / getting first technique to scaling texture
Dim t1 As Mogre.TextureUnitState = MatToBlend.GetTechnique(CType(0, UShort)).GetPass(CType(0, UShort)).GetTextureUnitState(CType(0, UShort))

'le paramètrage pour le fondu des deux texture / applying param to blend textures
t.SetColourOperationEx(LayerBlendOperationEx.LBX_BLEND_MANUAL, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_CURRENT, Mogre.ColourValue.White, Mogre.ColourValue.White, _ReflecionIntensity)
t.SetTextureAddressingMode(TextureUnitState.TextureAddressingMode.TAM_CLAMP)
t.SetProjectiveTexturing(True, _OgreCamera)
MatToBlend.SetSceneBlending(_SceneBlendingType)

'mise à l'échelle de la première texture / scaling first texture
t1.TextureUScale = _UScaleReflect
t1.TextureVScale = _vScaleReflect

'on affecte les évènement à faire avant et après rendu du viewport / affecting event in pre and post viewport render
AddHandler rttTex.PostRenderTargetUpdate, AddressOf postRenderTargetUpdate
AddHandler rttTex.PreRenderTargetUpdate, AddressOf preRenderTargetUpdate

'on affecte le materiel au plan / adding material to plan
mPlaneEnt.SetMaterialName(MatToBlend.Name)

OrigObjet.Visible = False 'on cache l'objet de base / hiding base object
ReflectOn = True 'on active les réflections / activate reflection
ReflectInit = True 'les reflection on été initialisées / reflection are initialised
End Sub


My dispose sub :


Public Sub DisposeOgre()
'Pemet de nettoyer les instances OGRES en cas de rechargement de la scène ou de l'ouverture d'une nouvelle
'Allow to destroy all ogre instances

Mogre.MeshManager.Singleton.Remove("ReflectPlane")
Mogre.MeshManager.Singleton.Dispose()

ResourceGroupManager.Singleton.DestroyResourceGroup("General")
ResourceGroupManager.Singleton.Dispose()

_RenderWindow.RemoveAllViewports()
_OgreRacine.DetachRenderTarget(_RenderWindow)
_RenderWindow.Destroy()

_OgreRacine.DestroySceneManager(_OgreSceneManager)
_OgreRacine.DestroyAllRenderQueueInvocationSequences()
_OgreRacine.Shutdown()
_OgreRacine.Dispose()


End Sub


Reflection sub crash here :

Mogre.MeshManager.Singleton.CreatePlane("ReflectPlane", Mogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, ReflectionPlane, 1024, 1024, 16, 16, True, 1, 1, 1, Mogre.Vector3.UNIT_Z)


or here :

Dim texture As Mogre.TexturePtr = Mogre.TextureManager.Singleton.CreateManual("RttTex", Mogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, Mogre.TextureType.TEX_TYPE_2D, CType(512, UInteger), CType(512, UInteger), 0, Mogre.PixelFormat.PF_R8G8B8, Mogre.TextureUsage.TU_RENDERTARGET)

It says that It can write on protected memory. If think that is because some pointer are not free, but, ogre shutdown properly when I close window. here the end of my ogre.log :

09:17:10: ********************************
09:17:10: ** oSceneLoader: Scene loaded **
09:17:10: ********************************
09:17:10: Creating viewport on target 'rtt/61073984', rendering from camera 'Camera01', relative dimensions L: 0.00 T: 0.00 W: 1.00 H: 1.00 ZOrder: 0
09:17:10: WARNING: Texture instance 'RttTex' was defined as manually loaded, but no manual loader was provided. This Resource will be lost if it has to be reloaded.
09:17:11: Texture: spot_shadow_fade.png: Loading 1 faces(PF_R8G8B8,128x128x1) with 7 generated mipmaps from Image. Internal format is PF_X8R8G8B8,128x128x1.
09:17:15: Destroying resource group General
09:17:15: Unloading resource group General
09:17:15: Finished unloading resource group General
09:17:15: Render Target 'rtt/61073984' Average FPS: 81.1977 Best FPS: 114.77 Worst FPS: 71.8504
09:17:15: *-*-* OGRE Shutdown
09:17:15: *-*-* OGRE Shutdown
09:17:15: Unregistering ResourceManager for type Compositor
09:17:15: Unregistering ResourceManager for type Font
09:17:15: Unregistering ResourceManager for type Skeleton
09:17:15: Unregistering ResourceManager for type Mesh
09:17:15: Unregistering ResourceManager for type HighLevelGpuProgram
09:17:15: Uninstalling plugin: GL RenderSystem
09:17:15: *** Stopping Win32GL Subsystem ***
09:17:15: Plugin successfully uninstalled
09:17:15: Unloading library .\Plugins\RenderSystem_GL
09:17:15: Uninstalling plugin: D3D9 RenderSystem
09:17:15: D3D9 : Shutting down cleanly.
09:17:15: Unregistering ResourceManager for type Texture
09:17:15: Unregistering ResourceManager for type GpuProgram
09:17:15: D3D9 : Direct3D9 Rendering Subsystem destroyed.
09:17:15: Plugin successfully uninstalled
09:17:15: Unloading library .\Plugins\RenderSystem_Direct3D9
09:17:15: Unregistering ResourceManager for type Material


So what?

feanor91

05-09-2008 11:23:34

Youhou....Nobody? I didn't find a way to solve my problem yet, and it is very disturbing.

feanor91

05-09-2008 12:12:49

So to help someone to answer, I dig into the deep core of mogre. I 've donwloaded source files to help me to debug. And on the first reopening of window, Mogre code crash here :

return static_cast<Ogre::TextureManager*>(_native)->createManual( o_name, o_group, (Ogre::TextureType)texType, width, height, num_mips, (Ogre::PixelFormat)format, usage );


But by now, I'm stuck on this line, because, apparently, there is a call in ogremain.dll, but I don't now how to handle this....This is far far away from my level of knowledge.

So to try to well explain. On first call, it works, my reflection, texture is well created. Second call it crash, third it works, fourth it crash, and so on, 1 time on 2, it crash (or it works, depending.... :D )

feanor91

05-09-2008 20:45:56

I continue to dig. I think my problem is linked to liberation of ressources, so I debug in this way. Here's mu dispose function :

Public Sub DisposeOgre()
'Pemet de nettoyer les instances OGRES en cas de rechargement de la scène ou de l'ouverture d'une nouvelle
'Allow to destroy all ogre instances

Mogre.MeshManager.Singleton.Dispose()
Mogre.TextureManager.Singleton.Dispose()

ResourceGroupManager.Singleton.DestroyResourceGroup("General")
ResourceGroupManager.Singleton.Dispose()

_RenderWindow.RemoveAllViewports()
_OgreRacine.DetachRenderTarget(_RenderWindow)
_RenderWindow.Destroy()

_OgreRacine.DestroySceneManager(_OgreSceneManager)
_OgreRacine.DestroyAllRenderQueueInvocationSequences()
_OgreRacine.Shutdown()
_OgreRacine.Dispose()


End Sub


The dispose for meshmanager and texturemanager :

!ResourceManager()
{
if (_createdByCLR &&_native)
{
delete _native;
_native = 0;
}
}


_createdByCLR is set to false so delete _native is nether fired and my ressource is not free. And more, when I close my application this is fired :

!TexturePtr()
{
if (_sharedPtr != 0)
{
delete _sharedPtr;
_sharedPtr = 0;
}
}


And I get an error. This is at the very end of my application and Ogre windows is closed for a long time ago, so it seems that ogre was not realy shutdown even if my ogre.log says that it is. And this things arrives only if I use reflection. So it is linked to the reflection texture that I create.

Bekas

05-09-2008 21:27:44

Good job digging into this!

I think it's related to this: http://www.ogre3d.org/wiki/index.php/Mogre_FAQ#Why_do_I_get_a_crash_when_I_dispose_Root_.3F
All SharedPtr objects (MaterialPtr, TexturePtr, GpuProgramPtr etc.) must be out of scope before disposing Root, so make sure that you clear all references of SharedPtr objects before the call (i.e. set your SharedPtr variables to null)

feanor91

06-09-2008 00:49:01

OK, fine, but how can achive this in VB how can I access sharedPtr, and whis is that ptr?

Edit:
Forget what I say, it was so stupid....I get the textureptr (sharedptr) from the createmanual function and in the dispose function, I dispose it....Sometimes I'm a noobs...But well It's usefull to debug with Mogre code underlying, I understand more things (even if I'm not a C++ god :D )

So the only thing to correct is the problem that texure reflection is not created on second opening of the window. I will manage to dig on that now.

feanor91

08-09-2008 12:47:11

Hello

So I dig a little more on that.

Here http://www.cramayailes-indoor.org/Documents/DebugReflection.xls,
You can find a list of what is executed during debug.

And, little precision, I work on 2 different computers. On one, the second opening crash is on createplane and createtexture (windows vista with Nvidia 76000 CG) on the other, plane is created but texture no (windows xp with onboard intel CG)...And more, on second computer under openGL, it works.

I will test on openGL on second computer when at home.

So, by now, I can say that behavior is different under 2 computers because system or video card (or both) and on one, it works under openGl but not under DirectX. I'm about to think, that I will not resolve my issue by something I will to code in VB...The truth seems to be elsewhere...

Edit :

So at home, it didn't work with opengl, smptom is the same than directX

feanor91

11-09-2008 07:18:03

Hi

Everybody's dead?