PDA

View Full Version : How does the OpenGL get (x,y) at the Z-Buffer?



litstrong
04-09-2011, 07:07 AM
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.

Lefteris
04-09-2011, 08:37 AM
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?

litstrong
04-09-2011, 10:44 PM
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.

http://acm.cugb.edu.cn/football/tri.jpg

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.

V-man
04-10-2011, 05:27 AM
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_Mistakes#Selection_and_Picking_and_Feedback _Mode

litstrong
04-10-2011, 07:38 AM
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_Mistakes#Selection_and_Picking_and_Feedback _Mode

Thanks a lot and i really appreciate it.

litstrong
04-20-2011, 07:53 PM
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_Mistakes#Selection_and_Picking_and_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!

Dark Photon
04-22-2011, 05:35 PM
Don't cross-post. (Re this duplicate post) (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=296002#Post2960 02)

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.

litstrong
04-23-2011, 06:05 AM
Don't cross-post. (Re this duplicate post) (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=296002#Post2960 02)

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~

kyle_
04-23-2011, 11:56 PM
Are you sure?
Occlusion queries are OpenGL 1.5, they should be supported universally (perhaps not on some ancient intel igfx's).

litstrong
04-24-2011, 12:09 AM
I am sorry that my version of OpenGL is 1.1 and my laptop is equipped with a integrated graphics card.

denizdiktas
04-28-2011, 05:05 AM
to find all visible triangles in opengl 1.1: you can assign a unique color value for each triangle using glColor3ub, render the screen, read the pixels. RGB value of each pixel then corresponds to the visible triangles. but be careful: the fixed pipeline should not modify the color of the triangle: so you have to disable lighting, texturing, blending etc. But this would be very slow since you would have to read the pixels from the back buffer and you would have to check each pixel (probably at least a million of them depending on your viewport size) in every iteration.

also this would not solve the issue of finding the fully visible triangles. to do that you would have to come up with a way to compute the number of pixels of a fully visible triangle (this might not be trivial since you would have to take clipping into account as well) and compare this number to the number of pixels visible for that triangle.

But you should seriously consider upgrading to a recent gpu to be able to use occlusion queries in order to solve your problem: it is the best solution offered in this page.

litstrong
05-04-2011, 06:26 AM
The method of coloring the triangle sounds good, however, as you said, it is could be trivial when solving the issue of finding the fully visible triangles.

I try to find some papers to look for other methods.

If i can not find it, i have to upgrade GPU and use occlusion queries.

Thank you very much !!!