# Calculating z-depth

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

## Recommended Posts

I've looked everywhere, and this was the one thing I always got wrong in the past. How is the z-depth calculated?

Position = gl_ModelViewProjectionMatrix*gl_Vertex;

depth = Position.z/Position.w;

What am I missing? I always had trouble with this.

##### Share on other sites
Do you mean in a GLSL shader? If so, gl_FragCoord.z is what you want. gl_FragCoord.z is relative to the near/far planes. So, if gl_FragCoord.z == 1.0 and your far-plane is 100.0f, than the depth is 100.0f. If gl_FragCoord.z == 0.0 and your near plane is 1.0f than the depth is 1.0f.

Just as a tip: If you want help on the forums you need to be SPECIFIC and DESCRIPTIVE. Take the time to tell us what your trying to accomplish, how you are accomplishing it, and provide ALL the relevant code with EXCEPTIONAL commenting. If you can't take the 10 minutes to make a good post people won't take the 2 minutes to help you.

##### Share on other sites
That was pretty much the question. How is the z-depth calculated?

I did find out the gl_FragCoord.z, which is fine and I can use. I would still like to know how it is computed manually though.

Point = Point in 3D

NDCPoint = Projection*ModelView*Point;

NDCPoint.z/= NDCPoint.w;

NDCPoint.z = NDCPoint*5 + .5; //scale from [0,1]

##### Share on other sites
Again, be more descriptive in your questions! Do you intend to do this calculation in the shader? If so, why do you need something other than glFragCoord.z? Do you intend to do this calculation in your program outside of your shader? If so, have you verified that ALL the "behind the scenes" code works as expected, and that your problem must caused by not knowing how to calculate it?

Here is a well-formed, detailed, and descriptive example question:

[ .:: Begin Example Question ::. ]

Hello! I am attempting to calculate the z-depth of a pixel inside my GLSL fragment shader. It is needed to calculate the fog-factor of geometry relative to the camera. However, the fog is not getting thicker relative to the depth of the scene. How would I calculate the z-depth of a pixel inside a GLSL fragment shader?

I have attempted to calculate it with the following code segment:
// The comments// the code// more comments// even MORE comments// some more code

All my other code works as expected, and the problem must be here in the previous code segment. I appreciate any help! Thanks beforehand!

[ .:: End Example Question ::. ]

Another tip: Don't ask questions that google can solve. Googling for gl_FragCoord.z has pulled up MANY solutions for your problem.

##### Share on other sites
theFrost, who are you? Yeah, he wasn't very descriptive, but I understood what he meant. If he is not to ask questions Google can't solve, then what is the purpose of this message board? If you cannot help someone without berating them, go somewhere else.

For what it's worth, Google is really only helpful if you know how the question should be worded. Google cannot interpret the meaning of a question like a person can. Ask it wrong and you're digging through pages of links.

[Edited by - maspeir on July 17, 2010 6:37:14 PM]

##### Share on other sites
maspeir, I do beleive you mis-interpreted my post. The post was not only intended to give him his solution, but also to give him better results from the forums in the future. If you interpreted it as a hell-bent message to solely destroy dpadam450 confidence you need to become more professional.

Not only did I give dpadam450 two solutions to the problem from his original post, but I also gave him solutions for two other underlaying problems:

A) Lack of information. It is not uncommon for problems to go unsolved on the forums because the poster does not provide enough information. dpadam450 will no longer have that issue. Not only that, but providing a detailed description of what you are trying to accomplish can open up the doors to other solutions that you may not have considered.

B) Alternative Sources. GameDev.net is not the only place for solving game-related issues. Doing a simple Google search can often solve a problem much faster than posting on GameDev.net. In this scenario much time could have been saved by simply searching: "Opengl get depth".

Altogether I beleive that my post(s) where beneficial. How you (or dpadam450) choose to react to this valuable information is your choice and hardly the consequences of my posting.

##### Share on other sites
Actually, your posts were quite dumb. Z-depth is the depth written to the depth buffer. All I'm asking is how its calculated, whether it be GLSL or not, its calculated the same way.

Your first post talks about z-depth = 0.0, and near plane is 1.0, then your depth is 1.0.........yea thats eye depth, I'm talking about the calculation in your equation which is the z-depth.

I gave 2 examples saying here is the pipeline what am I missing, which again the other guy understood and for some reason you didn't know what I was asking. Its a standard opengl graphics pipeline problem......not GLSL or FOG or anything else.

Again in your post you talk about fog, fog is relative to eye depth, I just want the standard calculation of what is written to the depth buffer. There is NOTHING more to ask than that one sentence.

##### Share on other sites
Try this site: http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
Or this: http://profs.sci.univr.it/~colombar/html_openGL_tutorial/en/06depth_020.html
Or even this: http://en.wikipedia.org/wiki/Z-buffer

##### Share on other sites
Quote:

Yet again, you misunderstood my post. Because you did not give enough information to actually show what you where trying to accomplish, I took the liberty of showing you how to ask a question with a good example. Hence the EXAMPLE question I provided.

I would also like to mention that had you actually provided this information in the original post the entire situation could have been avoided. Also, I not only provided you with a way to get the depth in a GLSL shader, but many other possible solutions, including a solution to the problem that you now accuse me of not understanding.

Like I said in my last post, you can choose how to react to my posts. You have obviously chosen to take offense, and for that very reason missed the purpose of the posts and the solutions provided therein.

Perhaps you best re-read my posts with a more professional outlook. You might then notice the section about Alternative Sources. You may then try googling about your question. You may then notice the MANY solutions listed, including the ones JackTheRapper provided.

I do hope that in the future you will be courteous enough to accept the help and advice of others, even if it ruffles your feathers.

##### Share on other sites
Quote:
 Original post by thefrostmaspeir, I do beleive you mis-interpreted my post. The post was not only intended to give him his solution, but also to give him better results from the forums in the future. If you interpreted it as a hell-bent message to solely destroy dpadam450 confidence you need to become more professional.

My issue was with your tone. It was quite easy to see what he was after and I would have answered him myself if I knew the answer. You came across as rude and berating. The answers you did give were not towards his question and seemed to almost be given grudgingly.

I have been on message boards for more than a decade. I have seen this type of "help" time and time again. I am sick of it. He came to a message board where people can ask questions to get help. He asked a question. What makes you think he didn't do a Google search first? Why should he have to post that he did (assuming that he did..)? Many times I post questions after searching Google and the forums, but lacking the proper search terms, the searches fail. Do not assume that the member did not do a search.

Now, as to the original question...

The z buffer is filled by interpolating the z coordinates across the polygons (actually 1 / z). There is quite a bit more to how OpenGL does it and I do not understand the need for or use of matrices in this regard. Hopefully someone can step in and explain it better than I can.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633385
• Total Posts
3011605
• ### Who's Online (See full list)

There are no registered users currently online

×