So in this case, if i have no elevation layers under my mouse, resulted altitude from geoPoint can vary (close to 0 if my camera is close to earth surface, but can be like -3000m if my camera is far from surface). I like to always display something like 0.0 if there is no elevation data under my mouse, not depending on camera altitude. Can i somehow achieve this?
I'd suggest taking a look at the TerrainGUI class for inspiration, in particular, the "draw(...)" method in that class. The TerrainGUI class is found in the "src/osgEarth/ImGui" folder.
The altitude (or z) returned in your call does include elevation (if present) which is based on what is in the scenegraph at the time the query is made. This will change though as you zoom in and out and detail is paged in and out which explains the variation you're seeing in the altitude returned.
If you need to check for actual elevation under the mouse, take a look at the "_sample.get().elevation().getValue()" in the "draw(...)" method to get this information. You should be able to use that to know when to set your altitude = 0.0 if "NO_DATA_VALUE" is returned (i.e. no elevation is present).
The method getWorldCoordsUnderMouse intersects the actual triangles being used to approximate the earth's surface; that is why you are seeing those negative values; they occur somewhere in the middle of a large triangle connecting "real" points.
One alternative is to call MapNode::getGeoPointUnderMouse() instead, an then calling toWorld() on the result.