Platform SDK: DirectX

Creating Cube-Map Textures

The creation of cube-map textures is similar to regular texture creation. However, in addition to the regular texture attributes, you can specify which faces of the cube map you wish to have created.

The following code uses the D3DXCreateCubeMapTexture function to create a cube-map texture:

    DWORD w = 256, h = 256;
    D3DX_SURFACEFORMAT sf = D3DX_SF_A8R8G8B8;
 
    hr = D3DXCreateCubeMapTexture(
                    m_pD3DDev,
                    0,                              // flags
                    DDSCAPS2_CUBEMAP_ALLFACES,      // faces
                    0,                              // colors for empty faces
                    &w,                             // width
                    &h,                             // height
                    &sf,                            // surface format
                    NULL,                           // palette
                    &m_pEnvTex,                     // resulting surface
                    NULL);                          // number of mipmap levels

Now, you should load the faces of the cube map.

    DDSCAPS2 ddsCaps;
    ZeroMemory(&ddsCaps, sizeof(ddsCaps));
    for (i = 0; i < 6; i++)
    {
        DWORD FacesArray[] = 
        {
            DDSCAPS2_CUBEMAP_POSITIVEX,
            DDSCAPS2_CUBEMAP_NEGATIVEX,
            DDSCAPS2_CUBEMAP_POSITIVEY, 
            DDSCAPS2_CUBEMAP_NEGATIVEY,
            DDSCAPS2_CUBEMAP_POSITIVEZ, 
            DDSCAPS2_CUBEMAP_NEGATIVEZ
        };

Map the file indexes to the various faces of the cube map.

        DWORD FileIndex[] =
        {
            2, 0, 5, 4, 1, 3
        };

You can use the following code to locate the correct face.

        LPDIRECTDRAWSURFACE7 pFace;
        if (i != 0)
        {
            ddsCaps.dwCaps2 = FacesArray[i];
            if (FAILED(hr = m_pEnvTex->GetAttachedSurface(&ddsCaps, &pFace)))
                return hr;
        }

Lastly, you need to compensate for a special case: the zero face. The zero face is the same as the top-level environment map.

        else
        {
            pFace = m_pEnvTex;
            pFace->AddRef();
        }

By default, all six faces of a cube map are created. Also, you can specify a color to use for the empty faces. This can help save memory when some of the faces of the cube map can be adequately represented with a single color. For example, if you are using a set of cube maps to simulate reflections on a car's hood from the overhead lights of a highway, you only need the POSITIVE_Y faces, while the rest of the faces can be approximated with black.