Z-Position of Texel in Fragment Shader

Is it possible to retrieve the Z-coordinate value of a particular point on the surface of a 3D object, in a GLSL Fragment shader?

I’m particularly interested in knowing if it’s possible to get the Z-position of a texel other than the one the shader is currently working on, in the same was as it is possible to look up the color values of a texture at an arbitrary texture position.

I ask because I’ve been working on this shader


which attempts to visualise the the z-position of points on a grid covering the surface of an object by displaying a number looked-up from a table of glyphs.

The problem is, because I’m sending a varying float variable of the z-position from the Vertex Shader, it’s being interpolated, so that in certain cases the Fragment shader is looking up two different number-glyphs in the same texture ‘tile’. You can see the problem in the bottom-left screenshot, where the red number 8 at the top has a section missing from it’s bottom-left corner.
I want to find a way to make sure that each tile of the texture is filled with a single glyph.

Any ideas, anyone?

alx
http://machinesdontcare.wordpress.com

It is pretty much the same, you need to copy first these Z values on a depth texture, then you can read values from your shader.

Hi ZbuffeR,

Thanks for getting back me me!

You couldn’t possibly provide a sample snippet of code for this, could you? I’m also not clear if this can be done purely in GLSL. I’m using an application that allows the use of GLSL shaders, but doesn’t give full access to the OpenGL pipeline, so I’m not able to use OpenGL commands, unfortunately.

Cheers,

alx

Too bad for you… What is this application ?
It will not be possible using only GLSL without any other GL command.
Can you do at least render to texture with your application ? In this case you might be able to fake it by color encoding distance on geometry, then copy that as a sort of deth texture, then sample from it on final pass.

The problem is, because I’m sending a varying float variable of the z-position from the Vertex Shader, it’s being interpolated, so that in certain cases the Fragment shader is looking up two different number-glyphs in the same texture ‘tile’.

Thinking more about this : what about using floor/fract commands to discretize the Z value ? Maybe less perfect, but could be enough.

Hi ZbuffeR,

It’s called Quartz Composer, part of the Developer Tools package that comes with Mac OS X. It’s a modular programming environment for GPU-accelerated effects.

It will not be possible using only GLSL without any other GL command.
Can you do at least render to texture with your application ? In this case you might be able to fake it by color encoding distance on geometry, then copy that as a sort of deth texture, then sample from it on final pass.

This sounds like a good solution. Unfortunately, Quartz Composer only allows one GLSL shader instance to be used, so there’s no way to pass the depth texture into another shader, unfortunately.

Thinking more about this : what about using floor/fract commands to discretize the Z value ? Maybe less perfect, but could be enough.

I’ve been using the fract function quite a lot lately. In fact, it’s what I used to create the repeating texture. I also tried using floor on the Z varying, exactly as you suggested (great minds thinking alike, :smiley: ). The problem is, since the depth varies across the width of each tile, there’s always the chance that the floored Z value will ‘snap’ from one value to another half-way across a tile. When this happens, half the tile will be filled-in with one glyph, and the other half with another, leading to glitches in the rendering. This is why I need to sample the depth at a single point for each tile of the texture.

Maybe I’ll try an alternative approach, like having glyphs fade from one to another, which might hide the problem, at least. The effect will be different, however.

Cheers,

alx

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.