How to pre-load textures? [solved]

Problems building or running the engine, queries about how to use features etc.
Post Reply
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1034
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
Contact:

How to pre-load textures? [solved]

Post by Oogst » Sun May 21, 2006 6:06 pm

For The Blob I have a lot of textures that are initially not in the game and are only used if the player paints a building in a new colour, which uses a different texture. Each time a texture is used for the very first time, the game freezes for a moment to load the texture and then continues. I know exactly which textures will be used, so how can I force the loading of these textures on start-up?

I am currently using something like this to load my resources on start-up:

Code: Select all

std::string groupName = "levelAssets";
Ogre::ResourceGroupManager& rGM = Ogre::ResourceGroupManager::getSingleton();
rGM.createResourceGroup(groupName);
rGM.addResourceLocation("Textures", "FileSystem", groupName);

//meshes are not automatically loaded by Ogre, so do this by hand
Ogre::StringVectorPtr meshes = rGM.findResourceNames(groupName, "*.mesh");
for (unsigned int i = 0; i < meshes->size(); i++)
{
	rGM.declareResource((*meshes)[i], "Mesh", groupName);
}
	
rGM.initialiseResourceGroup(groupName);
rGM.loadResourceGroup(groupName);
I figured a very dirty solution for this would be to render one screen in the background at start-up that uses all these textures, but I am hoping a better solution exists.

Thanks in advance!
Last edited by Oogst on Sat May 27, 2006 4:39 pm, edited 2 times in total.
0 x
blog.oogst3d.net: my dev blog and portfolio
Ronimo Games: my game dev company
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/PS3/PS4)
Swords & Soldiers: side-scrolling RTS (PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance

User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19261
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
Contact:

Post by sinbad » Sun May 21, 2006 9:37 pm

You also need to include the textures you want to pre-load in your calls to declareResource, or after loading each mesh you need to call load() on the dependent materials.

declareResource is not recursive - what happens here is that your meshes will get preloaded and the materials defined, but until you actually use a material it won't get loaded (because you may not use the default material for example).
0 x

Oogst
OGRE Expert User
OGRE Expert User
Posts: 1034
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
Contact:

Post by Oogst » Mon May 22, 2006 12:12 pm

Thanks for the help!

I tried declaring *.tga and *.png, but that gave me the error that a resource already exists. After that I tried loading all textures in the texture-manager, and that did work. So I used this code to get it to work:

Code: Select all

	//Pre-load all textures
	Ogre::ResourceManager::ResourceMapIterator iter = Ogre::TextureManager::getSingleton().getResourceIterator();
	while (iter.hasMoreElements())
	{
		Ogre::ResourceManager::ResourceHandleMap::mapped_type resource = iter.getNext();
		Logger::log(resource->getName());
		if (Ogre::ResourceGroupManager::getSingleton().resourceExists("Assets", resource->getName()))
		{
			resource->load();
		}
	}
0 x

User avatar
vilgeits
Goblin
Posts: 298
Joined: Tue Aug 02, 2005 10:41 pm

Post by vilgeits » Fri Jul 28, 2006 9:41 am

This is not working for me :( it compiles it links but still i have some lack when i look at some meshes that i have not look before because the texture loading time :S
0 x

Oogst
OGRE Expert User
OGRE Expert User
Posts: 1034
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
Contact:

Post by Oogst » Fri Jul 28, 2006 10:27 am

I kept having that as well. I think it is because the driver decides when something is put in video memory and this is only done when it is needed. The solution I wanted to make, but did not because of a lack of time, was to render an object with all important textures at start-up (without showing the object) to get as much as possible into video memory beforehand. I do not know whether that is the right thing to do, though.
0 x
blog.oogst3d.net: my dev blog and portfolio
Ronimo Games: my game dev company
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/PS3/PS4)
Swords & Soldiers: side-scrolling RTS (PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance

Post Reply