issue assigning texture based materials

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
supadenz
Halfling
Posts: 41
Joined: Sat Feb 16, 2013 2:18 am
x 1

issue assigning texture based materials

Post by supadenz »

Hey. I'm running ogre 1.9rc2 on ios.

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);
}
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: issue assigning texture based materials

Post by Kojack »

do you have uv coordinates defined in the entities? missing uv coords will cause textures to not appear (well, normally one corner pixel is stretched over the entire model, making it look like a single colour).
supadenz
Halfling
Posts: 41
Joined: Sat Feb 16, 2013 2:18 am
x 1

Re: issue assigning texture based materials

Post by supadenz »

Yes. UVS are defined. The issue is not that I can not apply a material that defines a texture, it is more that I can't switch between them dynamically, as stated above.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: issue assigning texture based materials

Post by c6burns »

Have you loaded those textures already from some resource group? Check the ogre log for errors, because something is going wrong and I don't think it's your code ... although ... how did your compiler like this line?

Code: Select all

Ogre::Entity *entity = OgreFramework::getSingletonPtr()->m_pSceneMgr->getEntity(Prop_Plane");
:lol:
supadenz
Halfling
Posts: 41
Joined: Sat Feb 16, 2013 2:18 am
x 1

Re: issue assigning texture based materials

Post by supadenz »

c6burns wrote:Have you loaded those textures already from some resource group? Check the ogre log for errors, because something is going wrong and I don't think it's your code ... although ... how did your compiler like this line?

Code: Select all

Ogre::Entity *entity = OgreFramework::getSingletonPtr()->m_pSceneMgr->getEntity(Prop_Plane");
:lol:
heh, good catch. there was some hand editing of that post, obviously.

Even in the case where I bind the default material to the subentity, start the loop which controls rendering, then hit the button that runs "applyTexture1", things don't work. All I see from the log is:

Texture: Texture1.png: Loading 1 faces(PF_A8R8G8B8,512x1024x1) Internal format is PF_A8B8G8R8,512x1024x1.

with no other output. This is the same output I get if I apply the material before applying the default and rendering it, which works fine.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: issue assigning texture based materials

Post by c6burns »

Well I slammed your code into the keyPressed method of a tutorial framework app and it worked for me, so you have me stumped. I have an entity called "PenguinBody" and I downloaded 2 random images from the web that I named "checkers.png" and "flowers.png". Everything works as expected:

Code: Select all

bool OgreApp::keyPressed(const OIS::KeyEvent &arg)
{
	if (arg.key == OIS::KC_Z) {
		String texName = "flowers.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 = mSceneMgr->getEntity("PenguinBody");
		entity->setVisible(true);
		Ogre::SubEntity *se = entity->getSubEntity(0);
    
		se->setMaterial(material);
	} else if (arg.key == OIS::KC_X) {
		String texName = "checkers.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 = mSceneMgr->getEntity("PenguinBody");
		entity->setVisible(true);
		Ogre::SubEntity *se = entity->getSubEntity(0);
    
		se->setMaterial(material);
	}

	return BaseApplication::keyPressed(arg);
}
supadenz
Halfling
Posts: 41
Joined: Sat Feb 16, 2013 2:18 am
x 1

Re: issue assigning texture based materials

Post by supadenz »

Hmm, that's very strange. Are there any variables in how I might have my environment set up that could cause this?

I'm using GLES2, not GLES. That's the only thing I can think that may be different?
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: issue assigning texture based materials

Post by c6burns »

Could be something to do with RTSS, which the GLES2 render system requires to emulate fixed function materials like you are using. I have a substantial GLES2 project going right now, but I don't use RTSS so I can't offer much help. I do switch around materials and textures at runtime in GLES2 and it does work for me.
supadenz
Halfling
Posts: 41
Joined: Sat Feb 16, 2013 2:18 am
x 1

Re: issue assigning texture based materials

Post by supadenz »

i'm assuming you mean you have a *non ogre* gles2 project going, right? since ogre gles2 needs rtss?

mc.
supadenz
Halfling
Posts: 41
Joined: Sat Feb 16, 2013 2:18 am
x 1

Re: issue assigning texture based materials

Post by supadenz »

i really believe this is a legitimate bug in ogre w/ rtss and gles2. how do i get this elevated to a tracked issue?

for the record, i can workaround this by setting the material *once* and using animated textures, but i do believe i should be able to swap texture based materials on objects.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: issue assigning texture based materials

Post by c6burns »

supadenz wrote:i'm assuming you mean you have a *non ogre* gles2 project going, right? since ogre gles2 needs rtss?

mc.
No I use ogre, but I have shaders for every material so no requirement for fixed function emulation.

Make a really simple example, perhaps by extending the GLES2 sample from the SDK, which is a very minimal native activity using RTSS. Then if it repros there file a JIRA, I guess. EDIT: actually I just assumed you are using android for some reason. But whatever you are using, make something easily reproducible before submitting it as a bug :)
Post Reply