Hello,
here is one simple example on how to replace arrow keys movement with mouse movement:
- left mouse button = forward
- right mouse button = backward
- moving mouse left/right = turning left/right
'---Load needed modules
Uses "TBGL"
Begin Const
' -- Scene IDs
%sScene = 1
' -- Entity IDs
%eCamera = 1
%eGrid
End Const
Function TBMain()
Dim hWnd As DWord
'---Creates OpenGL window and returns handle
hWnd = TBGL_CreateWindowEx("Moving using mouse - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED )
'---Shows TBGL window
TBGL_ShowWindow
' -- First we will create world as place to contain entities
TBGL_SceneCreate(%sScene)
' -- Our world will have two entities
' .. Camera
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos (%sScene, %eCamera, 0.0, 1.7, 0.0 )
' .. Grid
TBGL_EntityCreateDLSlot(%sScene, %eGrid, 0, CreateGridAsDList() )
Dim FrameRate As Double
Dim Sheeps As Long
Dim mouseCenterX, mouseCenterY As Long
'---Resets key status before checking
TBGL_ResetKeyState()
'---Main script loop
While TBGL_IsWindow(hWnd)
'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movements relative to frame rate
FrameRate = TBGL_GetFrameRate
' -- Center cursors and retrieve its position
TBGL_CenterCursor
mouseCenterX = TBGL_MouseGetPosX
mouseCenterY = TBGL_MouseGetPosY
Sleep 10
'---Prepares clear frame
TBGL_ClearFrame
TBGL_SceneRender(%sScene)
TBGL_DrawFrame ' Swaps the buffers - displays rendered image
' -- If mouse pressed, push the camera
If TBGL_MouseGetLButton Then TBGL_EntityPush(%sScene, %eCamera, 0, 0, 5/FrameRate) ' 5 meters/second
If TBGL_MouseGetRButton Then TBGL_EntityPush(%sScene, %eCamera, 0, 0, -5/FrameRate)
' -- If mouse is not in center anymore, turn
TBGL_EntityTurn(%sScene, %eCamera, 0, -5*(TBGL_MouseGetPosX - mouseCenterX)/FrameRate, 0)
If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) Then Exit While
Wend
'---Closes OpenGL window
TBGL_DestroyWindow
End Function
Function CreateGridAsDList() As Long ' Returns to which display list we save
local i, j as long
tbgl_NewList 1
'---Let's build a grid
TBGL_BeginPoly %GL_LINES ' Starts polygon definition based on 2 vertex lines
TBGL_Color 0,255,0 ' Defines color
For i = -20 To 20
For j = -20 To 20
TBGL_Vertex -20, 0, j ' Adds vertex
TBGL_Vertex 20, 0, j ' Adds vertex
TBGL_Vertex i, 0, -20 ' Adds vertex
TBGL_Vertex i, 0, 20 ' Adds vertex
Next
Next
TBGL_EndPoly
tbgl_EndList
function = 1
End Function
Petr
Bookmarks