When I draw a road as is (stencil:off, depth:on,less), the results are like this - ;
Then I tried adding stencil a test for the road:
// Write to Stencil, Color & Depth buffers
Gl.Enable(EnableCap.DepthTest);
Gl.Enable(EnableCap.StencilTest);
Gl.ClearStencil(0);
Gl.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit));
// Write 1, when image drawing (road)
Gl.StencilFunc(StencilFunction.Always, 1, 0xff);
// write all bits of "reference" param;
Gl.StencilMask(0xff);
// stencil fail; stencil pass, depth fail; stencil pass, depth pass
Gl.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);
road.Draw(); //draw road
// now, road in stencil buffer is 1, others - zeros;
//all not one - fail stencil, and do not drawing
Gl.StencilFunc(StencilFunction.Notequal, 1, 0xff);
// no more write to stencil buff
Gl.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep);
// draw terrain
EndlessTerrain.DrawVisibleChunks();
after 3 days of random tests, I managed to hide the road behind the mountain, but there are still glitches (blue-green fill - "stencil pass, depth fail" in GlStencilOP()")
Here are 2 calls for drawing the terrain and 2 calls for drawing the road, with the one additional step for clearing of the depth. I think this is not correct way.
I found What are the correct steps for the renderer, to do normal roads drawing on terrain mesh?, but no solution, also, other than modifying the mesh.