PDA

View Full Version : TBGL using a window control and events



ErosOlmi
07-10-2019, 21:43
Hi all,

while making few improvements to thinBasic UI engine, I was looking an old TBGL script from Robbek user at https://www.thinbasic.com/community/showthread.php?12412 (https://www.thinbasic.com/community/showthread.php?12412-Lorenz-4D-Lists&highlight=lorenz)
I transformed it into a script using new UI window and control events and what come out was the following script.

In this script you will see:

windows and control events
TBGL working on a window control
TBGL rendering function working on a timer base
TBGL rendering/resizing while drawing
TBGL rendering on dynamic values changed at runtime


Maybe it can inspire more new ideas.


10069 10073




'-----------------------------------------------------------
'Original script from user Robbek at:
' https://www.thinbasic.com/community/showthread.php?12412-Lorenz-4D-Lists&highlight=lorenz
'-----------------------------------------------------------


#minversion 1.11.1


Uses "UI"
uses "TBGL"


Dim x, y, z, dx, dy, dz, dt, a, b, c, sumx, sumy, sumz As Single
Dim i, j As Long


a = 8
b = 8 / 3
c = 30
dt= 0.01
x = 0.04 : y = 0.04 : z = 0.04

For i = 1 To 240 ' idle loops to a +- center point
singlestep()
x = x + dx
y = y + dy
z = z + dz
Next

'-----------------------------------------------------------
Sub dotheLorenz(n As Integer)
'-----------------------------------------------------------
Local xdup, ydup, zdup As Single

TBGL_Translate x, y, z

TBGL_PushMatrix
TBGL_NewList 1

sumx = 0 : sumy = 0 : sumy = 0
singlestep()
xdup = x : ydup = y : zdup = z
sumx = dx : sumy = dy : sumz = dz


TBGL_SetPrimitiveQuality TrackBar_GetPos (MainWindow.Handle, %barPrimitiveQuality1)
TBGL_PolygonLook %GL_LINE
For j = 2 To n / 2
singlestep()
TBGL_Color (150 + y * 5, 150 + x * 5, 150 + z * 5)
TBGL_Sphere 0.05 * (n / (n - j) * j)
TBGL_Translate dx, dy, dz
Next


TBGL_SetPrimitiveQuality TrackBar_GetPos (MainWindow.Handle, %barPrimitiveQuality2)
TBGL_PolygonLook %GL_FILL
For j = n / 2 To n
singlestep()
TBGL_Color (150 + y * 5, 150 + x * 5, 150 + z * 5)
TBGL_Sphere 1.2
TBGL_Translate dx, dy, dz
Next

x = xdup
y = ydup
z = zdup

TBGL_EndList
TBGL_PopMatrix

End Sub


'-----------------------------------------------------------
Sub singlestep()
'-----------------------------------------------------------
dx = (a * y - a * x) * dt
dy = (x * (c - z) - y) * dt
dz = (x * y - b * z) * dt
x = x + dx
y = y + dy
z = z + dz
End Sub




' -- ID numbers of controls
Begin ControlID
%lblCanvas
%btnClose
%barLorenzObjects
%barPrimitiveQuality1
%barPrimitiveQuality2

%myTimer
End ControlID


Begin Const
%MAIN_WIDTH = 1240
%MAIN_HEIGHT = 800


%timeOut = 40 ' -- Determines graphics refresh rate in milliseconds
End Const


'-----------------------------------------------------------
'
'-----------------------------------------------------------
Function TBMain()
'-----------------------------------------------------------


Dialog New Pixels, name MainWindow, 0, "Use arrow keys - Pg U/D - Lorenz 4D",
-1, -1, %MAIN_WIDTH, %MAIN_HEIGHT,
%WS_POPUP |
%WS_VISIBLE |
%WS_CLIPCHILDREN |
%WS_CLIPSIBLINGS |
%WS_CAPTION |
%WS_SYSMENU |
%WS_MINIMIZEBOX |
%WS_MAXIMIZEBOX |
%WS_THICKFRAME

Dialog Show Modal MainWindow.Handle

End Function


'-----------------------------------------------------------
' OnInit: fired only once when Dialog Show is executed
'-----------------------------------------------------------
CallBack Function MainWindow_OnInit()
'-----------------------------------------------------------


'---Add needed Controls
'---A lable will be used as rendering control
Control Add Label name CanvasControl, CBHNDL, %lblCanvas, "", 0, 0, %MAIN_WIDTH - 150, %MAIN_HEIGHT
Control Set Color CBHNDL, %lblCanvas, %BLACK, %BLACK
Control Set Resize CBHNDL, %lblCanvas, 1, 1, 1, 1

Control Add Trackbar, CBHNDL, %barLorenzObjects,"", %MAIN_WIDTH - 140, 35, 130, 25
TrackBar_SetRange CBHNDL, %barLorenzObjects, 100, 600
TrackBar_SetPos CBHNDL, %barLorenzObjects, 300
Control Set Resize CBHNDL, %barLorenzObjects, 0, 1, 1, 0


Control Add Trackbar, CBHNDL, %barPrimitiveQuality1,"", %MAIN_WIDTH - 140, 60, 130, 25
TrackBar_SetRange CBHNDL, %barPrimitiveQuality1, 3, 50
TrackBar_SetPos CBHNDL, %barPrimitiveQuality1, 3
Control Set Resize CBHNDL, %barPrimitiveQuality1, 0, 1, 1, 0


Control Add Trackbar, CBHNDL, %barPrimitiveQuality2,"", %MAIN_WIDTH - 140, 85, 130, 25
TrackBar_SetRange CBHNDL, %barPrimitiveQuality2, 3, 50
TrackBar_SetPos CBHNDL, %barPrimitiveQuality2, 3
Control Set Resize CBHNDL, %barPrimitiveQuality2, 0, 1, 1, 0


'---Button used to close window
Control Add Button name btnClose, CBHNDL, %btnClose, "Close", %MAIN_WIDTH - 140, %MAIN_HEIGHT - 35, 130, 25
Control Set Resize CBHNDL, %btnClose, 0, 1, 0, 1

'---Set window minimal dimension
Dialog Set Minsize CBHNDL, 320, 230


'---Set window timer used to call rendering function
Dialog Set Timer CBHNDL, %myTimer, %timeOut, %NULL


'---Init OpenGL Control
TBGL_BindCanvas(CanvasControl.Handle)
TBGL_SetDrawDistance 300
TBGL_UseLighting(%TRUE)
TBGL_UseLightSource(%GL_LIGHT0, %TRUE)
TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_POSITION, 15, 10, 15, 1)


end Function


'-----------------------------------------------------------
' OnCallBack: fired for all events not having a MainWindow_On... specific event function
'-----------------------------------------------------------
CallBack Function MainWindow_OnCallback()
'-----------------------------------------------------------
Select Case CBMSG


CASE %WM_SYSCOMMAND
'---Avoid to close with [X] button
'---Only btnClose will close the window
IF (CBWPARAM AND &HFFF0) = %SC_CLOSE THEN
FUNCTION = 1
END IF

End Select
End Function


'-----------------------------------------------------------
' OnSize: fired when window is resized
'-----------------------------------------------------------
CallBack Function MainWindow_OnSize()
'-----------------------------------------------------------
TBGL_UpdateCanvasProportions(CanvasControl.Handle)
RenderMyImage(CanvasControl.Handle)
end Function


'-----------------------------------------------------------
' OnTimer: executed at time interval
'-----------------------------------------------------------
CallBack Function MainWindow_OnTimer()
'-----------------------------------------------------------
RenderMyImage(CanvasControl.Handle)
end Function




'-----------------------------------------------------------
' OnClick: Fired when btnClose button is clicked
'-----------------------------------------------------------
CallBack Function btnClose_OnClick()
'-----------------------------------------------------------
TBGL_ReleaseCanvas(CanvasControl.Handle)
Dialog Kill Timer MainWindow.Handle, %myTimer
Dialog End MainWindow.Handle
end Function


'-----------------------------------------------------------
' executed by timer interval
'-----------------------------------------------------------
Function RenderMyImage(byval hControl as long)
'-----------------------------------------------------------
Static FrameRate As Double
Static vp,rx,ry As Single
Static cnt As Long

If TBGL_CanvasBound(hControl) Then


FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Translate sumx , sumy , sumz
TBGL_Camera(0, 0 , 80 + vp, 0, 0, 0)
TBGL_Rotate rx, 0, 1, 0
TBGL_Rotate ry, 1, 0, 0
dotheLorenz(TrackBar_GetPos (Win_GetParent(hControl), %barLorenzObjects))
TBGL_CallList 1

TBGL_DrawFrame

TBGL_DeleteList 1

cnt += 1
If cnt = 10000 Then
For i = 1 To 240 ' idle loops to a +- center point
singlestep()
Next


cnt = 0
a = 10
b = 8 / 3
c = 28
dt= 0.01
x = 0.04 : y = 0.04 : z = 0.04
End If


If TBGL_GetWindowKeyState(hControl, %VK_LEFT) Then rx += 15/framerate
If TBGL_GetWindowKeyState(hControl, %VK_RIGHT) Then rx -= 15/framerate
If TBGL_GetWindowKeyState(hControl, %VK_PGUP) Then vp += 15/framerate
If TBGL_GetWindowKeyState(hControl, %VK_PGDN) Then vp -= 15/framerate
If TBGL_GetWindowKeyState(hControl, %VK_UP) Then ry += 15/framerate
If TBGL_GetWindowKeyState(hControl, %VK_DOWN) Then ry -= 15/framerate


End If
End Function

Petr Schreiber
10-10-2019, 18:51
Beautifully modernised :eusaclap:

It is really a joy to watch UI handling code with less of the low level Win32 stuff :)

One question - how could I enable the X close button to work?

From TBGL / 3D perspective, just a tip - display lists give the performance benefit, especially in interpreter, if they are used to cache complex geometry and then you invoke it just by single function call.

In this example, display list object is created each frame, then called and then deleted.

This does not give any advantage, actually, it has some overhead.

You can safely remove TBGL_NewList 1, TBGL_EndList, TBGL_CallList 1 and TBGL_DeleteList 1 and it will still look the same and maybe run a bit faster.


Petr