【问题标题】:DirectWrite rendering issues - quads seem to be overlapping, with some aliasingDirectWrite 渲染问题 - 四边形似乎重叠,有一些锯齿
【发布时间】:2019-07-30 16:42:15
【问题描述】:

问题截图:

以下是相关的代码位 - 为清楚起见,省略了错误处理等。

D2D1_BITMAP_PROPERTIES1 bp;
bp.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;
bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_IGNORE;
bp.dpiX = 96.0f;
bp.dpiY = 96.0f;
bp.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW;
bp.colorContext = nullptr;

...

d2dDeviceContext->SetTarget(targetBitmap);

....

writeFactory->CreateTextFormat(L"Arial", nullptr, DWRITE_FONT_WEIGHT_LIGHT, DWRITE_FONT_STYLE_NORMAL, 
DWRITE_FONT_STRETCH_NORMAL, 32.0f, L"en-US", &textFormatAccountCreds);
textFormatAccountCreds->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
textFormatAccountCreds->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);

std::wostringstream outAccountName;
outAccountName << "Account Name:";
writeFactory->CreateTextLayout(outAccountName.str().c_str(), (UINT32)outAccountName.str().size(), textFormatAccountCreds, (float)800, 
(float)600, &textLayoutAccountName);

const float color[4] = { 1.0f, 0.5f, 0.3f, 1.0f };
immediateContext->ClearRenderTargetView(renderTargetView, color);

d2dDeviceContext->BeginDraw();
d2dDeviceContext->DrawTextLayout(D2D1::Point2F(2.0f, 5.0f), textLayoutAccountName, blackBrush);
d2dDeviceContext->EndDraw();

swapChain->Present(0, 0);

正如您在屏幕截图中看到的那样,文本的渲染效果很差——具体来说,第一个“n”似乎在左边被切断了,第一个“u”也是如此,大写的“N”有发生了一些奇怪的混叠。我可以更改文本的大小/位置,呈现的问题会有所不同,但它们仍然存在。

我在这里缺少什么?我已经阅读了 MSDN 上的大多数 DirectWrite 文档,但不清楚我的问题是什么。我确实看到有人提到我应该指定我的文本相对于 dpi 比例的起始位置,但是这个例子非常模糊。

还值得一提的是,我将 Direct3D 和 DirectWrite 与 Direct2D 一起使用,其方式与 this tutorial 中看到的类似。不确定这是否是我的问题。

如果能提供任何帮助,我将不胜感激。非常感谢。

【问题讨论】:

  • 如何设置渲染目标大小?窗口宽度(在屏幕截图上)不是 800 有点可疑。
  • 这条评论帮助我找到了问题。谢谢!

标签: windows directx direct3d direct2d directwrite


【解决方案1】:

我想通了。感谢 VTT 为我指明了正确的方向。

HWND hWnd = CreateWindow(
    szWindowClass,
    szTitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    800,
    600,
    NULL,
    NULL,
    hInstance,
    NULL
);

...

DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = 800;
sd.BufferDesc.Height = 600;
sd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 4;
sd.SampleDesc.Quality = m4xMsaaQuality - 1;
sd.Windowed = TRUE;
sd.Flags = 0;

显然,您传递给 CreateWindow 的尺寸包括窗口菜单和边框的空间,因此您可以在窗口内绘制的空间的实际尺寸更小。

RECT rect;
GetClientRect(hWnd, &rect);
// Returns 784 x 561

所以解决方案是: - 省略在 DXGI_SWAP_CHAIN_DESC 中指定 sd.BufferDesc.Width 和 sd.BufferDesc.Height,因为当它们设置为 0 时,它们将从父窗口继承尺寸。 - 稍后需要实际窗口尺寸时,使用 GetClientRect 获取实际尺寸。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2021-11-29
    相关资源
    最近更新 更多