Sign in to follow this  

alpha render state Q for point sprites

This topic is 4578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

lpDevice3D->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE ); That is the only alpha render state I set other than enabling it. Now I am very unfamiliar with how these states work. I am rendering a ton of point sprites and having them trail down the screen. It seems that any black in the texture I use is now transparent and whenever two point sprites overlap they get brighter. Well what I want is just the black transparent. I don't want two particles to overlap and get brighter since they are suppose to be solid. I think the texture I am using has had alpha value's set in it, but I'm not quite sure.

Share this post


Link to post
Share on other sites
Assuming your textures have proper alpha, this should work:

lpDevice3D->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
lpDevice3D->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
lpDevice3D->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

These states cause the sprite color to be multiplied by its alpha, and the existing pixel on the screen to be multiplied by (1-spriteAlpha), and the two results added together.

Share this post


Link to post
Share on other sites
Unfortunetly that didn't work.
I can see the black square around each snow particle now.
My snow texture was found from a point sprite example so I'm not sure if the alpha value's are set.

So this leads me to conclude I have to fix up the alpha value's.

How do I go about modifying the alpha channel of a bmp?

Share this post


Link to post
Share on other sites
i suggest u use the following set, they work for me, for transparency and alpha, and getting the black of the texture.

device.SetRenderState(RenderStates.AlphaBlendEnable, true);
device.SetRenderState(RenderStates.SourceBlend, (int)Blend.SourceAlpha);
device.SetRenderState(RenderStates.DestinationBlend, (int)Blend.InvSourceAlpha);
device.SetRenderState(RenderStates.AlphaTestEnable, true);
device.SetRenderState(RenderStates.ReferenceAlpha, 0x08);
device.SetRenderState(RenderStates.AlphaFunction, (int)Compare.GreaterEqual);

they r in managed directx, but im sure u can map them back to unmanaged.

i presonally have problems with the alpha states. i dont quite get them till now.

Share this post


Link to post
Share on other sites
Still doesn't work.
The only one that has worked so far is BLEND_ONE, the states I posted in the first post.


I am very sure I need to modify the alpha channel of the bmp, but I do not know how. Well I could do it on the bit level but that would be annoying, there must be a program out there like PaintShopPro which can do it.

This is how it looks and it does not work:


//Disable zbuffer to help artifact fighting
lpDevice3D->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );


lpDevice3D->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
lpDevice3D->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
lpDevice3D->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
lpDevice3D->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
lpDevice3D->SetRenderState(D3DRS_ALPHAREF, 0x08);
lpDevice3D->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

//Point sprite render states
lpDevice3D->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
lpDevice3D->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE );

Share this post


Link to post
Share on other sites
If you only need to remove the solid black areas, you can pass a colour key to D3DXCreateTextureFromFileEx that specifies that black (0xFF000000) should be set to transparent.

You may end up with black fringes though if there are areas that are almost black, which is likely. In this case the easiest thing to do is edit the file in a pain program (Paintshop, photoshop, Gimp, etc) and save it in a format that supports alpha like PNG.

Share this post


Link to post
Share on other sites
D3DXCreateTextureFromFileEx (cringe) I cannot believe I forget there was an EX version of the function!

THANK YOU!

I hope this helps, I'll bump if it don't.

btw I like your sign off "Stay casual"

Thanks,
Halsafar

Share this post


Link to post
Share on other sites
With the color key you should use

lpDevice3D->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
lpDevice3D->SetRenderState(D3DRS_ALPHAREF, 0x08);
lpDevice3D->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

if there's no gradient between opaque and transparent. This way things will even nicely stay z-sorted for you (no transparency problems).

Share this post


Link to post
Share on other sites
When using D3DXCreateTextureFromFileEx(), make sure that you specify an opaque color key value, otherwise it won't recognize color values without alpha as matching the specified color. For example, for a black color key, specify 0xff000000 (i.e. ARGB 255,0,0,0).

Share this post


Link to post
Share on other sites
Okay I got it working great except z-ordering.

The order in which the particles are rendered seems important. Some particles near the end of the buffer with a z closer to the screen than any other particles will render behind particles with a z far from the screen...

I figured all I would have to do is z-order the buffer, but for a snow storm constantly adding flakes, z-ordering would be a lag. But z-ordering would allow the alpha blending routine to work right.


Em I correct?
Or is it another reason why I might be experiencing this problem?




Edit:
It seems that in my earlier version of this I had to disable zbuffer to fight tons of artifacts.
With the simple transparent black method which looks more realistic for snow flakes than having them blend with everything.... I can keep the zbuffer on and there are no artifacts and now it renders properly without z-ordering the buffer. Mind you, with zbuffer back on, I can notice the ms/f rise linear to the number of particles being displayed where as with zbuffer off it was an un-noticable change with even 500 particles.

Share this post


Link to post
Share on other sites
I decided to create giant snow flakes to see an enlarged version of my snow to see how nice it might look. In my horror it looks rather terrible when it is very big.

As you can see in the screenshot some flakes have black outlines in places where others do not. I can guarantee there is no black in the picture except 0,0,0


Turning on MAGFILTER is what creates this problem, but with it off you can see individual pixels.
m_Graphics.Device3D()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);


Any assistance on eliminating that? Or any tips.


Edit: I have discovered that disableing the zbuffer eliminates this problem but then I have to z-order the point sprites. I still need help.

Share this post


Link to post
Share on other sites
The problem has to do with the default shading routine, the order in which they are being rendered due to the zbuffering cause's some to think they have a black behind when really it is another snow flake but the shading MAGFILTER doesn't realize this so you get black outlines now and then. Looks terrible and is noticeable from a distance even now that I seen it up close.


Will using a pixelshader help?
Are there any other solutions?

Share this post


Link to post
Share on other sites

This topic is 4578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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