Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenGL examples
#1
I'll say straight away that I literally only passed very light OpenGL. Still, I have a few absolutely basic things to post here.

The first program is completely primitive. Draws a triangle and a square in 2D, without texture.

Code: (Select All)
$Resize:On
Screen _NewImage(800, 600, 32)
Do While InKey$ <> Chr$(27)
    _Limit 20
Loop

Sub _GL
    _glViewport 0, 0, 800, 600
    _glMatrixMode _GL_PROJECTION '//                        set projection matrix
    _glLoadIdentity '             //                        Reset matrix
    _gluPerspective 45.0F, 800 / 600, 0.1F, 100.0F '        claculate perspective
    _glMatrixMode _GL_MODELVIEW '                           set matrix _GL_MODELVIEW
    _glLoadIdentity
    _glShadeModel _GL_SMOOTH '                              allow smooth shading
    _glClearColor 0.0F, 0.0F, 0.0F, 0.5F
    _glClearDepth 1.0F '                                    set depth buffer
    _glEnable _GL_DEPTH_TEST '                              allow depth testing
    _glDepthFunc _GL_LEQUAL '                               depth testing type
    _glHint _GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST '    set the best projection correction
    _glClear _GL_COLOR_BUFFER_BIT: _glClear _GL_DEPTH_BUFFER_BIT 'clear screen and depth buffer
    _glLoadIdentity '                                            matrix reset
    ' Sem kresli primitivy
    _glTranslatef -1.5F, 0.0F, -6.0F 'Shift to the left and to the depth - be careful, every other shift on the screen moves from the place where we moved before!
    _glBegin _GL_TRIANGLES '          Begin draw triangle
    _glVertex3f 0.0F, 1.0F, 0.0F '    upper point
    _glVertex3f -1.0F, -1.0F, 0.0F '  left bottom point
    _glVertex3f 1.0F, -1.0F, 0.0F '   right bottom point
    _glEnd '                          End draw triangle
    _glTranslatef 3.0F, 0.0F, 0.0F '  we will move in the x-axis by 1.5 to the center and 1.5 to the right, where we will paint a square
    _glBegin _GL_QUADS '              Square draw begin
    _glVertex3f -1.0F, 1.0F, 0.0F '   left upper point
    _glVertex3f 1.0F, 1.0F, 0.0F '    right upper point
    _glVertex3f 1.0F, -1.0F, 0.0F '   right bottom point
    _glVertex3f -1.0F, -1.0F, 0.0F '  left bottom point
    _glEnd '                          end draw square
End Sub


[Image: OGL1.png]


[size=1]182 / 5 000

[/size]



This source code shows how to draw a pyramid in color with blending colors on the walls, as well as how to draw a cube with walls of one color and how to rotate these objects.


Code: (Select All)
$Resize:On
Screen _NewImage(800, 600, 32) '
Dim Shared rquad As _Float: rquad = 7
Dim Shared rtri As _Float: rtri = 7

Do While InKey$ <> Chr$(27)
    _Limit 20
Loop

