3D Dot Ball lazarus / 39-Graphics ---------------------------------------------------------------------- Gigatron · 2025-05-13 19:06 Ok , 3D Dot ball in perspective , don't know if it's correct, but i'am trying to understand perspective projection formula. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,  BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes; const  Rd = 230;//*2  Circ = 20;          // n circle  DPerCircle = 30;    // dot / circle 30 if circle  PerspectiveZ = 1000; type  TDots = record    x, y, z: Single;    clr: TBGRAPixel;  end;   { TForm1 }   TForm1 = class(TForm)    BGRAVirtualScreen1: TBGRAVirtualScreen;    Timer1: TTimer;    procedure FormCreate(Sender: TObject);    procedure Timer1Timer(Sender: TObject);    procedure BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);  private    FDots: array of TDots;    FX, FY: Single;  public  end; var  Form1: TForm1; implementation {$R *.lfm} function ClampByte(Value: Integer): Byte;begin  if Value < 0 then    Result := 0  else if Value > 255 then    Result := 255  else    Result := Value;end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  lat, lon: Integer;  phi, theta: Single;  y, r: Single;  d: TDots;begin  SetLength(FDots, Circ * DPerCircle);  for lat := 0 to Circ-1 do  begin    phi := (lat / (Circ - 1)) * Pi;    y := Cos(phi) * Rd;    r := Sin(phi) * Rd+30; // premier cercle    for lon := 0 to DPerCircle - 1 do    begin      theta := (lon / DPerCircle) * Pi * 2;      d.x := Cos(theta) * r;      d.y := y;      d.z := Sin(theta) * r;      d.clr := BGRA(255, 255, 0); // blanc      FDots[lat * DPerCircle + lon] := d;    end;  end;  FX := 0;  FY := 0;end; procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);var  dot: TDots;  x, y, z: Single;  rx, ry, rz: Single;  cosX, sinX, cosY, sinY: Single;  scale: Single;  projX, projY: Integer;  alpha: Byte;  sz: Single;begin  Bitmap.Fill(BGRABlack);  cosX := Cos(FX); sinX := Sin(FX);  cosY := Cos(FY); sinY := Sin(FY);   for dot in FDots do  begin    // Rotation X    x := dot.x;    y := dot.y * cosX - dot.z * sinX;    z := dot.y * sinX + dot.z * cosX;     // Rotation Y    rx := x * cosY - z * sinY;    rz := x * sinY + z * cosY;    ry := y;     // Projection    if (PerspectiveZ + rz) <> 0 then    begin      scale := PerspectiveZ / (PerspectiveZ + rz);      projX := Round(Bitmap.Width / 2 + rx * scale);      projY := Round(Bitmap.Height / 2 + ry * scale);      sz := 2 * scale;      alpha := ClampByte(Round(255 * (rz + Rd) / ( Rd/2)));      Bitmap.FillEllipseAntialias(projX, projY, sz, sz, BGRA(255, 255, 255, alpha));// or alpha    end;  end;end; procedure TForm1.Timer1Timer(Sender: TObject);begin  FX += 0.008;  FY += 0.032;  BGRAVirtualScreen1.RedrawBitmap;end; end. ---------------------------------------------------------------------- Dzandaa · 2025-05-15 15:43 Hi, I don't know if that can help: https://www.mauriciopoppe.com/notes/computer- graphics/viewing/projection-transform/ B-> ---------------------------------------------------------------------- Gigatron · 2025-05-15 20:15 Quote from: Dzandaa on May 15, 2025, 05:43:47 pmHi, I don't know if that can help: https://www.mauriciopoppe.com/notes/computer- graphics/viewing/projection-transform/ B-> @Dzandaa Thank you very much , i will study this 3D projection, at the moment x,y,z camera mat2 mat3  mat4 and 2drotation are easy for me, the rest are for later ---------------------------------------------------------------------- TRon · 2025-05-15 20:37 @gigatron: coding-train has a topic on drawing a 3d object onto a 2d screen here with a brief explanation on perspective. In that video the host uses a small trick for the perspective multiplication after the matrix multiplications/rotation. It is a very simple solution only applicable for the situation (so no camera and fov control). Flipcode had a very nice article on that topic, step-by-step explained in layman's terminology, but I seem unable to locate it ---------------------------------------------------------------------- Gigatron · 2025-05-15 22:37 Quote from: TRon on May 15, 2025, 10:37:50 pm@gigatron: coding-train has a topic on drawing a 3d object onto a 2d screen here with a brief explanation on perspective. In that video the host uses a small trick for the perspective multiplication after the matrix multiplications/rotation. It is a very simple solution only applicable for the situation (so no camera and fov control). Flipcode had a very nice article on that topic, step-by-step explained in layman's terminology, but I seem unable to locate it  Perfect explanation , thank you very much @Tron , i will try to experiment this in pascal code a bit.