PDA

View Full Version : Cartesian coordinate of a 3D object by clicking on it ?



RobbeK
22-12-2014, 16:52
Hi Petr, all,

Is this possible ? (working on something where the user may mouse click a shpere (one of many) that needs to be known by the code).


thanks in advance,

Rob

ReneMiner
23-12-2014, 01:57
i don't know if understood right, but if you want to get the 3d-position of a certain pixel then TBGL_GetPixelInfo were the function you look for.
It has to be requested in a certain moment - instantly after rendering 3d and before drawing anything on top -to get correct results, some example function :



' assume long MousePosX & MousePosY hold current mouse-position (the pixel we point and want to get info about)
' and Mouse3D be a TBGL_tVector3F (float .x, .y, .z) which will RECEIVE the 3d-coordinates

Function RenderMyScene()

frameRate = TBGL_GetFrameRate

TBGL_ClearFrame
' as first do 3d, so apply the needed states

TBGL_UseDepth TRUE
TBGL_DepthFunc(%TBGL_LESS)
TBGL_UseLighting TRUE
TBGL_RenderMatrix3D

' -- draw 3D-objects to window here and then for example (if use entity-system)

TBGL_SceneRender(%SceneID)

' NOW store the pos to Mouse3d in this place not to get false results

TBGL_GetPixelInfo(MousePosX, MousePosY, %TBGL_PINFO_XYZ, Mouse3d.X, Mouse3d.Y, Mouse3d.Z)

' ---------------------------------------------
' actually you're done here
' ---------------------------------------------

' -- turn off the light before drawing anything on top
TBGL_UseLighting FALSE

' -- draw 2D-shapes to 3D-space (points, rectangles etc.) here --

TBGL_UseDepth FALSE

' -- draw 2d-fonts into 3D-space here --

TBGL_RenderMatrix2D(0, windowHeight, windowWidth, 0)
TBGL_DepthFunc(%TBGL_ALWAYS)

' - Draw simple 2D on top now like:

' TBGL_PrintFont2D "fps: " & frameRate , 24, windowHeight - 24

TBGL_DrawFrame


End Function



Now have to check if distance between Mouse3d and spheres position = radius of sphere to know if it was clicked.
The function slows very much down if executed every frame so you might consider to request pixel-info while mousebutton only

RobbeK
24-12-2014, 11:08
Thanks a lot , René !!

Rob

Petr Schreiber
24-12-2014, 11:55
Yes yes,

TBGL_GetPixelInfo is the right solution. It allows to retrieve the information after TBGL_DrawFrame as well, so constructions like this will work too:


TBGL_ClearFrame

TBGL_RenderMatrix3D
TBGL_SceneRender(%sScene)

TBGL_RenderMatrix2D
TBGL_PushStateProtect %TBGL_DEPTH
' -- Do some 2D rendering here
TBGL_PopStateProtect

TBGL_DrawFrame

TBGL_RenderMatrix3D
TBGL_GetPixelInfo(TBGL_MouseGetPosX, TBGL_MouseGetPosY, %TBGL_PINFO_XYZ, x, y, z)



Merry xMas!,
Petr