OpenGL to DirectX (Buffer To Texture To Screen)
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 中位于左下角。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL to DirectX (Buffer To Texture To Screen)
- How to Blend two texture in OpenGL ES 1
- CCRenderTexture 在子线程中崩溃 - "Could not attach texture to framebuffer"