03-25-2023, 04:01 PM
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.
[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.
More examples will follow.
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
[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
More examples will follow.