Page 1 of 1

How to pre-load textures? [solved]

Posted: Sun May 21, 2006 6:06 pm
by Oogst
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!

Posted: Sun May 21, 2006 9:37 pm
by sinbad
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).

Posted: Mon May 22, 2006 12:12 pm
by Oogst
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();
		}
	}

Posted: Fri Jul 28, 2006 9:41 am
by vilgeits
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

Posted: Fri Jul 28, 2006 10:27 am
by Oogst
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.