# gl_FragDepth eyeCoords to fragCoords transform?

This topic is 4578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm playing with a raytracer embryo (OGL+GLSL). The part I can't manage to fix is the eyeCoordinates to screenCoordinates transformation. I'm doing some ray/object intersection in eye space (gl_ModelViewMatrix*gl_Vertex) And I want to use the resulting z as a replacment for the fragment depth. So I have to do a gl_FragDepth = f(eyeCoordZ); The question is what f() is made of!?! Fiddling with near/far/gl_ProjectionMatrix 1/w to (try to) map the near/far to 0..1 for my z did not help me so far. Does any one of you have a clearer understanding of what's going on under the hood in fixed functionality? (read a lot on that topic but I guess I have eaten too much too fast to make a clear picture of it right now) So... anyone for gl_FragDepth? o:) Thank you all. Olivier

##### Share on other sites
Still trying to get that working.
Really it should be obvious... going from eye coords to screen coords (only the depth part is needed in fact).

BTW: in GLSL we have all the Model View Projection matrices and no handy Screen one? Would be easier to handle fragment coordinate transformations, wouldn't it?
Or am I missing something here?

Thx again... really, help would be appreciated here o:)

##### Share on other sites
the fixed fragdepth (check spec) but i think can be found with gl_FragCoord.z
thus this is standard
gl_FragDepth = gl_FragCoord.z;

have u checked out the gl spec i believe it has the math behind (i know in the red book theyre there) the various 4x4 projection matrixs gl uses, also seacrh for gl geometry pipeline (or something) u should find a discription of the math behind it

##### Share on other sites

I did read blue/red/orange book and glslang spec but must be a bit slow here o:)
The thing I want to be sure of is the way the eyecoordinates (gl_ModelView*gl_Vertex) are transformed to 'frag'coords (gl_FragCoords). I don't want to read the gl_FragCoord.z. I want to set it (using gl_FragDepth) to another value which is NOT the one interpolated while rasterizing the fragment. (I'm computing another one in the eye coordinate space using ray-tracing and want to set this one as new Z for the current fragment in the z-buffer)

What I've understood so far makes me think the frustum (I'm in perspective mode) with near/far Z in eye space is linearly mapped to 0-1 Z in the depth buffer.

But it does not seem to work as I expect... thus my question to real GLSL gurus. Hard to debug these damn' shaders so at least if I'm sure I'm concpetualy right I will persevere to fix my implementation... or in other words crush these damns bugs if a l33t coder make me sure of what I must do. o:)

Try again!

[Edited by - neoliv on March 7, 2006 2:18:51 PM]

##### Share on other sites
The thing I've checked.
- the valid range of z for the fragment is [0..1[
- the mapping is not linear.

Readings here and there give me clues on what I should do (http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html) but still can't manage to do it properly and debugging shaders without beeing able to read back debug values is not easy.

Really... I'm sure one of you guys know how to map a z in eye coords to z-buffer.

(BTW: I'm using a 7800GT)

##### Share on other sites
gl_FragDepth = ( a + b / z );
where: a = zFar / ( zFar - zNear )
b = zFar * zNear / ( zNear - zFar )
z = distance from the eye to the object
thx to Steve Baker (see http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html)

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632562
• Total Posts
3007098

×