I recently ran into some problems dynamically applying materials to entities and i wrote a relatively straightforward test case. I have a scene with an entity named Prop_Plane, and four buttons wired up to the four methods in the code posted below. I constructed this case because it seemed like I could dynamically apply materials unless they were texture based materials.
If i apply one of the texture based materials before rendering, the texture based code paths work, but I cannot switch to a different texture based material on that entity. The second texture based material renders all black, but if i reapply the original texture, it shows up again.
In my current test case, though, the default material is applied to my entity, and I poke the buttons activating the following four methods. The color based materials get applied appropriately every time, but the texture based ones result in black. Is there something I am missing here?
Code: Select all
-(void)applyRedColor
{
string matName = "redColor";
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingletonPtr()->getByName(matName);
if (material.isNull()) {
material =
Ogre::MaterialManager::getSingletonPtr()->create(matName,
"General",
false);
}
Ogre::Technique *technique = material->getTechnique(0);
Ogre::Pass *pass = technique->getPass(0);
pass->setDiffuse(1,0,0,1);
pass->setLightingEnabled(true);
Ogre::Entity *entity = OgreFramework::getSingletonPtr()->m_pSceneMgr->getEntity(Prop_Plane");
entity->setVisible(true);
Ogre::SubEntity *se = entity->getSubEntity(0);
se->setMaterial(material);
}
-(void)applyGreenColor
{
string matName = "greenColor";
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingletonPtr()->getByName(matName);
if (material.isNull()) {
material =
Ogre::MaterialManager::getSingletonPtr()->create(matName,
"General",
false);
}
Ogre::Technique *technique = material->getTechnique(0);
Ogre::Pass *pass = technique->getPass(0);
pass->setDiffuse(0,1,0,1);
pass->setLightingEnabled(true);
Ogre::Entity *entity = OgreFramework::getSingletonPtr()->m_pSceneMgr->getEntity("Prop_Plane");
entity->setVisible(true);
Ogre::SubEntity *se = entity->getSubEntity(0);
se->setMaterial(material);
}
-(void)applyTexture1
{
string texName = "Texture1.png";
Ogre::MaterialPtr material =
Ogre::MaterialManager::getSingletonPtr()->getByName(texName);
if (material.isNull()) {
material =
Ogre::MaterialManager::getSingletonPtr()->create(texName,
"General",
false);
}
Ogre::Technique *technique = material->getTechnique(0);
Ogre::Pass *pass = technique->getPass(0);
pass->setLightingEnabled(true);
pass->setCullingMode(Ogre::CULL_NONE);
pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
Ogre::TextureUnitState *tus = pass->createTextureUnitState();
tus->setTextureName(texName);
tus->setNumMipmaps(0);
tus->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
Ogre::Entity *entity = OgreFramework::getSingletonPtr()->m_pSceneMgr->getEntity("Prop_Plane");
entity->setVisible(true);
Ogre::SubEntity *se = entity->getSubEntity(0);
se->setMaterial(material);
}
-(void)applyTexture2
{
string texName = "Texture2.png";
Ogre::MaterialPtr material =
Ogre::MaterialManager::getSingletonPtr()->getByName(texName);
if (material.isNull()) {
material =
Ogre::MaterialManager::getSingletonPtr()->create(texName,
"General",
false);
}
Ogre::Technique *technique = material->getTechnique(0);
Ogre::Pass *pass = technique->getPass(0);
pass->setLightingEnabled(true);
pass->setCullingMode(Ogre::CULL_NONE);
pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
Ogre::TextureUnitState *tus = pass->createTextureUnitState();
tus->setTextureName(texName);
tus->setNumMipmaps(0);
tus->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
Ogre::Entity *entity = OgreFramework::getSingletonPtr()->m_pSceneMgr->getEntity("Prop_Plane");
entity->setVisible(true);
Ogre::SubEntity *se = entity->getSubEntity(0);
se->setMaterial(material);
}