Just a very short chapter on culling, what it is and why it is needed.
Imagine you want to draw a box, say a cube. Any angle you look at it, you will only see 3 faces at most. When you ask DirectX to draw your cube however, it will draw all of the 6 faces of your cube. So the 3 faces not facing us are in fact a waste of bandwidth and calculation cycles of your graphics card. Culling is a way for DirectX to determine which faces are facing us, and which other faces are not facing us, and thus not to draw.
So how does DirectX know which triangles are facing us? Say you would draw 2 triangles like the ones below. For each triangle we would have to define 3 points. Now imagine you draw a line from your eyes to the middle of each triangle. The points where these lines would intersect are indicated by the circled crosses. As you can see, the points (1,2,3) are defined clockwise relative to the circled crosses.
Now let’s see what happens if we would fold the left triangle, until we are facing the back of it. In this case, if the triangle would be part of a closed object, DirectX should know it shouldn’t draw the triangle. Let’s have another look at the circled crosses. For the right triangle, that is still facing us, nothing has changed: the three corners are still defined clockwise relative to us. The three corners of red triangle however are now defined counterclockwise relative to us. This is the way DirectX is able to know which triangles are not facing us.
Try defining your triangle in the counterclockwise way:
As you might have expected, your triangle has disappeared.
Although determining which triangles are defined clockwise relative to the viewer takes DirectX a few calculations, these calculations are way faster than the calculations it would take to draw all triangles.
Now imagine you would like to draw a simple triangle, and you would like to rotate this triangle. Due to culling, only 1 side of the triangle will remain visible while your triangle is rotating. Of course this is not what you would want. There are a few ways how you can still get both sides displayed:
One way is simply to define your triangle twice: one time clockwise and the other time counterclockwise. This way 1 triangle will always be visible. The other way is simply to turn of culling. Put this line at the end of your InitializeDevice method:
And you’ll see your triangle again. Culling is in fact only needed when working with solid objects, when you’re working only with planes you should turn culling off. As for now, we leave it turned off, because you easily forget to define your triangles the right way, and culling will then be the last possible cause you think of.
if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device))) { if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device))) { MessageBox(han_WindowToBindTo,"Failed to create even the reference device!","InitializeDevice()",MB_OK); } }