View Full Version : MathArt: Port of code by Stan Blank
Petr Schreiber
23-02-2010, 18:46
Here is port of code from book by Stan,
it looks great!
I made it completely TBGL, of course it could be done fully in OpenGL as well.
'
' MathArt by Stan Blank
' Ported to TBGL by Petr Schreiber
'
Uses "TBGL"
Begin Const
%listPoints = 1
End Const
Function TBMAIN()
Local hWnd As DWord
Local FrameRate As Double
Local width, height As Long
Local axrng As Long = 10
Local x, y, r As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("MathArt, based on code by Stan Blank", 500, 500, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_GetWindowClient( hWnd, width, height )
' -- Background color to white
TBGL_BackColor 255, 255, 255, 255
' -- Cache the image to display list
' -- Please note the display lists are garbage collected,
' -- so no need to explicitly create/destroy them
TBGL_NewList %listPoints
TBGL_BeginPoly(%GL_POINTS)
For x = -axrng To axrng Step 0.04
For y = -axrng To axrng Step 0.04
r = Cos(x) + Sin(y)
TBGL_Color Cos(y*r)*255, Cos(x*y*r)*255, Sin(r*x)*255
TBGL_Vertex x, y
Next
Next
TBGL_EndPoly
TBGL_EndList
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
' -- Read the current frame rate.
FrameRate = TBGL_GetFrameRate
' -- Clear the frame first
TBGL_ClearFrame
' -- Set the resolution and the coordinate system
TBGL_RenderMatrix2D (-axrng, axrng, axrng, -axrng)
' -- Draw the image
TBGL_CallList %listPoints
' -- Show the rendered stuff
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
End Function
Petr
Charles Pegge
23-02-2010, 19:16
Very impressive Stan and Petr!
But try stretching the window - the cells become separated by a pixel margin - looks like a rounding error.
Charles
Thanks Charles!
The pixel margin is caused by the "step 0.04" in the nested loops. As the graphics window dimensions increase, this step becomes too large. Replacing this value with smaller values... or better, with an expression that adjusts the step according to the dimensions of the graphics window (I'll work on this) should fix the issue.
Cheers,
Stan
Very impressive Stan and Petr!
But try stretching the window - the cells become separated by a pixel margin - looks like a rounding error.
Charles
Petr Schreiber
23-02-2010, 21:12
Good observation Charles,
the solution could be simply render to texture, which would then be interpolated across the screen.
Or, as Stan says, making the step smaller.
Petr
Petr Schreiber
23-02-2010, 21:58
Example how to built in TBGL render surface to dialog,
added button "Rebuild", which can command the program to recreate the graphic at appropriate level of detail.
Alternative solution would be to build it once, with enough detail to look nice even when maximized.
You can retrieve desktop size using TBGL_GetDesktopInfo, so this could be also the way.
Petr
Now that's cool!
I can see that I have a LOT to learn.
Cheers,
Stan
Example how to built in TBGL render surface to dialog,
added button "Rebuild", which can command the program to recreate the graphic at appropriate level of detail.
Alternative solution would be to build it once, with enough detail to look nice even when maximized.
You can retrieve desktop size using TBGL_GetDesktopInfo, so this could be also the way.
Petr
Charles Pegge
23-02-2010, 22:46
I would love to see how this texture looks on a snake :D
There must also be quite a large number of interesting variants of this algorithm.
Charles
That is really something Petr and Stan! Can't wait to see what other beautiful scripts come out!!!
ErosOlmi
24-02-2010, 00:39
Petr,
just noted the following:
maximize dialog and click "rebuild": memory consumption and CPU goes very high and stay high, in my case (1680x1024 px) it goes to around 220MB
restore original size and "rebuild": memory returns normal usage, around 30MB, and CPU close to zero
Is this due to huge display lists and TBGL automatic "display lists garbage collection"?
Eros
Petr Schreiber
24-02-2010, 08:38
Hi Eros,
good observation.
The garbage collection is very lightweight, the gigantic memory consumption is probably caused by fact the image is constructed by lot of points + some states.
The display list mechanism is vendor specific. On my card, when maximized at 1280x1024, it takes 50MB of memory.
Once I'll be back from school, I will show efficient method to save some memory.
Petr
Petr Schreiber
24-02-2010, 14:20
I updated the dialog example,
there was redundant detail on Y axis when resizing the window to non-square shape.
The efficient way of rendering these cases where display list is not really necessary (no state change) reminded me of idea I worked on and forgot about. I will continue on it and publish new TBGL once ready.
Petr
Lionheart008
24-02-2010, 19:59
hi stan, welcome from my side to this thinbasic forum too and good luck for learning and sending new ideas :)
hello all.
I have made some exercises some minutes before with this adepted code from petr and I can admit that's very beautiful and interesting pictures I've got by making experiments with this mathArt code. thanks petr and stan for this one! I am loving such stuff as well as plasma themes.
'
' MathArt by Stan Blank, Frank's Try 2
' Ported to TBGL by Petr Schreiber
'
Uses "TBGL"
Begin Const
%listPoints = 1
End Const
Function TBMAIN()
Local hWnd As DWord
Local FrameRate As Double
Local width, height As Long
Local axrng As Long = 14
Local x, y, r As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("MathArt, based on code by Stan Blank / Frank's Try2", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_GetWindowClient( hWnd, width, height )
' -- Background color to white
TBGL_BackColor 255, 255, 255, 255
' -- Cache the image to display list
' -- Please note the display lists are garbage collected,
' -- so no need to explicitly create/destroy them
TBGL_NewList %listPoints
TBGL_BeginPoly(%GL_POINTS)
For x = -axrng To axrng Step 0.02
For y = -axrng To axrng Step 0.02
r = Cos(x) + Sin(y)
TBGL_Color Sin(y*r)*255, Cos(x*y*r)*255, Log(r*x/y)*255
TBGL_Vertex x, y
Next
Next
TBGL_EndPoly
TBGL_EndList
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
' -- Read the current frame rate.
FrameRate = TBGL_GetFrameRate
' -- Clear the frame first
TBGL_ClearFrame
' -- Set the resolution and the coordinate system
TBGL_RenderMatrix2D (-axrng, axrng, axrng, -axrng)
' -- Draw the image
TBGL_CallList %listPoints
' -- Show the rendered stuff
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
End Function
In my examples there are some (at the right and left top corner of the scenes) pixel artefacts, but I don't know if this caused by my old graphic card or it's software generated. I like such math. art buildings ;)
correction: I have read the post from stan/charles and noticed the possible failure of pixel margins. I have chosen 0.02 value.
info:
there were some exercises at this page from my side to "plasma-playing" too:
http://community.thinbasic.com/index.php?topic=2700.75
and so far as I can remember charles pegge has done with oxygen similar techniques and made very nice examples :)
bye, servus, thanks, frank
Thanks for both the kind words and welcome, Frank...
Your examples are very nice... Math Art is a great way to expose students to mathematics. I've found that they are interested in the graphics and then they become interested in how to make the graphics and THEN they become interested in the math... and that's a good thing :-)
I'm hoping to find some time over the weekend to work with the new iComplex module Eros wrote so that I can create some fractals... should be fun!
Cheers,
Stan
I found this link for a pdf of Stan Blank's book "Python Programming in OpenGL- A Graphical Approach to Programming"
Link updated to the current version as mentioned by Stan in the next post.
http://community.thinbasic.com/index.php?action=dlattach;topic=3233.0;attach=6555
Stan let me know if this is OK, if not I will remove it. Looking forward to reading it!
Hi Kent,
Perfectly OK with me... I think the link in an earlier post (made with help from Petr) is a newer version of the text:
http://community.thinbasic.com/index.php?topic=3233.15
Let me know if you have any questions, comments, suggestions, or criticisms. Again, it would be great to convert the Python scripts to ThinBasic... and that should be a bit easier now that Eros has created the iComplex module.
There are so many wizards around here... I am in awe!
Cheers,
Stan
I found this link for a pdf of Stan Blank's book "Python Programming in OpenGL- A Graphical Approach to Programming"
http://new.math.uiuc.edu/math198/PyOpenGL9sep09.pdf
Stan let me know if this is OK, if not I will remove it. Looking forward to reading it!
I am having fun reading the book Stan. Exactly the kind of book I like. Lots of fun examples in well structured steps along with nice recommendations for further study.
Thanks for sharing your work so permanent students as myself can enjoy learning!
Thanks Kent... learning should be as much fun as possible and I'm glad you like the text.
Cheers,
Stan
I am having fun reading the book Stan. Exactly the kind of book I like. Lots of fun examples in well structured steps along with nice recommendations for further study.
Thanks for sharing your work so permanent students as myself can enjoy learning!