ReneMiner
24-03-2014, 16:57
Played a little around this morning to make some use of udt-functions.
Result is a small unit-file which can be used to control camera-entity & look-at-position in 3d-space.
Simple testscript, use arrow-keys to move on XZ-plane, pageup+pagedown to move along Y-axis
hold shift-key to send input to camera directly
home-key resets the look-at-position to 0,0,0
needs attached unit-file below
Uses "TBGL"
Begin Const
%sScene = 1
'---
%eCamera = 1
%eLight ' a light-source gets attached to the camera if specified
%eLookAt ' some "pointer" to visualize the position we look at
%eBox ' some visible stuff for the eyes
%eTorus
End Const
#INCLUDE "t_Camera.tBasicU" ' that unit takes care of camera
Dim Camera As t_camera
'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
Function TBMain()
Local hWin As DWord '-- handle of window
Local scrWidth, scrHeight, scrDepth As Long '-- store screen-info
Local FrameRate As Double '-- use this for constant speed
TBGL_GetDesktopInfo(ScrWidth, ScrHeight, ScrDepth)
hWin = TBGL_CreateWindowEx( "TBGL-script, ESC to quit", _
640, 480, scrDepth, _
%TBGL_WS_WINDOWED Or %TBGL_WS_DONTSIZE Or %TBGL_WS_CLOSEBOX _
)
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- open the window
TBGL_ShowWindow
If TBGL_IsWindow(hWin) Then
TBGL_SceneCreate(%sScene)
' init the camera with default white light-source attached
' |has light| Target X,Y,Z |Angle|Height|Distance
Camera.Init( %sScene, %eCamera, %eLight, 1.0, 2.0, 3.0, 360.0, 8.0, 32.0 )
' something to visualize where we look at
TBGL_EntityCreateSphere ( %sScene, %eLookAt, 0, 1)
' some stuff to please the eyes
TBGL_EntityCreateBox( %sScene, %eBox, 0, 3, 3, 3, 0, 0, 128, 255 )
TBGL_EntitySetPos ( %sScene, %eBox, 4, 3, 2 )
TBGL_EntityCreateTorus ( %sScene, %eTorus, 0, 2, 4, 0, 255, 0, 128 )
TBGL_EntitySetPos ( %sScene, %eTorus, -2, -3, -4 )
EndIf
' -- Main-Loop
While TBGL_IsWindow(hWin)
FrameRate = TBGL_GetFrameRate
' -- proceed inputs:
If TBGL_GetWindowKeyState(hWin, %VK_ESCAPE) Then Exit While
If TBGL_GetWindowKeyState(hWin, %VK_SHIFT) Then
' input moves camera
If TBGL_GetWindowKeyState(hWin, %VK_UP) Then
Camera.ZoomIn()
ElseIf TBGL_GetWindowKeyState(hWin, %VK_DOWN) Then
Camera.ZoomOut()
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGUP) Then
' height-change-speed depends on total distance
Camera.Lift( Sqr(Pow(Camera.Distance, 2) + Pow(Camera.Height, 2)) * Framerate/1000 )
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGDN) Then
Camera.Lift(-Sqr(Pow(Camera.Distance, 2) + Pow(Camera.Height, 2)) * FrameRate/1000 )
ElseIf TBGL_GetWindowKeyState(hWin, %VK_LEFT) Then
Camera.Turn(-FrameRate/16 )
ElseIf TBGL_GetWindowKeyState(hWin, %VK_RIGHT) Then
Camera.Turn( FrameRate/16 )
EndIf
Else
' input moves look-at-position (target) then
If TBGL_GetWindowKeyState(hWin, %VK_UP) Then
Camera.MoveTarget(%Move_Forward, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_DOWN) Then
Camera.MoveTarget(%Move_Backward, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGUP) Then
Camera.MoveTarget(%Move_Up, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGDN) Then
Camera.MoveTarget(%Move_Down, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_LEFT) Then
Camera.MoveTarget(%Move_Left, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_RIGHT) Then
Camera.MoveTarget(%Move_Right, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_HOME) Then
' simply look at 0,0,0
Camera.SetTargetPos( 0.0, 0.0, 0.0 )
EndIf
EndIf
TBGL_ClearFrame
' place a "look-at-pointer" so we don't lose orientation in space
TBGL_EntitySetPos(%sScene, %eLookAt, Camera.Target.X, Camera.Target.Y, Camera.Target.Z )
TBGL_SceneRender(%sScene)
TBGL_DrawFrame
Wend
End Function
Result is a small unit-file which can be used to control camera-entity & look-at-position in 3d-space.
Simple testscript, use arrow-keys to move on XZ-plane, pageup+pagedown to move along Y-axis
hold shift-key to send input to camera directly
home-key resets the look-at-position to 0,0,0
needs attached unit-file below
Uses "TBGL"
Begin Const
%sScene = 1
'---
%eCamera = 1
%eLight ' a light-source gets attached to the camera if specified
%eLookAt ' some "pointer" to visualize the position we look at
%eBox ' some visible stuff for the eyes
%eTorus
End Const
#INCLUDE "t_Camera.tBasicU" ' that unit takes care of camera
Dim Camera As t_camera
'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
Function TBMain()
Local hWin As DWord '-- handle of window
Local scrWidth, scrHeight, scrDepth As Long '-- store screen-info
Local FrameRate As Double '-- use this for constant speed
TBGL_GetDesktopInfo(ScrWidth, ScrHeight, ScrDepth)
hWin = TBGL_CreateWindowEx( "TBGL-script, ESC to quit", _
640, 480, scrDepth, _
%TBGL_WS_WINDOWED Or %TBGL_WS_DONTSIZE Or %TBGL_WS_CLOSEBOX _
)
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- open the window
TBGL_ShowWindow
If TBGL_IsWindow(hWin) Then
TBGL_SceneCreate(%sScene)
' init the camera with default white light-source attached
' |has light| Target X,Y,Z |Angle|Height|Distance
Camera.Init( %sScene, %eCamera, %eLight, 1.0, 2.0, 3.0, 360.0, 8.0, 32.0 )
' something to visualize where we look at
TBGL_EntityCreateSphere ( %sScene, %eLookAt, 0, 1)
' some stuff to please the eyes
TBGL_EntityCreateBox( %sScene, %eBox, 0, 3, 3, 3, 0, 0, 128, 255 )
TBGL_EntitySetPos ( %sScene, %eBox, 4, 3, 2 )
TBGL_EntityCreateTorus ( %sScene, %eTorus, 0, 2, 4, 0, 255, 0, 128 )
TBGL_EntitySetPos ( %sScene, %eTorus, -2, -3, -4 )
EndIf
' -- Main-Loop
While TBGL_IsWindow(hWin)
FrameRate = TBGL_GetFrameRate
' -- proceed inputs:
If TBGL_GetWindowKeyState(hWin, %VK_ESCAPE) Then Exit While
If TBGL_GetWindowKeyState(hWin, %VK_SHIFT) Then
' input moves camera
If TBGL_GetWindowKeyState(hWin, %VK_UP) Then
Camera.ZoomIn()
ElseIf TBGL_GetWindowKeyState(hWin, %VK_DOWN) Then
Camera.ZoomOut()
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGUP) Then
' height-change-speed depends on total distance
Camera.Lift( Sqr(Pow(Camera.Distance, 2) + Pow(Camera.Height, 2)) * Framerate/1000 )
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGDN) Then
Camera.Lift(-Sqr(Pow(Camera.Distance, 2) + Pow(Camera.Height, 2)) * FrameRate/1000 )
ElseIf TBGL_GetWindowKeyState(hWin, %VK_LEFT) Then
Camera.Turn(-FrameRate/16 )
ElseIf TBGL_GetWindowKeyState(hWin, %VK_RIGHT) Then
Camera.Turn( FrameRate/16 )
EndIf
Else
' input moves look-at-position (target) then
If TBGL_GetWindowKeyState(hWin, %VK_UP) Then
Camera.MoveTarget(%Move_Forward, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_DOWN) Then
Camera.MoveTarget(%Move_Backward, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGUP) Then
Camera.MoveTarget(%Move_Up, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_PGDN) Then
Camera.MoveTarget(%Move_Down, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_LEFT) Then
Camera.MoveTarget(%Move_Left, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_RIGHT) Then
Camera.MoveTarget(%Move_Right, FrameRate/1000)
ElseIf TBGL_GetWindowKeyState(hWin, %VK_HOME) Then
' simply look at 0,0,0
Camera.SetTargetPos( 0.0, 0.0, 0.0 )
EndIf
EndIf
TBGL_ClearFrame
' place a "look-at-pointer" so we don't lose orientation in space
TBGL_EntitySetPos(%sScene, %eLookAt, Camera.Target.X, Camera.Target.Y, Camera.Target.Z )
TBGL_SceneRender(%sScene)
TBGL_DrawFrame
Wend
End Function