Get 3D mouse position with the depth buffer


    I'm trying to compute the "projected" 3D position of the mouse when it's hovering graphical objects (eg AIS_Shape). To this end I'm trying to use the depth buffer as explained in this article (though I'm concerned only with the orthographic case).

    To retrieve the depth buffer I'm calling V3d_View::ToPixMap() and pass Graphic3d_BT_Depth as the buffer type, but I always get an empty "image" containing only the trihedron (enabled in the view with V3d_View::TriedronDisplay()).

    To view the contents of the depth buffer image I convert from "4bytes float grayscale" to a Qt's QImage with format QImage::Format_Grayscale8. See the attached picture for example.

    Is retrieval of the depth buffer from V3d_View supposed to work ? Have anyone already used this ?

Kirill Gavrilov's picture

Since OCCT started using multiple Z-layers by default with Depth Buffer clears in-between,
V3d_View::ToPixMap() with Graphic3d_BT_Depth become producing semi-broken / unexpected results.
This is because Frame buffer dump is performed after redrawing entire scene, so that at the end only content of last Z-Layer become available in Depth Buffer.

To solve this issue, ToPixMap() should be extended to specify exactly which layers should be dumped.
For that, you can provide a patch for OCCT or contact Support Services if this problem is important for you.

Note, however, that your specific problem is usually solved by using standard AIS tools - e.g. by picking detection results from Selection Manager after MoveTo().

Hugues Delorme's picture

Hello Kirill,

Thanks for your answer, I tried to go with your suggestion with using the AIS tools. After looking at the interesting headers and sources I can't find how to retrieve interesting picking results. Please can you give some basic hints ?

Kirill Gavrilov's picture

I would suggest looking into source code of Draw Harness command vstate, which seems to be prints some information, that can be useful in your case:

Draw[22]> vstate -entities
Detected entities:
  b1                   Depth: 109.763 Distance: 124.217 Point: 45.6609 50 64.9933 (Select3D_SensitiveTriangulation)
                       Detected Shape: TopoDS_TSolid
  b2                   Depth: 118.732 Distance: 130.122 Point: 40 43.8525 61.7375 (Select3D_SensitiveTriangulation)
                       Detected Shape: TopoDS_TSolid
  b0                   Depth: 124.353 Distance: 142.24 Point: 36.4524 40 59.6971 (Select3D_SensitiveTriangulation)
                       Detected Shape: TopoDS_TCompound
Hugues Delorme's picture

Thanks for your help, I could make it work, by simply using SelectMgr_ViewerSelector::PickedPoint() as in real code here :

See it a bit in action with that screencast at the end of the README page from my on-going Mayo project (develop branch) :