Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Example: Section 5.2 (page 41), Plotting 2D Functions

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Example: Section 5.2 (page 41), Plotting 2D Functions

    Petr, has two improved and optimized versions further along in this thread. Please refer to those.

    Original Post, outdated now!
    I'm going to need help on this one. My plot is not coming out like the one in the book, picture 5.3 on page 42.
    [code=thinbasic]' Example 5.25 Plotting 2D Functions

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

    ' Converted by Kent Sarikaya
    ' Last modified: February 25, 2010


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


    ' Handle for our window
    Local hWnd As DWord

    ' Create and show window
    hWnd = TBGL_CreateWindowEx("Function Plotter", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
    TBGL_ShowWindow

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

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

    ' Resets status of all keys
    TBGL_ResetKeyState()

    Local x,y As Single
    ' Main loop
    While TBGL_IsWindow(hWnd)

    TBGL_ClearFrame
    TBGL_Color(0,0,0)
    TBGL_PointSize 3
    For x = -5 To 5 Step 0.1
    y = x*x
    ' Like glBegin(GL_POINTS) in example code
    TBGL_BeginPoly(%GL_POINTS)
    TBGL_Vertex(x,y)
    TBGL_EndPoly
    Next
    TBGL_DrawFrame

    ' ESCAPE key to exit application
    If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
    Wend

    TBGL_DestroyWindow



    'Python source page 41
    '# PyFunc.py
    '# Plotting functions
    'from OpenGL.GL import *
    'from OpenGL.GLU import *
    'from OpenGL.GLUT import *
    'from numpy import *
    'import sys
    'def init():
    ' glClearColor(1.0, 1.0, 1.0, 1.0)
    ' gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
    'def plotfunc():
    ' glClear(GL_COLOR_BUFFER_BIT)
    ' glColor3f(0.0, 0.0, 0.0)
    ' glPointSize(3.0)
    ' For x In arange(-5.0, 5.0, 0.1):
    ' y = x*x
    ' glBegin(GL_POINTS)
    ' glVertex2f(x, y)
    ' glEnd()
    ' glFlush()
    'def main():
    ' glutInit(sys.argv)
    ' glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    ' glutInitWindowPosition(50,50)
    ' glutInitWindowSize(400,400)
    ' glutCreateWindow("Function Plotter")
    ' glutDisplayFunc(plotfunc)
    ' init()
    ' glutMainLoop()
    'main()
    '# End of program[/code]
    Attached Files Attached Files
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Just little problem Kent,

    try to use this:
    [code=thinbasic]
    TBGL_RenderMatrix2D( -5, -5, 5, 5 )
    [/code]
    instead of what you have in code.

    TBGL_RenderMatrix2D has lower left corner specified first, and then top right one.
    You will see the plot will look the same as in the book.

    I must GOTO school now, will be back by night,
    have fun!

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Very punny Petr

    I didn't think GOTO was legal anymore?

    Fantastic work, both of you! I am hoping to get some studying in over the weekend so that I can start contributing a bit.

    This is great... thanks!

    Stan

    Quote Originally Posted by Petr Schreiber

    ...I must GOTO school now, will be back by night,
    have fun!

    Petr

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Quote Originally Posted by Stan Blank
    I didn't think GOTO was legal anymore?
    In thinBasic (even if it is a Basic language) it is VERY ILLEGAL
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    In this example if you resize TBGL window you lose you plotting.
    Problem not related to the exercise but would be nice to have it right.
    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Eros,

    good point!

    This can be easily fixed by putting the TBGL_RenderMatrix2D inside the loop.
    I also added one more tweak - it is slightly more efficient to call the TBGL_BeginPoly/TBGL_EndPoly just once and then fill it with vertices in the loop:
    [code=thinbasic]
    ' Example 5.25 Plotting 2D Functions

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

    ' Converted by Kent Sarikaya, minor tweaks by Petr Schreiber
    ' Last modified: February 25, 2010


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


    ' Handle for our window
    Local hWnd As DWord

    ' Create and show window
    hWnd = TBGL_CreateWindowEx("Function Plotter", 400, 400, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
    TBGL_ShowWindow

    ' 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

    ' Resets status of all keys
    TBGL_ResetKeyState()

    Local x,y As Single
    ' Main loop
    While TBGL_IsWindow(hWnd)

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

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

    For x = -5 To 5 Step 0.1
    y = x*x
    TBGL_Vertex(x,y)
    Next
    TBGL_EndPoly

    TBGL_DrawFrame

    ' ESCAPE key to exit application
    If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
    Wend

    TBGL_DestroyWindow


    'Python source page 41
    '# PyFunc.py
    '# Plotting functions
    'from OpenGL.GL import *
    'from OpenGL.GLU import *
    'from OpenGL.GLUT import *
    'from numpy import *
    'import sys
    'def init():
    ' glClearColor(1.0, 1.0, 1.0, 1.0)
    ' gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
    'def plotfunc():
    ' glClear(GL_COLOR_BUFFER_BIT)
    ' glColor3f(0.0, 0.0, 0.0)
    ' glPointSize(3.0)
    ' For x In arange(-5.0, 5.0, 0.1):
    ' y = x*x
    ' glBegin(GL_POINTS)
    ' glVertex2f(x, y)
    ' glEnd()
    ' glFlush()
    'def main():
    ' glutInit(sys.argv)
    ' glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    ' glutInitWindowPosition(50,50)
    ' glutInitWindowSize(400,400)
    ' glutCreateWindow("Function Plotter")
    ' glutDisplayFunc(plotfunc)
    ' init()
    ' glutMainLoop()
    'main()
    '# End of program
    [/code]

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Here is version which uses rendering callback,
    just like in GLUT:

    [code=thinbasic]
    ' Example 5.25 Plotting 2D Functions

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

    ' Converted by Kent Sarikaya, minor tweaks by Petr Schreiber
    ' Last modified: February 25, 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("Function Plotter", 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

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

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

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

    For x = -5 To 5 Step 0.1
    y = x*x
    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

    'Python source page 41
    '# PyFunc.py
    '# Plotting functions
    'from OpenGL.GL import *
    'from OpenGL.GLU import *
    'from OpenGL.GLUT import *
    'from numpy import *
    'import sys
    'def init():
    ' glClearColor(1.0, 1.0, 1.0, 1.0)
    ' gluOrtho2D(-5.0, 5.0, -5.0, 5.0)
    'def plotfunc():
    ' glClear(GL_COLOR_BUFFER_BIT)
    ' glColor3f(0.0, 0.0, 0.0)
    ' glPointSize(3.0)
    ' For x In arange(-5.0, 5.0, 0.1):
    ' y = x*x
    ' glBegin(GL_POINTS)
    ' glVertex2f(x, y)
    ' glEnd()
    ' glFlush()
    'def main():
    ' glutInit(sys.argv)
    ' glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    ' glutInitWindowPosition(50,50)
    ' glutInitWindowSize(400,400)
    ' glutCreateWindow("Function Plotter")
    ' glutDisplayFunc(plotfunc)
    ' init()
    ' glutMainLoop()
    'main()
    '# End of program
    [/code]


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    I would say TBGL_BindPeriodicFunction function is great.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Agreed!

    Nice job!

    Stan

    Quote Originally Posted by Eros Olmi
    I would say TBGL_BindPeriodicFunction function is great.

  10. #10
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Example: Section 5.2 (page 41), Plotting 2D Functions

    Thanks Petr, I was hoping that you will show us optimizations and to fix things like my plot not being the same.
    So thanks for helping and showing them when you can. That bind function command is really neat. Another new one I had no idea that existed.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Page 1 of 2 12 LastLast

Similar Threads

  1. Example: Section 5.2 (page 38/39), Plot Point, Diagonale + mirror 2D Functions
    By Lionheart008 in forum ThinBASIC programming in OpenGL/TBGL
    Replies: 5
    Last Post: 27-02-2010, 05:28

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •