Option _Explicit
DefInt I
DefSng S
DefLng L
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

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


_Title "OpenGl 2D demo: step 3  LineLoop and transformation cohordinates"
Locate 2, 1: Print "press a key to quit"
_FullScreen

While InKey$ = "" ' 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
    _Limit 5 ' it is useful to limit the activity of CPU while we use the GPU (openGl)
Wend
System

'OpenGl area-----------------------------
Sub _GL ()

    ' 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

    If iGlInit Then GlInit ' only if we need initialization these instructions (in the sub GlInit) will be executed
    _glClear (_GL_COLOR_BUFFER_BIT) ' it clears buffer of color
    ' using the default colorclean

    '          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
    _glBegin _GL_LINE_LOOP 'it starts a graphic primitive
    ' _glVertex2f passes 2 float digits (x,y) to _GL_LINES_loop for drawing a point with specified color
    _glVertex2f Proportion(0, 799, -1, 1, 0), Proportion(0, 599, -1, 1, 0) 'Bottom Left point
    _glVertex2f Proportion(0, 799, -1, 1, 0), Proportion(0, 599, -1, 1, 599) ' Top Left point
    _glVertex2f Proportion(0, 799, -1, 1, 799), Proportion(0, 599, -1, 1, 599) 'Top Right point
    _glVertex2f Proportion(0, 799, -1, 1, 799), Proportion(0, 599, -1, 1, 0) 'Bottom Right point
    _glEnd ' it closes the instructions for the above graphic primitive invoked

    _glPointSize 4
    _glColor3f Proportion!(0, 255, 0, 1, 127), Proportion!(0, 255, 0, 1, 10), Proportion!(0, 255, 0, 1, 227)
    _glBegin _GL_POINTS
    _glVertex2f 0, 0 'Proportion!(0, 799, -1, 1, 400), Proportion!(0, 599, 1, -1, 300)
    _glEnd

End Sub

Sub GlInit ()
    Shared iMaxScreenX, iMaxScreenY, iMinScreenX, iMinScreenY
    _glViewport iMinScreenX, iMinScreenY, iMaxScreenX + 1, iMaxScreenY + 1
    _glClearColor 0, 0, .4, 1 ' it sets color _glclear() that is like CLS,color
    'and color is defined in RGBA mode with range 0-1
    _GLRender _Behind
    iGlInit = False
End Sub

Function Proportion! (min1 As Integer, max1 As Integer, min2 As Integer, max2 As Integer, Value As Single)
    Proportion! = ((Abs(max2 - min2) * Value) / Abs(max1 - min1)) + min2
End Function
