Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (5 posts)

Best Way to Change RasterizerState Cullmode?

Last post 3/24/2012 8:21 PM by MastrOfRenegde. 4 replies.
  • 3/24/2012 3:34 PM

    Best Way to Change RasterizerState Cullmode?

    Quick question, is this the best way to reset the cullmode after the spritebatch changes it for me?:

                game.rs = new RasterizerState();  
                game.rs.CullMode = CullMode.None;  
                game.GraphicsDevice.RasterizerState = game.rs; 

    e.g. I make a new rasterizerState and overwrite the old one everytime I'm finished with the spritebatch in my Draw(). I thought I could just go
    game.GraphicsDevice.RasterizerState.CullMode = CullMode.None; 
    But apparently you have to make a new one, is this okay?
  • 3/24/2012 4:07 PM In reply to

    Re: Best Way to Change RasterizerState Cullmode?

    You'd be "best" to only create the state once at boot instead of every frame, as you'll be creating a load of garbage.
    Alternatively, if you only want to change CullMode to None, there's already a built-in state for that, so you can do ...

    game.GraphicsDevice.RasterizerState = RasterizerState.CullNone; 
  • 3/24/2012 6:16 PM In reply to

    Re: Best Way to Change RasterizerState Cullmode?

    Thanks for the reply, I do actually do it at boot but when I use the spritebatch to draw text it changes the state to cullmode.counterclockise, so I just need to set it back. If I do
     
    game.GraphicsDevice.RasterizerState = RasterizerState.CullNone;  

    I get the old "Cannot change read-only RasterizerState. State objects become read-only the first time they are bound to a GraphicsDevice. To change property values, create a new RasterizerState instance."

    So I figure I have to make a new one everytime. The only alternative I see is to make one at initialise, set it to cullmode.none, and set the device to use that. Then before I draw my text, use a second one (whose states the spritebatch can change to whatever it wants), and the swap back after spritebatch.end. This seems equally expensive to me, although I'm totally guessing :D I notice it says the state objects become read only the first time they are bound to the device, so maybe I can change it the second time it is bound?

    In which case would I make 2 rasterizer states, and then bind them to the device each at least once in initialize, just so that I can do that cullmode.none magic line?!
  • 3/24/2012 6:33 PM In reply to

    Re: Best Way to Change RasterizerState Cullmode?

    Answer
    Reply Quote
    If you get the "read-only" message then you must be trying to change a state that has previously been bound to the device, which you can't do. The line of code I mentioned in my previous post should work fine - I use it in my own code. This is because you're not changing that RasterizerState instance, merely setting it on the device.

    If you need more than one state, you'll need to create them all at boot, then just set the required one before you render. You definitely do not need to create a new one each time you want to change a render state.
  • 3/24/2012 8:21 PM In reply to

    Re: Best Way to Change RasterizerState Cullmode?

    Okay thanks again, I was just getting a tad confused by that error message and the words "first time" :p Its all good now:

                _device.RasterizerState = game.rs;  
                game.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);  
                //Draw some nice text on top of you're 3D  
                game.spriteBatch.End();  
     
                //Reset the renderstates after drawing the text  
                _device.BlendState = BlendState.Opaque;  
                _device.DepthStencilState = DepthStencilState.Default;  
                _device.SamplerStates[0] = SamplerState.LinearWrap;  
     
                _device.RasterizerState = game.rs2; 

    (Where rs is the first rasterizerState and rs2 the second rasterizerState with cullmode set to None)
Page 1 of 1 (5 posts) Previous Discussion Next Discussion