通过sharpdx API绘制并填充4个顶点(2D中的位置)的多边形

时间:2014-12-04 08:36:11

标签: c# directx sharpdx

我有4个浮点(2D中的位置),我想使用sharpdx在2D中绘制并填充多边形(在这4个位置具有顶点)。有人可以指导我吗?我搞不清楚了。一个示例代码对我来说会更好,因为newbee提前谢谢

1 个答案:

答案 0 :(得分:6)

我找到了如何绘制和填充多边形。我使用了RenderTarget对象的drawGeometry和fillGeometry方法。

我不会提到如何初始化directx

    PathGeometry geo1;
    GeometrySink sink1;

        FactoryD2D factory = new FactoryD2D();
        var dpi = factory.DesktopDpi;
        RenderTarget renderTarget;

        renderTarget = new RenderTarget(factory, backBuffer, new RenderTargetProperties()
        {
            DpiX = dpi.Width,
            DpiY = dpi.Height,
            MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
            PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Ignore),
            Type = RenderTargetType.Hardware,
            Usage = RenderTargetUsage.None
        });

        // and after all initialization

            pta[0] = new SharpDX.Vector2(pts[4].X, pts[4].Y);
            pta[1] = new SharpDX.Vector2(pts[5].X, pts[5].Y);
            pta[2] = new SharpDX.Vector2(pts[6].X, pts[6].Y);
            pta[3] = new SharpDX.Vector2(pts[7].X, pts[7].Y);

            geo1 = new PathGeometry(factory);
            sink1 = geo1.Open();
            sink1.BeginFigure(pta[0], new FigureBegin());
            sink1.AddLines(new SharpDX.Vector2[] { pta[1], pta[2], pta[3] });
            sink1.EndFigure(new FigureEnd());
            sink1.Close();

        Color penColor = Color.Black;
        SolidColorBrush penBrush = new SolidColorBrush(g, new SharpDX.Color(penColor.R, penColor.G, penColor.B));
        Color color = AddColor(pt, zmin, zmax);
        SolidColorBrush aBrush = new SolidColorBrush(g, new SharpDX.Color(color.R, color.G, color.B));

            renderTarget.DrawGeometry(geo1, penBrush);
            renderTarget.FillGeometry(geo1, aBrush);
            geo1.Dispose();
            sink1.Dispose();

您可以添加其他地理位置和接收器,以便在其上添加更多形状

希望这对那些迷路的人有所帮助,并试图找到像我一样的答案