Back Face Culling


Back face culling is an operation that ensures that only those triangles facing the camera are rendered. With back face culling enabled, any triangles that are facing away from us are invisible. The front side of a triangle is defined two ways. The first definition is that the vertices of the triangle are specified in counter-clockwise order. The front side of the triangle is also defined as the direction in which the face normal vector is pointing.

The calculation of a face normal vector is illustrated in Listing 3 “13. The array V represents the array of three vertices defining the triangle. The variable N is the resulting normal vector.

Listing 3.13: Face Normal Calculations
start example
 // Calculate the vector from the second point back to the first point.  V1.x = V[0].x   V[1].x  V1.y = V[0].y   V[1].y  V1.z = V[0].z   V[1].z   // Calculate the vector from the third point back to the second point.  V2.x = V[1].x   V[2].x  V2.y = V[1].y   V[2].y  V2.z = V[1].z   V[2].z  // Calculate the magnitude of each vector.  mag1 = sqrt(V1.x * V1.x + V1.y * V1.y + V1.z * V1.z)  mag2 = sqrt(V2.x * V2.x + V2.y * V2.y + V2.z * V2.z)  // Normalize both vectors.  V1.x /= mag1  V1.y /= mag1  V1.z /= mag1  V2.x /= mag1  V2.y /= mag1  V2.z /= mag1  // Compute the cross product of the two vectors which is the normal of the vectors.  N.x = V1.y * V2.z   V1.z * V2.y  N.y = V1.Z * V2.y   V1.x * V2.z  N.z = V1.x * V2.y   V1.y * V2.x 
end example
 

We will need to calculate the face normal vector only if we are programmatically creating a 3D object. Modeling software normally includes the face normal information in the saved data format. The normal vector is not only used to determine the lighting characteristics for the face; when we know the direction a face is pointing, we can also calculate the angle of incidence of light striking the face.

We have established what back face culling is and how it is determined. Since culling back faces is the default, why would we ever want to turn it off? By turning off the back face culling for some objects, we can make a two-dimensional object visible from both sides. One example of using this technique would be a checkered banner over the finish line of a race. Since both sides of the banner look the same, there is no reason we cannot have one set of triangles do double duty and make the simple banner visible from either side. The code to turn off back face culling is shown here:

 Device.SetRenderState(RS.CullMode, Cull.None); 

We must make sure that when we are done rendering that object we restore the culling state to its previous value. This is true with any of the rendering states. Forgetting to restore rendering states is a sure recipe for strange things happening on the screen.




Introduction to 3D Game Engine Design Using DirectX 9 and C#
Introduction to 3D Game Engine Design Using DirectX 9 and C#
ISBN: 1590590813
EAN: 2147483647
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net