Loading Textures in Directx

2019-06-13 15:47发布

Im Trying to load textures in directx to draw a textured quad.

but the D3DXCreateTextureFromFile never returns D3D_OK ....

here is my code to load the texture....

FeralTexture(string Name,FeralVector2 Position,IDirect3DDevice9 *device)
    {
        FileName = Name;
        m_pDevice = device;
        x= Position.x;
        y= Position.y;
        if(D3DXCreateTextureFromFile(m_pDevice,FileName.c_str(),&m_pTextureFile) != D3D_OK)
        {
            TextureCreated = false;
            m_pTextureFile = NULL;
            D3DXCreateTextureFromFile(m_pDevice,FileName.c_str(),&m_pTextureFile);
        }
        else
        {
            if(D3DXGetImageInfoFromFile(FileName.c_str(),&ImageInfo) == D3D_OK)
            {
                TextureCreated = true;
                Width = ImageInfo.Width;
                Height = ImageInfo.Height;
                MinVector = FeralVector2(x,y);
                MaxVector = FeralVector2(x+Width,y+Height);
                //BoundingRect = FeralRect2(MinVector,MaxVector);
            }
            else
            {
                Width = 0;
                Height = 0;
            }
        }
    }

i placed copies of the image in both the debug folder of my project and in the main folder of my project... neither works....

Any input will be greatly appreciated ....

2条回答
叛逆
2楼-- · 2019-06-13 16:37
  1. Make sure the texture file name is correct
  2. Try using absolute path of the texture file in your program if step 1 does not work.

If that's still not work try using DirectX debug runtime, Open DirectX control panel(dxcpl.exe) from C:\Program Files\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86(the path depends on where you install DirectX SDK) and make the settings as below enter image description here

then running your app in debug mode, you will get the detail error message from the output window of Visual studio, it will tell you what's the problem.

D3DXCreateTextureFromFile support the following texture format

.bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga

make sure your texture format in the list above.

D3DXCreateTextureFromFile has the following return codes, you can check the return value and fix your app.

  • D3D_OK
  • D3DERR_NOTAVAILABLE
  • D3DERR_OUTOFVIDEOMEMORY
  • D3DERR_INVALIDCALL
  • D3DXERR_INVALIDDATA
  • E_OUTOFMEMORY
查看更多
Anthone
3楼-- · 2019-06-13 16:48

Always check error codes!

Here is a helper macro to transform error codes to human-readable error message:

#include <dxerr.h>

#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x)                                          \
    {                                                  \
        HRESULT hr = x;                                \
        if (FAILED(hr))                                \
        {                                              \
        DXTrace(__FILE__, __LINE__, hr, #x, TRUE);     \
        }                                              \
    }
#endif

#else
#ifndef HR
#define HR(x) x;
#endif
#endif 

And usage:

HR(D3DXCreateTextureFromFile(...))

查看更多
登录 后发表回答