Advertisement
mabu

Draw OpenGL Cube

Jun 14th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define unicode
  2. #include "windows.bi"
  3. '
  4. #include "fbgfx.bi"
  5. #include "GL/gl.bi"
  6. #include "GL/glu.bi"
  7.  
  8.  
  9. Declare Sub DrawCube(ByVal a As Single, ByVal r1 As Single, ByVal g1 As Single, ByVal b1 As Single, ByVal a1 As Single, _
  10.                         ByVal r2 As Single, ByVal g2 As Single, ByVal b2 As Single, ByVal a2 As Single, _
  11.                         ByVal r3 As Single, ByVal g3 As Single, ByVal b3 As Single, ByVal a3 As Single, _
  12.                         ByVal r4 As Single, ByVal g4 As Single, ByVal b4 As Single, ByVal a4 As Single, _
  13.                         ByVal r5 As Single, ByVal g5 As Single, ByVal b5 As Single, ByVal a5 As Single, _
  14.                         ByVal r6 As Single, ByVal g6 As Single, ByVal b6 As Single, ByVal a6 As Single)
  15.  
  16.  
  17. Declare Sub DrawPoligon(ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, _
  18.                  ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, _
  19.                  ByVal x3 As Single, ByVal y3 As Single, ByVal z3 As Single, _
  20.                  ByVal x4 As Single, ByVal y4 As Single, ByVal z4 As Single, _
  21.                  ByVal r As Single, ByVal g As Single, ByVal b As Single, ByVal a As Single)
  22. Declare Function CalculateScaler(ByVal Param As Single, ByVal mMin As Single, _
  23.                                     ByVal mMax As Single, ByVal Scaler As Single)As Single
  24.  
  25.  
  26.  
  27. Sub DrawPoligon(ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, _
  28.                  ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, _
  29.                  ByVal x3 As Single, ByVal y3 As Single, ByVal z3 As Single, _
  30.                  ByVal x4 As Single, ByVal y4 As Single, ByVal z4 As Single, _
  31.                  ByVal r As Single, ByVal g As Single, ByVal b As Single, ByVal a As Single)
  32.     glBegin(GL_QUADS)
  33.         glColor4f(r,g,b,a)
  34.         glTexCoord2f(0, 0)
  35.         glVertex3d(x1, y1, z1)
  36.         glTexCoord2f(0, 1)
  37.         glVertex3d(x2, y2, z2)
  38.         glTexCoord2f(1, 1)
  39.         glVertex3d(x3, y3, z3)
  40.         glTexCoord2f(1, 0)
  41.         glVertex3d(x4, y4, z4)
  42.     glEnd()
  43.  
  44. End Sub
  45.  
  46. ' Вычисляет коэффициент
  47. Private Function CalculateScaler(ByVal Param As Single, ByVal mMin As Single, ByVal mMax As Single, ByVal Scaler As Single)As Single
  48.     If Param > mMax Then
  49.         Return -1
  50.     Else
  51.         If Param < mMin Then
  52.             Return 1
  53.         Else
  54.             Return Scaler
  55.         End If
  56.     End If
  57. End Function
  58.  
  59. Private Sub DrawCube(ByVal a As Single, ByVal r1 As Single, ByVal g1 As Single, ByVal b1 As Single, ByVal a1 As Single, _
  60.                         ByVal r2 As Single, ByVal g2 As Single, ByVal b2 As Single, ByVal a2 As Single, _
  61.                         ByVal r3 As Single, ByVal g3 As Single, ByVal b3 As Single, ByVal a3 As Single, _
  62.                         ByVal r4 As Single, ByVal g4 As Single, ByVal b4 As Single, ByVal a4 As Single, _
  63.                         ByVal r5 As Single, ByVal g5 As Single, ByVal b5 As Single, ByVal a5 As Single, _
  64.                         ByVal r6 As Single, ByVal g6 As Single, ByVal b6 As Single, ByVal a6 As Single)
  65.     ' нижняя грань
  66.     DrawPoligon(0,0,0, a,0,0, a,0,a, 0,0,a, r1,g1,b1,a1)
  67.     ' задняя грань
  68.     DrawPoligon(0,0,0, a,0,0, a,a,0, 0,a,0, r2,g2,b2,a2)
  69.     ' левая грань
  70.     DrawPoligon(0,0,0, 0,0,a, 0,a,a, 0,a,0, r3,g3,b3,a3)
  71.     ' верхняя грань
  72.     DrawPoligon(0,a,0, a,a,0, a,a,a, 0,a,a, r4,g4,b4,a4)
  73.     ' передняя грань
  74.     DrawPoligon(0,0,a, a,0,a, a,a,a, 0,a,a, r5,g5,b5,a5)
  75.     ' правая грань
  76.     DrawPoligon(a,0,0, a,0,a, a,a,a, a,a,0, r6,g6,b6,a5)
  77. End Sub
  78.  
  79.     ' Длина, ширина и цветовое разрешение экрана
  80.     Dim w As Integer = Any, h as Integer = Any, depth As Integer = Any
  81.     Dim ppos(3) As Single = {3,3,3,1}
  82.     Dim pdir(3) As Single = {-1,-1,-1}
  83.    
  84.     ' Генератор случайных чисел
  85.     Randomize(Timer)
  86.     ' Проверяем разрешение экрана
  87.     ScreenInfo(w, h, depth)
  88.     ' Устанавливаем графический режим с рисованием в опенгёл
  89.     ' с полученными параметрами длины и ширины экрана
  90.     ScreenRes(w, h, depth, , FB.GFX_OPENGL Or FB.GFX_FULLSCREEN)
  91.    
  92.     ' Конфигурация OpenGL
  93.     glMatrixMode(GL_PROJECTION)      ' Определение матрицы
  94.     glLoadIdentity()
  95.     glViewport(0,0,w,h)      ' Установка оси координат
  96.     gluPerspective(45,w/h, 1, 100)      ' Перспективная проекция
  97.     glMatrixMode(GL_MODELVIEW)       ' Отключить вывод невидимых частей
  98.     'glEnable(GL_CULL_FACE)
  99.     'glCullFace(GL_BACK)
  100.     'glEnable(GL_TEXTURE_2D)           ' Включение текстур
  101.     'glLoadIdentity()
  102.     glEnable(GL_DEPTH_TEST)          ' Тест глубины
  103.     'glDepthFunc(GL_LESS)
  104.     glEnable(GL_ALPHA_TEST)          ' Тест Альфа
  105.     'glAlphaFunc(GL_GREATER, 0.1)
  106.     ' разрешаем смешение цветов
  107.     glEnable(GL_BLEND)
  108.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  109.     gluLookAt(0,0,20, 0,0,0, 0,1,0)
  110.     ' glColor будет устанавливать
  111.     ' свойства материала
  112.     ' вам не надо дополнительно
  113.     ' вызывать glMaterialfv
  114.     'glEnable(GL_COLOR_MATERIAL)
  115.     ' разрешаем освещение
  116.     'glEnable(GL_LIGHTING)
  117.     '// включаем нулевую лампу
  118.     'glEnable(GL_LIGHT0)
  119.     ' устанавливаем положение нулевой лампы
  120.     'glLightfv(GL_LIGHT0, GL_POSITION, @ppos(0))
  121.     'glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, @pdir(0))
  122.    
  123.     ' Длина стороны куба
  124.     Dim CubeLength As Single = 1 + Rnd()
  125.     Dim CubeScaler As Single = 1
  126.    
  127.     ' Скалеры по координатам вершины куба
  128.     Dim ScaleX As Single = (-1)^(CInt(Rnd*10)), ScaleY As Single = (-1)^(CInt(Rnd*10)), ScaleZ As Single = (-1)^(CInt(Rnd*10))
  129.     Dim CubeX As Single = 2, CubeY As Single = 2, CubeZ As Single = 2
  130.  
  131.     ' Цвета граней куба
  132.     Dim ScaleCubeColor(5, 3) As Single = Any
  133.     Dim CubeColor(5, 3) As Single = Any
  134.     For k As Integer = 0 To 5
  135.         For i As Integer = 0 To 2
  136.             ScaleCubeColor(k, i) = (-1)^(CInt(Rnd*10))
  137.             CubeColor(k, i) = Rnd()
  138.         Next i
  139.     Next k
  140.     For i As Integer = 0 To 5
  141.         CubeColor(i, 3) = 0.89
  142.         ScaleCubeColor(i, 3) = (-1)^(CInt(Rnd*10))
  143.     Next i
  144.  
  145.     Do
  146.         glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
  147.         ' Рисую куб
  148.         DrawCube(CubeLength, CubeColor(0, 0), CubeColor(0, 1), CubeColor(0, 2),CubeColor(0, 3), CubeColor(1, 0), CubeColor(1, 1), CubeColor(1, 2),CubeColor(1, 3), CubeColor(2, 0), CubeColor(2, 1), CubeColor(2, 2),CubeColor(2, 3), CubeColor(3, 0), CubeColor(3, 1), CubeColor(3, 2),CubeColor(3, 3), CubeColor(4, 0), CubeColor(4, 1), CubeColor(4, 2),CubeColor(4, 3), CubeColor(5, 0), CubeColor(5, 1), CubeColor(5, 2), CubeColor(5, 3))
  149.         ' Вращаю сцену
  150.         glRotatef(ScaleX * ScaleY * ScaleZ * 0.5, CubeColor(0, 0), CubeColor(0, 1), CubeColor(0, 2))
  151.         ' Сдвигаю сцену (параллельный перенос)
  152.         glTranslatef(ScaleX * 0.009, ScaleY * 0.009, ScaleZ * 0.009)
  153.        
  154.         ' Вычисление вектора переноса сцены
  155.         ScaleX = CalculateScaler(CubeX, -10, 5, ScaleX)
  156.         CubeX += ScaleX * 0.009
  157.         ScaleY = CalculateScaler(CubeY, -10, 5, ScaleY)
  158.         CubeY += ScaleY * 0.009
  159.         ScaleZ = CalculateScaler(CubeZ, -10, 5, ScaleZ)
  160.         CubeZ += ScaleZ * 0.009
  161.         ' Изменяю цвета куба
  162.         For k As Integer = 0 To 5
  163.             For i As Integer = 0 To 2
  164.                 CubeColor(k, i) += ScaleCubeColor(k, i) * 0.005
  165.                 ScaleCubeColor(k, i) = CalculateScaler(CubeColor(k, i), 0, 1, ScaleCubeColor(k, i))
  166.             Next i
  167.         Next k
  168.         For i As Integer = 0 To 5
  169.             CubeColor(i, 3) += ScaleCubeColor(i, 3) * 0.005
  170.             ScaleCubeColor(i, 3) = CalculateScaler(CubeColor(i, 3), 0.7, 1, ScaleCubeColor(i, 3))
  171.         Next i
  172.         ' Вычисляю размеры куба
  173.         CubeScaler = CalculateScaler(CubeLength, 0.1, 10, CubeScaler)
  174.         CubeLength += CubeScaler * 0.01
  175.        
  176.         'glFlush()
  177.         Flip()
  178.         ' Задержку нужно вычислять отдельно
  179.         Sleep(10)
  180.     Loop Until MultiKey(FB.SC_ESCAPE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement