glsl - Depth offset in OpenGL -
what best way of offsetting depth in opengl? have index vertex attribute per polygon passing vertex shader in opengl. goal offset polygons in depth highest index in-front of lower index. have simple approach modifying gl_position.z.
gl_position.z += -index * 0.00001;
the usual way set automatic offset depth glpolygonoffsett(glfloat factor,glfloat units)
when
gl_polygon_offset_fill,gl_polygon_offset_line, orgl_polygon_offset_pointenabled, each fragment's depth value offset after interpolated depth values of appropriate vertices. value of offsetfactor * dz + r * units,dzmeasurement of change in depth relative screen area of polygon, ,rsmallest value guaranteed produce resolvable offset given implementation. offset added before depth test performed , before value written depth buffer.
glenable( gl_polygon_offset_fill ); glpolygonoffset( 1.0, 1.0 ); if want manually manipulate depth have set gl_fragdepth inside fragment shader.
gl_fragdepth, fragment shader:
available in fragment language,
gl_fragdepthoutput variable used establish depth value current fragment. if depth buffering enabled , no shader writesgl_fragdepth, fixed function value depth used (this value contained in z component ofgl_fragcoord) otherwise, value writtengl_fragdepthused.
in general, gl_fragdepth calculated follows (see glsl gl_fragcoord.z calculation , 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 need add or subtract depth minimum difference depends on format of deep buffer.
the depht buffer formats gl_depth_component16, gl_depth_component24 , gl_depth_component32 normalized integer formats, 16, 24 or 32 bit integer range maped onto depth values [0, 1].
on other hand, format gl_depth_component32f ieee 754 standard 32 bit floating point format.
Comments
Post a Comment