PDA

View Full Version : Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set



sblank
01-03-2010, 06:51
Ok...here is my first pathetic effort...

I did use the iComplex module, so that's a start!

If you run the script, be patient. It takes a couple of minutes for the set to appear, depending on your machine.

Improvements and enhancements are MUCH MUCH appreciated!

Cheers... and I'm learning,

Stan

Petr Schreiber
01-03-2010, 10:21
Hi Stan,

very nice example.
Here is slightly modified version:


' Example: Section 7.7 (page 222-224), Explorations with the Mandlebrot Set

' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming

' Converted by Stan Blank, this version is slightly downgraded in detail by Petr Schreiber
' Using the Predator-Prey framework by Michael Clease
' Last modified: February 28, 2010

' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"

' insert Eros Olmi's new iComplex module
Uses "iComplex"


Function TBMain()

' Handle for our window
Local hWnd As DWord
Local Width As Long
Local Height As Long
Local n As Long
Local x,y, zz As Double

' Declare iComplex variables
Local z, c As tComplex

'# Initial values of width And height
width = 400
height = 400
'
' Create and show window
hWnd = TBGL_CreateWindowEx("Mandelbrot Set", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX Or %TBGL_WS_DONTSIZE)
TBGL_ShowWindow

' Set Foreground from default White to Green
TBGL_Color(76,152,51)
' Set background from default black to white
TBGL_BackColor(0,0,0)
' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -2, -1.5, 1, 1.5 )

TBGL_PointSize 1

' Resets status of all keys
TBGL_ResetKeyState()

Local percent As Double

TBGL_NewList 1

' Choose each pixel in the graphics window
TBGL_BeginPoly(%GL_POINTS)

For x = -2 To 1 Step 0.0075
For y = -1.5 To 1.5 Step 0.0075

' Set complex points
c = iComplex_Set(x,y)
z = iComplex_Set(x,y)
n = 0

For n = 1 To 10
' Equation is z = z*z + c
z = iComplex_Mul(z,z)
z = iComplex_Add(z,c)

' Distance from origin to complex point
zz = iComplex_Abs(z)

' if distance is > 2.0, then point is NOT in the M-Set
' so plot it... the M-Set will be black
If zz > 2.0 Then
TBGL_Color zz*100,0,0
TBGL_Vertex(x,y)
End If
Next
Next

' ESCAPE key to exit application + status
percent = (x-(-2))/3*100
TBGL_SetWindowTitle(hWnd, Format$(percent, "#.00")+"%")
If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit For

Next
TBGL_EndPoly

TBGL_EndList

TBGL_SetWindowTitle(hWnd, "Behold - the Mandlebrot! :)")

' Main loop
While TBGL_IsWindow(hWnd)

TBGL_ClearFrame ' glClear(GL_COLOR_BUFFER_BIT)
TBGL_CallList 1
TBGL_DrawFrame

If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow
End Function

'Python code omitted due to length... see text.


With new release of thinBASIC, there will be possibility to avoid usage of display lists, resulting in slightly faster render, and much lower memory consumption.

Regarding processing speed ... I think Charles could have idea how.

The modified example above adds more color detail, and possibility to abort calculation to see at least part of the image.


Petr

Charles Pegge
01-03-2010, 21:50
Stan,

I'm using your coefficients but calculating with plain floats, and I've got one working with Petr's test TBGL (to be released with the next ThinBasic beta.)

The fractal is rendered to a texture which is then mapped to a vertex array instead of an Opengl display list.
Using Oxygen compiled code, it renders in a a fraction of second.

Here is the code for generating the texture. Anybody want to try it in a TBGL prog :)

Charles.




uses "TBGL","Oxygen"
'.......
'
'
' test texture

