Android OpenGL ES 1.1: reading from Stencil buffer

Using Android OpenGL ES 1.1 (HTC Desire) ...

The problem I have in general is this:

I have various 3D objects created in a complex scene. I want to check if the user clicked on a specific object. This object may be partially hidden and therefore may seem like almost any form of scene. I want the object to be โ€œselectedโ€ if the user clicks on the part of the object that is visible in the scene. This means that I cannot use vector-based calculations to intersect objects, because they could not easily account for hidden areas of the object.

So, I came up with an idea ...

I set the stencil buffer so that wherever the object was visible, the stencil buffer was filled for 1 s, and everywhere in the stencil buffer it was 0. When the user clicks on a specific pixel in the scene, I just need to check the stencil buffer to see whether it is 1 or 0, which indicates whether the object was clicked or not.

This works fine on PC, but on Android OpenGL ES 1.1 it seems that I cannot read from the stencil buffer using glReadPixels (), since GL_STENCIL_INDEX is not supported.

Does anyone know if there is a way to read this 0/1 from the stencil buffer? Or can anyone think of a better algorithm to determine if my object was clicked?

Many thanks

+4
source share
1 answer

You can implement the same algorithm using a color buffer.

Create a FrameBuffer. Select the scene in it, but draw each object with a clear color. The color should not be the same as the actual color of the object, because what you draw in the framebuffer is not for the eyes, but only for the mouse search. When the whole scene is drawn, read the pixels with glReadPixels. When you get the mouse coordinates from a mouse event, look at them on a pixel map. The color you find may indicate the object that the mouse is currently on.

Using a stencil buffer may be more efficient, but reading a stencil buffer into memory is not possible in OpenGL ES. On the other hand, this advantage of the method is that you are not limited to 255 objects, as in the case of an 8-bit stencil buffer.

+1
source

Source: https://habr.com/ru/post/1393742/


All Articles