Lazarus

Programming => Graphics => Graphics and Multimedia => BGRABitmap and LazPaint => Topic started by: zxandris on December 17, 2024, 05:31:16 pm

Title: Trying to create a shaded sphere in Bgra
Post by: zxandris on December 17, 2024, 05:31:16 pm
Hey there from an example on the wiki I'm trying to create a sphere that's generated by settings given on a form.  I want it to be nicely shaded with the light pointing in a way that makes it look faux 3D and generally rounded.  Currently I have the below. I'm hoping someone can help me improve it. It looks a little flat, the light isn't very...rounded if that makes sense.

Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGPerlin.GenerateSphere;
  2. var
  3.   Phong: TPhongShading;
  4.   Light : TPoint;
  5. begin
  6.     if not assigned(bgraView) then BgraView := TBGRABitmap.Create(seWidth.value, seHeight.value, BGRAPixelTransparent)
  7.     else BgraView.SetSize(seWidth.value, seHeight.value);
  8.     // Clear background
  9.     BgraView.Rectangle(0, 0, seWidth.value, seHeight.value, BGRAPixelTransparent, BGRAPixelTransparent);
  10.     phong := TPhongShading.Create;
  11.     try
  12.         phong.LightPositionZ := 150;
  13.         phong.SpecularIndex := 20;
  14.         phong.AmbientFactor := 0.4;
  15.         phong.LightSourceIntensity := 250;
  16.         phong.LightSourceDistanceTerm := (seWidth.value div 2);
  17.         Light.x := (seWidth.value div 2);
  18.         Light.y := (seHeight.value div 2);
  19.         phong.LightPosition := Light;
  20.         phong.DrawSphere(BgraView, rect(0, 0, seWidth.value, seHeight.value), (seWidth.value div 2), ColorToBgra(ColorToRGB(cb01.ButtonColor)));
  21.     finally
  22.        Phong.free;
  23.     end;
  24.     rst.x := bgraView.width;
  25.     rst.y := BgraView.Height;
  26. end;
  27.  

Any help given is appreciated :)
Title: Re: Trying to create a shaded sphere in Bgra
Post by: Dzandaa on December 17, 2024, 06:18:11 pm
Hi,
@Zxandris:

Did you read the BGRA Tutorial 9?

https://wiki.freepascal.org/BGRABitmap_tutorial_9 (https://wiki.freepascal.org/BGRABitmap_tutorial_9)

B->
Title: Re: Trying to create a shaded sphere in Bgra
Post by: zxandris on December 17, 2024, 07:08:26 pm
Yes, that's from where I converted my, (probably wrong) procedure from.  But it does somewhat work (my code) but it doesn't look as good as theirs.  I think because they are working with properly known values and I'm going from input that can be just about anything.  I think what I'm mostly trying to do is take a sphere of intermediate size, and add the light in the center of bottom right.  Does that make sense?

Hi,
@Zxandris:

Did you read the BGRA Tutorial 9?

https://wiki.freepascal.org/BGRABitmap_tutorial_9 (https://wiki.freepascal.org/BGRABitmap_tutorial_9)

B->
Title: Re: Trying to create a shaded sphere in Bgra
Post by: zxandris on December 17, 2024, 07:10:23 pm
I think the biggest issue from my experiments is the cone of light is tiddly and doesn't look at all right.  I don't actually know which value to change to correct that.  Does anyone? 

Thanks

CJ
Title: Re: Trying to create a shaded sphere in Bgra
Post by: Boleeman on December 18, 2024, 07:42:40 am
I too was trying to achieve the perfect round sphere Phong Effect.
Not exactly sure how to achieve the perfect spherical effect.
Title: Re: Trying to create a shaded sphere in Bgra
Post by: zxandris on December 18, 2024, 10:45:29 am
Thanks for the notes, it's an issue, I think I'm going to create a form with a load of sliders for the settings and fiddle lol until something looks okay and try that on a few different settings.  I think really there needs to be a bloom not so much a cone but I'm really not sure right now.

Hopefully the aforementioned gentlemen will weigh in :)

CJ

I too was trying to achieve the perfect round sphere Phong Effect. Not exactly sure how to achieve the perfect spherical effect. Circular and Josh would probably know !

Meanwhile, here are some 3D-ish bgrabmp BmpScanner examples
(i had to compress the png so it lost quality but the real life TBgrabmp renderings are superb)

and

The TRon and Gigatron Demo Scene Coloured Blitter Objects (containing 3D animated Phong shaded spheres).
Title: Re: Trying to create a shaded sphere in Bgra
Post by: zxandris on December 18, 2024, 10:49:23 am
Actually isn't there some 3D handling in Bgra, perhaps that would be the source of the solution.  I will look into that, but if anyone has any experience of say creating a primitive then texturing it with a light source that would be marvellous :)
Title: Re: Trying to create a shaded sphere in Bgra
Post by: bobby100 on December 18, 2024, 04:55:17 pm
Do you have BGRA Controls installed? In the Test directory you can find test_bcdlea, and inside this directory you have the ThemeBuilder. Compile it and you can test the light parameters. Take a look at the source of the components and see how to implement all these parameters.
Title: Re: Trying to create a shaded sphere in Bgra
Post by: circular on December 21, 2024, 01:39:17 pm
Hi! Can you provide a screenshot so that we can appreciate how it looks, this can give us more insight.  :)
Title: Re: Trying to create a shaded sphere in Bgra
Post by: zxandris on December 21, 2024, 03:50:37 pm
The attached is what I have so far, and as you can see it doesn't look at all sphere like.  Ignore the other controls on the dialog, that's actually for something else.  This is just where I was experimenting lol.

This is the used code,
Code: Pascal  [Select][+][-]
  1. procedure TfrmDLGPerlin.GenerateSphere;
  2. var
  3.   Phong: TPhongShading;
  4.   Light : TPoint;
  5. begin
  6.     if not assigned(bgraView) then BgraView := TBGRABitmap.Create(seWidth.value, seHeight.value, BGRAPixelTransparent)
  7.     else BgraView.SetSize(seWidth.value, seHeight.value);
  8.     // Clear background
  9.     BgraView.Rectangle(0, 0, seWidth.value, seHeight.value, BGRAPixelTransparent, BGRAPixelTransparent);
  10.     phong := TPhongShading.Create;
  11.     try
  12.         phong.LightPositionZ := 150;
  13.         phong.SpecularIndex := 20;
  14.         phong.AmbientFactor := 0.4;
  15.         phong.LightSourceIntensity := 250;
  16.         phong.LightSourceDistanceTerm := 150;
  17.         Light.x := 150;
  18.         Light.y := 150;
  19.         phong.LightPosition := Light;
  20.         phong.DrawSphere(BgraView, rect(0, 0, seWidth.value, seHeight.value), (seWidth.value div 2), ColorToBgra(ColorToRGB(cb01.ButtonColor)));
  21.     finally
  22.        Phong.free;
  23.     end;
  24.     rst.x := bgraView.width;
  25.     rst.y := BgraView.Height;
  26. end;
  27.  

Hi! Can you provide a screenshot so that we can appreciate how it looks, this can give us more insight.  :)

Title: Re: Trying to create a shaded sphere in Bgra
Post by: circular on December 21, 2024, 04:08:58 pm
Thanks for the screenshot. The light might be too close, as if it were under the surface of the sphere. Try bigger values than 150, it should be bigger than seWidth.value div 2. Light position X/Y also could be zero or negative for example. Then if the light effect becomes more standard, you can try reducing the specular index.
TinyPortal © 2005-2018