Alpha blending, point sprites

Started by
4 comments, last by jollyjeffers 17 years, 10 months ago
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);
Advertisement
Try turning off a ZBufferWriteEnable state. In my case it helped
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]... and [source]...[/source] tags for displaying properly formatted code. See the forum guidelines [smile]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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?





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.
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement