Sign in to follow this  
renanrosa

Texture Alpha Fade

Recommended Posts

Buckeye    10747
Assuming you're using a shader and have parameters set as below, decrease the vertex color alpha over time.

AlphaBlendEnable = true;
// alpha blending
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
ZWriteEnable = false;

Share this post


Link to post
Share on other sites
renanrosa    132
Quote:
Original post by Buckeye
Assuming you're using a shader and have parameters set as below, decrease the vertex color alpha over time.

AlphaBlendEnable = true;
// alpha blending
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
ZWriteEnable = false;


but how decrease the color of the vertex ?

Share this post


Link to post
Share on other sites
unbird    8335
For instance linear (pseuso code):
color.a = color.a * max(0, (1 - time / duration));
Where time is the particle's current lifetime (a variable) and duration is the time needed to fully fade out the particle (a constant).

Share this post


Link to post
Share on other sites
renanrosa    132
Quote:
Original post by unbird
For instance linear (pseuso code):
color.a = color.a * max(0, (1 - time / duration));
Where time is the particle's current lifetime (a variable) and duration is the time needed to fully fade out the particle (a constant).


but how i access the color of the vertex ?
(pseuso code):

ID3DXMesh* mesh;
loadXFile(&mesh, "xFile.x");
........
//configure alpha
........
g_pDevice->setTexture(0, text);
// how mesh->getColor ??
mesh->DrawSubset(0);



or



struct VERTEX
{
enum { FVF = D3DFVF_XYZ | D3DFVF_TEX1 }; //D3DFVF_DIFFUSE
float px, py, pz;
float tu, tv;
};

//create the vertex
VERTEX vtxBillBoard[4] =
{
{ -1, 0, 0, 0, 1 },
{ -1, 4, 0, 0, 0 },
{ 1, 0, 0, 1, 1 },
{ 1, 4, 0, 1, 0 }
};

g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, vtxBillBoard, sizeof(VERTEX) );


Share this post


Link to post
Share on other sites
Adam_42    3629
Try something like this:


struct VERTEX
{
enum { FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 };
float px, py, pz;
DWORD colour;
float tu, tv;
};

//create the vertex
VERTEX vtxBillBoard[4] =
{
{ -1, 0, 0, 0, 0, 1 },
{ -1, 4, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1 },
{ 1, 4, 0, 0, 1, 0 }
};

// Set random alpha value for all vertices
// TODO: pick better alpha values
for(int i=0; i < 4 ; i++)
{
vtxBillBoard[i].colour = D3DCOLOR_ARGB(rand()%256, 0, 0, 0);
}

g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, vtxBillBoard, sizeof(VERTEX) );




You might also find http://msdn.microsoft.com/en-us/library/bb206332%28VS.85%29.aspx useful.

Share this post


Link to post
Share on other sites
renanrosa    132
Quote:
Original post by Adam_42
Try something like this:

*** Source Snippet Removed ***

You might also find http://msdn.microsoft.com/en-us/library/bb206332%28VS.85%29.aspx useful.


hi, thank, worked, but only changing the color and not the alpha:

for(int i=0; i < 4 ; i++)
{
vtxBillBoard[i].colour = D3DCOLOR_ARGB(100, rand()%256, rand()%256, rand()%256);
}
why ?

Share this post


Link to post
Share on other sites
programci_84    336
Quote:
hi, thank, worked, but only changing the color and not the alpha:

for(int i=0; i < 4 ; i++)
{
vtxBillBoard[i].colour = D3DCOLOR_ARGB(100, rand()%256, rand()%256, rand()%256);
}
why ?


Look at your code: You're using ARGB and setting .a (alpha) parameter to 100. This means that alpha value will be constant.

Every frame, try changing .a parameter. Like one guy suggested:

for(int i=0; i < 4 ; i++)
{
DWORD a = 255 * max (0, time / duration);
vtxBillBoard[i].colour = D3DCOLOR_ARGB(a, rand()%256, rand()%256, rand()%256);
}

Share this post


Link to post
Share on other sites
renanrosa    132
Quote:
Original post by programci_84
Quote:
hi, thank, worked, but only changing the color and not the alpha:

for(int i=0; i < 4 ; i++)
{
vtxBillBoard[i].colour = D3DCOLOR_ARGB(100, rand()%256, rand()%256, rand()%256);
}
why ?


Look at your code: You're using ARGB and setting .a (alpha) parameter to 100. This means that alpha value will be constant.

Every frame, try changing .a parameter. Like one guy suggested:
*** Source Snippet Removed ***


sure, I understood, is working perfectly,
but if I change just the alpha does not work, I have to change the color too, regardless if I change the alpha value.
but how I do it in a mesh? (.x File)

Share this post


Link to post
Share on other sites
Buckeye    10747
Quote:
is working perfectly, but if I change just the alpha does not work..

Then it's not doing what you asked about in your original post. The effects you posted pictures for are done by changing the alpha.

Are you trying to get an object that you are drawing to fade into the background?

Do you have alpha blending enabled? Do you have the source and destination blend factors set as mentioned above?

You should post your code for rendering the object.

Share this post


Link to post
Share on other sites
renanrosa    132
Hi Buckeye
See my code:

const DWORD dwCustomFVF = (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);

struct SimpleVertex
{
float x,y,z;
DWORD diffuse;
float u,v;
};
//wall
SimpleVertex parede[4] =
{

{ -1.0f, 1.0f, 3.0f, 0, 0,0 },
{ -1.0f, -1.0f, 3.0f, 0, 0,1 },
{ 1.0f, 1.0f, 3.0f, 0, 1,0 },
{ 1.0f, -1.0f, 3.0f, 0, 1,1 },
};
//object alpha
SimpleVertex Vertices[4] =
{

{ -1.0f, 1.0f, 3.0f, 0, 0,0 },
{ -1.0f, -1.0f, 3.0f, 0, 0,1 },
{ 1.0f, 1.0f, 3.0f, 0, 1,0 },
{ 1.0f, -1.0f, 3.0f, 0, 1,1 },
};

//configure all and load texture
void AdjustStates()
{



g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );


g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_DESTALPHA );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );


g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);


D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.5f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );


//create texture
D3DXCreateTextureFromFile( g_pd3dDevice, L"textura.jpg", &texturaAlpha);
D3DXCreateTextureFromFile( g_pd3dDevice, L"parede.jpg", &texturaWall );

}
//draw wall
void drawParede()
{
g_pd3dDevice->SetFVF(D3DFVF_TEX1);
SetPosition(200,200,20);
g_pd3dDevice->SetTexture(0, texturaWall );
g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, parede, sizeof(SimpleVertex) );
}

void Render()
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(150,150,150), 1.0f, 0 );

if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{

drawParede();
SetPosition(1,1,1);
g_pd3dDevice->SetFVF(dwCustomFVF);


DWORD alpha = 255 * max (0, timeGetTime() / 15);

for(int i = 0; i < 4; ++i)
{
//only alpha dont work
Vertices[i].diffuse = D3DCOLOR_ARGB(alpha, alpha, alpha, alpha);

}

g_pd3dDevice->SetTexture(0, texturaAlpha);
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, Vertices, sizeof(SimpleVertex) );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);




g_pd3dDevice->EndScene();
}

g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


Share this post


Link to post
Share on other sites
Buckeye    10747
Change your cullmode to D3DCULL_CCW. Troubleshooting with an incorrect parameter is not a good idea.

Also, change your destination blend.

g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

How do you setup to view matrix?

What does SetPosition(...) do?

Your projection matrix is setup for a depth range of 1 to 100. For now, unless there's a reason to do otherwise, use a range such as 1 to 1000.0f.

The way you set the alpha is definitely a problem. timeGetTime() is the number of milliseconds since the last system start. timeGetTime()/15 will just be some large number.

DWORD alpha = 255 * max (0, timeGetTime() / 15);


For now, just to get blending working, try:

BYTE alpha = 128;
Vertices[i].diffuse = D3DCOLOR_ARGB(alpha, 256, 128, 64);

EDIT:
This:

g_pd3dDevice->SetFVF(D3DFVF_TEX1);

should be:

g_pd3dDevice->SetFVF(dwCustomFVF);

Suggestion: You really ought to start with something much simpler until you get a better grasp of how to program DirectX.

[Edited by - Buckeye on November 30, 2010 8:31:49 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this