# Thread: How does the OpenGL get (x,y) at the Z-Buffer?

1. ## How does the OpenGL get (x,y) at the Z-Buffer?

In my application, I drag a rectangle and want to get the object in the frontest of me in it.

I try to enumerate the objects(triangles) and compute the three vertex mapped into Z-Buffer, (x,y,z)->(winX,winY,float_z), through gluUnProject.

Then I get the buffer_z though glReadPixels(winX,winY), and try to judge if it is frontest by comparing float_z and buffer_z.

But the (winX,winY) I compute have some error compare with the (winX',winY') OpenGL compute, How does the OpenGL get (winX', winY') at the Z-Buffer?

I'm thankful for any hint or comment.

2. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Hmm I am not sure I understand exactly what you ask but in case it might be that, you should know that openGL counts X and Y coordinates as strarting from the bottom left corner of the viewport as opposed to most GUI libraries so maybe you would need to invert the Y axis?

3. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Thank you.

It is not the problem of Y-conversion.

I want to check whether a triangle is blocked by other triangles or not through using Z-Buffer in OpenGL. However, it is the fact that this method is not good because of the error that converting Vertex to Pixel produces.

I know that i can use ray method to check it and accelerate it by using Octree.

But what I want to do is to check all the triangles in a dynamic rectangle by dragging mouse.

The solid triangle in the image above which is in the frontest is what i what.

by the way, the number of triangles in the rectangle is about 10^4.

Thank you.

4. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

http://www.opengl.org/wiki/Vertex_Transformation
explains how vertices are transformed to window space.
The final conversion is that windowCoordinate[2] gets converted to a integer and written to the depth buffer.
The conversion depends on the format of the z-buffer : 16 or 24 or 32 bit.

0.0 to 1.0 values get remmaped to 0 to 0xFFF...FF

If the precision is a problem, then use
http://www.opengl.org/wiki/Common_Mi..._Feedback_Mode

5. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Originally Posted by V-man
http://www.opengl.org/wiki/Vertex_Transformation
explains how vertices are transformed to window space.
The final conversion is that windowCoordinate[2] gets converted to a integer and written to the depth buffer.
The conversion depends on the format of the z-buffer : 16 or 24 or 32 bit.

0.0 to 1.0 values get remmaped to 0 to 0xFFF...FF

If the precision is a problem, then use
http://www.opengl.org/wiki/Common_Mi..._Feedback_Mode
Thanks a lot and i really appreciate it.

6. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Originally Posted by V-man
http://www.opengl.org/wiki/Vertex_Transformation
explains how vertices are transformed to window space.
The final conversion is that windowCoordinate[2] gets converted to a integer and written to the depth buffer.
The conversion depends on the format of the z-buffer : 16 or 24 or 32 bit.

0.0 to 1.0 values get remmaped to 0 to 0xFFF...FF

If the precision is a problem, then use
http://www.opengl.org/wiki/Common_Mi..._Feedback_Mode
I am sorry that the selection and picking in the OpenGL just could hit the triangles in the frustum.

It can not get the frontest triangles which I can see in the view position.

Thank you!

7. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Don't cross-post. (Re this duplicate post)

Lots of ways to do that depending on your needs.

One for instance is: render your entire opaque scene with depth tests (LEQUAL) and writes enables. Then re-draw with depth test EQUAL and occlusion query for each object. Any object you get a non-zero occlusion query back for, you can see. Anyone you don't, you can't.

8. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Originally Posted by Dark Photon
Don't cross-post. (Re this duplicate post)

Lots of ways to do that depending on your needs.

One for instance is: render your entire opaque scene with depth tests (LEQUAL) and writes enables. Then re-draw with depth test EQUAL and occlusion query for each object. Any object you get a non-zero occlusion query back for, you can see. Anyone you don't, you can't.
I can't use occlusion query because of the version of OpenGL my hardware can support. Does other method which using some graceful algorithms on object space exist?

Thank you very mush~

9. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

Are you sure?
Occlusion queries are OpenGL 1.5, they should be supported universally (perhaps not on some ancient intel igfx's).

10. ## Re: How does the OpenGL get (x,y) at the Z-Buffer?

I am sorry that my version of OpenGL is 1.1 and my laptop is equipped with a integrated graphics card.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•