Advertisement
UEZ

OpenGL PT Test

UEZ
Oct 6th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Ported to FreeBasic by UEZ build 2016-10-01
  2. 'OpenGL version by Elor
  3.  
  4. #lang "FB"
  5.  
  6. #include "fbgfx.bi"
  7. #include "GL/gl.bi"
  8.  
  9. Declare Sub InitOpenGL (ByVal W As Integer, ByVal H As Integer)
  10. Declare Sub PythagorasTreeRec(iX1 As Single, iY1 As Single, iX2 As Single, iY2 As Single, iRecDepth As UByte)
  11. Declare Function _Sin6th(fX As Double) As Double
  12. Declare Function _Cos6th(fX As Double) As Double
  13.  
  14. Dim As String sTitle = "OpenGL Animated Pythagoras Tree v2.5 / FPS: "
  15. Dim As ULong iFPS = 0
  16. Dim As Double fTime, fTimer
  17.  
  18. InitOpenGL(1200, 700)
  19.  
  20. fTimer= Timer ()
  21.  
  22. Do
  23.     glClear (GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
  24.    
  25.     PythagorasTreeRec(550, 700, 650, 700, 15)
  26.     Flip()
  27.    
  28.     glFlush()
  29.    
  30.     If(Timer - fTimer > 0.99) Then
  31.         WindowTitle (sTitle & iFPS)
  32.         iFPS = 0
  33.         fTimer = Timer
  34.     Else
  35.         iFPS += 1
  36.     EndIf
  37.    
  38.     Sleep (10)
  39. Loop Until InKey = Chr(27)
  40.    
  41. /' --- Impementation --- '/
  42. Sub InitOpenGL (ByVal W As Integer, ByVal H As Integer)
  43.     ScreenRes (W, H, 32,, FB.GFX_OPENGL)' Or FB.GFX_NO_SWITCH)
  44.     glMatrixMode(GL_PROJECTION)
  45.     glLoadIdentity ()
  46.    
  47.     glViewport (0, 0, W, H)
  48.     glOrtho (0, W, H, 0, -128, 128)
  49.     glMatrixMode (GL_MODELVIEW)
  50.     glEnable (GL_CULL_FACE)
  51.     glCullFace (GL_BACK)
  52.     glLoadIdentity ()
  53.  
  54.     glClearColor (1.0, 1.0, 1.0, 0.5)
  55.     glEnable (GL_DEPTH_TEST)
  56.     glDepthFunc (GL_LESS)
  57. End Sub
  58.  
  59. Sub PythagorasTreeRec(iX1 As Single, iY1 As Single, iX2 As Single, iY2 As Single, iRecDepth As UByte)
  60.     Static i As Single= 0
  61.     'Dim As ULong iBGR1 = (255 - (iRecDepth + 10) * 10) Shl 8 + (iRecDepth * 20) Shl 0 'r=0
  62.     Dim As ULong iC1 = (iRecDepth * 20), iC2 = (iRecDepth + 5) * 8
  63.     iC1 = IIf(iC1 > 255, 255, iC1)
  64.     iC2 = IIf(iC2 > 255, 255, iC2)
  65.     Dim As ULong iBGR2 = iC2 Shl 16 + (255 - iC1) Shl 8
  66.      
  67.     Dim As Single dx = iX2 - iX1, dy = iY1 - iY2
  68.     Dim As Single iX3 = iX2 - dy, iY3 = iY2 - dx
  69.     Dim As Single iX4 = iX1 - dy, iY4 = iY1 - dx
  70.     Dim As Single iX5 = iX4 + (dx - dy) / (2.5 - _Cos6th((iX4 + i) / 400) / 1.5)
  71.     Dim As Single iY5 = iY4 - (dx + dy) / (2.25 + _Sin6th((iX5 + iY4 - i) / 500))
  72.     i += 0.00015
  73.    
  74.     glBegin(GL_POLYGON)
  75.     glColor3ub (iBGR2 Shr 16, iBGR2 Shr 8 And &HFF, iBGR2 And &HFF)
  76.     glVertex2f (iX1, iY1)
  77.     glVertex2f (iX2, iY2)
  78.     glVertex2f (iX3, iY3)
  79.     glVertex2f (iX4, iY4)
  80.     glVertex2f (iX1, iY1)
  81.     glEnd()
  82.    
  83.   If(iRecDepth > 0) Then
  84.         PythagorasTreeRec(iX4, iY4, iX5, iY5, iRecDepth - 1)
  85.         PythagorasTreeRec(iX5, iY5, iX3, iY3, iRecDepth - 1)
  86.   End If
  87. End Sub
  88.  
  89. Function _Sin6th(fX As Double) As Double
  90.    Asm
  91.       jmp _Sin6th_Start
  92.          _Sin6th_Mul: .double 683565275.57643158
  93.          _Sin6th_Div: .double -0.0000000061763971109087229
  94.          _Sin6th_Rnd: .double 6755399441055744.0
  95.        
  96.       _Sin6th_Start:
  97.          movq xmm0, [fX]
  98.          mulsd xmm0, [_Sin6th_Mul]
  99.          addsd xmm0, [_Sin6th_Rnd]
  100.          movd ebx, xmm0
  101.    
  102.          lea  eax, [ebx*2+0x80000000]
  103.          sar  eax, 2
  104.          imul eax
  105.          sar  ebx, 31
  106.          lea  eax, [edx*2-0x70000000]
  107.          lea  ecx, [edx*8+edx-0x24000000]
  108.          imul edx
  109.          xor  ecx, ebx
  110.          lea  eax, [edx*8+edx+0x44A00000]
  111.          imul ecx
  112.          
  113.          cvtsi2sd xmm0, edx
  114.          mulsd xmm0, [_Sin6th_Div]
  115.          movq [Function], xmm0
  116.    End Asm
  117. End Function
  118.  
  119. Function _Cos6th(fX As Double) As Double
  120.    Asm
  121.       jmp _Cos6th_Start
  122.          _Cos6th_Mul: .double 683565275.57643158
  123.          _Cos6th_Div: .double -0.0000000061763971109087229
  124.          _Cos6th_Rnd: .double 6755399441055744.0
  125.        
  126.       _Cos6th_Start:
  127.          movq xmm0, [fX]
  128.          mulsd xmm0, [_Cos6th_Mul]
  129.          addsd xmm0, [_Cos6th_Rnd]
  130.          movd ebx, xmm0
  131.          
  132.          add ebx, 0x40000000 'SinToCos
  133.    
  134.          lea  eax, [ebx*2+0x80000000]
  135.          sar  eax, 2
  136.          imul eax
  137.          sar  ebx, 31
  138.          lea  eax, [edx*2-0x70000000]
  139.          lea  ecx, [edx*8+edx-0x24000000]
  140.          imul edx
  141.          xor  ecx, ebx
  142.          lea  eax, [edx*8+edx+0x44A00000]
  143.          imul ecx
  144.          
  145.          cvtsi2sd xmm0, edx
  146.          mulsd xmm0, [_Cos6th_Div]
  147.          movq [Function], xmm0
  148.    End Asm
  149. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement