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, or gl_polygon_offset_point enabled, each fragment's depth value offset after interpolated depth values of appropriate vertices. value of offset factor * dz + r * units, dz measurement of change in depth relative screen area of polygon, , r smallest 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_fragdepth output variable used establish depth value current fragment. if depth buffering enabled , no shader writes gl_fragdepth, fixed function value depth used (this value contained in z component of gl_fragcoord) otherwise, value written gl_fragdepth used.

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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -