如果您不愿意使用遮挡查询第二遍,您可以尝试对 Z 缓冲区进行采样以与您的测试点进行比较。
由于您要在某个点旁边添加文本,因此请获取该点的标准化 Z 缓冲区值(例如使用 gluProject),然后将该值与该点的采样 Z 缓冲区(使用 glReadPixels)值进行比较。如果您的点落后于(大于)您采样的深度值,则您的点应该被遮挡,您可以选择不绘制文本。
这当然要求您在文本之前渲染所有几何图形,但这应该不是问题。
示例代码:
// Assumed to already hold 3D coordinates of point
GLdouble point3DX, point3DY, point3DZ;
// Project 3D point coordinates to 2D
GLdouble point2DX, point2DY, point2DZ; // 2D coordinates of point
gluProject( point3DX, point3DY, point3DZ,
mMatrix, pMatrix, vMatrix, // MVP matrices
&point2DX, &point2DY, &point2DZ);
// Read depth buffer at 2D coordinates obtained from above
GLfloat bufDepth = 0.0;
glReadPixels( static_cast<GLint>( point2DX ), static_cast<GLint>( point2DY ), // Cast 2D coordinates to GLint
1, 1, // Reading one pixel
GL_DEPTH_COMPONENT, GL_FLOAT,
&bufDepth);
// Compare depth from buffer to 2D coordinate "depth"
GLdouble EPSILON = 0.0001; // Define your own epsilon
if (fabs(bufDepth - point2DZ) < EPSILON)
// 3D point is not occluded
else
// 3D point is occluded by something