Changing textures on materials

Problems building or running the engine, queries about how to use features etc.
User avatar
eventhorizon
Kobold
Posts: 38
Joined: Wed Oct 20, 2010 4:56 am
x 12

Changing textures on materials

Post by eventhorizon »

For my simulator project, I currently have code in place that changes textures of objects on a per-polygon basis, which works great (it's using setMaterialName() on Ogre::SubMesh). I wrote a custom animation controller that is similar to Ogre's texture animation system, but that it defines a duration for each image instead of having a single duration for the whole animation like Ogre has, and then switches between the images, using simulator timers. Currently I don't seem to be able to get Ogre to change the texture on the related material, I've tried a number of different things, including setting the dirty state on the material. I was going through a lot of the Ogre code to find out how the animation system sets the textures, but haven't found anything yet.

I'm using Ogre 14.3.4.

One of the things I tried was to remove the texture unit state and recreate it, this is probably doing it wrong though:

Code: Select all

bool TextureManager::SetTexture(const std::string &name, const std::string &texture)
{
	//get texture unit state
	Ogre::MaterialPtr mMat = GetMaterialByName(name);
	mMat->getTechnique(0)->getPass(0)->removeTextureUnitState(0);

try
{
	//set texture unit's texture image
	Ogre::TextureUnitState *state = mMat->getTechnique(0)->getPass(0)->createTextureUnitState(texture);
	mMat->_dirtyState();
}
catch (Ogre::Exception &e)
{
	return ReportError("Error setting texture " + name + " to filename " + texture + "\n" + e.getDescription());
}
return true;
}

I've also tried the SetTexture() and SetTextureName() functions on the unit state. The problem might be something simple that I'm missing.

paroj
OGRE Team Member
OGRE Team Member
Posts: 2238
Joined: Sun Mar 30, 2014 2:51 pm
x 1217

Re: Changing textures on materials

Post by paroj »

are you using the RTSS? If so, there is a difference between Technique 0 and bestTechnique. See https://ogrecave.github.io/ogre/api/lat ... -Structure

User avatar
eventhorizon
Kobold
Posts: 38
Joined: Wed Oct 20, 2010 4:56 am
x 12

Re: Changing textures on materials

Post by eventhorizon »

paroj wrote: Mon Jul 28, 2025 11:02 am

are you using the RTSS? If so, there is a difference between Technique 0 and bestTechnique. See https://ogrecave.github.io/ogre/api/lat ... -Structure

I'll check that, I'm using RTSS since the geometry is mostly procedural.

User avatar
eventhorizon
Kobold
Posts: 38
Joined: Wed Oct 20, 2010 4:56 am
x 12

Re: Changing textures on materials

Post by eventhorizon »

I tried using the getBestTechnique() function, and here's the code when using setTextureName(), this isn't working:

Code: Select all

bool TextureManager::SetTexture(const std::string &name, const std::string &texture)
{
	//get texture unit state
	Ogre::MaterialPtr mMat = GetMaterialByName(name);
	Ogre::TextureUnitState *state = mMat->getBestTechnique()->getPass(0)->getTextureUnitState(0);
	try
	{
		//set texture unit's texture image
		state->setTextureName(texture);
	}
	catch (Ogre::Exception &e)
	{
		return ReportError("Error setting texture " + name + " to filename " + texture + "\n" + e.getDescription());
	}
	return true;
}
rpgplayerrobin
Orc Shaman
Posts: 788
Joined: Wed Mar 18, 2009 3:03 am
x 447

Re: Changing textures on materials

Post by rpgplayerrobin »

What if you loop through all techniques and set it for all of them instead?

Also:

  • Have you controlled that it is the correct material that you are setting it to?
  • Have you controlled that you only have 1 pass per technique?
  • Have you controlled that it is the correct texture unit that you are trying to set the new texture to?
User avatar
eventhorizon
Kobold
Posts: 38
Joined: Wed Oct 20, 2010 4:56 am
x 12

Re: Changing textures on materials

Post by eventhorizon »

rpgplayerrobin wrote: Tue Jul 29, 2025 10:53 am

What if you loop through all techniques and set it for all of them instead?

Also:

  • Have you controlled that it is the correct material that you are setting it to?
  • Have you controlled that you only have 1 pass per technique?
  • Have you controlled that it is the correct texture unit that you are trying to set the new texture to?

I might've found the problem, so far I've found that there is only one technique when initially setting the texture on startup, but after startup (during the texture change) there are 2 or more techniques. I'm assuming that RTSS is doing something.

This is the code for creating the material and setting the initial texture:

Code: Select all

mMat = Ogre::MaterialManager::getSingleton().create(ToString(sbs->InstanceNumber) + ":" + name, path);
Ogre::TextureUnitState *state = mMat->getTechnique(0)->getPass(0)->createTextureUnitState(texture_name);

When testing it, after loading, the material had 2 techniques, and the code assumes the first only. I could try setting them for all, and see what happens.

User avatar
eventhorizon
Kobold
Posts: 38
Joined: Wed Oct 20, 2010 4:56 am
x 12

Re: Changing textures on materials

Post by eventhorizon »

I changed the code so that it now sets for all techniques, and appears to be working now.