Recreating Polygon Offset in shader

Started by
-1 comments, last by MattFranklin 11 years, 11 months ago
I'm trying to create a silhouette effect by drawing outlines of object then filling with polygons. I'm trying to avoid offseting the polygons because it effects the look of other objects close to the surface. Therefore I am trying to offset the depth of pixels that have been drawn using GL_LINES. I am using a mobile device and can't call polygonoffset on GL_LINES, and I can't draw polygons using "GL_LINES" mode.

I believe the depthbuffer stores the value 0.5 + 0.5(z/w) with a range of [0,1]. The value used by gl_FragDepth = z/w with a range of [-1,1]

All I want to do is offset the depth value to the next resovable value in the depth buffer. For a 16bit buffer I thought I could use the following vert shader:

float s = 65535 // 2^n-1
[size="2"]vec4 pos = mvpMatrix * vertexPoint;
[size="2"]pos.w = pos.z/((z/w) + 1/(0.5s));
[size="2"][size="2"]gl_Position = pos;

[size="2"]or even:

[size="2"]float s = 65535 // 2^n-1
[size="2"][size="2"]vec4 pos = mvpMatrix * vertexPoint;
[size="2"][size="2"]pos.z = pos.z + w/(0.5s);
gl_Position = pos;

[size="2"]Neither of these seem to work correctly across the range of depths. Any ideas what I may be doing wrong? Am I trying to do something impossible? Perhaps there is another technique I could use to draw silhouettes?

[size="2"]Thanks in advance,

[size="2"]Matt

This topic is closed to new replies.

Advertisement