changing, removing textures and meshes at runtime

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
JoeriDC
Posts: 18
Joined: Mon May 22, 2006 5:22 pm

changing, removing textures and meshes at runtime

Post by JoeriDC »

Hi.

The title says it al. is it possible to change a texture at runtime and release the old one?
and is it also possible to remove a mesh that isn't any longer required at runtime.

All this has to be done without removing other textures/ objects

Thanx in advance
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Oh, please first have a search at the docu and/or the forums for any questions !!! :roll:

Who would ever thought of it that you need two functions:
removeTexture(...) and removeMesh(...) !?!?! :wink:
Well, it's really dificult to guess this...

Perhaps you'll be able to find the function to change a texture on your own !?!?!?!
Little hint: Tutorial#1
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
JoeriDC
Posts: 18
Joined: Mon May 22, 2006 5:22 pm

Post by JoeriDC »

I tried this but they weren't removed. The only thing i got then wheni close my form is an AccesViolationException

Maybe i don't add them correctly or whatever. Those objects are made in my CObject, CSky and CTexture.

I save the only the node, pos etc in it. by removing i give as param that node.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Hmmm...
Well, I think the mesh or texture was removed, but at the end something wants to remove them again or use them, and because they are already removed it gives an AccessViolationError...

You have to be really carefull if you remove something from the engine...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
JoeriDC
Posts: 18
Joined: Mon May 22, 2006 5:22 pm

Post by JoeriDC »

Those nodes are declared as private class attributes.

- within my constructor, I assign them a value like it is done in the tutorials.

- The only method that can remove is my public sub destroy()

( tested this function when i press the x key to remove, other events works, so that isn't the problem :) )

- this is done with the functions mentioned by you.

they stay when that is called and as said, that exception occurs when i close my form.

I need those functions working for unloading old parts of for example: a dungeon and after that loading new parts, so the memory usage will not raise to high.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Well, just guessing what you coded is something hard... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
stodge
Posts: 216
Joined: Fri Dec 05, 2003 5:57 pm

Post by stodge »

Post the line of code that causes the access violation.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

A texture or mesh should only be remove()'d when the only valid pointer to it is held by the video driver base class's (CNullDriver's) texture cache or the scene manager's mesh cache (IMeshCache). Like almost everything else in Irrlicht, meshes and textures inherit from IUnknown, giving them reference counting through grab() and drop(). Newly created objects have a reference count of 1, which increases as new objects take a pointer and call grab() and decrease when they release the pointer with drop(). when the reference count reaches 0, the destructor is called. When you call a get*() function, the burden of destruction is passed to some internal manager, while when you call a create*() function or use 'new' to create a class, you hold the 'last' pointer and are responsible for dropping it.

from what I can see, mesh loaders and materials don't grab textures, so you're going to have a hard time knowing which ones you can safely remove. and it wouldn't help anyway because there's no access to IUnknown's reference count.

So, I guess the easy hacky way would be to empty your scene manager(s) and GUI, flush the SceneManager's mesh cache, then flush all textures from the driver and load everything back up again as needed.
The hard and nicer way would be to add a getRefCount function to IUnknown, make sure every class in the engine that holds a texture pointer uses grab and drop properly (~70 files plus possible class changes :x), and add functions to the texture and mesh caches to release everything with a reference count of 1. Then it would be as simple as calling the clean-up functions periodically to claw back unreferenced memory.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply