2016-01-13 235 views
1

我想弄清楚如何使用Delphi XE7 Firemonkey填充3D多边形。在使用内置组件的GLScene后,Firemonkey对我来说似乎对健康有害,因为内置控件少,样本少,文档少。Delphi Firemonkey绘制并填充任意三维形状或多边形

使用此代码生成我的多边形:

Context.BeginScene; 
try 
    Context.DrawLine(TPoint3D.Create(1, -1, 0), TPoint3D.Create(1, 1, 0), 0.5, TAlphaColorRec.Black); 
    Context.DrawLine(TPoint3D.Create(1, 1, 0), TPoint3D.Create(0, 1, 0), 0.5, TAlphaColorRec.Black); 
    Context.DrawLine(TPoint3D.Create(0, 1, 0), TPoint3D.Create(-1, 0.5, 0), 0.5, TAlphaColorRec.Black); 
    Context.DrawLine(TPoint3D.Create(-1, 0.5, 0), TPoint3D.Create(-1, 0, 0), 0.5, TAlphaColorRec.Black); 
    Context.DrawLine(TPoint3D.Create(-1, 0, 0), TPoint3D.Create(-0.5, 0, 0), 0.5, TAlphaColorRec.Black); 
    Context.DrawLine(TPoint3D.Create(-0.5, 0, 0), TPoint3D.Create(-0.5, -1, 0), 0.5, TAlphaColorRec.Black); 
    Context.DrawLine(TPoint3D.Create(-0.5, -1, 0), TPoint3D.Create(1, -1, 0), 0.5, TAlphaColorRec.Black); 
finally 
    Context.EndScene; 
end; 

该代码生成这样的多边形:https://cyberflexsoftware.tinytake.com/sf/NDQ5NTIxXzI0MjgzNjg

不过,我需要填补这一形状与颜色的材料,我不知道如何做这个。我想我需要创建一个TMesh,但是如果没有数学博士学位,我很难弄清楚,而且我完全迷失了。任何帮助都会很棒。

回答

0

尝试的TCanvas的FillPolyton方法:http://docwiki.embarcadero.com/Libraries/XE7/en/FMX.Graphics.TCanvas.FillPolygon

样品:

var 
    p1, p2, p3, p4, p5: TPointF; 
    MyPolygon: TPolygon; 
begin 
Image1.Bitmap.Clear($FFFFFF); 
    // sets the points that define the polygon 
    p1 := TPointF.Create(210, 220); 
    p2 := TPointF.Create(330, 360); 
    p3 := TPointF.Create(380, 260); 
    p4 := TPointF.Create(200, 180); 
    p5 := TPointF.Create(140, 160); 
    // creates the polygon 
    SetLength(MyPolygon, 5); 
    MyPolygon[0] := p1; 
    MyPolygon[1] := p2; 
    MyPolygon[2] := p3; 
    MyPolygon[3] := p4; 
    MyPolygon[4] := p5; 
    // fills and draws the polygon on the canvas 
    Image1.Bitmap.Canvas.BeginScene; 
    Image1.Bitmap.Canvas.FillPolygon(MyPolygon, 50); 
    Image1.Bitmap.Canvas.EndScene; 
+0

谢谢,但这是我已经知道的2D画布。我需要弄清楚TContext3D没有相同的方法。 –

0

有点挖掘和玩耍后,我想出了这个解决方案:

procedure TForm1.DummyObjectRender(Sender: TObject; Context: TContext3D); 
var 
    MyPolygon: TPolygon; 
    I: Integer; 
begin 
    Context.BeginScene; 
    try 

    // creates the polygon 
    SetLength(MyPolygon, 8); 

    MyPolygon[0] := TPointF.Create(1, -1); 
    MyPolygon[1] := TPointF.Create(1, 1); 
    MyPolygon[2] := TPointF.Create(0, 1); 
    MyPolygon[3] := TPointF.Create(-1, 0.5); 
    MyPolygon[4] := TPointF.Create(-1, 0); 
    MyPolygon[5] := TPointF.Create(-0.5, 0); 
    MyPolygon[6] := TPointF.Create(-0.5, -1); 
    MyPolygon[7] := TPointF.Create(1, -1); 

    // Draw the polygon lines 
    for I := 0 to Length(MyPolygon) - 1 do 
     if I = Length(MyPolygon) - 1 then 
     Context.DrawLine(TPoint3D.Create(MyPolygon[I].X, MyPolygon[I].Y, 0), 
      TPoint3D.Create(MyPolygon[0].X, MyPolygon[0].Y, 0), 1, TAlphaColorRec.Red) 
     else 
     Context.DrawLine(TPoint3D.Create(MyPolygon[I].X, MyPolygon[I].Y, 0), 
      TPoint3D.Create(MyPolygon[I + 1].X, MyPolygon[I + 1].Y, 0), 1, TAlphaColorRec.Red); 

    // Fill the polygon shape 
    Context.FillPolygon(TPoint3D.Create(0, 0, 0), TPoint3D.Create(2, 2, 0), MyPolygon.MaxEntents, MyPolygon, 
     TMaterialSource.ValidMaterial(ColorMaterialSource1), 1); 

    finally 
    Context.EndScene; 
    end; 

end; 

我也为多边形的MaxEntents创建了一个Polygon Helper:

TPolygonHelper = record helper for TPolygon 
    function MaxEntents: TRectF; 
end; 

{ TPolygonHelper } 

function TPolygonHelper.MaxEntents: TRectF; 
var 
    I: Integer; 
begin 
    for I := 0 to Length(Self) - 1 do 
    begin 
    Result.Left := Min(Result.Left, Self[I].X); 
    Result.Right := Max(Result.Right, Self[I].X); 
    Result.Top := Max(Result.Top, Self[I].Y); 
    Result.Bottom := Min(Result.Bottom, Self[I].Y); 
    end; 

end; 

希望这可以帮助别人,某处,某种程度上...

+0

瑞克,看到你的答案也是很棒的。 :) – ThN