Dim Texture(65536*4) As tbgl_tRGBA
'--------------------------------------------
'OXYGEN COMPILE & EXEC TEXTURE GENERATION
'--------------------------------------------
dim src as string
src="
basic
dim xman,yman,xcor,ycor, xtemp,xstp,ystp,lmt as double
dim i,p,maxit, xpix, ypix, color as long
dim tex at p as long : p=#Texture
'
xstp=3/512
ystp=3/512
lmt=4
'
ycor=-1.5
for ypix=1 to 512
xcor=-2
for xpix=1 to 512
xman=0
yman=0
i=0
maxit=50
do
xtemp=xman*xman + yman*yman
if xtemp>lmt then exit do
if i>=maxit then exit do
xtemp = xman*xman - yman*yman + xcor 'REAL PART
yman = 2*xman*yman + ycor 'IMAGINARY PART
xman = xtemp
i+=1
end do
if i>=maxit then color = 200*65536 else color = i*40
tex=color : p+=4
xcor+=xstp
next
ycor+=ystp
next

"
'msgbox 0,o2_prep src
o2_asmo src
if len(o2_error) then
msgbox 0,o2_error
stop
else
o2_exec
end if
'--------------------------------------------

Local TexString As String = Peek$(VarPtr(Texture(1)), 65536*4*SizeOf(tbgl_tRGBA))

kryton9
01-03-2010, 22:17
@Stan That is great that you are able to do something so complex already Stan with thinBasic. The teacher is a good student too, we see :)

@PetrYou are always there to help us learn how to use your modules in better ways. Another great example, thanks. It looks beautiful.

@CharlesWhen I was rendering Miller's Madness in Super Function Plotter, I thought... I bet if Charles wrote parts of this in Oxygen it would render very fast. So in the meantime I cheated. I cut the step size in the for loop and used line strips instead of points to get a faster render. If you have the time, can you convert plotFunc and plotFunc2 to oxygen, then super plotter function can be named super function plotter turbo! You can find the source here:http://community.thinbasic.com/index.php?topic=3261.msg24445

Charles Pegge
02-03-2010, 00:00
Hi Kent, If we precompile using the the function strings as source, it should speed things up quite a lot.



Here is a quicky Example using the Mandelbrot texture above. I have borrowed one of Petr's standard TBGL examples for this.

Charles Pegge
02-03-2010, 07:58
Instead of using the iteration tally to determine colour, there are other possibilities to produce a wide range of imagery from the Mandelbrot algorithm.

kryton9
02-03-2010, 09:42
Charles those are super fast and cool! Thanks just in time. I working on plotter program 2 which is a lot more advanced and hopefully studying your code I can make it render really fast. Thanks!

sblank
02-03-2010, 16:34
Hi Petr, Charles, and Kent...

Very, very nice!

Thanks Petr... and another "thanks" to Charles for the speedup. I knew that I would learn something from the experts. And Kent, your function plotter just keeps getting better!

Charles, would the new iComplex library benefit from Oxygen? The reason I'm asking is that one of the nice things about having a complex number library is to ease the mathematical manipulation of complex numbers a bit. The Mandelbrot Set is fairly easy to convert to floats (which you have done) as long as we use z = z^2 + c, but when we try z = z^n + c (where n > 2) or some of the transcendental functions, it becomes more difficult. Calculating the classic Newton's Method in the complex plane using z = z - (z^3 - 1)/(3*z^2) is much more convoluted using floats.

Just curious... and I am REALLY impressed with your rotating M-Set! Just beautiful!

Cheers,

Stan

ErosOlmi
02-03-2010, 16:48
Stan,

if possible I would like to know how much slower is thinBasic compared to Python in Mandelbrot example.
As soon as I will release next version (that will come with about other 20 new function working on complex numbers) I would like to work a bit on execution speed optimization.

Thanks a lot
Eros

sblank
02-03-2010, 18:17
Hi Eros,

Using Petr's version above and trying to match the algorithm and coloration exactly, Python takes just under 5 seconds on my computer. ThinBasic using iComplex takes about 12 seconds.

One big difference in rendering is that Python renders line by line (at least in my code) so that you can see the M-Set being built. Petr added a nice % progress indicator in the caption bar, but it would be nice to see the "real-time" rendering take place. The other issue... and it may not be fixable at this point... is that in Python you can simply type: z = z**2 + c for the equation and as long as z and c are complex number types, the math manipulation is handled correctly. This is probably an operator overloading "thing" that may not be possible in ThinBasic?

I'm thinking that when I use an equation such as: z = z - (z^3 - 1)/(3*z^2) it will be a bit more complicated to use the correct steps in your iComplex library... I'll work on that!

Please don't view this as a complaint... it isn't! I'm thrilled to see the current library and the upcoming modifications and additions! Interesting times lay ahead, I think!

Cheers... and thanks,

Stan



Stan,

if possible I would like to know how much slower is thinBasic compared to Python in Mandelbrot example.
As soon as I will release next version (that will come with about other 20 new function working on complex numbers) I would like to work a bit on execution speed optimization.

Thanks a lot
Eros

ErosOlmi
02-03-2010, 18:27
Thanks a lot Stan.

I'm always willing to try to optimize thinBasic so whatever indication even critics (I know you are not criticizing but just to give your the idea) are for me a point to take into consideration for understanding where I can improve.

Yes, using current iComplex module can be quite difficult when you have such equations.
Operators overloading is something I cannot give you at the moment. I will think about it for future releases but for the moment I cannot promise anything.
Maybe I can get some more speed in iComplex math, actually it was built quite quickly and there is a lot of memory moving (every complex is byte moved into another one when operations take place) so I think I will be able to get more speed for future thinBasic versions.

Thanks again
Eros

Petr Schreiber
02-03-2010, 18:31
Hi Stan,

rendering in way the lines would be visible during computation is possible.

It will become very easy and ellegant in next release of ThinBASIC, where I introduced the promised optimized way to render big amounts of primitives.

Stay tuned! :)


Petr

sblank
02-03-2010, 18:56
Thanks Eros!

No problem with overloading... I'll just have to think a bit on how to represent a more complicated equation. I'm certain is can be done, probably using intermediate variables... I'll just have to experiment.

The new added iComplex functions sound great and any optimizing is welcome, certainly!

Petr, the new TBGL features have my interest! Any news on when the release will be?

Cheers,

Stan



Thanks a lot Stan.

I'm always willing to try to optimize thinBasic so whatever indication even critics (I know you are not criticizing but just to give your the idea) are for me a point to take into consideration for understanding where I can improve.

Yes, using current iComplex module can be quite difficult when you have such equations.
Operators overloading is something I cannot give you at the moment. I will think about it for future releases but for the moment I cannot promise anything.
Maybe I can get some more speed in iComplex math, actually it was built quite quickly and there is a lot of memory moving (every complex is byte moved into another one when operations take place) so I think I will be able to get more speed for future thinBasic versions.

Thanks again
Eros

Charles Pegge
02-03-2010, 20:38
Hi Stan,

About a year ago I introduced operator sets into Oxygen which could support all kinds of exotic operators and variables. I wrote a simple set of complex number operations to test it out, but I have not done anything with it since. my complex operator set contains +-*/^=. I think of complex numbers as an arcane form of Cartesian geometry so I am not accustomed to using them per se. But I'll try them out with the Mandelbrot and see whether they are well tempered or not.

Charles

sblank
02-03-2010, 21:09
Hi Charles,

Now THAT would be very interesting!

I usually introduce complex numbers in my programming class by illustrating the Cartesian coordinate link... it seems to help a bit. Otherwise, most students think "i" is imaginary, and therefore doesn't really exist.

Thanks!

Stan



Hi Stan,

About a year ago I introduced operator sets into Oxygen which could support all kinds of exotic operators and variables. I wrote a simple set of complex number operations to test it out, but I have not done anything with it since. my complex operator set contains +-*/^=. I think of complex numbers as an arcane form of Cartesian geometry so I am not accustomed to using them per se. But I'll try them out with the Mandelbrot and see whether they are well tempered or not.

Charles

Petr Schreiber
02-03-2010, 21:16
Hi Stan,

if new TB won't be out till the Sunday of this week, I will post the TBGL DLL here on forum.
I will do some testing meanwhile to make sure it works as it should.

It is internally based on Vertex Arrays/Vertex Buffer Objects, but much easier to setup (1 command create, 1 command fill, 1 command render, 1 command destroy).


Petr

sblank
02-03-2010, 22:34
Sounds great... looking forward to it!

Thanks,

Stan



Hi Stan,

