[MDX or any DX] State Management.

Started by
0 comments, last by Krzysiek K 18 years ago
Hi I’m kind of new to using Dirext X and so far as I’m sure is common I’m finding the huge number of options to be quite staggering. One thing I’ve noticed is that keeping track of what options are on and which are off seems to be a huge problem for me so far. Take this following code for example:

device.RenderState.SourceBlend = Blend.SourceAlpha
device.RenderState.DestinationBlend = Blend.InvSourceAlpha
device.RenderState.AlphaBlendEnable = True
device.TextureState(0).AlphaOperation = TextureOperation.BlendDiffuseAlpha

device.Transform.World = PosMatrix
obj.DrawObject(device, Drawing.Color.FromArgb(100, obj.OverLayColor))

This seems to work great but when I next draw something I’m using the options I have set for the last thing I draw in my scene. What if I need different options? The question boils down to: Do I have to reset every option every time I change one to draw something?


Dim previousAlpha As Boolean = device.RenderState.AlphaBlendEnable
Dim SourceBlend As Blend = device.RenderState.SourceBlend
Dim DestinationBlend As Blend = device.RenderState.DestinationBlend
Dim AlphaOperation As TextureOperation = device.TextureState(0).AlphaOperation

device.RenderState.SourceBlend = Blend.SourceAlpha
device.RenderState.DestinationBlend = Blend.InvSourceAlpha
device.RenderState.AlphaBlendEnable = True
device.TextureState(0).AlphaOperation = TextureOperation.BlendDiffuseAlpha

device.Transform.World = PosMatrix
obj.DrawObject(device, Drawing.Color.FromArgb(100, obj.OverLayColor))


device.RenderState.AlphaBlendEnable = previousAlpha
device.RenderState.SourceBlend = SourceBlend
device.RenderState.DestinationBlend = DestinationBlend
device.TextureState(0).AlphaOperation = TextureOperation.BlendDiffuseAlpha

Or is there a better way? Looking for any advice. Thank you.
Advertisement
My solution is to develop render state change policy and stick to it. You should decide which render states you'll reset and which ones you'll leave changed, so future rendering will have to set them. For example, you can decide to always turn off blending if you used it, so future rendering can rely on the fact that it's off. Similary, you can decide to leave blending mode changed - as long as blending is off, blending selected operations doesn't matter and when something will need blending, you usually have to set blending mode anyway.

This topic is closed to new replies.

Advertisement