# [MDX or any DX] State Management.

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

## Recommended Posts

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.

##### Share on other sites
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.

• 9
• 10
• 9
• 48
• 12
• ### Forum Statistics

• Total Topics
631387
• Total Posts
2999712
×