if new TB won't be out till the Sunday of this week, I will post the TBGL DLL here on forum.
I will do some testing meanwhile to make sure it works as it should.

It is internally based on Vertex Arrays/Vertex Buffer Objects, but much easier to setup (1 command create, 1 command fill, 1 command render, 1 command destroy).


Petr

Charles Pegge
03-03-2010, 04:54
Hi Stan,

Here is the Oxygen version with the mini complex number library embedded in the script. It is slightly less efficient than using floats directly due to the generic complex operator calls. But I hope it will give you the gist of how specialised operator sets can be implemented.

I had to fix the ^ operator, (which is not used here) so complex exponents will only work in the next ThinBasic Beta.

The core Mandelbrot algorithm now looks like this:


i=0
zzman=>0,0
zzcor=>xcor,ycor
do while i<maxit
zzman=zzman*zzman+zzcor 'COMPLEX OPERATIONS
esc=zzman.r * zzman.r + zzman.i * zzman.i
if esc>lmt then exit do
inc i
end do


Charles

ErosOlmi
03-03-2010, 08:50
Incredible Charles :eusaclap:
Oxygen module always astonished me !

kryton9
03-03-2010, 10:41
That is fast isn't it, wow! Oxygen is always impressing Charles. I can't wait till I get up to fractals and the physics stuff in the book.

sblank
03-03-2010, 16:25
Wow!!

Fantastic Charles... I'll second Eros and Kent. This is amazing stuff and I can see that I need to learn Oxygen... or I need some Oxygen?

Question (hopefully not too naive): You have a maxit range of from 20...100. Is this a hard limit (byte size)? I'm wondering about trying to zoom in and provide higher resolution?

I really like the complex operator definitions (macros? functions?). It looks like it would be possible to add additional functions for trig, exp, log, etc. It might be possible to create a fractal explorer or toolkit?

Just curious and getting ahead of myself, as usual.

Cheers,

Stan

Charles Pegge
03-03-2010, 20:59
Thank you all!

Well here are some keyboard controls for navigation:

Arrow keys: move around

PageUp: zoom in x2
PageDown: zoom out x.5
Home: Increase resolution (slower)
End: Decrease resolution (faster)

Esc: quit



@Stan: The resolution control works by increasing/reducing the maxit variable. Thanks for suggesting it.

To up performance I reverted to assembler for the core calculation. It makes a big difference in speed by eliminating a lot of memory accesses.

Would anybody like to add a coordinate/zoom factor display :) ?

Charles

sblank
03-03-2010, 22:49
This is getting better and better... again, fantastic work, Charles!

Question: Is maxit limited to the range 20...100 or is there a limitation to the resolution? It seems as though once I reach 100, I can't increase the resolution on the screen at all. The M-Set does take proportionally longer to computer, but the resolution seems to stay constant after 100.

I may be missing something, though...

Cheers... and great job!

Stan



Thank you all!

Well here are some keyboard controls for navigation:

Arrow keys: move around

PageUp: zoom in x2
PageDown: zoom out x.5
Home: Increase resolution (slower)
End: Decrease resolution (faster)

Esc: quit



@Stan: The resolution control works by increasing/reducing the maxit variable. Thanks for suggesting it.

To up performance I reverted to assembler for the core calculation. It makes a big difference in speed by eliminating a lot of memory accesses.

Would anybody like to add a coordinate/zoom factor display :) ?

Charles

Petr Schreiber
03-03-2010, 23:37
That is fantastic demo Charles! :eusaclap:

sblank
04-03-2010, 04:39
Hi Charles,

I WAS missing something... maxit works exactly as you intended. As I zoom into the M-Set and then increase maxit, I can see the resolution increase with each press of 'Home'

My fault on the misunderstanding... and great job with the program!

Cheers,

Stan

kryton9
04-03-2010, 09:39
That is really great Charles. Zooming in and scrolling around I saw so many cool textures in there. Thanks for a cool demo!

Charles Pegge
04-03-2010, 15:17
I've added different shading schemes. Number Keys 1..5
Try number 3 for the Hendrix option :)


Number keys: 1..5 shading scheme

