PDA

View Full Version : TBGL_Camera



David
01-11-2013, 20:18
Hi all,

I'm trying to understand how to TBGL works.

I had join two samples in one program. this is the result:


'=============================================================================
'= Font from texture =
'= =
'= Petr Schreiber, 2007 =
'=============================================================================

Uses "TBGL"
randomize timer

type TSphere
x as single
y as single
radius as single
cR as long
cG as long
cB as long
end type

Dim hWnd As Dword
dim NumSphere as long = 8
dim mySphere(NumSphere) as TSphere
DIM runText As Single
dim FrameRate as double
local i as long
Local SphereHeld, SphereHeldR, SphereHeldW as long
local x,y,z as double

hWnd = TBGL_CreateWindowex("Left Click: Drag / Right Click: Info", 640, 480, 32, %TBGL_WS_WINDOWED or %tbgl_WS_CLOSEBOX) ' Creates OpenGL window, it returns handle
TBGL_ShowWindow ' Shows the window

Tbgl_LoadBMPFont App_SourcePath+"Textures\TBGL_Font.bmp" ' Loads texture for font

for i = 1 to NumSphere
mySphere(i).x = rnd(-5,5)
mySphere(i).y = rnd(-3,3)
mySphere(i).radius = rndf(0.2, 1)

mySphere(i).cR = rnd(128,255)
mySphere(i).cG = rnd(128,255)
mySphere(i).cB = rnd(128,255)
next

runText = 50

TBGL_ResetKeyState() ' Resets key status before keypress checking

while TBGL_IsWindow(hWnd)

'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movement relative to framerate
FrameRate = tbgl_GetFrameRate

TBGL_ClearFrame ' Prepares clear frame

tbgl_UseLighting %TRUE
tbgl_UseLightSource %GL_LIGHT0, %TRUE

TBGL_Camera 0,0,10,0,0,0 ' Setups camera to look from 0,0,5 to 0,0,0

for i = 1 to NumSphere
tbgl_Pushmatrix
tbgl_Translate mySphere(i).x, mySphere(i).y, 0
tbgl_Color mySphere(i).cR, mySphere(i).cG, mySphere(i).cB
tbgl_Sphere mySphere(i).radius
tbgl_Popmatrix
next

if tbgl_MouseGetLButton then
if SphereHeld = 0 then SphereHeld = GetSphereID( tbgl_MouseGetPosX, tbgl_MouseGetPosY )
elseif tbgl_MouseGetLButton = 0 then
SphereHeld = 0
end if

if tbgl_MouseGetRButton then
if SphereHeldR = 0 then SphereHeldR = GetSphereID( tbgl_MouseGetPosX, tbgl_MouseGetPosY )
elseif tbgl_MouseGetRButton = 0 then
SphereHeldR = 0
end if

if SphereHeld then
TBGL_GetPixelInfo tbgl_MouseGetPosX, tbgl_MouseGetPosY, %TBGL_PINFO_XYZ, x,y,z
mySphere(SphereHeld).x = x
mySphere(SphereHeld).y = y
end if

if SphereHeldR then
TBGL_GetPixelInfo tbgl_MouseGetPosX, tbgl_MouseGetPosY, %TBGL_PINFO_XYZ, x,y,z
mySphere(SphereHeldR).x = x
mySphere(SphereHeldR).y = y
msgbox (0, "Selected Sphere:" & str$(SphereHeldR))
end if

TBGL_DrawFrame
TBGL_ClearFrame

tbgl_UseLighting %False
tbgl_UseLightSource %GL_LIGHT0, %False

TBGL_Camera 0,0,0.2,0,0,0
TBGL_Color 255,128,0
tbgl_printBMP "THIN",14,6

TBGL_Color 0,0,255
tbgl_printBMP "BASIC",18,6

TBGL_UseBlend %TRUE
TBGL_Color 255,255,0
tbgl_printBMP "- using TBGL module -",8,8+sin(timer)
TBGL_UseBlend %FALSE

runText -= 10 / FrameRate

if runText < -100 then runText = 50
TBGL_Color 255,255,255
tbgl_printBMP "thinBasic - interpreted language with no limits", runText, 25

TBGL_DrawFrame ' Swaps the buffers - displays rendered image

if TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) then EXIT WHILE

wend

TBGL_DestroyWindow ' Closes OpenGL window



function GetSphereID( mouseX as long, mouseY as long )
local cX, cY, cz as single
local i as long

TBGL_GetPixelInfo ( mouseX, mouseY, %TBGL_PINFO_XYZ, cx, cy, cz )
function = 0

for i = 1 to NumSphere
if cx >< mySphere(i).x-mySphere(i).radius, mySphere(i).x+mySphere(i).radius and _
cy >< mySphere(i).y-mySphere(i).radius, mySphere(i).y+mySphere(i).radius then
function = i
end if
next
end function


Sample works, but not really fine...

I had detected the TBGL_Camera changes from 0,0,10,0,0,0 to 0,0,0.2,0,0,0

the difference between 10 and 0.2 is very significative.

I'm trying to understand what means the TBGL_Camera values, in order to normalize with only one value.

Sometimes TBGL_Camera values appairs as Zoom of the scene and changes the logic of the x,y choords of the sphere (-5 to 5) instead (0 to 640)

Sometimes, the text appairs toooo small, almost invisible...

I can't understand this logic.
Could any tell me about the logic of TBGL_Camera, and it relation with the x,y choords ?

Thanks in advance,
David

