Sign in to follow this  
sunrisefe

how to set material alpha blend?

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

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