How to use CullMode in XNA

In this article you will learn how to use CullMode in XNA



CullMode is an enumeration and takes 3 values. But before working with lets talk about what Cull Mode is.

Its actually come from Culling term.

In computer graphics, back-face culling determines whether a polygon of a graphical object is visible. It is a step in the graphical pipeline that tests whether the points in the polygon appear in clockwise or counter-clockwise order when projected onto the screen. If the user has specified that front-facing polygons have a clockwise winding, if the polygon projected on the screen has a counter-clockwise winding it has been rotated to face away from the camera and will not be drawn.

The process makes rendering objects quicker and more efficient by reducing the number of polygons for the program to draw. For example, in a city street scene, there is generally no need to draw the polygons on the sides of the buildings facing away from the camera; they are completely occluded by the sides facing the camera.

A related technique is clipping, which determines whether polygons are within the camera's field of view at all.
Another similar technique is Z-culling, also known as occlusion culling, which attempts to skip the drawing of polygons which are covered from the viewpoint by other visible polygons.

This technique only works with single-sided polygons, which are only visible from one side. Double-sided polygons are rendered from both sides, and thus have no back-face to cull.

One method of implementing back-face culling is by discarding all polygons where the dot product of their surface normal and the camera-to-polygon vector is negative.

Backface Culling:

image1.gif


Back-Face Culling Off:

image2.gif

Its usable in 2.5 games in which the character enters to a house and some walls become invisible which helps us see inside the house.

To give a good sample from real world lets talk about this sample screens(Divine Divinity Game):

Back-Face Culling ON:

image3.gif

Back-Face Culling OFF:

image4.gif

See if a character gets inside a house it will look like the roof disappears,and when we get out of the house we see the roofs again.This is we call Culling.

So lets get back to CullingMode

CullMode specifies how back-facing triangles are culled, if at all.The default value is CullMode.CounterClockwise
When drawing sprites, SpriteBatch.Begin does not save your current render state, and will change certain render state properties that may make 3D objects render incorrectly. This includes setting CullMode to CullMode.CullCounterClockwiseFace. You can choose to either reset the render state yourself after the call to SpriteBatch.End, or call SpriteBatch.Begin and pass in SaveStateMode.SaveState, which will restore the render state after sprites are drawn. (MSDN Remark on RenderState.CullMode Property)
CullMode takes 3 values.As we talked about earlier.Lets see what they are:

  1. CullClockwiseFace

  2. CullCounterClockwiseFace

  3. None

What they do?

CullClockwiseFace:

Cull back faces with clockwise vertices.

CullCounterClockwiseFace:

Cull back faces with counterclockwise vertices.

None:

Do not cull back faces.

Sample Usage:

RasterizerState stat = newRasterizerState();
stat.CullMode = CullMode.CullClockwiseFace;