【问题标题】:Loading Textures in Directx在 Directx 中加载纹理
【发布时间】:2013-09-05 04:14:17
【问题描述】:

我正在尝试在 directx 中加载纹理以绘制带纹理的四边形。

但 D3DXCreateTextureFromFile 永远不会返回 D3D_OK ....

这是我加载纹理的代码......

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

我将图像的副本放在项目的调试文件夹和项目的主文件夹中... 两者都不起作用....

任何意见将不胜感激......

【问题讨论】:

  • 建议:如果您从 IDE 运行,那么工作目录可以不同于输出目录。究竟是什么代码返回加载函数?
  • D3DXCreateTextureFromFile 的返回值是多少。您应该将它放入一个变量中以进一步调试:HRESULT result = D3DXCreateTextureFromFile(...); 您可以查看作为返回值获得的错误值并从那里获取。

标签: c++ directx textures directx-9


【解决方案1】:
  1. 确保纹理文件名正确
  2. 如果第 1 步不起作用,请尝试在程序中使用纹理文件的绝对路径。

如果还是不行,尝试使用 DirectX 调试运行时,从 C:\Program Files\Microsoft DirectX SDK (June 2010)\Utilities\bin\x86 打开 DirectX 控制面板(dxcpl.exe)(路径取决于你在哪里安装 DirectX SDK)并进行如下设置

然后在调试模式下运行您的应用程序,您会从 Visual Studio 的输出窗口中获得详细的错误消息,它会告诉您问题所在。

D3DXCreateTextureFromFile 支持以下纹理格式

.bmp、.dds、.dib、.hdr、.jpg、.pfm、.png、.ppm 和 .tga

确保您的纹理格式在上面的列表中。

D3DXCreateTextureFromFile 有以下返回码,你可以检查返回值并修复你的应用程序。

  • D3D_OK
  • D3DERR_NOTAVAILABLE
  • D3DERR_OUTOFVIDEOMEMORY
  • D3DERR_INVALIDCALL
  • D3DXERR_INVALIDDATA
  • E_OUTOFMEMORY

【讨论】:

    【解决方案2】:

    始终检查错误代码!

    这是一个帮助宏,用于将错误代码转换为人类可读的错误消息:

    #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 
    

    及用法:

    HR(D3DXCreateTextureFromFile(...))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      相关资源
      最近更新 更多