PDA

View Full Version : Example section 5.3 ff: circles, ellipse and more (page 50-53)



christianssen
02-03-2010, 11:58
my first attempt :)

page 50-53, section 5.3 ff.
nearly exactly code conversion from stans PyOpenGL book.
modified only a little the view point because there were no exactly definitions for this.


' Example 5.3 ff. plotting circles, ellipses and more

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

' Converted by denis christianssen, march 02, 2010


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

' -- Function where program begins
Function TBMain()
' Handle for our window
Local hWnd As DWord

' Create and show window
hWnd = TBGL_CreateWindowEx("page 50-52: section 5.3ff: Circle, Ellipse.. by Denis", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

Init()

' Resets status of all keys
TBGL_ResetKeyState()

' Define "Callback" to be fired + that it should be fired each 40ms
TBGL_BindPeriodicFunction(hWnd, "PlotFunc", 40)

' -- Once the command below is executed, further script execution
' -- is halted and only periodic calling of the bound function is performed
TBGL_ProcessPeriodicFunction(hWnd)

' -- Close window, if we have quite periodic function
TBGL_DestroyWindow

End Function

Sub Init()
' Set background from default black to white
TBGL_BackColor(255,255,255)

' Set rendering color to black
TBGL_Color(0,0,0)

' Set point size to 3
TBGL_PointSize 3
End Sub

'def plotfunc():
'glClear(GL_COLOR_BUFFER_BIT)
'glColor3f(0.0, 0.0, 0.0)
'glPointSize(1.0)
'# Plot the coordinate axes
'glBegin(GL_LINES)
'glVertex2f(-2.0, 0.0)
'glVertex2f(2.0, 0.0)
'glVertex2f(0.0, 2.0)
'glVertex2f(0.0, -2.0)
'glEnd()
'# Plot the parametric equations
'For t In arange(0.0,6.28, 0.001):
'x = Sin(t)
'y = Cos(t)
'glBegin(GL_POINTS)
'glVertex2f(x, y)
'glEnd()
'glFlush()
'# End plotfunc()


Sub PlotFunc()
' -- Which window does call?
Local hWnd As DWord = TBGL_CallingWindow
Local x,y,t As Single

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

TBGL_Color(0,0,0)
TBGL_PointSize 1
x = 0.5
y = 0.5

TBGL_BeginPoly(%GL_LINES)
TBGL_Vertex(-2.0,0.0)
TBGL_Vertex(2.0,0.0)
TBGL_Vertex(0.0,2.0)
TBGL_Vertex(0.0,-2.0)
TBGL_EndPoly

' Like glBegin(GL_POINTS) in example code
TBGL_BeginPoly(%GL_LINES)

'For t In arange(0.0,6.28, 0.001):
'x = Sin(t)
'y = Cos(t)
'glBegin(GL_POINTS)
'glVertex2f(x, y)

TBGL_Color(0,0,255)
For t = 0.0 To 6.28 Step 0.001
x = Sin(t)
y = Cos(t)
TBGL_Vertex(x,y)
Next

'What happens If we change the
'equations For x And y? What happens If we let t range from -6.28 To 6.28?3 Try it!
'Change the x And y equations To: x = Cos(3*t) And y = Sin(5*t) respectively
'And change the range of t To: For t In arange(-6.28, 6.28, 0.01):

TBGL_Color(255,0,0)
For t = -6.28 To 6.28 Step 0.01
x = Cos(3*t)
y = Sin(5*t)
TBGL_Vertex(x,y)
Next

TBGL_EndPoly

TBGL_DrawFrame

' ESCAPE key to disable callback
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then
TBGL_UnBindPeriodicFunction( hWnd )
Exit Sub
End If

End Sub



makes fun! -> by the way: what's the math. name for the spiral thing ? ;)

bye, denis

sblank
02-03-2010, 16:42
Nicely done, Denis...

As a class of objects, these spirals are usually called Bowditch or Lissajous curves although particular ones may have their own specific names as stated in the text. They are formed using a set of parametric equations [x = Asin(at+c), y = Bsin(bt)] or something similar. I call them "Etch-a-Sketch" curves, although I've probably violated copyright laws by doing so :)

Does this answer your question? Anyway, well done!

Stan



my first attempt :)

page 50-53, section 5.3 ff.
nearly exactly code conversion from stans PyOpenGL book.
modified only a little the view point because there were no exactly definitions for this.


makes fun! -> by the way: what's the math. name for the spiral thing ? ;)

bye, denis

Lionheart008
02-03-2010, 23:43
hello denis, stan,

denis, you made a good work for your circle, ellipse conversion ! cool job for a beginner! 8)

best regards, frank

kryton9
03-03-2010, 04:15
Thanks for joining the fun Denis and good job converting, especially since you are so new to thinBasic too.