Direct2D (32) : 把几何图形网格化为若干三角形 - ID2D1Geometry.Tessellate()



{相关接口}

ID2D1TessellationSink //用于接收 ID2D1Geometry.Tessellate() 方法输出的三角形数组

ID2D1Mesh             //是使用 ID2D1TessellationSink 填充的网格,它负责 Open ID2D1TessellationSink



{相关方法}

TDirect2DCanvas.RenderTarget.CreateMesh() //建立 ID2D1Mesh

TDirect2DCanvas.RenderTarget.FillMesh()   //填充 ID2D1Mesh,只能在 D2D1_ANTIALIAS_MODE_ALIASED 模式下使用

ID2D1TessellationSink.AddTriangles()      //手动添加三角形数组到 ID2D1TessellationSink



在 GDI、GDI+ 中可以把一个区域看做是若干矩形的集合,看来在 D2D 中成了如果三角形的集合了。
不过 D2D 给的操作权限很低,暂时不知道这会有什么用处。

测试 ID2D1Geometry.Tessellate()


uses Direct2D, D2D1;



procedure TForm1.FormPaint(Sender: TObject);

var

  cvs: TDirect2DCanvas;

  iGeometry: ID2D1EllipseGeometry;

  iTessellationSink: ID2D1TessellationSink;

  iMesh: ID2D1Mesh;

  iBrush: ID2D1SolidColorBrush;

begin

  D2DFactory.CreateEllipseGeometry(D2D1Ellipse(D2D1PointF(ClientWidth/2, ClientHeight/2), ClientWidth/3, ClientHeight/3), iGeometry);



  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);

  cvs.RenderTarget.CreateMesh(iMesh);

  iMesh.Open(iTessellationSink);

  iGeometry.Tessellate(TD2DMatrix3x2F.Identity, 0, iTessellationSink);

  iTessellationSink.Close;



  cvs.RenderTarget.CreateSolidColorBrush(D2D1ColorF(clRed), nil, iBrush);

  cvs.BeginDraw;

  cvs.RenderTarget.SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED); //只能在此模式下使用 RenderTarget.FillMesh()

  cvs.RenderTarget.FillMesh(iMesh, iBrush);

  cvs.RenderTarget.SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);

  cvs.EndDraw;

  cvs.Free;

end;



procedure TForm1.FormResize(Sender: TObject);

begin

  Repaint;

end;



测试 ID2D1TessellationSink.AddTriangles()


uses Direct2D, D2D1;



procedure TForm1.FormPaint(Sender: TObject);

const

  x = 8;

var

  cvs: TDirect2DCanvas;

  iGeometry: ID2D1RectangleGeometry;

  iTessellationSink: ID2D1TessellationSink;

  iMesh: ID2D1Mesh;

  rTriangles: array[0..1] of TD2D1Triangle;

  iBrush: ID2D1SolidColorBrush;

  R: TRect;

begin

  R := ClientRect;

  InflateRect(R, -ClientWidth div 5, -ClientHeight div 5);

  D2DFactory.CreateRectangleGeometry(R, iGeometry);



  rTriangles[0].point1 := D2D1PointF(R.Left-x, R.Top-x);

  rTriangles[0].point2 := D2D1PointF(R.Left-x, R.Bottom+x);

  rTriangles[0].point3 := D2D1PointF(R.Right-x, R.Bottom+x);

  rTriangles[1].point1 := D2D1PointF(R.Left+x, R.Top-x);

  rTriangles[1].point2 := D2D1PointF(R.Right+x, R.Top-x);

  rTriangles[1].point3 := D2D1PointF(R.Right+x, R.Bottom+x);



  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);

  cvs.RenderTarget.CreateMesh(iMesh);

  iMesh.Open(iTessellationSink);

  iTessellationSink.AddTriangles(@rTriangles[0], Length(rTriangles));

  iTessellationSink.Close;



  cvs.RenderTarget.CreateSolidColorBrush(D2D1ColorF(clRed), nil, iBrush);

  iBrush.SetOpacity(0.5);

  cvs.BeginDraw;

  cvs.Brush.Color := clBlack;

  cvs.FillGeometry(iGeometry);

  cvs.RenderTarget.SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);

  cvs.RenderTarget.FillMesh(iMesh, iBrush);

  cvs.RenderTarget.SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);

  cvs.EndDraw;

  cvs.Free;

end;



procedure TForm1.FormResize(Sender: TObject);

begin

  Repaint;

end;



两个测试的效果图:

Direct2D (32) : 把几何图形网格化为若干三角形 - ID2D1Geometry.Tessellate()

你可能感兴趣的:(try)