【问题标题】:How can I create texture in DirectX 9 without d3dx9?如何在没有 d3dx9 的情况下在 DirectX 9 中创建纹理?
【发布时间】:2013-04-24 20:13:51
【问题描述】:

Windows 8 应用商店不想在我的应用程序中看到d3dx9,这是用DirectX 9 编写的。但我需要D3DXCreateTexture 功能。我被发现DirectXTex,但它想要DirectX 11。有什么办法可以避免DirectX 11中的全部重写?

【问题讨论】:

    标签: directx windows-store-apps


    【解决方案1】:

    首先您必须加载原始位图数据。方法有很多:

    • 编写自己的加载程序(不是在 21 世纪 oO)
    • 为您需要的格式使用库。比如 libpng、libjpeg,更多在 Google =)
    • 使用多格式库 (C/C++ Image Loading)。 FreeImage 是我的最爱。

    然后您必须通过D3DXCreateTextureFromFileInMemoryD3DXCreateTextureFromFileInMemoryEx 创建IDirect3DTexture9,然后您就可以开始了=)

    更新:

    好的。我们不能使用它D3DXCreateTextureFromFileInMemory。所以...我们可以实现它。 如前所述,我们必须以某种方式将位图加载到内存中(我更喜欢使用 FreeImage)。然后我们通过 CreateTexture() 方法创建 empty IDirect3DTexture9*。然后我们使用LockRect()/UnlockRect() 将位图的内容复制到该纹理。那个时候我们肯定准备好了,因为我已经测试过了! =) 测试包含 FreeType 的 VS2012 解决方案:link(又脏又乱,请重写并封装在一个类中) 核心功能:

    void CreateTexture(const wchar_t* filename)
    {
    unsigned int width(0), height(0);
    std::vector<unsigned char> bitmap;
    LoadBitmapFile(filename, bitmap, width, height); // Wrapped FreeImage
    
    // Create empty IDirect3DTexture9*
    pDevice->CreateTexture(width, height, 1, 0, 
                               D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, 0);
    if (!pTexture)
    {
        throw std::runtime_error( "CreateTexture failed");
    }
    
    D3DLOCKED_RECT rect;
    pTexture->LockRect( 0, &rect, 0, D3DLOCK_DISCARD );
    unsigned char* dest = static_cast<unsigned char*>(rect.pBits);
    memcpy(dest, &bitmap[0], sizeof(unsigned char) * width * height * 4);
    pTexture->UnlockRect(0);
    

    }

    希望对你有帮助。

    附:其实还有一个问题:投影矩阵。您将需要手动创建它或使用一些数学库,因为您无法使用 D3DXMatrix..() 函数。

    【讨论】:

    • D3DXCreateTextureFromFileInMemory 在d3dx9.lib,我不能使用它
    • 我已经更新了我的答案:我们可以在没有 D3DXCreateTextureFromFileInMemory 的情况下创建纹理资源。
    • 如果它工作就是这样)矩阵 - DirectXMath 使用(#ifdef Win8)。但它会被Windows应用商店接受吗?看完这篇我不知道social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/…
    【解决方案2】:

    似乎DirectX 9 代码必须用DirectX 11 重写,因为DirectXTKDirectXTexDirectXMath 库仅适用于DX11。我在http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/c082b208-0d95-4c41-852f-9450340093f4/找到了更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多