Sub _GL
    _glViewport 0, 0, _Width, _Height
    _glMatrixMode _GL_PROJECTION '                          Set projection matrix
    _glLoadIdentity '                                       Matrix reset
    _gluPerspective 45.0F, _Width / _Height, 0.1F, 100.0F ' Perspective calculation
    _glMatrixMode _GL_MODELVIEW '                           set modelview matrix
    _glLoadIdentity
    _glShadeModel _GL_SMOOTH '                              allow smooth shading
    _glClearColor 0.0F, 0.0F, 0.0F, 0.5F
    _glClearDepth 1.0F '                                    set depth buffer
    _glEnable _GL_DEPTH_TEST '                              allow depth testing
    _glDepthFunc _GL_LEQUAL '                               set depth testing method
    _glHint _GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST '    set nicest projection matrix
    _glClear _GL_COLOR_BUFFER_BIT: _glClear _GL_DEPTH_BUFFER_BIT 'clear screen and depth buffer
    _glLoadIdentity '                                             matrix reset
    ' draw primitives here

    _glTranslatef -1.5F, 0.0F, -6.0F '       Shift to the left and to the depth - be careful, every other shift on the screen moves from the place where we moved before!


    _glRotatef rtri, 0.0F, 1.0F, 0.0F ' Rotate the triangle around the y-axis

    _glBegin _GL_TRIANGLES '            The beginning of drawing the PYRAMID

    _glColor3f 1.0F, 0.0F, 0.0F '       Red color (it is as _glColor3f Red, Green, Blue and values can be calculated as 1 / 255)
    _glVertex3f 0.0F, 1.0F, 0.0F '      Upper point
    _glColor3f 0.0F, 1.0F, 0.0F '       Green color
    _glVertex3f -1.0F, -1.0F, 1.0F '    left bottom point
    _glColor3f 0.0F, 0.0F, 1.0F '       blue color
    _glVertex3f 1.0F, -1.0F, 1.0F '     right bottom point

    _glColor3f 1.0F, 0.0F, 0.0F '  red color
    _glVertex3f 0.0F, 1.0F, 0.0F ' upper point (right wall)
    _glColor3f 0.0F, 0.0F, 1.0F '  blue color
    _glVertex3f 1.0F, -1.0F, 1.0F 'left point (right wall)
    _glColor3f 0.0F, 1.0F, 0.0F '  green
    _glVertex3f 1.0F, -1.0F, -1.0F ' right point (right wall)                 '

    _glColor3f 1.0F, 0.0F, 0.0F '    red
    _glVertex3f 0.0F, 1.0F, 0.0F '   upper point (rear wall)
    _glColor3f 0.0F, 1.0F, 0.0F '    green
    _glVertex3f 1.0F, -1.0F, -1.0F ' left point (rear wall)
    _glColor3f 0.0F, 0.0F, 1.0F '    blue
    _glVertex3f -1.0F, -1.0F, -1.0F 'right point (rear wall)

    _glColor3f 1.0F, 0.0F, 0.0F '    red
    _glVertex3f 0.0F, 1.0F, 0.0F '   upper point (back wall)
    _glColor3f 0.0F, 0.0F, 1.0F '    blue
    _glVertex3f -1.0F, -1.0F, -1.0F 'left point (left wall)
    _glColor3f 0.0F, 1.0F, 0.0F '    green
    _glVertex3f -1.0F, -1.0F, 1.0F ' right point (left wall)

    _glEnd 'triangle draw end

    _glTranslatef 3.0F, 0.0F, 0.0F 'we will move in the x-axis by 1.5 to the center and 1.5 to the right, where we will paint a quad
    'FOR THE ENTIRE OBJECT IN ONE COLOR:

    _glLoadIdentity '                  we call it to align the X Y Z axes to the original direction, without it it would default to the previous rotated state
    _glTranslatef 1.5F, 0.0F, -7.0F '  The displacement of the origin is higher than in a quad

    _glRotatef rquad, 1.0F, 1.0F, 1.0F 'Rotate the quad around the x-axis

    _glBegin _GL_QUADS '               begin draw quad
    _glColor3f 0.0F, 1.0F, 0.0F '      green color
    _glVertex3f 1.0F, 1.0F, -1.0F '    left upper point
    _glVertex3f -1.0F, 1.0F, -1.0F '   right upper point
    _glVertex3f -1.0F, 1.0F, 1.0F '    right bottom point
    _glVertex3f 1.0F, 1.0F, 1.0F '     left bottom point

    _glColor3f 1.0F, 0.5F, 0.0F '      orange color
    _glVertex3f 1.0F, -1.0F, 1.0F '    right upper point (bottom wall)
    _glVertex3f -1.0F, -1.0F, 1.0F '   left upper point  (bottom wall)
    _glVertex3f -1.0F, -1.0F, -1.0F '  left bottom point (bottom wall)
    _glVertex3f 1.0F, -1.0F, -1.0F '   right bottm point (bottom wall)

    _glColor3f 1.0F, 0.0F, 0.0F '       red
    _glVertex3f 1.0F, 1.0F, 1.0F '     right upper point (front wall)
    _glVertex3f -1.0F, 1.0F, 1.0F '    Left upper point (front wall)
    _glVertex3f -1.0F, -1.0F, 1.0F '   left bottom point (front wall)
    _glVertex3f 1.0F, -1.0F, 1.0F '    right bottom point (front wall)

    _glColor3f 1.0F, 1.0F, 0.0F '       yellow
    _glVertex3f 1.0F, -1.0F, -1.0F '   right upper point (rear wall)
    _glVertex3f -1.0F, -1.0F, -1.0F '  left upper point (rear wall)
    _glVertex3f -1.0F, 1.0F, -1.0F '   left bottom point (rear wall)
    _glVertex3f 1.0F, 1.0F, -1.0F '    right bottom point (rear wall)

    _glColor3f 0.0F, 0.0F, 1.0F '      blue
    _glVertex3f -1.0F, 1.0F, 1.0F '   right upper point (left wall)
    _glVertex3f -1.0F, 1.0F, -1.0F '  left upper point (left wall)
    _glVertex3f -1.0F, -1.0F, -1.0F ' left bottom point (left wall)
    _glVertex3f -1.0F, -1.0F, 1.0F '  right bottom point (left wall)


    _glColor3f 1.0F, 0.0F, 1.0F ' purple
    _glVertex3f 1.0F, 1.0F, -1.0F ' right upper point (right wall)
    _glVertex3f 1.0F, 1.0F, 1.0F ' left upper point (right wall)
    _glVertex3f 1.0F, -1.0F, 1.0F 'Left bottom point (right wall)
    _glVertex3f 1.0F, -1.0F, -1.0F 'Right bottom point (right wall)

    _glEnd 'quad draw end
    rtri = rtri + 0.52F 'Incrementing the angle of rotation of the triangle
    rquad = rquad - 0.515F 'Incrementing the angle of rotation of the quad

    'it is important to RESET THE AXES so that they are rotated to the basic setting (otherwise the X axis can move to the Y axis) using _glLoadIdentity,
    'and it is EXTREMELY IMPORTANT to always move in the scene to the beginning of the scene using _glTranslateF
    'moving the object in openGL is not done by recalculating _glVertex, but by moving the start of rendering using _glTranslatef
