gpt4 book ai didi

c++ - 使用图像的一部分创建 directx9 纹理

转载 作者:行者123 更新时间:2023-11-30 04:32:12 25 4
gpt4 key购买 nike

我有一张图片(208x8),我想从它的不同区域复制 8x8 的正方形,然后将所有正方形连接起来创建一个 IDirect3DTexture9*

最佳答案

具体取决于您要执行的操作,IDirect3DDevice9::UpdateSurface 或 IDirect3DDevice9::StretchRect 可能会对您有所帮助。

对于像您所描述的非常小的纹理的简单操作,使用 CPU 操作它们可能是有利的(即使用 IDirect3DTexture9::LockRect)。对于 D3D9,这通常意味着将纹理重新上传到 VRAM,因此它通常只对小的或不经常修改的纹理有用。但有时,如果您受限于渲染,并且注意在循环中更新纹理的位置,则可以隐藏此类操作的成本并“免费”获得它们。

为避免 VRAM 上传,您可以使用 POOL_MANAGED 资源并结合适当的使用和锁定标志,将资源置于 AGP 孔径内,从而允许 CPU 和 GPU 进行高速访问,请参阅:http://msdn.microsoft.com/en-us/library/windows/desktop/ee418784(v=vs.85).aspx

如果您在 CPU 上进行操作,请注意各种纹理格式的平铺和对齐限制。有关此的最佳信息在 SDK 附带的文档(包括几份白皮书)中,在线文档不完整。

这是一个基本的例子:

IDirect3DTexture9* m_tex = getYourTexture();
m_tex->LockRect(0, &outRect, d3dRect, D3DLOCK_DISCARD);

// Stride depends on your texture format - this is the number of bytes per texel.
// Note that this may be less than 1 for DXT1 textures in which case you'll need
// some bit swizzling logic. Can be inferred from Pitch and width.
int stride = 1;
int rowPitch = outRect.Pitch;
// Choose a pointer type that suits your stride.
unsigned char* pixels = (unsigned char*)outRect.pBits;
// Clear to black.
for (int y=0; y < d3dRect.height; ++y)
{
for (int x=0; x < d3dRect.width; ++x)
{
pixels[x + rowPitch * y] = 0x0;
}
}

m_tex->UnlockRect(0);

关于c++ - 使用图像的一部分创建 directx9 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7810489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com