[color=blue]Arrow keys: move around

PageUp: zoom in x2
PageDown: zoom out x.5
Home: Increase resolution (slower)
End: Decrease resolution (faster)

Esc: quit

D.J.Peters
04-03-2010, 16:45
is this syntax braching forward/backward ?

(
cmp ecx,3 : jz exit
fmul st(0),st(0)
)
the same as ?

cmp ecx,3
jz exit
fmul st(0),st(0)
exit:

Charles Pegge
04-03-2010, 17:46
Yes Joshy. This enables block structured programming in Asm. You can exit or repeat the blocks which are nestable.

(
...
dec ecx : jnz repeat
)

(
cmp al,32
jnz exit
...
)


Another feature is that any label defined inside the brackets is invisible to any code on the outside.
So you can have local labelling.

Also, you may use several assembler instructions on one line - just like Basic :)

Charles

Lionheart008
04-03-2010, 19:31
my 5 cent for today ;)

as my notebook is old and not very fast, I have changed x+y arrange values to 0.015.
and for iComplex: z = iComplex_Sin(z).

some examples before I tested something like


z = iComplex_Mul(z,z) '-----------> z*p or z+p or c*p doesn't work. out of range!

and after rendering the scene about 45 per cent I've got error message that I am out of range :shock: so rendering was aborted.


' Empty GUI script created on 03-04-2010 16:47:52 by (ThinAIR)

' Example: Section 7.7 (page 222-224), Explorations with the Mandlebrot Set

' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming

' Converted by Stan Blank, this version is slightly downgraded in detail by Petr Schreiber
' Using the Predator-Prey framework by Michael Clease
' Last modified: February 28, 2010

' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"

' insert Eros Olmi's new iComplex module
Uses "iComplex"


Function TBMain()

' Handle for our window
Local hWnd As DWord
Local Width As Long
Local Height As Long
Local n As Long
Local x,y, zz As Double

' Declare iComplex variables
Local z, c As tComplex

'# Initial values of width And height
width = 680
height = 500
'
' Create and show window
hWnd = TBGL_CreateWindowEx("Frankos_Mandelbrot Set", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX Or %TBGL_WS_DONTSIZE)
TBGL_ShowWindow

' -- Set background from default black to white
TBGL_BackColor(0,0,0)

' -- Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -2, -1.5, 1, 1.5 )

TBGL_PointSize 1

' Resets status of all keys
TBGL_ResetKeyState()

Local percent As Double
Local p,v As Double

' -- Create new GBuffer
Local gbMandelbrot As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_2D)
Local MandelbrotIndex As Long
Local MandelbrotVertex(160000) As tbgl_tVector2f ' -- TYPE from TBGL
Local MandelbrotColor (320000) As tbgl_tRGB ' -- TYPE from TBGL
p = -0.001*Cos(GetTickCount/5000)*100

' -- Fill it with data
For x = -2 To 1 Step 0.015

For y = -1.5 To 1.5 Step 0.015
Incr MandelbrotIndex
Incr MandelbrotColor(MandelbrotIndex).r

' Set complex points
c = iComplex_Set(x,y)
z = iComplex_Set(x,y)
v = iComplex_Set(x,y)
n = 0

For n = 1 To 50
' Equation is z = z*z + c
z = iComplex_Mul(z,z) '-----------> z*p or z+p or c*p doesn't work. out of range!
z = iComplex_Add(z,c)
z = iComplex_Sin(z)
'z = iComplex_sub(z,v)

' Distance from origin to complex point
zz = iComplex_Abs(z)

' if distance is > 2.0, then point is NOT in the M-Set
' so plot it... the M-Set will be black
If zz > 2.0 Then
MandelbrotColor (MandelbrotIndex).R = 455 '--> ? problem
MandelbrotVertex(MandelbrotIndex).x = x
MandelbrotVertex(MandelbrotIndex).y = y
End If
Next
Next

' -- Write status
percent = (x-(-2))/3*100
TBGL_SetWindowTitle(hWnd, Format$(percent, "#.00")+"%, rendering full detail")
DoEvents

