Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Tutorial for complex meshes for 3d?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Feb 1, 2016 5:18 PM Last Post By: Gerhard Eichhorn
Gerhard Eichhorn

Posts: 13
Registered: 9/20/02
Tutorial for complex meshes for 3d?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 31, 2016 10:30 AM
Is there anything like an adaptation of
http://edn.embarcadero.com/article/42007

for Delphe xe7?

Or is there any other documentation?
Gilbert Padilla

Posts: 315
Registered: 3/8/04
Re: Tutorial for complex meshes for 3d?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 31, 2016 1:29 PM   in response to: Gerhard Eichhorn in response to: Gerhard Eichhorn

for Delphe xe7?

Or is there any other documentation?

Following this post
http://stackoverflow.com/questions/26022438/how-to-modify-the-code-written-in-old-firemonkey-3d

I did replace the some code and add a TextureMateriqalSource component
to make it work in XE6, here is the whole unit.

unit Unit1;
 
interface
 
uses
   System.SysUtils, System.Types, System.Variants, System.UITypes, 
System.UIConsts,
  System.Classes, FMX.Types, FMX.Graphics, FMX.Dialogs, FMX.Types3D, 
FMX.Forms,
  FMX.Forms3D, FMX.Controls3D, System.Math.Vectors, FMX.Objects3D,
  FMX.MaterialSources, FMX.Layers3D, FMX.Controls, FMX.StdCtrls;
 
 
type
  TForm1 = class(TForm3D)
    Camera1: TCamera;
    Light1: TLight;
    Layer3D1: TLayer3D;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Layout3D1: TLayout3D;
    GridXY: TGrid3D;
    GridXZ: TGrid3D;
    GridYZ: TGrid3D;
    Mesh1: TMesh;
    Text3D1: TText3D;
    TextureMaterialSource1: TTextureMaterialSource;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Single);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: 
Single);
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure GenerateMesh(Func : Integer);
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.fmx}
 
uses Math;
 
var
  Down: TPointF;
 
var
  MinF : Double = 2000;
  MaxF : Double = -2000;
 
function f(Func : Integer; x,z : Double) : Double;
var
  temp : Double;
begin
  temp := x*x+z*z;
  if temp < Epsilon then
    temp := Epsilon;
 
  case Func of
    1 : Result := -2000*Sin(temp/180*Pi)/temp;
    2 : Result := -5*Exp(Sin(x/10)-Cos(z/10));
    3 : Result := -5*Exp(Sin(x/10)+Cos(z/10));
    4 : Result := -5*Exp(Sin(Sqrt(Abs(x)))+Cos(Sqrt(Abs(z))));
    5 : Result := -7*Exp(ArcTan2(x*x,z*z));
  end;
 
  if Result < MinF then
    MinF := Result;
  if Result > MaxF then
    MaxF := Result;
end;
 
procedure TForm1.GenerateMesh(Func : Integer);
const
  MaxX = 30;
  MaxZ = 30;
var
  u, v : Double;
  P : array [0..3] of TPoint3D;
  d : Double;
  NP, NI : Integer;
  BMP : TBitmap;
  k : Integer;
  BD: TBitmapData;
 
 
begin
  Mesh1.Data.Clear;
 
  d := 0.5;
 
  NP := 0;
  NI := 0;
 
  // We have to set these up front. The buffers are cleared every time 
Length is set.
  Mesh1.Data.VertexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*4;
  Mesh1.Data.IndexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*6;
 
  BMP := TBitmap.Create(1,360);
  BMP.Map(TMapAccess.ReadWrite, BD);
 
  for k := 0 to 359 do
   //BMP.Pixels[0,k] := CorrectColor(HSLtoRGB(k/360,0.75,0.5));
 
    BD.SetPixel(0, k, CorrectColor(HSLtoRGB(k / 360, 0.75, 0.5)));
     BMP.Unmap(BD);
 
  u := -MaxX;
 
  while u < MaxX do begin
    v := -MaxZ;
    while v < MaxZ do begin
      // Set up the points in the XZ plane
      P[0].x := u;
      P[0].z := v;
      P[1].x := u+d;
      P[1].z := v;
      P[2].x := u+d;
      P[2].z := v+d;
      P[3].x := u;
      P[3].z := v+d;
 
      // Calculate the corresponding function values for Y = f(X,Z)
      P[0].y := f(Func,P[0].x,P[0].z);
      P[1].y := f(Func,P[1].x,P[1].z);
      P[2].y := f(Func,P[2].x,P[2].z);
      P[3].y := f(Func,P[3].x,P[3].z);
 
      with Mesh1.Data do begin
        // Set the points
        with VertexBuffer do begin
          Vertices[NP+0] := P[0];
          Vertices[NP+1] := P[1];
          Vertices[NP+2] := P[2];
          Vertices[NP+3] := P[3];
        end;
 
        // Map the colors
        with VertexBuffer do begin
          TexCoord0[NP+0] := PointF(0,(P[0].y+35)/45);
          TexCoord0[NP+1] := PointF(0,(P[1].y+35)/45);
          TexCoord0[NP+2] := PointF(0,(P[2].y+35)/45);
          TexCoord0[NP+3] := PointF(0,(P[3].y+35)/45);
        end;
 
        // Map the triangles
        IndexBuffer[NI+0] := NP+1;
        IndexBuffer[NI+1] := NP+2;
        IndexBuffer[NI+2] := NP+3;
        IndexBuffer[NI+3] := NP+3;
        IndexBuffer[NI+4] := NP+0;
        IndexBuffer[NI+5] := NP+1;
      end;
 
      NP := NP+4;
      NI := NI+6;
 
      v := v+d;
    end;
    u := u+d;
  end;
   TextureMaterialSource1.Texture := BMP;
   Mesh1.MaterialSource := TextureMaterialSource1;
  //Mesh1.Material.Texture := BMP;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Text3D1.Text := Button1.Text;
  GenerateMesh(1);
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  Text3D1.Text := Button2.Text;
  GenerateMesh(2);
end;
 
procedure TForm1.Button3Click(Sender: TObject);
begin
  Text3D1.Text := Button3.Text;
  GenerateMesh(3);
end;
 
procedure TForm1.Button4Click(Sender: TObject);
begin
  Text3D1.Text := Button4.Text;
  GenerateMesh(4);
end;
 
procedure TForm1.Button5Click(Sender: TObject);
begin
  Text3D1.Text := Button5.Text;
  GenerateMesh(5);
end;
 
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Single);
begin
  Down := PointF(X, Y);
end;
 
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Single);
begin
  if (ssLeft in Shift) then
  begin
    { rotate Z }
    Layout3D1.RotationAngle.Z := Layout3D1.RotationAngle.Z + ((X - Down.X) * 
0.3);
    { rotate X }
    Layout3D1.RotationAngle.Y := Layout3D1.RotationAngle.Y + ((Y - Down.Y) * 
0.3);
    Down := PointF(X, Y);
  end;
end;
 
procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; var Handled: Boolean);
begin
// Camera1.Position.Vector := Vector3DAdd(Camera1.Position.Vector, 
Vector3DScale(Vector3D(0, 0, 1), (WheelDelta / 120) * 0.3));
  Camera1.Position.Vector := Camera1.Position.Vector + Vector3D(0, 0, 1) * 
((WheelDelta / 120) * 0.3);
  end;
 
end.
 
Gerhard Eichhorn

Posts: 13
Registered: 9/20/02
Re: Tutorial for complex meshes for 3d?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 1, 2016 5:18 PM   in response to: Gerhard Eichhorn in response to: Gerhard Eichhorn
Thank you!
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02