DirectX 11 - 如何创建非常简单的 2D 纹理
DirectX 11 - How to create a very simple 2D texture
我是 Direct X 11 的新手。我想创建一个简单的 2d 纹理(类型为 ID3D11Texture2D)。我已阅读有关 CreateTexture2D 的文档,我理解:
pDesc 是我们定义图像的方式。
pInitialData 包含表示图像纹理每个像素的字节数组
ppTexture2D 是我们的结果 - DirectX 11 的 2D 纹理。
我想创建一个非常简单的 2D 纹理:一个粉红色的矩形。但我不知道如何为粉红色矩形创建字节数组。
我有以下代码:
D3D11_TEXTURE2D_DESC Desc;
D3D11_SUBRESOURCE_DATA InitialData;
ID3D11Texture2D* pTexture2D;
Desc.Usage = D3D11_USAGE_DEFAULT;
BYTE* array;//How to have an array of Pink rectangle?
InitialData.pSysMem = array;
InitialData.SysMemPitch = 0;
InitialData.SysMemSlicePitch = 0;
m_device->CreateTexture2D(&Desc, &InitialData, &pTexture2D);//ID3D11Device m_device has been created before.
非常感谢。
假设您将粉色定义为 RGB (255, 174, 201),这将创建一个 1x1 粉色纹理:
ComPtr<ID3D11ShaderResourceView> texSRV;
{
static const uint32_t s_pixel = 0xffc99aff;
D3D11_SUBRESOURCE_DATA initData = { &s_pixel, sizeof(uint32_t), 0 };
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = desc.Height = desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
ComPtr<ID3D11Texture2D> tex;
HRESULT hr = mDevice->CreateTexture2D( &desc, &initData, tex.GetAddressOf() );
if (SUCCEEDED(hr))
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
hr = mDevice->CreateShaderResourceView( tex.Get(),
&SRVDesc, texSRV.GetAddressOf() );
}
if (FAILED(hr))
// error!
}
I'm using the recommended C++ smart-pointer for COM Microsoft::WRL::ComPtr
in this code rather than 'raw' pointers which are harder to ensure proper reference counts. See this page for more.
当然,这通常不是您创建纹理的方式。您通常使用图像文件,并且经常使用 DDS
具有 mipmap 链并使用专门的纹理压缩格式的文件。
请参阅 MSDN 上的 How to: Initialize a Texture From a File。
A good place to start with DirectX 11 using C++ is DirectX Tool Kit for DirectX 11 and the tutorials for it.
我是 Direct X 11 的新手。我想创建一个简单的 2d 纹理(类型为 ID3D11Texture2D)。我已阅读有关 CreateTexture2D 的文档,我理解:
pDesc 是我们定义图像的方式。
pInitialData 包含表示图像纹理每个像素的字节数组
ppTexture2D 是我们的结果 - DirectX 11 的 2D 纹理。
我想创建一个非常简单的 2D 纹理:一个粉红色的矩形。但我不知道如何为粉红色矩形创建字节数组。 我有以下代码:
D3D11_TEXTURE2D_DESC Desc;
D3D11_SUBRESOURCE_DATA InitialData;
ID3D11Texture2D* pTexture2D;
Desc.Usage = D3D11_USAGE_DEFAULT;
BYTE* array;//How to have an array of Pink rectangle?
InitialData.pSysMem = array;
InitialData.SysMemPitch = 0;
InitialData.SysMemSlicePitch = 0;
m_device->CreateTexture2D(&Desc, &InitialData, &pTexture2D);//ID3D11Device m_device has been created before.
非常感谢。
假设您将粉色定义为 RGB (255, 174, 201),这将创建一个 1x1 粉色纹理:
ComPtr<ID3D11ShaderResourceView> texSRV;
{
static const uint32_t s_pixel = 0xffc99aff;
D3D11_SUBRESOURCE_DATA initData = { &s_pixel, sizeof(uint32_t), 0 };
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = desc.Height = desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
ComPtr<ID3D11Texture2D> tex;
HRESULT hr = mDevice->CreateTexture2D( &desc, &initData, tex.GetAddressOf() );
if (SUCCEEDED(hr))
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
hr = mDevice->CreateShaderResourceView( tex.Get(),
&SRVDesc, texSRV.GetAddressOf() );
}
if (FAILED(hr))
// error!
}
I'm using the recommended C++ smart-pointer for COM
Microsoft::WRL::ComPtr
in this code rather than 'raw' pointers which are harder to ensure proper reference counts. See this page for more.
当然,这通常不是您创建纹理的方式。您通常使用图像文件,并且经常使用 DDS
具有 mipmap 链并使用专门的纹理压缩格式的文件。
请参阅 MSDN 上的 How to: Initialize a Texture From a File。
A good place to start with DirectX 11 using C++ is DirectX Tool Kit for DirectX 11 and the tutorials for it.