OpenGL:如何确定 3D(渲染)点是否被其前面的其他 3D(渲染)图元遮挡?

标签 opengl occlusion

在我的 OpenGL 程序中,我按顺序执行以下操作:

// Drawing filled polyhedrons
// Drawing points using GL_POINTS
// Displaying information for each above point beside it

为了显示点信息(例如点标识符/编号),我使用 gluProject 将点的 3D 坐标转换为 2D 窗口坐标。 ()。我使用 glRasterPos 在该 2D 窗口位置写入点标识符()和2D字符渲染代码。

当渲染点被另一个图元遮挡时,由于 OpenGL 管道中发生的自动遮挡测试和深度测试,它会自动显示。但是,我的点标识 rune 本显示在该点旁边,即使它被遮挡,因为我没有获得此遮挡信息。

如何确定 3D(渲染)点是否被其前面的其他 3D(渲染)图元遮挡?或者是否有更好的方法在没有被遮挡时在其旁边显示点信息文本?

注意:我知道需要额外渲染 channel 的方法。我觉得这些对于我的目的来说太昂贵了。

最佳答案

如果您不愿意使用第二遍遮挡查询,您可以尝试对 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

关于OpenGL:如何确定 3D(渲染)点是否被其前面的其他 3D(渲染)图元遮挡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1311869/

相关文章:

c++ - OpenGL - 遮挡查询深度缓冲区?

c++ - glGenQueries 总是返回非查询对象,glBeginQuery 返回错误

c++ - 曲面 segmentation 着色器没有输出?

java - Opengl 多种纹理混合颜色

audio - XAudio2 遮挡处理

r - ggplot2:如何在情节上透明地遮蔽隔天

java - 在片段着色器中更改 OpenGL 中纹理的颜色不太有效

opengl - 有什么优雅的方法可以处理 OpenGL 计算着色器中的数组边距吗?

opengl - 使用 OpenGL 和 GLSL 时视差贴图无法正常工作

opencv - 如何处理遮挡和碎片