' -- Dynamically say to TBGL where the data are, this is just pointer gymnastics, no copy
TBGL_GBufferDefineFromArray(gbMandelbrot, %TBGL_DYNAMIC, MandelbrotIndex, MandelbrotVertex(1), MandelbrotColor(1))

' -- Redraw
TBGL_ClearFrame
TBGL_GBufferRender(gbMandelbrot)
TBGL_DrawFrame

' -- Escape sooner?
If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit For

Next

TBGL_SetWindowTitle(hWnd, "Behold - the Mandlebrot! :)")

' Main loop
While TBGL_IsWindow(hWnd)

TBGL_ClearFrame

TBGL_GBufferRender(gbMandelbrot)

TBGL_DrawFrame

If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow

End Function


I haven't tested at all charles examples. Will do that next time.

thanks to all for this great mandelbrot experiences by iComplex module examples. frank

Lionheart008
04-03-2010, 19:47
this example didn't work complete. rendering abort at about 53 per cent.


' Empty GUI script created on 03-04-2010 16:47:52 by (ThinAIR)

' Example: Section 7.7 (page 222-224), Explorations with the Mandlebrot Set

' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming

' Converted by Stan Blank, this version is slightly downgraded in detail by Petr Schreiber
' Using the Predator-Prey framework by Michael Clease
' Last modified: February 28, 2010

' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"

' insert Eros Olmi's new iComplex module
Uses "iComplex"


Function TBMain()

' Handle for our window
Local hWnd As DWord
Local Width As Long
Local Height As Long
Local n As Long
Local x,y, zz As Double

' Declare iComplex variables
Local z, c As tComplex

'# Initial values of width And height
width = 680
height = 500
'
' Create and show window
hWnd = TBGL_CreateWindowEx("Frankos_Mandelbrot Set", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX Or %TBGL_WS_DONTSIZE)
TBGL_ShowWindow

' -- Set background from default black to white
TBGL_BackColor(0,0,0)

' -- Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -2, -1.5, 1, 1.5 )

TBGL_PointSize 1

' Resets status of all keys
TBGL_ResetKeyState()

Local percent As Double
Local p,v As Double

' -- Create new GBuffer
Local gbMandelbrot As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_2D)
Local MandelbrotIndex As Long
Local MandelbrotVertex(160000) As tbgl_tVector2f ' -- TYPE from TBGL
Local MandelbrotColor (320000) As tbgl_tRGB ' -- TYPE from TBGL
p = -0.001*Cos(GetTickCount/5000)*100

' -- Fill it with data
For x = -2 To 1 Step 0.0055

For y = -1.5 To 1.5 Step 0.0055
Incr MandelbrotIndex
Incr MandelbrotColor(MandelbrotIndex).r

' Set complex points
c = iComplex_Set(x,y)
z = iComplex_Set(x,y)
v = iComplex_Set(x,y)
n = 0

For n = 1 To 50
' Equation is z = z*z + c
z = iComplex_Mul(z,z)
z = iComplex_Add(z,c)
z = iComplex_Sin(z)
'z = iComplex_sub(z,v)

' Distance from origin to complex point
zz = iComplex_Abs(z)

' if distance is > 2.0, then point is NOT in the M-Set
' so plot it... the M-Set will be black
If zz > 2.0 Then
MandelbrotColor (MandelbrotIndex).R = 455 '--> ? problem
MandelbrotVertex(MandelbrotIndex).x = x
MandelbrotVertex(MandelbrotIndex).y = y
End If
Next
Next

' -- Write status
percent = (x-(-2))/3*100
TBGL_SetWindowTitle(hWnd, Format$(percent, "#.00")+"%, rendering full detail")
DoEvents

' -- Dynamically say to TBGL where the data are, this is just pointer gymnastics, no copy
TBGL_GBufferDefineFromArray(gbMandelbrot, %TBGL_DYNAMIC, MandelbrotIndex, MandelbrotVertex(1), MandelbrotColor(1))

' -- Redraw
TBGL_ClearFrame
TBGL_GBufferRender(gbMandelbrot)
TBGL_DrawFrame

' -- Escape sooner?
If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit For

Next

TBGL_SetWindowTitle(hWnd, "Behold - the Mandlebrot! :)")

