### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Newton's Fractal Curve: Rendering to OpenGL Win 10 Thanx iLya2IK  (Read 1215 times)

#### Boleeman

• Hero Member
• Posts: 652
##### Newton's Fractal Curve: Rendering to OpenGL Win 10 Thanx iLya2IK
« on: August 02, 2024, 12:52:32 pm »
Here is a Newton's Fractal Curve Maker on a TPaintbox.

It's a bit slow on the rendering. I wonder how it could be rendered much more quickly? Possibly (Bgrabmp)

When colour changing with the TColorbutton it refreshes.
When changing the 2 TSpinedits please use the refresh button after making changes.

I found the Mods in the curve by experimenting.

I wonder if anyone knows of any other interesting ways to color it?

The last reddish attached png is sort of like a "Lost in a Universe type pic."
« Last Edit: August 09, 2024, 02:40:50 pm by Boleeman »

#### Boleeman

• Hero Member
• Posts: 652
##### Re: Newton's Fractal Curve: Any better Colouring Method?
« Reply #1 on: August 02, 2024, 01:00:51 pm »
I used a different way to colour and got some colour splitting, but it was not consistently splitting up into many colours for different TColorbutton colour choices.

Code: Pascal  [Select][+][-]
1. procedure TForm1.PaintBox1Paint(Sender: TObject);
2. const
3.   max = 1e6;
4.   min = 1e-6;
5. var
6.   z, t, d: Complex;
7.   p: Real;
8.   x, y, n, iter: Integer;
9.   mx, my: Integer;
10.   baseColor: TColor;
11.   r, g, b: Byte;
12.   col: TColor;
13. begin
14.   mx := PaintBox1.Width div 2;
15.   my := PaintBox1.Height div 2;
16.
17.   baseColor := ColorButton1.ButtonColor;
18.   r := Red(baseColor);
19.   g := Green(baseColor);
20.   b := Blue(baseColor);
21.
22.   iter := seRecursion.Value;
23.
24.   for y := -my to my do
25.     for x := -mx to mx do
26.     begin
27.       n := 0;
28.       z.x := x * 0.005;
29.       z.y := y * 0.005;
30.       d := z;
31.       while (sqr(z.x) + sqr(z.y) < max) and (sqr(d.x) + sqr(d.y) > min) and (n < iter) do
32.       begin
33.         t := z;
34.         {z^3 - 1}
35.         p := sqr(sqr(t.x) + sqr(t.y));
36.         if p = 0 then
37.           break;
38.         z.x := 2 / 3 * t.x + (sqr(t.x) - sqr(t.y)) / (seCoeffPonZX.value * p);
39.         z.y := 2 / 3 * t.y * (1 - t.x / p);
40.         d.x := abs(t.x - z.x);
41.         d.y := abs(t.y - z.y);
42.         Inc(n);
43.       end;
44.
45.       col := RGBToColor((r * n) mod 255, (g * n) mod 255, (b * n) mod 255);
46.       if (r * n mod 255 = 0) and (g * n mod 255 = 0) and (b * n mod 255 = 0) then
47.         col := baseColor;
48.       PaintBox1.Canvas.Pixels[mx + x, my + y] := col;
49.     end;
50. end;
« Last Edit: August 02, 2024, 01:10:07 pm by Boleeman »

#### iLya2IK

• New Member
• Posts: 45
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #2 on: August 06, 2024, 11:01:25 am »
I love fractals. I tried to do something similar using opengl (glsl)

#### Boleeman

• Hero Member
• Posts: 652
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #3 on: August 06, 2024, 01:17:48 pm »
Thanks iLya2IK for your OpenGL fractal code example.

I tried it, but get an error as the form loads up.

GL Framebuffer error 36062

I tried compiling on an Intel and on an AMD CPU laptop (each running Windows 10).
Perhaps I have the older version of OpenGL or need to compile on Linux ? Not sure.

« Last Edit: August 07, 2024, 08:16:04 am by Boleeman »

#### circular

• Hero Member
• Posts: 4342
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #4 on: August 06, 2024, 01:43:20 pm »
Indeed, using OpenGL could help. If each pixel can be translated into a simple algorithm in C shader language, then this can be implemented using TBGLFullCanvasShader class.

This is in latest release:
https://github.com/bgrabitmap/bgrabitmap/releases/tag/v11.6.3

Here is an example made by Gigatron:
https://forum.lazarus.freepascal.org/index.php/topic,67755.msg522793.html#msg522793

If you have any question, I'll be happy to assist.

Warm regards
Conscience is the debugger of the mind

#### iLya2IK

• New Member
• Posts: 45
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #5 on: August 08, 2024, 07:25:58 am »
Thanks iLya2IK for your OpenGL fractal code example.

I tried it, but get an error as the form loads up.

GL Framebuffer error 36062

I tried compiling on an Intel and on an AMD CPU laptop (each running Windows 10).
Perhaps I have the older version of OpenGL or need to compile on Linux ? Not sure.

Thank you for the opportunity to play with such a wonderful thing as fractal  Unfortunately, this is my mistake. If you want to run the program, try changing the line
Code: Pascal  [Select][+][-]
1. e := glCheckFramebufferStatus(fb);
to
Code: Pascal  [Select][+][-]
1. e := glCheckFramebufferStatus(GL_FRAMEBUFFER);

The drivers on my computer allow such a mistake, but on your device they follow the specification more precisely.

#### Boleeman

• Hero Member
• Posts: 652
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #6 on: August 09, 2024, 11:17:15 am »
Hi iLya2IK

I tried that change to:

//e := glCheckFramebufferStatus(fb);
e := glCheckFramebufferStatus(GL_FRAMEBUFFER);

GL_Framebuffer_Error 36053
Did some Googling and that code is for The framebuffer is complete
I thing this might have something to do with needing to clear the color or depth
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
So perhaps need to clear the framebuffer somewhere? I don't know where?

Sorry for the hassles. Not sure how to correct this error for Windows 10 OS?
« Last Edit: August 09, 2024, 12:42:54 pm by Boleeman »

#### iLya2IK

• New Member
• Posts: 45
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #7 on: August 09, 2024, 01:43:16 pm »
Hi iLya2IK

I tried that change to:

//e := glCheckFramebufferStatus(fb);
e := glCheckFramebufferStatus(GL_FRAMEBUFFER);

GL_Framebuffer_Error 36053
Did some Googling and that code is for The framebuffer is complete
I thing this might have something to do with needing to clear the color or depth
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
So perhaps need to clear the framebuffer somewhere? I don't know where?

Sorry for the hassles. Not sure how to correct this error for Windows 10 OS?

Hello. That's ok  - just add this code to the switch-case statement:

Code: Pascal  [Select][+][-]
1. procedure CheckFBStatus();
2. var
3.   e : glenum;
4. begin
5.   e := glCheckFramebufferStatus(GL_FRAMEBUFFER);
6.   case e of
7.   GL_FRAMEBUFFER_COMPLETE: begin // framebuffer ok
8.     end;
9.

#### Boleeman

• Hero Member
• Posts: 652
##### Re: Newton's Fractal Curve: Speed Rendering Issue & Any better Colouring Method?
« Reply #8 on: August 09, 2024, 02:39:25 pm »
And eventually ... I SEE THE CURVES

Thanks  iLya2IK for that extra help. Much appreciated.

That OpenGL rendering on Win 10 OS looks nicely shaded. Groovey.

Gave the TSpinedits larger variations.
« Last Edit: August 09, 2024, 03:27:31 pm by Boleeman »