Advertisement

Culling in SharpDX

Started by April 15, 2015 01:24 PM
9 comments, last by gomidas 7 years, 5 months ago

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.

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);

Advertisement

In DX11. Yes I know the ways that it's possible to cull, but I just need to know how to tell it to do that. I don't know where the enumeration gets used.

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.

Well I don't know if changing the state will be necessary but thanks for the advice. Yes I know cephalo answered the question, but it was edited after my post, which is why I didn't see his answer initially. Anyway, thanks both of you.

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;

Advertisement

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.

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

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.

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.

This topic is closed to new replies.

Advertisement