DirectX SDK

Creating Textures

When using the Direct3DX utility library, textures can be created as "blank" textures (each pixel is initialized with a value of zero) with the D3DXCreateTexture function.

Also, textures can be loaded and created in a single call using the D3DXCreateTextureFromFile function which supports the loading of BMP, DIB, TGA, and DDS file formats.

For example, the following call loads a bitmap file (texture.bmp):

    IDirectDrawSurface7 *m_ptex;
    m_pd3dDevice = m_pd3dx->GetD3DDevice();
 
    hr = D3DXCreateTextureFromFile(
            m_pd3dDevice,
            NULL,                   // dwFlags
            NULL,                   // auto-width
            NULL,                   // auto-height
            NULL,                   // auto-surface type
            NULL,                   // pointer to Palette
            &m_ptex,                // returned pointer to texture
            NULL,                   // returned number of mipmaps
            "texture.bmp",          // file name for texture
            D3DX_FT_DEFAULT);       // default scaling

However, surfaces created with the D3DXCreateTextureFromFile function may have been resized, or had their surface format changed because of the requirements of the rasterization device. As a result, calling IDirectDrawSurface7::Lock on surfaces created in this manner is not recommended; since the height, width, number of mipmap levels, and even the pixel format of the surface may vary on different devices.

To transfer pixels to surfaces created with the D3DXCreateTexture or D3DXCreateTextureFromFile functions, it is recommended that you use the D3DXLoadTextureFromSurface and D3DXLoadTextureFromFile functions, which are designed to handle all of the various possible formats and perform the appropriate resampling operations.