[英]OpenGL to DirectX (Buffer To Texture To Screen)
我想创建一个从指针到像素缓冲区的纹理,其格式为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纹理。
“不确定纹理是否会看到这些更改。”
不,这不是因为Unlock
矩形后您正在对内存进行更改。 在D3D中锁定和解锁资源类似于在OpenGL中取消映射缓冲区对象。
至于为什么它可以在OpenGL中工作,我只能想象您正在映射和取消映射功能BufferToTexture (...)
之外的PBO。 如果您也更改了D3D代码使其也能以这种方式工作,那么您应该在解决这一问题上有所作为。
您还应该注意,D3D使用不同的纹理原点:(0,0)在D3D中位于左上角,而在OpenGL中则位于左下角。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.