Option _Explicit
DefInt I
DefSng S
DefLng L
DefStr T
Const True = -1, False = Not True

Dim Shared iGlInit
Dim lMyScreen, iMaxScreenX, iMaxScreenY, iMinScreenX, iMinScreenY ' variables for managing screen visible
Dim sMyBallX, sMyBallY, sMyBallSpeedX, sMyBallSpeedY 'variables for managing ball
Dim iRed, iGreen, iBlue, iAlpha, sRed, sGreen, sBlue, sAlpha ' variables for managing colors
Dim tInk

iMaxScreenX = 799
iMaxScreenY = 599
iMinScreenX = 0
iMinScreenY = 0
iGlInit = True

lMyScreen = _NewImage(iMaxScreenX + 1, iMaxScreenY + 1, 32) ' it creates a graphic surface of 800x600 pixels (0-799,0-599)
If lMyScreen < -1 Then Screen lMyScreen Else Print "Error creating screen" 'it gives feedback if there is a graphic error

'          STANDARD SYSTEM COHORDINATES
' 0,0      TopLeft-------------TopRight    799,0
'             |                   |
'             |                   |
'             |                   |
'             |                   |
' 0, 599  BottomLeft------------BottomRight     799, 599

'                     OPENGL SYSTEM COHORDINATES
' -1,1            TopLeft-------------TopRight    1,1
'                     |                   |
'                     |                   |
'                     |                   |
'                     |                   |
' -1, -1         BottomLeft------------BottomRight   1,-1

_Title "OpenGl 2D demo: step 2 viewport and Lines"
Locate 2, 1: Print "press a ESCAPE key to quit, SPACE key to change viewport"

While tInk <> Chr$(27) ' main loop to manage the flow of application
    ' to see OpenGl results you must wait at least the first second after window of program has been displayed
    tInk = InKey$ ' input manager
    If tInk = Chr$(32) Then iGlInit = Not iGlInit ' command manager
    _Limit 20 ' it is useful to limit the activity of CPU while we use the GPU (openGl)
Wend
System

'OpenGl area-----------------------------
Sub _GL ()
    Shared iMinScreenX, iMinScreenY, iMaxScreenX, iMaxScreenY
    0 'this sub runs about 60 times for second
    1 ' here you must put all OpenGl statements
    2 ' here you can put standard QB64/QB statements
    3 ' putting here statements that change or stop the flow of program is a BAD PRACTICE, deprecated!
    4 ' it is useful to have an initialization routine called only when we need it

    If iGlInit Then
        _glViewport iMinScreenX, iMinScreenY, (iMaxScreenX + 1), (iMaxScreenY + 1)
    Else ' only if we need initialization these instructions (in the sub GlInit) will be executed

        _glViewport iMinScreenX, iMinScreenY, (iMaxScreenX + 1) / 2, (iMaxScreenY + 1) / 2
        ' setting Opengl viewport to half width an half height of screen window
    End If


    ' 2D graphic primitives area-----------------------
    ' 1 defining color
    ' 2 _glBEGIN primitives name
    ' 3 _glvertex2f needed from graphic primitives
    ' 4 _glEND

    ' graphic primitives available
    'GL_POINTS   Draws points on screen. Every vertex specified is a point.
    'GL_LINES    Draws lines on screen. Every two vertices specified compose a line.
    'GL_LINE_STRIP   Draws connected lines on screen. Every vertex specified after first two are connected.
    'GL_LINE_LOOP    Draws connected lines on screen. The last vertex specified is connected to first vertex.
    'GL_TRIANGLES    Draws triangles on screen. Every three vertices specified compose a triangle.
    'GL_TRIANGLE_STRIP   Draws connected triangles on screen. Every vertex specified after first three vertices creates a triangle.
    'GL_TRIANGLE_FAN Draws connected triangles like GL_TRIANGLE_STRIP, except draws triangles in fan shape.
    'GL_QUADS    Draws quadrilaterals (4 – sided shapes) on screen. Every four vertices specified compose a quadrilateral.
    'GL_QUAD_STRIP   Draws connected quadrilaterals on screen. Every two vertices specified after first four compose a connected quadrilateral.
    'GL_POLYGON  Draws a polygon on screen. Polygon can be composed of as many sides as you want.
    'https://en.wikibooks.org/wiki/OpenGL_Programming/GLStart/Tut3

    '1          Red, Green, Blue
    _glColor3f 1.0, 0.0, 0.0 ' here it defines the color to use for drawing like in _RGB32 with value from 0.0 to 1.0
    'it is red color
    '2
    _glBegin _GL_LINES 'it starts a graphic primitive, in this case the primitive is a line of size of 8 pixels
    '3
    ' _glVertex2f passes 2 float digits (x,y) to _GL_LINES for drawing a line with specified color
    _glVertex2f -1, -1 'Bottom Left point
    _glVertex2f 1, 1 'Top Right point
    '4
    _glEnd


    '1          Red, Green, Blue
    _glColor3f 0.0, 1.0, 0.0 ' it is green color
    '2
    _glBegin _GL_LINES
    '3
    _glVertex2f -1, 1 ' Top Left point
    _glVertex2f 1, -1 'Bottom Right point
    '4
    _glEnd ' it closes the instructions for the above graphic primitive invoked

End Sub


