Texture Alpha Fade

Started by
10 comments, last by Buckeye 13 years, 4 months ago
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;};//wallSimpleVertex 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 alphaSimpleVertex 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 texturevoid 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 wallvoid 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.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 );}
Advertisement
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.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]

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement