OpenGL中的深度偏移 [英] Depth offset in OpenGL

查看:536
本文介绍了OpenGL中的深度偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenGL中补偿深度的最佳方法是什么?我目前每个多边形都有索引顶点属性,该属性将传递给OpenGL中的顶点着色器".我的目标是在深度上偏移多边形,其中最高索引将始终位于较低索引的前面.我目前有修改gl_Position.z的简单方法.

What would be the best way of offsetting depth in OpenGL? I currently have index vertex attribute per polygon which I am passing to the Vertex Shader in OpenGL. My goal is to offset the polygons in depth where the highest index would be always in-front of the lower index. I currently have this simple approach modifying gl_Position.z.

gl_Position.z += -index * 0.00001;

推荐答案

设置深度自动偏移量的常用方法是

The usual way to set an automatic offset for the depth is glPolygonOffset(GLfloat factor,GLfloat units)

启用GL_POLYGON_OFFSET_FILLGL_POLYGON_OFFSET_LINEGL_POLYGON_OFFSET_POINT时, 从适当顶点的深度值进行插值后,每个片段的深度值都将偏移. 偏移值是factor * DZ + r * units, 其中,DZ是相对于多边形的屏幕区域深度的变化的度量, r是为特定实现保证产生可解决的偏移量的最小值. 在执行深度测试之前和在将值写入深度缓冲区之前添加偏移量.

When GL_POLYGON_OFFSET_FILL, GL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT is enabled, each fragment's depth value will be offset after it is interpolated from the depth values of the appropriate vertices. The value of the offset is factor * DZ + r * units, where DZ is a measurement of the change in depth relative to the screen area of the polygon, and r is the smallest value that is guaranteed to produce a resolvable offset for a given implementation. The offset is added before the depth test is performed and before the value is written into the depth buffer.

glEnable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( 1.0, 1.0 );

如果要手动操作深度,则必须在片段着色器内设置gl_FragDepth.

If you want to manually manipulate the depth then you have to set gl_FragDepth inside the fragment shader.

gl_FragDepth

仅在片段语言中可用,gl_FragDepth是用于为当前片段建立深度值的输出变量. 如果启用深度缓冲并且没有着色器写入gl_FragDepth,则将使用深度的固定函数值 (此值包含在gl_FragCoord的z分量中),否则,将使用写入gl_FragDepth的值.

Available only in the fragment language, gl_FragDepth is an output variable that is used to establish the depth value for the current fragment. If depth buffering is enabled and no shader writes to gl_FragDepth, then the fixed function value for depth will be used (this value is contained in the z component of gl_FragCoord) otherwise, the value written to gl_FragDepth is used.

通常,gl_FragDepth的计算方式如下(请参阅 GLSL gl_FragCoord.z计算和设置gl_FragDepth ):

In general, gl_FragDepth is calculated as follows (see GLSL gl_FragCoord.z Calculation and Setting gl_FragDepth):

float ndc_depth = clip_space_pos.z / clip_space_pos.w;
gl_FragDepth    = (((farZ-nearZ) * ndc_depth) + nearZ + farZ) / 2.0;

您需要在深度上加上或减去以获得最小差异的最小偏移量取决于深度缓冲区的格式.

The minimum offset you need to add or subtract to the depth to get a minimum difference depends on the format of the depth buffer.

深度缓冲区格式GL_DEPTH_COMPONENT16GL_DEPTH_COMPONENT24GL_DEPTH_COMPONENT32是归一化的整数格式, 其中16、24或32位整数范围映射到深度值[0,1].

The depth buffer formats GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24 and GL_DEPTH_COMPONENT32 are a normalized integer formats, where the 16, 24 or 32 bit integer range is maped onto the depth values [0, 1].

另一方面,格式GL_DEPTH_COMPONENT32F是IEEE 754标准的32位浮点格式.

On the other hand, the format GL_DEPTH_COMPONENT32F is a IEEE 754 standard 32 bit floating point format.

这篇关于OpenGL中的深度偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