myvraccount

Culling in SharpDX

Recommended Posts

How do I set the culling mode in SharpDX?  I know there's a Cull enumeration, but where does it get used?

 

The real problem is that it's only rendering clockwise polygons, which would be fine if they were all facing the right direction, but somehow they're not, so I though I could at least get a temporary solution by not culling the counterclockwise ones, and then figure out what's wrong when I see them.

Share this post


Link to post
Share on other sites

Is this DX11 or DX9? In DX11 you can cull both ways or no cull.

            RasterizerStateDescription renderStateDesc = new RasterizerStateDescription
            {
                CullMode = CullMode.Back,
                DepthBias = 0,
                DepthBiasClamp = 0,
                FillMode = FillMode.Solid,
                IsAntialiasedLineEnabled = false,
                IsDepthClipEnabled = true,
                IsFrontCounterClockwise = false,
                IsMultisampleEnabled = true,
                IsScissorEnabled = false,
                SlopeScaledDepthBias = 0
            };
            rasterStateSolid = new RasterizerState(Game.GraphicsDevice, renderStateDesc);

Edited by cephalo

Share this post


Link to post
Share on other sites
cephalo just showed you (*), it's the first entry in the above description. The D3D9 style is gone - if you're looking for something like that: if you need to change any of the rasterizer state parameters you have to create another state. Then bind it with DeviceContext.Rasterizer.State = myRasterizerState.

(*) I apologize: I overlooked the edit time.

Share this post


Link to post
Share on other sites

Here is an example of switching the raster state and where that happens during the render phase. Every time you want something different than you have, you change the state. Here I wanted to reverse the winding temporarily to reuse data:

            Game.GraphicsDevice.ImmediateContext.Rasterizer.State = rasterStateReverseWinding;
            foreach (HexMapChunk chunk in chunksInView) //chunksInView determined in update function
            {
                chunk.DrawHexCeiling(hexTopBuffer);
            }

            Game.GraphicsDevice.ImmediateContext.Rasterizer.State = rasterStateSolid;

Share this post


Link to post
Share on other sites

I think that works now.  It's not culling anymore, because if I look at a triangle I can see it from both sides.  It's not actually drawing all the triangles though (just the first few in a list), but I think it must be because of some data size anomaly or something.

Share this post


Link to post
Share on other sites
On 6/25/2017 at 9:38 AM, gomidas said:

can anyone tell what it does if back face culling or front face culling set.

If Back-face culling is set, then any primitives that are determined to be back facing will be discarded, similarly front-face culling will discard any primitives that are determined to the front facing.

This determination is done by looking at the interpolator for SV_POSITION and checking to see if the winding order in screenspace is clockwise or counter-clockwise. The IsFrontCounterClockwise field indicates whether CCW or CW is "front facing". Note that normals are not considered at all for this.

So, if your polygons are counter clock wise and you want to not draw polygons that are facing away from the camera, you want IsFrontCounterClockwise to be true, and CullMode to be Back.

If you want then to not draw polygons that are facing towards the camera, and only draw the ones facing away, you would then change CullMode to Front.

Share this post


Link to post
Share on other sites
On 29.06.2017 at 3:36 AM, Digitalfragment said:

If Back-face culling is set, then any primitives that are determined to be back facing will be discarded, similarly front-face culling will discard any primitives that are determined to the front facing.

This determination is done by looking at the interpolator for SV_POSITION and checking to see if the winding order in screenspace is clockwise or counter-clockwise. The IsFrontCounterClockwise field indicates whether CCW or CW is "front facing". Note that normals are not considered at all for this.

So, if your polygons are counter clock wise and you want to not draw polygons that are facing away from the camera, you want IsFrontCounterClockwise to be true, and CullMode to be Back.

If you want then to not draw polygons that are facing towards the camera, and only draw the ones facing away, you would then change CullMode to Front.

thanks for the answer, I got it and used well. I used it to check edges if one of their triangle is back facing and the other one is front facing. But another question in my mind is : Is it possible to turn back face culling on only single area of the model when mouse over example Draw a virtual circle around mouse (2D coord) and make only that part back face when mouse is moving on model. Just like in the sims series I remember there was something like that. Shortly, that will show whats inside in buildings. If it is possible I will open a new topic. Thank You.

Edited by gomidas

Share this post


Link to post
Share on other sites

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