Sign in to follow this  

Alpha blending, point sprites

This topic is 4202 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

Hi I'm having some trouble with point sprites and alpha channels. Basically what I want to do is to make a galaxy of stationary stars (point sprites) and everything is working out fine besides the fact that alpha blending has a very weird behaviour. Look at this screenshot: http://www.tjelvar.org/~1nv02a_mape/wopee.jpg Notice how alpha blending works properly only sometimes, while other times a sprite in front of another blocks it although it should have been drawn. Basically I'm clueless. Of course I just started with direct3d so it could be something simple. Pleeeez help me, so I can get on with my coding :P Here are some (hopefully) important code snippets: (C#) InitGraphics(): device.RenderState.Lighting = false; device.RenderState.CullMode = Cull.None; device.SetRenderState(RenderStates.ZEnable, true); device.RenderState.PointScaleEnable = true; device.RenderState.PointSpriteEnable = true; device.RenderState.ZBufferEnable = true; device.RenderState.ZBufferWriteEnable = true; device.RenderState.SourceBlend = Blend.SourceAlpha; device.RenderState.DestinationBlend = Blend.InvSourceAlpha; device.RenderState.BlendOperation = BlendOperation.Add; device.RenderState.AlphaBlendEnable = true; CreateVertexBuffer(): VertexBuffer buf = new VertexBuffer( typeof(CustomVertex.PositionColored), verts.Length, device, 0, CustomVertex.PositionColored.Format, Pool.Default ); GraphicsStream stm = buf.Lock(0, 0, 0); stm.Write(verts); buf.Unlock(); Creating the texture: stars = TextureLoader.FromFile( Sys.env.device, "stars.png", 0, 0, 1, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, System.Drawing.Color.Magenta.ToArgb()); Rendering the point sprites: device.SetTexture(0,Sys.res.stars); device.SetStreamSource(0, vb, 0); device.DrawPrimitives(PrimitiveType.PointList, 0, Sys.obj.stars.Length);

Share this post


Link to post
Share on other sites
Common problem that is more generally described by blending being a "draw-order dependent" algorithm. Ideally you'd want to sort all of your transparent geometry in a back-to-front (from the camera) order and then render them.

In practice that'd usually be horrifically slow; for particle systems simply toggling Z-Writes is usually sufficient.

Change your rendering code to:

device.SetTexture(0,Sys.res.stars);
device.SetStreamSource(0, vb, 0);
device.RenderState.ZBufferWriteEnable = false;
device.DrawPrimitives(PrimitiveType.PointList, 0, Sys.obj.stars.Length);
device.RenderState.ZBufferWriteEnable = true;


Also, as an unrelated note - these forums use [code]...[/code] and [source]...[/source] tags for displaying properly formatted code. See the forum guidelines [smile]

hth
Jack

Share this post


Link to post
Share on other sites
Okay, thanks for your help jollyjeffers and Master of Riddles BUT (big but):
With ZBufferWriteEnable false all the sprites are drawn in backwards order, near drawn first and far away drawn last. You can imagine what it looks like :) Everything is ontop of eachother.
Any clues?





Share this post


Link to post
Share on other sites
try changing your DestinationBlend... for example: Blend.One

This is what I use and it results in an additive effect so that if two points overlap, it gets brighter... There are other ideas too... just make sure your device can handle it by checking the Device Caps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zaphodile
With ZBufferWriteEnable false all the sprites are drawn in backwards order, near drawn first and far away drawn last.
This is to be expected - but the whole point about changing that render-state is that it usually does not matter. A simple example would be a smoke effect - the draw-order doesn't really matter much in that case.

If, however, your particular requirements require draw-order correctness then you're either going to have to test different blend modes (as EnochDagor suggested) or implement some different algorithms. Depth sorting (aka "painters algorithm") can be implemented in a number of ways depending on how accurate you need the results. The problem is that it'll be VERY slow. You pick your trade off - quality of performance [smile]

hth
Jack

Share this post


Link to post
Share on other sites

This topic is 4202 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