' Main loop
While TBGL_IsWindow(hWnd)

TBGL_ClearFrame

TBGL_GBufferRender(gbMandelbrot)

TBGL_DrawFrame

If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow

End Function


perhaps somebody can help to fix this code that's run fine or give hint where I can do that, would be nice ?

best regards, frank

Petr Schreiber
04-03-2010, 21:25
Hi Frank,

the reason of failing is obvious - array not big enough.
Try to think how to calculate how much points it will have, if you go from -2 to 1 by 0.0055 and -1.5 to 1.5 by 0.0055.

Three hints:

1-(-2) = 3, 1.5-(-1.5) = 3
step is not well chosen
size of vertex and color array does not match



Petr

Charles Pegge
05-03-2010, 01:23
By bending the algorithm, distorted structures can be produced. as shown in the images below. You
can see where additional Asm instructions may be inserted. (The fsin & fmul instructions commented out).

I've also improved the navigation by using smaller steps and improved the frame rate a bit though I cannot match Joshy's fractal program!
(http://community.thinbasic.com/index.php?topic=3273.msg24524;topicseen#new)

Charles



mov edx,shmo
mov ecx,0
(
'zzman=zzman*zzman+zzcor 'COMPLEX OPERATIONS
'esc=zzman.r * zzman.r + zzman.i * zzman.i
'if esc>lmt then jmp fwd xff
fld qword zzman.r
fmul st(0),st(0)
fld qword zzman.i
fmul st(0),st(0)
'fsin
fsubp st(1),st(0)
fld qword zzman.r
fmul qword zzman.i
'fsin
fadd st(0),st(0)
fadd qword zzcor.i
fst qword zzman.i
(
cmp edx,3 : jz exit
fmul st(0),st(0)
'fmul st(0),st(0)
)
fstp qword esc
fadd qword zzcor.r
fst qword zzman.r
(
cmp edx,3 : jz exit
fmul st(0),st(0)
'fmul st(0),st(0)
)
fadd qword esc
fst qword esc
fld qword lmt
fcomip st(1)
fcomp st(0),st(0)
jb exit
inc ecx : cmp ecx,maxit : jl long repeat
)
mov i,ecx



http://lh6.ggpht.com/_dA9zgFHs-1A/S5A37bJ0uBI/AAAAAAAAAcE/4xhgv051-5Y/s144/GreenSquirly.jpg http://lh6.ggpht.com/_dA9zgFHs-1A/S5A371lSL5I/AAAAAAAAAcI/NC1HBaR8zRo/s144/Magma.jpg http://lh4.ggpht.com/_dA9zgFHs-1A/S5A38in1YFI/AAAAAAAAAcM/mFMc5FTTlEk/s144/Organic.jpg

http://picasaweb.google.co.uk/charlespegge/Fractals?feat=directlink



Number keys: 1..7 shading scheme

Arrow keys: move around

PageUp: zoom in
PageDown: zoom out
Home: Increase resolution (slower)
End: Decrease resolution (faster)

Esc: quit

Lionheart008
05-03-2010, 02:17
@petr: thanks! so I found after a while and three coffees one good solution for me !


' Empty GUI script created on 03-04-2010 16:47:52 by (ThinAIR)

' Example: Section 7.7 (page 222-224), Explorations with the Mandlebrot Set

' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming

' Converted by Stan Blank, this version is slightly downgraded in detail by Petr Schreiber

Uses "TBGL"

' insert Eros Olmi's new iComplex module
Uses "iComplex"


Function TBMain()

' Handle for our window
Local hWnd As DWord
Local Width As Long
Local Height As Long
Local n As Long
Local x,y, zz As Double

' Declare iComplex variables
Local z, c As tComplex

'# Initial values of width And height
width = 400
height = 400
'
' Create and show window
hWnd = TBGL_CreateWindowEx("Frankos_Mandelbrot Set", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX Or %TBGL_WS_DONTSIZE)
TBGL_ShowWindow

' -- Set background from default black to white
TBGL_BackColor(0,0,0)

' -- Init OpenGl, like gluOrtho2D in example code
'TBGL_RenderMatrix2D( -2, -1.5, 1, 1.5 )
TBGL_RenderMatrix2D( -2, -2.5, 2, 2.5 )

TBGL_PointSize 1

' Resets status of all keys
TBGL_ResetKeyState()

Local percent As Double
Local p,v As Double

' -- Create new GBuffer
Local gbMandelbrot As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_2D)
Local MandelbrotIndex As Long
Local MandelbrotVertex(220000) As tbgl_tVector2f ' -- TYPE from TBGL
Local MandelbrotColor (220000) As tbgl_tRGB ' -- TYPE from TBGL
p = -0.001*Cos(GetTickCount/5000)*100

