Recent

Author Topic: 3D Dot Ball  (Read 611 times)

Gigatron

  • Sr. Member
  • ****
  • Posts: 282
  • Amiga Rulez !!
3D Dot Ball
« on: May 13, 2025, 09:06:56 pm »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes;
  10.  
  11. const
  12.   Rd = 230;//*2
  13.   Circ = 20;          // n circle
  14.   DPerCircle = 30;    // dot / circle 30 if circle
  15.   PerspectiveZ = 1000;
  16.  
  17. type
  18.   TDots = record
  19.     x, y, z: Single;
  20.     clr: TBGRAPixel;
  21.   end;
  22.  
  23.   { TForm1 }
  24.  
  25.   TForm1 = class(TForm)
  26.     BGRAVirtualScreen1: TBGRAVirtualScreen;
  27.     Timer1: TTimer;
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure Timer1Timer(Sender: TObject);
  30.     procedure BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  31.   private
  32.     FDots: array of TDots;
  33.     FX, FY: Single;
  34.   public
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. function ClampByte(Value: Integer): Byte;
  45. begin
  46.   if Value < 0 then
  47.     Result := 0
  48.   else if Value > 255 then
  49.     Result := 255
  50.   else
  51.     Result := Value;
  52. end;
  53.  
  54. { TForm1 }
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. var
  58.   lat, lon: Integer;
  59.   phi, theta: Single;
  60.   y, r: Single;
  61.   d: TDots;
  62. begin
  63.   SetLength(FDots, Circ * DPerCircle);
  64.   for lat := 0 to Circ-1 do
  65.   begin
  66.     phi := (lat / (Circ - 1)) * Pi;
  67.     y := Cos(phi) * Rd;
  68.     r := Sin(phi) * Rd+30; // premier cercle
  69.     for lon := 0 to DPerCircle - 1 do
  70.     begin
  71.       theta := (lon / DPerCircle) * Pi * 2;
  72.       d.x := Cos(theta) * r;
  73.       d.y := y;
  74.       d.z := Sin(theta) * r;
  75.       d.clr := BGRA(255, 255, 0); // blanc
  76.       FDots[lat * DPerCircle + lon] := d;
  77.     end;
  78.   end;
  79.   FX := 0;
  80.   FY := 0;
  81. end;
  82.  
  83. procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  84. var
  85.   dot: TDots;
  86.   x, y, z: Single;
  87.   rx, ry, rz: Single;
  88.   cosX, sinX, cosY, sinY: Single;
  89.   scale: Single;
  90.   projX, projY: Integer;
  91.   alpha: Byte;
  92.   sz: Single;
  93. begin
  94.   Bitmap.Fill(BGRABlack);
  95.   cosX := Cos(FX); sinX := Sin(FX);
  96.   cosY := Cos(FY); sinY := Sin(FY);
  97.  
  98.   for dot in FDots do
  99.   begin
  100.     // Rotation X
  101.     x := dot.x;
  102.     y := dot.y * cosX - dot.z * sinX;
  103.     z := dot.y * sinX + dot.z * cosX;
  104.  
  105.     // Rotation Y
  106.     rx := x * cosY - z * sinY;
  107.     rz := x * sinY + z * cosY;
  108.     ry := y;
  109.  
  110.     // Projection
  111.     if (PerspectiveZ + rz) <> 0 then
  112.     begin
  113.       scale := PerspectiveZ / (PerspectiveZ + rz);
  114.       projX := Round(Bitmap.Width / 2 + rx * scale);
  115.       projY := Round(Bitmap.Height / 2 + ry * scale);
  116.       sz := 2 * scale;
  117.       alpha := ClampByte(Round(255 * (rz + Rd) / ( Rd/2)));
  118.       Bitmap.FillEllipseAntialias(projX, projY, sz, sz, BGRA(255, 255, 255, alpha));// or alpha
  119.     end;
  120.   end;
  121. end;
  122.  
  123. procedure TForm1.Timer1Timer(Sender: TObject);
  124. begin
  125.   FX += 0.008;
  126.   FY += 0.032;
  127.   BGRAVirtualScreen1.RedrawBitmap;
  128. end;
  129.  
  130. end.
  131.  
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

Dzandaa

  • Sr. Member
  • ****
  • Posts: 452
  • From C# to Lazarus
Re: 3D Dot Ball
« Reply #1 on: May 15, 2025, 05:43:47 pm »
Regards,
Dzandaa

Gigatron

  • Sr. Member
  • ****
  • Posts: 282
  • Amiga Rulez !!
Re: 3D Dot Ball
« Reply #2 on: May 15, 2025, 10:15:48 pm »
Hi,

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 :) 
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: 3D Dot Ball
« Reply #3 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  :-\
Today is tomorrow's yesterday.

Gigatron

  • Sr. Member
  • ****
  • Posts: 282
  • Amiga Rulez !!
Re: 3D Dot Ball
« Reply #4 on: May 16, 2025, 12:37:46 am »
@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.
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

 

TinyPortal © 2005-2018