OpenGL中的深度偏移 [英] Depth offset in 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_FILL
,GL_POLYGON_OFFSET_LINE
或GL_POLYGON_OFFSET_POINT
时, 从适当顶点的深度值进行插值后,每个片段的深度值都将偏移. 偏移值是factor * DZ + r * units
, 其中,DZ
是相对于多边形的屏幕区域深度的变化的度量,r
是为特定实现保证产生可解决的偏移量的最小值. 在执行深度测试之前和在将值写入深度缓冲区之前添加偏移量.
When
GL_POLYGON_OFFSET_FILL
,GL_POLYGON_OFFSET_LINE
, orGL_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 isfactor * DZ + r * units
, whereDZ
is a measurement of the change in depth relative to the screen area of the polygon, andr
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 ,
仅在片段语言中可用, Available only in the fragment language, 通常, In general, 您需要在深度上加上或减去以获得最小差异的最小偏移量取决于深度缓冲区的格式. 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. 深度缓冲区格式 The depth buffer formats 另一方面,格式 On the other hand, the format 这篇关于OpenGL中的深度偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!gl_FragDepth
是用于为当前片段建立深度值的输出变量.
如果启用深度缓冲并且没有着色器写入gl_FragDepth
,则将使用深度的固定函数值
(此值包含在gl_FragCoord
的z分量中),否则,将使用写入gl_FragDepth
的值.
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 ):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;
GL_DEPTH_COMPONENT16
,GL_DEPTH_COMPONENT24
和GL_DEPTH_COMPONENT32
是归一化的整数格式,
其中16、24或32位整数范围映射到深度值[0,1].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位浮点格式.GL_DEPTH_COMPONENT32F
is a IEEE 754 standard 32 bit floating point format.