' -- Fill it with data
'For x = -2 To 1.4 Step 0.00988 'ok
For x = -2 To 2 Step 0.00988

For y = -2.5 To 2.5 Step 0.00988
Incr MandelbrotIndex
'Incr MandelbrotColor(MandelbrotIndex).r

' Set complex points
c = iComplex_Set(x,y)
z = iComplex_Set(x,y)
v = iComplex_Set(x,y)
n = 0

For n = 1 To 30
' Equation is z = z*z + c
z = iComplex_Mul(z,z)
z = iComplex_Add(z,c)
z = iComplex_Sin(z)
'z = iComplex_sub(z,v)

' Distance from origin to complex point
zz = iComplex_Abs(z)

' if distance is > 2.0, then point is NOT in the M-Set
' so plot it... the M-Set will be black
If zz > 2.0 Then
MandelbrotColor (MandelbrotIndex).R = 255
MandelbrotColor (MandelbrotIndex).B = 255
MandelbrotVertex(MandelbrotIndex).x = x
MandelbrotVertex(MandelbrotIndex).y = y
End If
Next
Next

' -- Write status
percent = (x-(-2))/3*100
TBGL_SetWindowTitle(hWnd, Format$(percent, "#.00")+"%, rendering full detail")
DoEvents

' -- Dynamically say to TBGL where the data are, this is just pointer gymnastics, no copy
TBGL_GBufferDefineFromArray(gbMandelbrot, %TBGL_DYNAMIC, MandelbrotIndex, MandelbrotVertex(1), MandelbrotColor(1))

' -- Redraw
TBGL_ClearFrame
TBGL_GBufferRender(gbMandelbrot)
TBGL_DrawFrame

' -- Escape sooner?
If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit For

Next

TBGL_SetWindowTitle(hWnd, "Behold - the other Mandlebrot! :)")

' Main loop
While TBGL_IsWindow(hWnd)

TBGL_ClearFrame

TBGL_GBufferRender(gbMandelbrot)

TBGL_DrawFrame

If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow

End Function


I don't give up ;)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2) part:

Question!

(last edit: this morning)

about my thoughts for mandlebrot calculations:

a) first and good attempt:


Local MandelbrotVertex(160000) As tbgl_tVector2f ' -- TYPE from TBGL
Local MandelbrotColor (160000) As tbgl_tRGB ' -- TYPE from TBGL

' -- Fill it with data
For x = -2 To 1 Step 0.0075 '--> difference 3.0

For y = -1.5 To 1.5 Step 0.0075 '--> difference 3.0



b) my thoughts for bigger arranges, just an example.


Local MandelbrotVertex(260000) As tbgl_tVector2f ' -- TYPE from TBGL
Local MandelbrotColor (260000) As tbgl_tRGB ' -- TYPE from TBGL

' -- Fill it with data
For x = -2 To 3 Step 0.0025 '--> difference 5.0

For y = -1.5 To 3.5 Step 0.0025 '--> difference 5.0


=> that's calculation could be correct ?

best regards, frank

Petr Schreiber
05-03-2010, 11:20
Hi Frank,

you were on the right track with the differences.
The total amount of items in array is equal to:

(difference/step+1) * (difference/step+1)

(so (5/0.0025+1) * (5/0.0025+1) = 4 004 001 in your second example).


Petr

Apongea
29-12-2023, 13:23
Have you successfully resolved the issue with the Mandelbrot set rendering, and how do you plan to optimize the array size for larger arrangements in your calculations?