how to set material alpha blend?

Started by
5 comments, last by sunrisefe 14 years, 10 months ago
I want to use a rectangle with leaf texture to render a tree's leaf. So how to set that in directx?
Advertisement
Quote:Original post by sunrisefe
I want to use a rectangle with leaf texture to render a tree's leaf.
So how to set that in directx?


If you are using shaders, render a quad (2 triangles) and apply over them the leaf texture. (Remember to activate the AlphaBlending blendstate)
Leaves are often done with AlphaTesting rather than AlphaBlending. Alphatest is a pure 100% on or off. When a pixel is discarded, the Z buffer is not affected, leaving proper holes in your Z buffer. This allows you to render without worrying about draw order.

Setting the following render states will make any alpha value over 128 draw.
D3DRS_ALPHATESTENABLE, true
D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL
D3DRS_ALPHAREF, 128

For full blending, where you can get soft transparent edges, you need to set some different states, and render in a certain way. Blending typically is done after you've drawn everything that's solid, and you must render blended objects sorted back to front, to blend correctly with other overlapping blended objects.

D3DRS_ALPHABLENDENABLE, true
D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
D3DRS_BLENDOP, D3DBLENDOP_ADD

You can mix blending and testing so large alpha values get blended, and small alpha values leave holes. Combining them is usually only used to get a performance boost on large transparent areas. Typically you'll have it discard very small alphas (like an ALPHAREF of 8) which wouldn't really contribute to the final image anyway.

Thank you,Namethatnobodyelsetook and feal87.

I will check out how to do alpha test and alpha blending for leaf.

BTW,Personal DirectX Related Blog cannt open.
I use alpha-blend to draw the leaf. Though through alpha-test will have performance inhance, I dont know how to do it. My alpha-blend code is as follows. BTW,how to get the texture's format(like r8g8b8) to know how large is the alhpa? Any tool or code?
	// Create the texture and set filters.	D3DXCreateTextureFromFile(Device,"leaf.png",&Tex);	Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);	Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);	Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);	Device->SetRenderState(D3DRS_LIGHTING, false);//close lighting	// Set alpha blending states.	Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);	Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);        //draw code        Device->SetTexture(0, Tex);	Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);// turn on alpha-blend	Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);	Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);//turn off alpha-blend       [\source]
You can get the format via:

D3DSURFACE_DESC desc;
pTexture->GetLevelDesc(0, &desc);
Namethatnobodyelsetook,thank you.
In directx sdk2008, the DirectX Texture Tool doesn't work. When click it, the error "unable to create direct3d device,please make sure your desktop color depth is 16 or 32 bit, and that d3dref.dll is installed.". However,my desktop color depth is 32bit and I download one d3dref.dll and put it in the C:\WINDOWS\system32. So how to mend this error? Thank you.

This topic is closed to new replies.

Advertisement