OpenGL es 2.0 Read depth buffer

[亡魂溺海] 提交于 2019-12-03 03:42:40

Using an FBO, you can render without displaying the results. If you're in ES 2.0, your fragment shader can access the current fragment's depth (in window coordinates) as part of gl_FragCoord, so you can write that to the colour buffer, use glReadPixels to get the result back and proceed. Alternatively, you can load world-space z as a varying and write that from your fragment shader, in case that's an easier way around.

To convince yourself, try writing a quick shader that puts gl_FragCoord.z out hastily in low precision, e.g. just

gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0);

You should get a greyscale with the intensity of the colour representing depth. Because you're in window coordinates, intensity will range from 0.0 (closest possible unclipped fragment) to 1.0 (farthest possible unclipped fragment). In order not to lose quite a lot of precision, it's probably more helpful to split the value between components, as your vendor almost certainly doesn't support floating point target buffers.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!