The following example is even simpler and still very fast.
It uses a compiled FreeBasic function passing BYREF the 3 Single element of the UDT.
The main loop FOR/NEXT is inside thinBasic interpreted code but the hard job is done by FreeBASIC compiled function.
Only pay attention to t variable that I declared Static to the function so it will retain its value on function exit.
Uses "TBGL"'Uses "math"
Type Point3D
x As Single
y As Single
z As Single
End Type
Type Colr
r As Byte
g As Byte
b As Byte
End Type
Function TBMain()
Local hWnd As DWord
Local FrameRate As Double
Local i as long
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers- Calculate with freebasic..Esc to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_BackColor 255,255,255
'TBGL_SetDrawDistance 250
' -- Create 3D points buffer
DWord gbPoints = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
Global Nb As DWord = 22619 ' 72*pi/0.01
' -- Define data for it
dim VertexA(Nb) As Point3D ' single
dim ColorA(Nb) As Colr ' Byte
for i=1 to Nb
'call freeBasic function to fill the arrays with data
FB_3SingleByRef(VertexA(i).x, VertexA(i).y, VertexA(i).z)
ColorA(i).r = 255
ColorA(i).g = 0
ColorA(i).b = 0
next
' -- Create buffer dynamically linked to the arrays above
TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, CountOf(VertexA), VertexA(1), ColorA(1))
' -- Resets status of all keys
TBGL_ResetKeyState()
TBGL_PointSize 2
' -- Main loop
While TBGL_IsWindow(hWnd)
'init
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera(0, 0, 90, 0, 0, 0)
' -- Turn triangle
TBGL_Rotate GetTickCount/50, 0, 1, 0
TBGL_Scale 0.6, 0.6, 0.6
' -- Render it
TBGL_GBufferRender(gbPoints)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
' -- Destroying the buffer is not necessary,
' -- the garbage collector will take care of it
' -- Destroy window
TBGL_DestroyWindow
End Function
#compiled "===Passing 3 single variable ByRef==="
Function theta(a As Single) As Single
If a<=0 Then
Function = 0
Else
Function = 1
End If
End Function
'----------------------------------------
function FB_3SingleByRef Cdecl (byref x as single, byref y as single, byref z as single) As long Export
'----------------------------------------
Static t As SINGLE '---<<<< STATIC !
Dim Pi As SINGLE
Pi = 3.141592
'---This contain BIG expression too complex for thinBasic interpeter
#INCLUDE ".\BartSimpsonsData.tbasic"
x = x * 0.05
y = y * 0.05
z = 0
t = t + 0.01
return 0
end Function
#endcompiled
Bookmarks