2016-01-13 229 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