【发布时间】: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 纹理绘制到屏幕上。
【问题讨论】: