Michael Clease
26-02-2010, 20:15
' Example: Section 6.4 (page 129), The Barnsley Fern
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Michael Clease
' Last modified: February 26, 2010
' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"
' Handle for our window
Local hWnd As DWord
Local Width As Long
Local Height As Long
Local x,y As Single
Local xx,yy As Single
Local a,b,c As Single
Local d,e,f As Single
Local v As Single
Local n As DWord
'# Initial values of width And height
width = 600
height = 600
'
' Create and show window
hWnd = TBGL_CreateWindowEx("The Barnsley Fern", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' Set Foreground from default White to Green
TBGL_Color(76,152,51)
' Set background from default black to white
TBGL_BackColor(255,255,255)
' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -3, 0, 3, 10.5 )
' Resets status of all keys
TBGL_ResetKeyState()
TBGL_PointSize 1
' Main loop
While TBGL_IsWindow(hWnd)
x = -1.5
y = 0.75
TBGL_ClearFrame ' glClear(GL_COLOR_BUFFER_BIT)
For n = 0 To 100000
v = Rnd
If v >= 0 And v <= 0.8000 Then
a = 0
b = 1.6
c = -2.5*M_PI/180
d = -2.5*M_PI/180
e = 0.85
f = 0.85
' TBGL_Color(255,0,0)
ElseIf v > 0.8000 And v <= 0.8050 Then
a = 0
b = 0
c = 0*M_PI/180
d = 0*M_PI/180
e = 0
f = 0.16
' TBGL_Color(0,255,0)
ElseIf v > 0.8050 And v <= 0.9025 Then
a = 0
b = 1.6
c = 49*M_PI/180
d = 49*M_PI/180
e = 0.3
f = 0.34
' TBGL_Color(0,0,255)
ElseIf v > 0.9025 And v <= 1.0 Then
a = 0
b = 0.44
c = 120*M_PI/180
d = -50*M_PI/180
e = 0.3
f = 0.37
' TBGL_Color(255,0,255)
EndIf
xx = x
yy = y
x = e * xx * Cos(c) - f * yy * Sin(d) + a
y = e * xx * Sin(c) + f * yy * Cos(d) + b
If n > 10 Then
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x,y)
TBGL_EndPoly
EndIf
Next
TBGL_DrawFrame
' ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
'Section 6.4 The Barnsley Fern
'Exercise 9 In the previous section asked you To remember the plant-like
'structures that were plotted when you added And modified the lines of code listed In the
'problem. We are now going To expand On the ideas introduced In the Chaos Game
'section To produce an graphics plot that raises the “plant-like” descriptor To a New level.
'In Section 6.3 we used a simple rule With a random choice of vertices To produce the
'Sierpinski Gasket attractor. In THIS section we are going To enlarge our scope And use a
'combination of 4 sets of PARAMETERS, each one assigned a different probability of being
'Selected At random. Once a parameter Set has been randomly chosen we’ll apply the
'Selected PARAMETERS To our rules Or equations. The result of THIS iterated Function system
'is both surprising And beautiful!
'Here is the program listing For THIS section.
'# PyBarnsleyFern.py
'from OpenGL.GL import *
'from OpenGL.GLU import *
'from OpenGL.GLUT import *
'from random import *
'from numpy import *
'import sys
'# Globals For Window width And height
'Global width
'Global height
'# Initial values of width And height
'width = 600
'height = 600
'def init():
'# White background
'glClearColor(1.0, 1.0, 1.0, 0.0)
'# Green Plot… it IS a Fern
'glColor3f(0.3, 0.6, 0.2)
'# Set the projection matrix... our "view"
'glMatrixMode(GL_PROJECTION)
'glLoadIdentity()
'# Set the plot Window range
'gluOrtho2D(-3.0, 3.0, 0.0, 10.5)
'# Set the matrix For the object we are drawing
'glMatrixMode(GL_MODELVIEW)
'glLoadIdentity()
'def plotfunc():
'# Choose an initial point... Any point
'# You can Randomize THIS If you wish
'x = -1.5
'y = 0.75
'glClear(GL_COLOR_BUFFER_BIT)
'# Plot 100000 points. THIS Number is very large.
'# Feel free To experiment With smaller values.
'For n In range(0,100000):
'# n allows us To reject the first few points
'# To give the attractor a chance To Do its “thing”
'# Choose a random Value Between 0 And 1 And
'# Then Select a Set of PARAMETERS based On THIS Value.
'v = random()
'If v >= 0 And v <= 0.8000:
'a = 0
'b = 1.6
'c = -2.5*Pi/180
'd = -2.5*Pi/180
'e = 0.85
'f = 0.85
'#glColor3f(1.0, 0.0, 0.0)
'elif v > 0.8000 And v <= 0.8050:
'a = 0
'b = 0
'c = 0*Pi/180
'd = 0*Pi/180
'e = 0
'f = 0.16
'#glColor3f(0.0, 1.0, 0.0)
'elif v > 0.8050 And v <= 0.9025:
'a = 0
'b = 1.6
'c = 49*Pi/180
'd = 49*Pi/180
'e = 0.3
'f = 0.34
'#glColor3f(0.0, 0.0, 1.0)
'elif v > 0.9025 And v <= 1.0:
'a = 0
'b = 0.44
'c = 120*Pi/180
'd = -50*Pi/180
'e = 0.3
'f = 0.37
'#glColor3f(1.0, 0.0, 1.0)
'# Save the old values of x And y so we can Iterate
'# those values according To the chosen PARAMETERS
'# And rules.
'xx = x
'yy = y
'# Apply the PARAMETERS To the rule equations
'x = e * xx * Cos(c) - f * yy * Sin(d) + a
'y = e * xx * Sin(c) + f * yy * Cos(d) + b
'# Start plotting after the 10th point
'If n > 10:
'glBegin(GL_POINTS)
'glVertex2f(x,y)
'glEnd()
'glFlush()
'def keyboard(key, x, y):
'# Allows us To quit by pressing 'Esc' or 'q'
'If key == chr(27):
'sys.Exit()
'If key == "q":
'sys.Exit()
'def main():
'Global width
'Global height
'glutInit(sys.argv)
'glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE)
'glutInitWindowPosition(100,100)
'glutInitWindowSize(width,height)
'glutCreateWindow("The Chaos Game... Fern!")
'glutDisplayFunc(plotfunc)
'glutKeyboardFunc(keyboard)
'init()
'glutMainLoop()
'main()
'#End of Program
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Michael Clease
' Last modified: February 26, 2010
' thinBasic does not use GLUT, we use instead tbgl
Uses "TBGL"
' Handle for our window
Local hWnd As DWord
Local Width As Long
Local Height As Long
Local x,y As Single
Local xx,yy As Single
Local a,b,c As Single
Local d,e,f As Single
Local v As Single
Local n As DWord
'# Initial values of width And height
width = 600
height = 600
'
' Create and show window
hWnd = TBGL_CreateWindowEx("The Barnsley Fern", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' Set Foreground from default White to Green
TBGL_Color(76,152,51)
' Set background from default black to white
TBGL_BackColor(255,255,255)
' Init OpenGl, like gluOrtho2D in example code
TBGL_RenderMatrix2D( -3, 0, 3, 10.5 )
' Resets status of all keys
TBGL_ResetKeyState()
TBGL_PointSize 1
' Main loop
While TBGL_IsWindow(hWnd)
x = -1.5
y = 0.75
TBGL_ClearFrame ' glClear(GL_COLOR_BUFFER_BIT)
For n = 0 To 100000
v = Rnd
If v >= 0 And v <= 0.8000 Then
a = 0
b = 1.6
c = -2.5*M_PI/180
d = -2.5*M_PI/180
e = 0.85
f = 0.85
' TBGL_Color(255,0,0)
ElseIf v > 0.8000 And v <= 0.8050 Then
a = 0
b = 0
c = 0*M_PI/180
d = 0*M_PI/180
e = 0
f = 0.16
' TBGL_Color(0,255,0)
ElseIf v > 0.8050 And v <= 0.9025 Then
a = 0
b = 1.6
c = 49*M_PI/180
d = 49*M_PI/180
e = 0.3
f = 0.34
' TBGL_Color(0,0,255)
ElseIf v > 0.9025 And v <= 1.0 Then
a = 0
b = 0.44
c = 120*M_PI/180
d = -50*M_PI/180
e = 0.3
f = 0.37
' TBGL_Color(255,0,255)
EndIf
xx = x
yy = y
x = e * xx * Cos(c) - f * yy * Sin(d) + a
y = e * xx * Sin(c) + f * yy * Cos(d) + b
If n > 10 Then
TBGL_BeginPoly(%GL_POINTS)
TBGL_Vertex(x,y)
TBGL_EndPoly
EndIf
Next
TBGL_DrawFrame
' ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
'Section 6.4 The Barnsley Fern
'Exercise 9 In the previous section asked you To remember the plant-like
'structures that were plotted when you added And modified the lines of code listed In the
'problem. We are now going To expand On the ideas introduced In the Chaos Game
'section To produce an graphics plot that raises the “plant-like” descriptor To a New level.
'In Section 6.3 we used a simple rule With a random choice of vertices To produce the
'Sierpinski Gasket attractor. In THIS section we are going To enlarge our scope And use a
'combination of 4 sets of PARAMETERS, each one assigned a different probability of being
'Selected At random. Once a parameter Set has been randomly chosen we’ll apply the
'Selected PARAMETERS To our rules Or equations. The result of THIS iterated Function system
'is both surprising And beautiful!
'Here is the program listing For THIS section.
'# PyBarnsleyFern.py
'from OpenGL.GL import *
'from OpenGL.GLU import *
'from OpenGL.GLUT import *
'from random import *
'from numpy import *
'import sys
'# Globals For Window width And height
'Global width
'Global height
'# Initial values of width And height
'width = 600
'height = 600
'def init():
'# White background
'glClearColor(1.0, 1.0, 1.0, 0.0)
'# Green Plot… it IS a Fern
'glColor3f(0.3, 0.6, 0.2)
'# Set the projection matrix... our "view"
'glMatrixMode(GL_PROJECTION)
'glLoadIdentity()
'# Set the plot Window range
'gluOrtho2D(-3.0, 3.0, 0.0, 10.5)
'# Set the matrix For the object we are drawing
'glMatrixMode(GL_MODELVIEW)
'glLoadIdentity()
'def plotfunc():
'# Choose an initial point... Any point
'# You can Randomize THIS If you wish
'x = -1.5
'y = 0.75
'glClear(GL_COLOR_BUFFER_BIT)
'# Plot 100000 points. THIS Number is very large.
'# Feel free To experiment With smaller values.
'For n In range(0,100000):
'# n allows us To reject the first few points
'# To give the attractor a chance To Do its “thing”
'# Choose a random Value Between 0 And 1 And
'# Then Select a Set of PARAMETERS based On THIS Value.
'v = random()
'If v >= 0 And v <= 0.8000:
'a = 0
'b = 1.6
'c = -2.5*Pi/180
'd = -2.5*Pi/180
'e = 0.85
'f = 0.85
'#glColor3f(1.0, 0.0, 0.0)
'elif v > 0.8000 And v <= 0.8050:
'a = 0
'b = 0
'c = 0*Pi/180
'd = 0*Pi/180
'e = 0
'f = 0.16
'#glColor3f(0.0, 1.0, 0.0)
'elif v > 0.8050 And v <= 0.9025:
'a = 0
'b = 1.6
'c = 49*Pi/180
'd = 49*Pi/180
'e = 0.3
'f = 0.34
'#glColor3f(0.0, 0.0, 1.0)
'elif v > 0.9025 And v <= 1.0:
'a = 0
'b = 0.44
'c = 120*Pi/180
'd = -50*Pi/180
'e = 0.3
'f = 0.37
'#glColor3f(1.0, 0.0, 1.0)
'# Save the old values of x And y so we can Iterate
'# those values according To the chosen PARAMETERS
'# And rules.
'xx = x
'yy = y
'# Apply the PARAMETERS To the rule equations
'x = e * xx * Cos(c) - f * yy * Sin(d) + a
'y = e * xx * Sin(c) + f * yy * Cos(d) + b
'# Start plotting after the 10th point
'If n > 10:
'glBegin(GL_POINTS)
'glVertex2f(x,y)
'glEnd()
'glFlush()
'def keyboard(key, x, y):
'# Allows us To quit by pressing 'Esc' or 'q'
'If key == chr(27):
'sys.Exit()
'If key == "q":
'sys.Exit()
'def main():
'Global width
'Global height
'glutInit(sys.argv)
'glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE)
'glutInitWindowPosition(100,100)
'glutInitWindowSize(width,height)
'glutCreateWindow("The Chaos Game... Fern!")
'glutDisplayFunc(plotfunc)
'glutKeyboardFunc(keyboard)
'init()
'glutMainLoop()
'main()
'#End of Program