ID2D1Bitmap::CreateBitmap 的 srcData 是什么
What is the srcData for ID2D1Bitmap::CreateBitmap
我需要为我在 Direct2D 中处理的光线投射器创建一个像素数组的位图。但是,我无法理解如何使用 CreateBitmap 函数。具体来说,我不确定 srcData
参数应该是什么。我很漂亮 sure/hoping 它是一个指向像素数组的指针,但我不确定如何设置该数组。它应该是什么样的数组?什么数据类型?等等
这是我试过的方法:
int width = 400, height = 400;
D2D1::ColorF * arr = (D2D1::ColorF*)calloc(width * height * 4, sizeof(D2D1::ColorF));
for (int i = 0; i < width * height * 4; i++) { arr[i] = D2D1::ColorF(0.0f, 1.0f, 0.0f); }
// Create the bitmap and draw it on the screen
ID2D1Bitmap * bmp;
HRESULT hr;
hr = renderTarget->CreateBitmap(
D2D1::SizeU(width, height),
arr,
width * sizeof(int) * 4,
D2D1::BitmapProperties(),
&bmp);
if (hr != S_OK) { return; } // I've tested and found that hr does not equal S_OK
// Draw the bitmap...
第二行和第三行应该是什么样的?我还有什么地方做错了吗?
HRESULT CreateBitmap(
D2D1_SIZE_U size,
const void *srcData,
UINT32 pitch,
const D2D1_BITMAP_PROPERTIES & bitmapProperties,
ID2D1Bitmap **bitmap
);
您的代码:
hr = renderTarget->CreateBitmap(
D2D1::SizeU(width, height),
arr, // <<--- Wrong, see (a) below
width * sizeof(int) * 4, // <<--- Close but wrong, see (b) below
D2D1::BitmapProperties(), // <<--- Wrong, see (c) below
&bmp);
(a) - 你应该在这里提供一个像素数据数组,其中的格式取决于位图的格式。请注意,这是可选的,您可以在不初始化的情况下创建位图。像素不完全 D2D1::ColorF
。如果您请求相应的位图格式,它们可以是 4 字节 RGBA 数据,请参见下面的 (c)。
(b) - 这是以字节为单位的行之间的距离,如果您的像素应该是 32 位值,您通常需要 Width * 4
此处
(c) - 这请求 DXGI_FORMAT_UNKNOWN
D2D1_ALPHA_MODE_UNKNOWN
并导致位图创建错误。这里需要一个真实的格式,例如 DXGI_FORMAT_B8G8R8A8_UNORM
(参见 Pixel Formats and also Supported Pixel Formats and Alpha Modes)
上面的第一个 link 显示了内存中的字节如何精确映射到像素颜色,您应该分别准备数据。
UPD
使用 DXGI_FORMAT_B8G8R8A8_UNORM
你的初始化结构是这样的:
UINT8* Data = malloc(Height * Width * 4);
for(UINT Y = 0; Y < Height; Y++)
for(UINT X = 0; X < Width; X++)
{
UINT8* PixelData = Data + ((Y * Width) + X) * 4;
PixelData[0] = unsigned integer blue in range 0..255;
PixelData[1] = unsigned integer red in range 0..255;
PixelData[2] = unsigned integer green in range 0..255;
PixelData[3] = 255;
}
我需要为我在 Direct2D 中处理的光线投射器创建一个像素数组的位图。但是,我无法理解如何使用 CreateBitmap 函数。具体来说,我不确定 srcData
参数应该是什么。我很漂亮 sure/hoping 它是一个指向像素数组的指针,但我不确定如何设置该数组。它应该是什么样的数组?什么数据类型?等等
这是我试过的方法:
int width = 400, height = 400;
D2D1::ColorF * arr = (D2D1::ColorF*)calloc(width * height * 4, sizeof(D2D1::ColorF));
for (int i = 0; i < width * height * 4; i++) { arr[i] = D2D1::ColorF(0.0f, 1.0f, 0.0f); }
// Create the bitmap and draw it on the screen
ID2D1Bitmap * bmp;
HRESULT hr;
hr = renderTarget->CreateBitmap(
D2D1::SizeU(width, height),
arr,
width * sizeof(int) * 4,
D2D1::BitmapProperties(),
&bmp);
if (hr != S_OK) { return; } // I've tested and found that hr does not equal S_OK
// Draw the bitmap...
第二行和第三行应该是什么样的?我还有什么地方做错了吗?
HRESULT CreateBitmap(
D2D1_SIZE_U size,
const void *srcData,
UINT32 pitch,
const D2D1_BITMAP_PROPERTIES & bitmapProperties,
ID2D1Bitmap **bitmap
);
您的代码:
hr = renderTarget->CreateBitmap(
D2D1::SizeU(width, height),
arr, // <<--- Wrong, see (a) below
width * sizeof(int) * 4, // <<--- Close but wrong, see (b) below
D2D1::BitmapProperties(), // <<--- Wrong, see (c) below
&bmp);
(a) - 你应该在这里提供一个像素数据数组,其中的格式取决于位图的格式。请注意,这是可选的,您可以在不初始化的情况下创建位图。像素不完全 D2D1::ColorF
。如果您请求相应的位图格式,它们可以是 4 字节 RGBA 数据,请参见下面的 (c)。
(b) - 这是以字节为单位的行之间的距离,如果您的像素应该是 32 位值,您通常需要 Width * 4
此处
(c) - 这请求 DXGI_FORMAT_UNKNOWN
D2D1_ALPHA_MODE_UNKNOWN
并导致位图创建错误。这里需要一个真实的格式,例如 DXGI_FORMAT_B8G8R8A8_UNORM
(参见 Pixel Formats and also Supported Pixel Formats and Alpha Modes)
上面的第一个 link 显示了内存中的字节如何精确映射到像素颜色,您应该分别准备数据。
UPD
使用 DXGI_FORMAT_B8G8R8A8_UNORM
你的初始化结构是这样的:
UINT8* Data = malloc(Height * Width * 4);
for(UINT Y = 0; Y < Height; Y++)
for(UINT X = 0; X < Width; X++)
{
UINT8* PixelData = Data + ((Y * Width) + X) * 4;
PixelData[0] = unsigned integer blue in range 0..255;
PixelData[1] = unsigned integer red in range 0..255;
PixelData[2] = unsigned integer green in range 0..255;
PixelData[3] = 255;
}