PDA

View Full Version : GBuffer tests



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

ErosOlmi
13-11-2015, 19:38
Wow, thanks!
This is an EXTREME test for thinBasic parser and interpreter!

I've never interpreted a math expression so big.
For example simpsons_curve.tbasic example contains two math expressions calculating x and y:


X calculation is a math function 6575 bytes long
y calculation is a math function 6593 bytes long


They are calculated for 22624 times.

3 things regarding thinBasic tricks:

no need to use A or B at NEXT A and NEXT B, just NEXT. ThinBasic will know what variable to increment
no need to add _ for line continuation when last char of the previous line is a delimiter like a math symbol
because in this examples you got formulas from external source, you can set the X and Y calculation in an external file and include it in the script just like it would be inline with your code.
In this way you can keep your script into one source and BIG formulas into another script. Example:

While t <= 72*Pi

#INCLUDE "simpsons.txt.tbasic"


'x = ((-5/37 *Sin(61/42-8 *t)-112/41 ...
'Y = ((-9/23 *Sin(38/25-6 *t)-67/38 ...


VertexA(N).x = x*0.04
VertexA(N).y = y*0.04
ColorA(N).r = 255 :ColorA(N).g = 0 :ColorA(N).b = 0


t + 0.01 : N + 1


Wend



Thanks a lot for this present.
I want to see Petr Schreiber face when he will see this :)

Ciao
Eros

Petr Schreiber
13-11-2015, 20:25
Hehe,

this is just perfect :D


Petr

primo
13-11-2015, 21:29
thanks Eros, Petr for the valuable info, especialy the #INCLUDE "simpsons.txt.tbasic" this will makes the life easier with the very huge files available in wolfram alpha site and other sources.
there is a blog about these topics:
http://blog.wolframalpha.com/2013/01/08/mathematics-as-an-art-form-visualizing-equations/
http://blog.wolfram.com/2013/05/17/making-formulas-for-everything-from-pi-to-the-pink-panther-to-sir-isaac-newton/

regards

Petr Schreiber
14-11-2015, 10:02
Just a hint - because the loading takes a while, what about adding some animation to track the progress?

Something to get you started:

#1 Add this function to your code


Function ReportProgress(ratio As Number)
Single i
Single radians

' -- Whatever color is set inside block, we want it to be reset to white
' -- once block is finished
TBGL_PushColor 255, 255, 255

TBGL_ClearFrame
TBGL_Rotate 360*ratio/2+180 ' -- Rotating the following shape to appear symetrical

TBGL_Color 255,255-255*ratio,255-255*ratio ' -- Nice color fade out

TBGL_BeginPoly %GL_POLYGON

TBGL_Vertex 0, 0, -1

For i = 0 To 360*ratio Step 0.1
radians = DegToRad(-i+90)
TBGL_Vertex Cos(radians)*0.1, Sin(radians)*0.1, -1
Next

TBGL_EndPoly

TBGL_DrawFrame

TBGL_PopColor

End Function


#2 Add this function call right before Wend in your While loop in FillArrays


If Mod(N, 500) = 0 Then
ReportProgress(t/(72*Pi))
End If
Petr

ErosOlmi
14-11-2015, 11:31
Very nice Petr!

A growing pie let the time (in some way) pass quickly !
It's a new relativistic postulate unable to be explained :)

primo
14-11-2015, 12:50
Petr, this is what i thought of before but don't know how to do that, thanks for this addition.
i have found also that we don't need to dimension the structured arrays in exact number previously, so this is good, this is my new approach:
Global VertexA(1) As TBGL_TVECTOR3F
Global ColorA(1) As TBGL_TRGB
and in the FillArrays sub we do depends on
ReDim Preserve VertexA(n)
ReDim Preserve ColorA(n)
to resize the arrays, it consumes a little additional time
here is the simpsons curve again but adding the Eros and Petr suggestions about loading data from external files, and the adding of the waiting animation
but i am confused by the addition of #INCLUDE "BartSimpsons.tbasic" inside the loop: While t <= 72*Pi , is it executed every time the loop run ?, my hard disk led does not work in my computer so i can't see the hard disk activity while waiting the calculation. but i think it runs once somehow by monitoring the execution time.

Uses "TBGL"
Uses "math"

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 points buffer
Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
'Dim gbPoints As DWord = TBGL_GBufferCreate(%TBGL_LineStrip, %TBGL_3D)

' -- Define data for it
Global VertexA(1) As TBGL_TVECTOR3F
Global ColorA(1) As TBGL_TRGB

FillArrays ' call the sub to fill VertexA , ColorA arrays

' -- 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()

' -- Main loop
While TBGL_IsWindow(hWnd)

FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Camera(0, 0, 100, 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 x, y, z, t As Single

DWord n = 1
ReDim VertexA(n)
ReDim colorA(n)

While t <= 72*Pi
#INCLUDE "BartSimpsonsData.tbasic"

VertexA(n).x = x*0.04
VertexA(n).y = y*0.04
ColorA(n).r = 255 :ColorA(N).g = 0 :ColorA(N).b = 0


t + 0.01 : n + 1
ReDim Preserve VertexA(n)
ReDim Preserve ColorA(n)
If Mod(n, 500) = 0 Then
ReportProgress(t/(72*Pi))
End If
Wend

End Sub

Function theta(a As Single) As Single
If a<=0 Then
Function = 0
Else
Function = 1
End If

End Function

Function ReportProgress(ratio As Number)
Single i
Single radians

' -- Whatever color is set inside block, we want it to be reset to white
' -- once block is finished
TBGL_PushColor 255, 255, 255

TBGL_ClearFrame
TBGL_Rotate 360*ratio/2+180 ' -- Rotating the following shape to appear symetrical

TBGL_Color 255,255-255*ratio,255-255*ratio ' -- Nice color fade out

TBGL_BeginPoly %GL_POLYGON

TBGL_Vertex 0, 0, -1

For i = 0 To 360*ratio Step 0.1
radians = DegToRad(-i+90)
TBGL_Vertex Cos(radians)*0.1, Sin(radians)*0.1, -1
Next

TBGL_EndPoly

TBGL_DrawFrame

TBGL_PopColor

End Function

ErosOlmi
14-11-2015, 14:06
but i am confused by the addition of #INCLUDE "BartSimpsons.tbasic" inside the loop: While t <= 72*Pi , is it executed every time the loop run ?


#Include is resolved just once during pre-parsing of thinBasic scripts.
Code founds into included files is just included once regardless where #Include is placed.