绘制时如何使ID2D1SolidColorBrush替换而不混合颜色?

时间:2019-08-03 23:07:45

标签: c++ direct2d

我有一个小游戏,其中的地形以线条显示。示例“巨石”:

enter image description here

绘制对象时,先绘制线条,然后在线条的末端绘制圆角以平滑拐角。这是有关如何创建画笔和绘制的相关代码:

// bi.color is of type D2D1::ColorF
bi.color = D2D1::ColorF::White;
bi.color.a = 1.0f - transparency;

ID2D1SolidColorBrush* pBrush = NULL;
p_RT->CreateSolidColorBrush(
    bi.color,
    &pBrush
);

p_RT->DrawLine(
    D2D1::Point2F(static_cast<FLOAT>(spos.x), static_cast<FLOAT>(spos.y)),
    D2D1::Point2F(static_cast<FLOAT>(epos.x), static_cast<FLOAT>(epos.y)),
    pBrush,
    bi.size
);

D2D1_ELLIPSE ellipse;
ellipse.point.x = cpos.x;
ellipse.point.y = cpos.y;
ellipse.radiusX = width;
ellipse.radiusY = height;

p_RT->FillEllipse(
    ellipse,
    pBrush
);

当巨石渐渐消失并且color.a的值变得小于1时,就会出现问题。每个绘图函数使用的画笔相同,但是颜色最终合并并产生以下结果:

enter image description here

有没有办法让画笔替换颜色而不是混合颜色?如果是,怎么办?

Microsoft文档对此没有提及。 ID2D1StrokeStyle*函数中的DrawLine参数可能是我需要更改的,但是我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

如果可以使用ID2D1DeviceContext,则可以使用SetPrimitiveBlend()切换混合模式。但是我认为更好的解决方案是一次绘制整个多边形-您需要创建几何图形,向其中添加线,然后对想要的线连接行为进行DrawGeometry()。通过ID2D1StrokeStyle指定加入模式。

答案 1 :(得分:0)

您应该使用ID2D1GeometrySink创建多边形对象,并使用ID2D1RenderTarget::DrawGeometry提供ID2D1PathGeometry来绘制多边形对象。

DrawGeometry方法接受D2D1_STROKE_STYLE_PROPERTIES,您可以在其中定义所需的线连接,例如D2D1_LINE_JOIN_ROUND