# Direct3D Transparency

## Recommended Posts

Garra    103
I'm working on a project using Direct3D. I have a array of objects declared as ghost G[20]. What i'm trying to accomplish is make it so the 20 object fade in and out. The function i'm using to do this is below, however right now it's set up so if the alpha is 0.3 then change it to 0 for testing purposes. tarGhost is the ghost i want to change transparency on, index is the index of the ghost. I need the index because i have a array of 20 materials called ghostMat, this is declared in the private section of my class.

void ghost::fadeGhost(ghost tarGhost, int index){	if (tarGhost.ghostMat[index].Diffuse.a == 0.3f)	{		tarGhost.ghostMat[index].Diffuse.a = 0.0f;	}	//if (tarGhost.ghostMat[index].Diffuse.a <= 0)	//{	//	tarGhost.ghostMat[index].Diffuse.a = 0;	//}}

in my main game loop i call the function like so:
	for (int i = 0; i < 20; i++)	{		G[i].moveGhost();		G[i].draw(device, i);                //===============================		G[i].fadeGhost(G[i], i);                //===============================	}

The program acts as if the comparison is never made, so the transparency never changes and is always set to 0.3f (which is the initial value for the alpha).

##### Share on other sites
Molle85    172
you can't compare floating point like that, you need to compare if the value is within an epsilon.

##### Share on other sites
Buckeye    10747
Alternately, try something like:
if( diffuse.a <= 0.3f ) ...

As Molle85 implies, a == comparison between floats or doubles is asking for trouble. Remember, those float values are converted to and compared as binary numbers. Decimal numbers such as 0.3 can't be converted exactly to binary.

##### Share on other sites
Endurion    5408
For one you probably want to change the ghosts material before drawing. Second, show the code where you set the material and draw the ghost.