End Sub

[Image: OGL2.png]


More examples will follow.


Reply


Messages In This Thread
OpenGL examples - by Petr - 03-25-2023, 04:01 PM
RE: OpenGL examples - by AshishKingdom - 03-25-2023, 09:46 PM
RE: OpenGL examples - by mnrvovrfc - 03-25-2023, 10:15 PM
RE: OpenGL examples - by Petr - 03-26-2023, 12:26 PM
RE: OpenGL examples - by Petr - 03-29-2023, 01:58 PM
RE: OpenGL examples - by dcromley - 03-29-2023, 03:35 PM
RE: OpenGL examples - by TerryRitchie - 03-29-2023, 04:19 PM
RE: OpenGL examples - by AshishKingdom - 03-31-2023, 11:25 AM
RE: OpenGL examples - by Petr - 03-31-2023, 02:31 PM
RE: OpenGL examples - by Petr - 03-31-2023, 03:12 PM
RE: OpenGL examples - by Petr - 03-31-2023, 06:24 PM
RE: OpenGL examples - by Petr - 03-31-2023, 06:59 PM
RE: OpenGL examples - by TempodiBasic - 04-13-2023, 10:53 PM
RE: OpenGL examples - by Petr - 04-01-2023, 12:56 PM
RE: OpenGL examples - by Petr - 04-01-2023, 01:19 PM
RE: OpenGL examples - by MasterGy - 04-01-2023, 05:20 PM
RE: OpenGL examples - by Petr - 04-01-2023, 06:56 PM
RE: OpenGL examples - by Petr - 04-01-2023, 07:25 PM
RE: OpenGL examples - by Petr - 04-02-2023, 06:36 PM
RE: OpenGL examples - by mnrvovrfc - 04-02-2023, 09:48 PM
RE: OpenGL examples - by Petr - 04-02-2023, 09:03 PM
RE: OpenGL examples - by MasterGy - 04-03-2023, 12:21 PM
RE: OpenGL examples - by Petr - 04-03-2023, 02:12 PM
RE: OpenGL examples - by mnrvovrfc - 04-03-2023, 03:59 PM
RE: OpenGL examples - by Petr - 04-03-2023, 02:23 PM
RE: OpenGL examples - by Petr - 04-03-2023, 07:44 PM
RE: OpenGL examples - by Petr - 04-03-2023, 09:02 PM
RE: OpenGL examples - by Petr - 04-08-2023, 08:40 PM
RE: OpenGL examples - by Petr - 04-09-2023, 07:12 PM
RE: OpenGL examples - by mnrvovrfc - 04-10-2023, 12:42 PM
RE: OpenGL examples - by Petr - 04-12-2023, 08:05 PM
RE: OpenGL examples - by Petr - 04-13-2023, 09:11 PM
RE: OpenGL examples - by mnrvovrfc - 04-14-2023, 01:46 AM
RE: OpenGL examples - by TerryRitchie - 04-14-2023, 03:10 AM
RE: OpenGL examples - by TempodiBasic - 04-14-2023, 08:52 AM
RE: OpenGL examples - by bplus - 04-14-2023, 09:17 AM
RE: OpenGL examples - by MasterGy - 04-14-2023, 10:19 AM
RE: OpenGL examples - by Petr - 04-14-2023, 02:00 PM
RE: OpenGL examples - by Petr - 04-14-2023, 04:23 PM
RE: OpenGL examples - by Petr - 04-14-2023, 08:04 PM
RE: OpenGL examples - by Petr - 04-14-2023, 09:22 PM
RE: OpenGL examples - by Petr - 04-15-2023, 03:08 PM
RE: OpenGL examples - by bplus - 04-15-2023, 05:09 PM
RE: OpenGL examples - by Petr - 04-15-2023, 09:05 PM
RE: OpenGL examples - by Petr - 04-15-2023, 09:27 PM
RE: OpenGL examples - by bplus - 04-15-2023, 10:01 PM
RE: OpenGL examples - by Petr - 04-16-2023, 05:40 AM
RE: OpenGL examples - by bplus - 04-16-2023, 02:22 PM
RE: OpenGL examples - by Petr - 04-17-2023, 06:46 PM
RE: OpenGL examples - by MasterGy - 04-19-2023, 02:43 PM
RE: OpenGL examples - by Petr - 04-19-2023, 02:49 PM
RE: OpenGL examples - by MasterGy - 04-26-2023, 10:12 AM
RE: OpenGL examples - by Petr - 04-26-2023, 02:11 PM



Users browsing this thread: 19 Guest(s)