Transparent Textures

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Trip42
Posts: 10
Joined: Sun Jun 06, 2004 9:49 pm

Transparent Textures

Post by Trip42 »

I'm trying to create a transparent texture which contains a row of chairs and apply that texture to a material within a model of an arena. So far I've been unable to achieve the desired effect. The result is that what should be the transparent color turns black, not transparent. Also from certian camera angles the black changes to red (the color key).

Basically I need to be able to create a texture with some transparent areas and apply it to materials within my mesh. I don't want to model individual chairs. The code I'm currently testing is below.

Thanks,
-Brian

int main() {

const int FLOOR_SEATS = 12;

IrrlichtDevice *device = createDevice(EDT_DIRECTX9, dimension2d<s32>(800,600));

IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();

IAnimatedMesh* arenaMesh = smgr->getMesh("Arena/arena.3ds");
IAnimatedMeshSceneNode* arena = smgr->addAnimatedMeshSceneNode(arenaMesh,0,1,vector3df(0,0,0),vector3df(-90,0,0));

arena->setMaterialFlag(EMF_LIGHTING, false);

ITexture* floor_seats = driver->getTexture("Arena/floor_se.jpg");
driver->makeColorKeyTexture(floor_seats, position2d<s32>(0,0));
arena->getMaterial(FLOOR_SEATS).Texture1 = floor_seats;

ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(120,35,0), vector3df(75,35,0));

camera->addAnimator(smgr->createFlyCircleAnimator(vector3df(75,35,0), 125, 0.0005));
camera->setFOV(2);

while(device->run()) {
driver->beginScene(true, true, SColor(0,100,100,100));
smgr->drawAll();
driver->endScene();
}

device->drop();
return 0;
}
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

try:
arena->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR); //makes it transparent

fix it according to how you declared stuff in your code
Image
Trip42
Posts: 10
Joined: Sun Jun 06, 2004 9:49 pm

Post by Trip42 »

I've tried this, the result is the entire arena model becomes somewhat transparent. I only want one particular material to have transparency. I want the part matching the color key to be completely transparent and the other portions to be 0% transparent.

-Brian
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

oh, IC, you mean like alpha transparency for fences and stuff like that?

There's a discussion somewhere in the forums on that. Try to search for it, I'll do the same.
Image
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

Check how the 2D graphics example did it.
etcaptor
Posts: 871
Joined: Fri Apr 09, 2004 10:32 pm
Location: Valhalla
Contact:

Post by etcaptor »

You first must find those material.
Use fuction getMaterialCount() and getID ( ) or getName () , to identify material which you need.
I don't know wheter irrlicht take id and names of materials automaticaly, when mesh is loaded - probably no.
In this case you must at begining to set id or names of materials.
I be waiting for other advices.
Trip42
Posts: 10
Joined: Sun Jun 06, 2004 9:49 pm

Hmmm

Post by Trip42 »

Okay,

I've been able to sort of get something working. First I made the chairs a different mesh than rest of the arena so that the material type only applies to the chairs. The problem is that there appears to be a certian camera distance / camera angle where the color key doesn't take effect. As you can see in this screenshot the chairs closest to the camera appear somewhat correctly, those further away show the color that should be transparent:

Image

If I change the material type from EMT_TRANSPARENT_ADD_COLOR to EMT_TRANSPARENT_ALPHA_CHANNEL I am able to create chairs something closer to the effect of what I want, however as before at certian camera distances/angles the chairs simply dissappear.

Image

Just for reference, here is a screenshot without any transparency:

Image

My code listing is below.

Thanks,
-Brian

Code: Select all

int main() {

	IrrlichtDevice *device = createDevice(EDT_DIRECTX9, dimension2d<s32>(800,600));

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();

	IAnimatedMesh* arenaMesh = smgr->getMesh("Arena/arena.3ds");
	IAnimatedMeshSceneNode* arena = smgr->addAnimatedMeshSceneNode(arenaMesh,0,-1,vector3df(0,0,0),vector3df(-90,0,0));
	arena->setMaterialFlag(EMF_LIGHTING, false);

	IAnimatedMesh* seatsMesh = smgr->getMesh("Arena/floor_seats.ms3d");
	IAnimatedMeshSceneNode* seats = smgr->addAnimatedMeshSceneNode(seatsMesh);
	seats->setMaterialFlag(EMF_LIGHTING, false);

	ITexture* floor_seats = driver->getTexture("Arena/floor_se.jpg");
	driver->makeColorKeyTexture(floor_seats, position2d<s32>(0,0));

	seats->getMaterial(0).Texture1 = floor_seats;
	seats->getMaterial(0).BackfaceCulling = false;

	seats->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
	//seats->setMaterialType(EMT_TRANSPARENT_ADD_COLOR);
	
	ICameraSceneNode* camera = smgr->addCameraSceneNodeMaya();
	camera->setFOV(2);

	while(device->run()) {
		driver->beginScene(true, true, SColor(0,0,0,100));
		smgr->drawAll();
		driver->endScene();
	}

	device->drop();
	return 0;
}
[/code]
etcaptor
Posts: 871
Joined: Fri Apr 09, 2004 10:32 pm
Location: Valhalla
Contact:

Post by etcaptor »

Not sure, but try this:

Code: Select all

int mcount =seats->getMaterialCount();
for(int i = 0; i < mcount-1; i++)
{
   seats->getMaterial(i).BackfaceCulling = false;
   seats->getMaterial(i).MaterialType = irr::video::  EMT_TRANSPARENT_ALPHA_CHANNEL);;
//seats->getMaterial(i+1).MaterialType = irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL ;
}
slurmmunger

Post by slurmmunger »

try to switch off mipmapping

device->getVideoDriver()->setTextureCreationFlag(irr::video::ETCF_ALWAYS_32_BIT,true);
device->getVideoDriver()->setTextureCreationFlag(irr::video::ETCF_ALWAYS_16_BIT,false);
device->getVideoDriver()->setTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS,false);
Trip42
Posts: 10
Joined: Sun Jun 06, 2004 9:49 pm

Fixed

Post by Trip42 »

Turning off Mip Mapping seemed to fix the problem. Thanks for everyone's help. I've included a screenshot of the corrected version.

-Brian

Image
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Maybe you're interested in the EMT_TRANSPARENT_ALPHA_CHANNEL bug that I found:

http://irrlicht.sourceforge.net/phpBB2/ ... hp?p=16466
Post Reply