views:

400

answers:

2

Hi!

As far as i know, we can't read the Z(depth) value in OpenGL ES 2.0. So I am wondering how we can get the 3D world coordinates from a point on the 2D screen?

Actually I have some random thoughts might work. Since we can read the RGBA value by using glReadPixels, how about we duplicate the depth buffer and store it in a color buffer(say ColorforDepth). Of course there need to be some nice convention so that we don't lose any information of the depth buffer. And then when we need a point's world coordinates , we attach this ColorforDepth color buffer to the framebuffer and then render it. So when we use glReadPixels to read the depth information at this frame.

However, this will lead to 1 frame flash since the colorbuffer is a weird buffer translated from the depth buffer. I am still wondering if there is some standard way to get the depth in OpenGL es 2.0?

Thx in advance!:)

A: 

Hi,

I use a basic ray casting for picking a 3D object from a screen touch. Actually I calculate the intersection between the screen normal at the touch point and a sphere containing my object. For a very precise picking or complicated shape you have to use several sphere.

You can also project some key points of you object in 2D space of you screen (my multiplying your 3D point by your transformation matrix) and then make some 2D comparison (distance) with you touch point.

Vincent Zgueb
A: 

i would also like to be able to read values in the depth buffer, but research is indicating it can't be done.

as vincent suggests, if you have simple shapes like spheres, ray-casting is probably better.

for more complex shapes tho, i'm thinking of rendering the object to a (potentially smaller) offscreen buffer, manually assigning one of the color components of each vertex to be the depth of that vertex, and then reading the color values. this is somewhat inelegant and annoying tho, and requires you to be able to convert object-space to screen space (i'm using my own quaternions to drive the matrices, so that takes care of that). there may be a way with shaders to write the depth information into the color or stencil buffer (does GL ES even have a stencil buffer?)

if anybody has a cleaner approach i'd love to hear it.

orion elenzil