primo
13-11-2015, 16:22
i have made some tests on GBuffers as posted by Petr http://www.thinbasic.com/community/showthread.php?12606-glDrawElements-and-related-opengl-functions&p=92240#post92240
and it is really facilitating the plotting and reducing the code amount and provide a speedy rendering of the arrays it works on
here is the math curve plotted in the previous link but here with GBuffers
tested with TB beta 1.9.16.3
9353
Uses "TBGL"
Function TBMain()
Local hWnd As DWord
Local FrameRate As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers - press ESC to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_BackColor 255,255,255
' -- Create 3D triangle buffer
Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
'Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_LINES, %TBGL_3D)
Global Nb As Integer = 120
' -- Define data for it
Global VertexA(Nb,Nb) As TBGL_TVECTOR3F
Global ColorA(Nb,Nb) As TBGL_TRGB
FillArrays ' call the sub to fill VertexA , ColorA arrays
Dim NumOfarrayElements As Long = CountOf(VertexA(1))* CountOf(VertexA(2))
' -- Create buffer dynamically linked to the arrays above
TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, NumOfarrayElements, VertexA(1,1), ColorA(1,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, 3, 4, 0, 0, 0)
' -- Turn triangle
TBGL_Rotate GetTickCount/50, 0, 1, 0
' -- 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
Sub FillArrays()
Dim xMin, yMin, zMin, xMax, yMax, zMax, range, step1, x, y, z As Single
xMin = -1 : yMin = -1: zMin = -1: xMax = 1: yMax = 1: zMax = 1
'xMin = -0.5 : zMin = -0.5 : xMax = 0.5: zMax = 0.5
Dim a,b As Integer
range = xMax - xMin
step1 = range / Nb
x = xMin: z = zMin : y = yMin
For b=1 To Nb
For a=1 To Nb
y = Sin(10*(x^2+z^2))/5
VertexA(a,b).x = x
VertexA(a,b).y = y
VertexA(a,b).z = z
If y >= 0 Then 'color red if y>=0 esle color blue
ColorA(a,b).r = 255
ColorA(a,b).g = 0
ColorA(a,b).b = 0
Else
ColorA(a,b).r = 0
ColorA(a,b).g = 0
ColorA(a,b).b = 255
End If
x = x + step1
Next a
x = xMin
z = z + step1
Next b
'=================================================================================
End Sub
as a funny variation, using the same GBuffer method but plotting the shapes data produced somehow by WolphramAlpha site , as an example: Bart Simpson_like curve:
http://www.wolframalpha.com/input/?i=Bart+Simpson%E2%80%90like+curve&lk=1&a=ClashPrefs_*PopularCurve.BartSimpsonCurve-
its x,y data is 13KB, very big, and to obtain it hover the mouse at the bottom of its equations in the page look the picture
9354
and click on 'A' for a copyable text version, save it to a file and apply the attached "VBRegExp_Test_Replace.tbasic" which is essentialy the thinbasic official example applying it to the file such as simpsons.txt to convert its equations to the thinbasic version
note that the x = ... is going to about 6000 characters so we need to partition it in every about 2000 characters or so using the underscore _ , and thinbasic do a good job in this
VBRegExp_Test_Replace.tbasic: opening "simpsons.txt" file and applying several simple regexes to it ans saving it to simpsons2.txt. apply the same method with other curves equations downloaded from wolframAlpha, the "simpsons.txt" available in the attached rar file
Uses "VBREGEXP", "file"
dim lpRegExp as dword
dim strText as string value "The quick brown fox jumped over the lazy dog."
Dim strRetVal As String
Dim FileHandle As DWord
Dim FileName As String
'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New
'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
MSGBOX 0, "Unable to create an instance of the RegExp object." & $crlf & "Script terminated"
stop
end if
'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1
Dim i As Integer
FileName = APP_SourcePath + "simpsons.txt" ' Build filename
strText = FILE_Load(FileName)
VBRegExp_SetPattern lpRegExp, "(\d\s)sin"
strRetVal = VBRegExp_Replace(lpRegExp, strText, "$1*sin")
VBRegExp_SetPattern lpRegExp, "(\d\s)t"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "$1*t")
VBRegExp_SetPattern lpRegExp, "(\)\s)theta"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "$1*theta")
VBRegExp_SetPattern lpRegExp, "(\d\s)pi"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "$1*pi")
VBRegExp_SetPattern lpRegExp, "sqrt"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "sqr")
FILE_Save(APP_SourcePath + "simpsons2.txt", strRetVal)
'---Deallocate regular expression resource
IF istrue lpRegExp THEN VBREGEXP_Release(lpRegExp)
MsgBox 0, "ok"
since the files x,y data are too big i attach the tbasic files as rar files , simpsons.curve, ketty_curve, cannabis_curve, rose_curve, there is also curves3.txt which contains more curves data you can test, some files such as rose_curve needs 10 seconds to display depends on your system speed
and it is really facilitating the plotting and reducing the code amount and provide a speedy rendering of the arrays it works on
here is the math curve plotted in the previous link but here with GBuffers
tested with TB beta 1.9.16.3
9353
Uses "TBGL"
Function TBMain()
Local hWnd As DWord
Local FrameRate As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Plotting 3D Data in an array using GBuffers - press ESC to quit", 600, 600, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
TBGL_BackColor 255,255,255
' -- Create 3D triangle buffer
Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
'Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_LINES, %TBGL_3D)
Global Nb As Integer = 120
' -- Define data for it
Global VertexA(Nb,Nb) As TBGL_TVECTOR3F
Global ColorA(Nb,Nb) As TBGL_TRGB
FillArrays ' call the sub to fill VertexA , ColorA arrays
Dim NumOfarrayElements As Long = CountOf(VertexA(1))* CountOf(VertexA(2))
' -- Create buffer dynamically linked to the arrays above
TBGL_GBufferDefineFromArray(gbPoints, %TBGL_DYNAMIC, NumOfarrayElements, VertexA(1,1), ColorA(1,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, 3, 4, 0, 0, 0)
' -- Turn triangle
TBGL_Rotate GetTickCount/50, 0, 1, 0
' -- 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
Sub FillArrays()
Dim xMin, yMin, zMin, xMax, yMax, zMax, range, step1, x, y, z As Single
xMin = -1 : yMin = -1: zMin = -1: xMax = 1: yMax = 1: zMax = 1
'xMin = -0.5 : zMin = -0.5 : xMax = 0.5: zMax = 0.5
Dim a,b As Integer
range = xMax - xMin
step1 = range / Nb
x = xMin: z = zMin : y = yMin
For b=1 To Nb
For a=1 To Nb
y = Sin(10*(x^2+z^2))/5
VertexA(a,b).x = x
VertexA(a,b).y = y
VertexA(a,b).z = z
If y >= 0 Then 'color red if y>=0 esle color blue
ColorA(a,b).r = 255
ColorA(a,b).g = 0
ColorA(a,b).b = 0
Else
ColorA(a,b).r = 0
ColorA(a,b).g = 0
ColorA(a,b).b = 255
End If
x = x + step1
Next a
x = xMin
z = z + step1
Next b
'=================================================================================
End Sub
as a funny variation, using the same GBuffer method but plotting the shapes data produced somehow by WolphramAlpha site , as an example: Bart Simpson_like curve:
http://www.wolframalpha.com/input/?i=Bart+Simpson%E2%80%90like+curve&lk=1&a=ClashPrefs_*PopularCurve.BartSimpsonCurve-
its x,y data is 13KB, very big, and to obtain it hover the mouse at the bottom of its equations in the page look the picture
9354
and click on 'A' for a copyable text version, save it to a file and apply the attached "VBRegExp_Test_Replace.tbasic" which is essentialy the thinbasic official example applying it to the file such as simpsons.txt to convert its equations to the thinbasic version
note that the x = ... is going to about 6000 characters so we need to partition it in every about 2000 characters or so using the underscore _ , and thinbasic do a good job in this
VBRegExp_Test_Replace.tbasic: opening "simpsons.txt" file and applying several simple regexes to it ans saving it to simpsons2.txt. apply the same method with other curves equations downloaded from wolframAlpha, the "simpsons.txt" available in the attached rar file
Uses "VBREGEXP", "file"
dim lpRegExp as dword
dim strText as string value "The quick brown fox jumped over the lazy dog."
Dim strRetVal As String
Dim FileHandle As DWord
Dim FileName As String
'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New
'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
MSGBOX 0, "Unable to create an instance of the RegExp object." & $crlf & "Script terminated"
stop
end if
'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1
Dim i As Integer
FileName = APP_SourcePath + "simpsons.txt" ' Build filename
strText = FILE_Load(FileName)
VBRegExp_SetPattern lpRegExp, "(\d\s)sin"
strRetVal = VBRegExp_Replace(lpRegExp, strText, "$1*sin")
VBRegExp_SetPattern lpRegExp, "(\d\s)t"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "$1*t")
VBRegExp_SetPattern lpRegExp, "(\)\s)theta"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "$1*theta")
VBRegExp_SetPattern lpRegExp, "(\d\s)pi"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "$1*pi")
VBRegExp_SetPattern lpRegExp, "sqrt"
strRetVal = VBRegExp_Replace(lpRegExp, strRetVal, "sqr")
FILE_Save(APP_SourcePath + "simpsons2.txt", strRetVal)
'---Deallocate regular expression resource
IF istrue lpRegExp THEN VBREGEXP_Release(lpRegExp)
MsgBox 0, "ok"
since the files x,y data are too big i attach the tbasic files as rar files , simpsons.curve, ketty_curve, cannabis_curve, rose_curve, there is also curves3.txt which contains more curves data you can test, some files such as rose_curve needs 10 seconds to display depends on your system speed