Petr Schreiber
02-11-2013, 11:19
Hi David,

the TBGL Camera is pretty simple - imagine it as real world camera. You place objects at given positions in the virtual world, and then you can look at them.

How the world objects are displayed depends on:

where is camera located (parameters 1..3, represent x,y,z position)
where camera looks (parameters 4..6, represent x,y,z position of place where it looks to)


The issue with text is caused by fact it is designed to be used in 2D mode. But there is easy workaround - pair of TBGL_BeginPrintBMP/TBGL_EndPrintBMP will temporarily enable 2D mode.

Have a look at this modification, I added comments where it made sense:



Uses "TBGL"


Type TSphere
x As Single
y As Single
radius As Single
cR As Long
cG As Long
cB As Long
End Type

Global NumSphere As Long = 8
Global mySphere(NumSphere) As TSphere

Function TBMain()

Randomize Timer

Local hWnd As DWord
Local runText As Single
Local FrameRate As Double
Local i As Long
Local SphereHeld, SphereHeldR, SphereHeldW As Long
Local x,y,z As Double

hWnd = TBGL_CreateWindowEx("Left Click: Drag / Right Click: Info", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX) ' Creates OpenGL window, it returns handle
TBGL_ShowWindow ' Shows the window

TBGL_LoadBMPFont APP_SourcePath+"Textures\TBGL_Font.bmp" ' Loads texture for font

For i = 1 To NumSphere
mySphere(i).x = Rnd(-5,5)
mySphere(i).y = Rnd(-3,3)
mySphere(i).radius = Rndf(0.2, 1)

mySphere(i).cR = Rnd(128,255)
mySphere(i).cG = Rnd(128,255)
mySphere(i).cB = Rnd(128,255)
Next

runText = 50

'[] -- Initial setup
' -- It is enough to enable the light and source once, before the main loop
TBGL_UseLighting %TRUE
TBGL_UseLightSource %GL_LIGHT0, %TRUE

TBGL_ResetKeyState() ' Resets key status before keypress checking

While TBGL_IsWindow(hWnd)

'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movement relative to framerate
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame ' Prepares clear frame

'[] -- Render 3D Scene
TBGL_Camera 0, 0,10, ' -- Look from point X=0, Y=0, Z=10
0, 0, 0 ' -- to origin of coordinates

For i = 1 To NumSphere
TBGL_PushMatrix
TBGL_Translate mySphere(i).x, mySphere(i).y, 0
TBGL_Color mySphere(i).cR, mySphere(i).cG, mySphere(i).cB
TBGL_Sphere mySphere(i).radius
TBGL_PopMatrix
Next

'[] -- Draw text
TBGL_PushStateProtect %TBGL_LIGHTING Or %TBGL_DEPTHMASK ' -- We don't want the text to be lighted +
' -- We don't want the text to write to depth memory (confuses sphere drag)
TBGL_BeginPrintBMP ' -- This makes sure text is not affected by camera

TBGL_Color 255,128,0
TBGL_PrintBMP "THIN",14,6

TBGL_Color 0,0,255
TBGL_PrintBMP "BASIC",18,6

TBGL_PushState %TBGL_BLEND
TBGL_Color 255,255,0
TBGL_PrintBMP "- using TBGL module -",8,8+Sin(Timer)
TBGL_UseBlend %FALSE

runText -= 10 / FrameRate

If runText < -100 Then runText = 50
TBGL_Color 255,255,255
TBGL_PrintBMP "thinBasic - interpreted language with no limits", runText, 25

TBGL_EndPrintBMP ' -- End of text section

TBGL_PopStateProtect' -- Re-enables light and depth mask

TBGL_DrawFrame ' -- Displays image

'[] -- Handle input
If TBGL_MouseGetLButton Then
If SphereHeld = 0 Then SphereHeld = GetSphereID( TBGL_MouseGetPosX, TBGL_MouseGetPosY )
ElseIf TBGL_MouseGetLButton = 0 Then
SphereHeld = 0
End If

If TBGL_MouseGetRButton Then
If SphereHeldR = 0 Then SphereHeldR = GetSphereID( TBGL_MouseGetPosX, TBGL_MouseGetPosY )
ElseIf TBGL_MouseGetRButton = 0 Then
SphereHeldR = 0
End If

If SphereHeld Then
TBGL_GetPixelInfo TBGL_MouseGetPosX, TBGL_MouseGetPosY, %TBGL_PINFO_XYZ, x,y,z
mySphere(SphereHeld).x = x
mySphere(SphereHeld).y = y
End If

If SphereHeldR Then
TBGL_GetPixelInfo TBGL_MouseGetPosX, TBGL_MouseGetPosY, %TBGL_PINFO_XYZ, x,y,z
mySphere(SphereHeldR).x = x
mySphere(SphereHeldR).y = y
MsgBox (0, "Selected Sphere:" & Str$(SphereHeldR))
End If

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow ' -- Closes TBGL window
End Function

Function GetSphereID( mouseX As Long, mouseY As Long ) As Long
Local cX, cY, cz As Single
Local i As Long

TBGL_GetPixelInfo ( mouseX, mouseY, %TBGL_PINFO_XYZ, cx, cy, cz )
Function = 0

For i = 1 To NumSphere
If cx >< mySphere(i).x-mySphere(i).radius, mySphere(i).x+mySphere(i).radius And _
cy >< mySphere(i).y-mySphere(i).radius, mySphere(i).y+mySphere(i).radius Then
Function = i
End If
Next
End Function



Petr