XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Problems loading effect file
  Posted by: dellams
  When: 03/02/2012 at 17:34:53

 rotating a model around itself
  Posted by: maxcabalfin
  When: 03/02/2012 at 08:41:25

 bones and quaternions
  Posted by: D_A_V_E
  When: 02/02/2012 at 12:18:22

 import
  Posted by: Anonymous
  When: 02/02/2012 at 06:53:32

 remove internal walls in the city
  Posted by: Anonymous
  When: 02/02/2012 at 05:01:09

 XNA 4.0
  Posted by: dellams
  When: 30/01/2012 at 19:38:27

 Adding Glow around Street Lights
  Posted by: pranavbagur
  When: 30/01/2012 at 13:44:01

 error x3000:syntax error
  Posted by: Anonymous
  When: 30/01/2012 at 06:06:37

 Another Effects error
  Posted by: Anonymous
  When: 29/01/2012 at 15:43:01

 feedback
  Posted by: amtyler
  When: 29/01/2012 at 09:17:38


Ads

Culling

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:

 cv_Vertices[2].x = 150;
 cv_Vertices[2].y = 100;
 cv_Vertices[2].z = 0;
 cv_Vertices[2].weight = 1;
 cv_Vertices[2].color = 0xffff0000;
 
 cv_Vertices[1].x = 350;
 cv_Vertices[1].y = 100;
 cv_Vertices[1].z = 0;
 cv_Vertices[1].weight = 1;
 cv_Vertices[1].color = 0xff00ff00;
 
 cv_Vertices[0].x = 250;
 cv_Vertices[0].y = 300;
 cv_Vertices[0].z = 0;
 cv_Vertices[0].weight = 1;
 cv_Vertices[0].color = 0xff00ffff;

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:

 p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

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.




DirectX Tutorial 6 - Culling

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • Blank Space
          I think there should be a diagram in this chapter ...



    The code after this chapter:


    #include<windows.h>
    #include<d3d9.h>
    #include<d3dx9.h>
    struct OURCUSTOMVERTEX
    {
        float x,y,z,weight;
        DWORD color;
    };

    int int_AppRunning = 1;

    LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
    {
        switch(uint_Message)
        {
            case WM_KEYDOWN:
            {
                int_AppRunning = 0;
                break;
            }
            break;
        }

        return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
    }

    HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
    {
        WNDCLASSEX wnd_Structure;

        wnd_Structure.cbSize = sizeof(WNDCLASSEX);
        wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
        wnd_Structure.lpfnWndProc = OurWindowProcedure;
        wnd_Structure.cbClsExtra = 0;
        wnd_Structure.cbWndExtra = 0;
        wnd_Structure.hInstance = GetModuleHandle(NULL);
        wnd_Structure.hIcon = NULL;
        wnd_Structure.hCursor = NULL;
        wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
        wnd_Structure.lpszMenuName = NULL;
        wnd_Structure.lpszClassName = "WindowClassName";
        wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

        RegisterClassEx(&wnd_Structure);

        return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);
    }

    LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo)
    {
        LPDIRECT3D9 p_dx_Object;
        LPDIRECT3DDEVICE9 p_dx_Device;

        p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
        if (p_dx_Object == NULL)
        {
            MessageBox(han_WindowToBindTo,"DirectX Runtime library not installed!","InitializeDevice()",MB_OK);
        }

        D3DPRESENT_PARAMETERS dx_PresParams;
        ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
        dx_PresParams.Windowed = TRUE;
        dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
        dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;

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


         p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
     
         return p_dx_Device;
     }
     
     void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer)
     {
         p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
         p_dx_Device->BeginScene();
     
         p_dx_Device->SetStreamSource(0, p_dx_VertexBuffer, 0, sizeof(OURCUSTOMVERTEX));
         p_dx_Device->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);
         p_dx_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
     
         p_dx_Device->EndScene();
         p_dx_Device->Present(NULL, NULL, NULL, NULL);
     }
     
     LPDIRECT3DVERTEXBUFFER9 FillVertices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device)
     {
         OURCUSTOMVERTEX cv_Vertices[3];
         LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;
     
         cv_Vertices[2].x = 150;
         cv_Vertices[2].y = 100;
         cv_Vertices[2].z = 0;
         cv_Vertices[2].weight = 1;
         cv_Vertices[2].color = 0xffff0000;
     
         cv_Vertices[1].x = 350;
         cv_Vertices[1].y = 100;
         cv_Vertices[1].z = 0;
         cv_Vertices[1].weight = 1;
         cv_Vertices[1].color = 0xff00ff00;
     
         cv_Vertices[0].x = 250;
         cv_Vertices[0].y = 300;
         cv_Vertices[0].z = 0;
         cv_Vertices[0].weight = 1;
         cv_Vertices[0].color = 0xff00ffff;
     
         if (FAILED(p_dx_Device->CreateVertexBuffer(3*sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZRHW|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))
         {
             MessageBox(han_Window,"Error while creating VertexBuffer","FillVertices()",MB_OK);
         }
     
         VOID* p_Vertices;
         if (FAILED(p_dx_VertexBuffer->Lock(0, 3*sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0)))
         {
             MessageBox(han_Window,"Error trying to lock","FillVertices()",MB_OK);
         }else{
             memcpy(p_Vertices, cv_Vertices, 3*sizeof(OURCUSTOMVERTEX));
             p_dx_VertexBuffer->Unlock();
         }
     
         return p_dx_VertexBuffer;
     }
     
     int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
     {
         MSG msg_Message;
     
         HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
         LPDIRECT3DDEVICE9 p_Device = InitializeDevice(han_Window);
         LPDIRECT3DVERTEXBUFFER9 p_dx_VB = FillVertices(han_Window, p_Device);
     
         while(int_AppRunning)
         {
             if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE))
             {
                 DispatchMessage(&msg_Message);
             }
             DrawScene(p_Device, p_dx_VB);
         }
     
         p_Device->Release();
         DestroyWindow(han_Window);
     
         return 0;
     }


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2011 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2011 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 4.0 using C# (89)
    DirectX using C# (54)
    DirectX using C++ (15)
    Series 1: Terrain (15)
    Opening a window
    Ending the game loop
    Linking to the Device
    Clearing your window
    Drawing a triangle
    Culling
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing .bmp files
    Adding colors
    DirectX Light basics
    DirectX using VB (2)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!