随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


{相关接口}
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;


两个测试的效果图:



posted on   万一  阅读(2316)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2010-04-08 学 Win32 汇编[15]: LOOP 与标号
2010-04-08 学 Win32 汇编[14]: 使用中括号 []
2008-04-08 Windows 编程[1] - 窗体生成的过程一


点击右上角即可分享
微信分享提示