【问题标题】:OpenGL to DirectX (Buffer To Texture To Screen)OpenGL 到 DirectX(缓冲区到纹理到屏幕)
【发布时间】:2013-12-21 22:05:26
【问题描述】:

我想用 BGRA 格式从指向像素缓冲区的指针创建纹理,并且它会不断更改/更新其值。 我想在每一帧的左上角将这个纹理绘制到屏幕上。

使用我的一些 XNA 和 OpenGL 知识,我想出了其中的一些,但我无法获取纹理以查看缓冲区的更改。我也不知道如何正确绘制纹理,所以我创建了一个精灵(就像在 XNA 中一样)。

我写了以下..

DirectX 9:

void CreateSprite(IDirect3DDevice9* Device, ID3DXSprite* &Sprite)
{
    D3DXCreateSprite(Device, Sprite);
}

void DrawSprite(IDirect3DTexture9* Texture, ID3DXSprite* Sprite, D3DXVECTOR3 Position)
{
    Sprite->Begin(D3DXSPRITE_ALPHABLEND);
        sprite->Draw(Texture, nullptr, nullptr, &Position, 0xFFFFFFFF);
    Sprite->End();
    //SafeRelease(Sprite);
    //SafeRelease(Texture);
}

void BufferToTexture(IDirect3DDevice9* Device, BufferInfo* BuffPtr)
{
    if (BuffPtr != nullptr)
    {
        if (Texture == nullptr)
        {
            Device->CreateTexture(BuffPtr->width, BuffPtr->height, 1, 0, D3DFMT_X8B8G8R8, D3DPOOL_MANAGED, &Texture, 0);
        }
        else
        {
            D3DLOCKED_RECT rect;
            Texture->LockRect(&rect, 0, D3DLOCK_DISCARD);
            std::uint8_t* dest = static_cast<std::uint8_t*>(rect.pBits);
            memcpy(dest, BuffPtr->Pixels, BuffPtr->width * BuffPtr->height * 4);
            Texture->UnlockRect(0);
        }

        //I modify the Pixel Buffer. Not sure if the Texture will see these changes.
        //Would be better if I can make the texture's rect.pBits point to BuffPtr->Pixels.
        //So that I can avoid the above memcpy.
        std::uint8_t* Ptr = (std::uint8_t*)BuffPtr->Pixels;
        for (int I = 0; I < BuffPtr->height; ++I)
        {
            for (int J = 0; J < BuffPtr->width; ++J)
            {
                std::uint8_t B = *(Ptr++);
                std::uint8_t G = *(Ptr++);
                std::uint8_t R = *(Ptr++);
                *(Ptr++) = (B == 0 && G == 0 && R == 0) ? 0 : 0xFF;
            }
        }
    }
}

在 OpenGL 中,代码直接使用像素缓冲区。对缓冲区的任何更改也会发生在纹理上,并且不会进行复制。该代码允许我将像素数组直接绘制到屏幕上。我试图在 DirectX 中完成同样的事情,但我不确定如何让纹理看到缓冲区中的变化。另外,我不确定如何将我的 DirectX 纹理绘制到屏幕上。

【问题讨论】:

    标签: c++ opengl directx-9


    【解决方案1】:

    “不确定纹理是否会看到这些变化。”

    不,这不会是因为您在Unlock rect 之后对内存进行了更改。在 D3D 中锁定和解锁资源类似于在 OpenGL 中取消映射缓冲区对象。

    至于它在 OpenGL 中工作的原因,我只能想象您在函数 BufferToTexture (...) 的某个位置 映射和取消映射您的 PBO。如果您更改您的 D3D 代码也以这种方式工作,那么您应该是解决这个问题的一部分。

    您还应该知道,D3D 使用不同的纹理原点:(0,0) 在 D3D 中位于左上角,在 OpenGL 中位于左下角。

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多