Recent

Author Topic: Bouncing balls with Phong effect  (Read 2650 times)

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Bouncing balls with Phong effect
« on: April 18, 2021, 06:19:40 am »
Hello!

Here are bouncing balls with Phong effect.

https://github.com/rchastain/collisions

Adaptated (with the help of an expert) from a program found on the excellent website corpsman.de.

Regards.

Roland.
My projects are on Codeberg.

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: Bouncing balls with Phong effect
« Reply #1 on: April 18, 2021, 12:44:55 pm »
Nice.

Note that it doesn't show anything on MacOS. To make it compatible, you need to draw within the OnPaint event of the form. So for example, call Form1.Invalidate after you have rendered and do the final Draw in the OnPaint event.
Conscience is the debugger of the mind

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Re: Bouncing balls with Phong effect
« Reply #2 on: April 18, 2021, 04:03:48 pm »
Note that it doesn't show anything on MacOS. To make it compatible, you need to draw within the OnPaint event of the form. So for example, call Form1.Invalidate after you have rendered and do the final Draw in the OnPaint event.

Done, thank you.
My projects are on Codeberg.

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: Bouncing balls with Phong effect
« Reply #3 on: April 18, 2021, 06:43:34 pm »
Yep, that fixes the problem.

Does it work the same way after this change on your system?
Conscience is the debugger of the mind

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Re: Bouncing balls with Phong effect
« Reply #4 on: April 18, 2021, 07:05:51 pm »
Does it work the same way after this change on your system?

I have the impression that the movement is a little less fluid.
My projects are on Codeberg.

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: Bouncing balls with Phong effect
« Reply #5 on: April 18, 2021, 07:34:16 pm »
Hmm

What if you call Repaint instead of Invalidate?

Note that is doesn't really fix this problem on MacOS but it might on Windows.

Edit: something that seems to work, is to add a boolean WaitingForDisplay. After calling Invalidate, set this boolean to True. In the Idle event, do not call render when it is True. And in the FormPaint, set it to False.
« Last Edit: April 18, 2021, 07:56:47 pm by circular »
Conscience is the debugger of the mind

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Re: Bouncing balls with Phong effect
« Reply #6 on: April 18, 2021, 10:03:20 pm »
Hmm

What if you call Repaint instead of Invalidate?

Note that is doesn't really fix this problem on MacOS but it might on Windows.

Edit: something that seems to work, is to add a boolean WaitingForDisplay. After calling Invalidate, set this boolean to True. In the Idle event, do not call render when it is True. And in the FormPaint, set it to False.

I don't see a clear difference here (on Linux). Still less fast and smooth than the first version. (I did reset the code on GitHub to the initial state, so that everybody can try and compare.)
My projects are on Codeberg.

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Re: Bouncing balls with Phong effect
« Reply #7 on: December 26, 2025, 08:13:11 pm »
For information, the latest state of this project is here. I lost my first GitHub account.  :)
My projects are on Codeberg.

Boleeman

  • Hero Member
  • *****
  • Posts: 1158
Re: Bouncing balls with Phong effect
« Reply #8 on: December 27, 2025, 02:31:03 am »
Nice Phong ball bouncing effects Roland.
Corpsman is also very talented.

For random colors I placed in unit ball.

Code: Pascal  [Select][+][-]
  1.  function BrightColor: TBGRAPixel;
  2. begin
  3.   Result := BGRA(180 + Random(76), 180 + Random(76), 180 + Random(76), 255);
  4. end;
  5.  
  6. function VividColor: TBGRAPixel;
  7. var
  8.   r, g, b: Byte;
  9.   idx: Integer;
  10. begin
  11.   r := Random(256);
  12.   g := Random(256);
  13.   b := Random(256);
  14.  
  15.   idx := Random(3);
  16.   case idx of
  17.     0: r := 255;
  18.     1: g := 255;
  19.     2: b := 255;
  20.   end;
  21.  
  22.   Result := BGRA(r, g, b, 255);
  23. end;
  24.  
  25. function HSVtoBGRA(H, S, V: Single): TBGRAPixel;
  26. var
  27.   c, x, m: Single;
  28.   r, g, b: Single;
  29.   hi: Integer;
  30. begin
  31.   c := V * S;
  32.   hi := Floor(H / 60) mod 6;
  33.   x := c * (1 - Abs((H / 60) mod 2 - 1));
  34.  
  35.   case hi of
  36.     0:
  37.       begin
  38.         r := c; g := x; b := 0;
  39.       end;
  40.     1:
  41.       begin
  42.         r := x; g := c; b := 0;
  43.       end;
  44.     2:
  45.       begin
  46.         r := 0; g := c; b := x;
  47.       end;
  48.     3:
  49.       begin
  50.         r := 0; g := x; b := c;
  51.       end;
  52.     4:
  53.       begin
  54.         r := x; g := 0; b := c;
  55.       end;
  56.     5:
  57.       begin
  58.         r := c; g := 0; b := x;
  59.       end;
  60.   end;
  61.  
  62.   m := V - c;
  63.   Result := BGRA(
  64.     Round((r + m) * 255),
  65.     Round((g + m) * 255),
  66.     Round((b + m) * 255),
  67.     255
  68.   );
  69. end;
  70.  
  71. function BrightRainbowColor: TBGRAPixel;
  72. begin
  73.   Result := HSVtoBGRA(Random(360), 1.0, 1.0);
  74. end;
  75.  
  76. procedure TBall.MakeBitmap;
  77. var
  78.   LMap: TBGRABitmap;
  79.   LPhong: TPhongShading;
  80. begin
  81.   LMap := CreateSpherePreciseMap(Round(FRadius * 2), Round(FRadius * 2));
  82.   FImage := TBGRABitmap.Create(Round(FRadius * 2), Round(FRadius * 2));
  83.   LPhong := TPhongShading.Create;
  84.   //LPhong.Draw(FImage, LMap, Round(FRadius), 0, 0, BGRA(0, 0, 255, 255));
  85.   //LPhong.Draw(FImage, LMap, Round(FRadius), 0, 0, BrightColor);
  86.   //LPhong.Draw(FImage, LMap, Round(FRadius), 0, 0, BrightRainbowColor);
  87.   LPhong.Draw(FImage, LMap, Round(FRadius), 0, 0, VividColor);
  88.   LPhong.Free;
  89.   LMap.Free;
  90. end;

Roland57

  • Hero Member
  • *****
  • Posts: 605
    • msegui.net
Re: Bouncing balls with Phong effect
« Reply #9 on: December 27, 2025, 09:05:31 am »
@Boleeman

Very nice, thank you. I pushed your modifications.
My projects are on Codeberg.

 

TinyPortal © 2005-2018