Search code examples
c#direct2dsharpdx

SharpDX.Direct2D1 rendering a colored polygonal image


Is there any way to draw a colored polygonal image in SharpDX.Direct2D1? I need to draw a image using ColorMatrix in a given polygon, so that I can control the UV coordinates of the image inside the polygon

Here's what I've already tried:

// begin draw
d2dContext.Target = d2dTarget;
d2dContext.BeginDraw();
d2dContext.Clear( new RawColor4(0.9f,0.9f,0.9f,1) );

// colored image
SharpDX.Direct2D1.Effects.ColorMatrix effect = new SharpDX.Direct2D1.Effects.ColorMatrix(d2dContext);
effect.Matrix = zzzGetColorMultiplyMatrix( Color.FromArgb(255,0,0), 1f );
effect.SetInput( 0, imgTest1, false );
d2dContext.DrawImage( effect, new RawVector2(50,50) );

// scaled colored image
var state = new SharpDX.Direct2D1.DrawingStateBlock(d2dContext.Factory);
d2dContext.SaveDrawingState( state );
d2dContext.Transform = new RawMatrix3x2( 1.5f, 0f, 0f, 1.5f, 0f, 0f );
effect.Matrix = zzzGetColorMultiplyMatrix( Color.FromArgb(0,255,0), 0.5f );
effect.SetInput( 0, imgTest1, false );
effect.GetInput( 0 );
d2dContext.DrawImage( effect, new RawVector2(50,250) );
d2dContext.RestoreDrawingState( state );

// polygon
SharpDX.Direct2D1.PathGeometry triangGeo2 = new SharpDX.Direct2D1.PathGeometry(d2dContext.Factory);
using (SharpDX.Direct2D1.GeometrySink geoSink = triangGeo2.Open())
{
    geoSink.BeginFigure(new RawVector2(400+100, 210), SharpDX.Direct2D1.FigureBegin.Filled);
    geoSink.AddLine(new RawVector2(450+100, 210));
    geoSink.AddLine(new RawVector2(450+100, 260));
    geoSink.AddLine(new RawVector2(400+100, 260));
    geoSink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed);
    geoSink.Close();
}
d2dContext.FillGeometry( triangGeo2, new SharpDX.Direct2D1.SolidColorBrush(d2dContext, new RawColor4(1f,0f,0f,1f)) );

// image rect
d2dContext.DrawImage( imgTest1, new RawVector2(400,300), new RawRectangleF(30, 30, 30+60, 30+60), SharpDX.Direct2D1.InterpolationMode.Linear, SharpDX.Direct2D1.CompositeMode.SourceOver );

// polygonal image
SharpDX.Direct2D1.PathGeometry triangGeo3 = new SharpDX.Direct2D1.PathGeometry(d2dContext.Factory);
using (SharpDX.Direct2D1.GeometrySink geoSink = triangGeo3.Open())
{
    geoSink.BeginFigure(new RawVector2(400+80, 310), SharpDX.Direct2D1.FigureBegin.Filled);
    geoSink.AddLine(new RawVector2(500+80, 310));
    geoSink.AddLine(new RawVector2(500+80, 360));
    geoSink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed);
    geoSink.Close();
}
d2dContext.FillGeometry( triangGeo3, new SharpDX.Direct2D1.ImageBrush(d2dContext, imgTest1, new SharpDX.Direct2D1.ImageBrushProperties() { ExtendModeX = SharpDX.Direct2D1.ExtendMode.Wrap, ExtendModeY = SharpDX.Direct2D1.ExtendMode.Wrap, InterpolationMode = SharpDX.Direct2D1.InterpolationMode.Linear, SourceRectangle = new RawRectangleF(0,0,20,20) }) );

// end draw
d2dContext.EndDraw();
swapChain.Present(1, PresentFlags.None);

Solution

  • I solved the problem with superimposing the same polygon on top, but painted in a solid color and the mode turned on d2dContext.PrimitiveBlend = SharpDX.Direct2D1.PrimitiveBlend.Minimum;