Thursday, September 2, 2010

[android-developers] Re: OpenGL ES - Color Picking - Front/Back Buffer

Thiago,

I'm going to answer by providing a totally different way to do your
picking. If you separate the object models and the rendering into two
different things, you can have it so that when it's time to pick, you
get a ray into your world using glUnProject (assuming you have your
own view transform matrix that you use) and run intersection tests of
that ray against your models. I recommend doing a simple ray-sphere
intersection test to see if the ray was in the area of the object and
then test against the faces of the object itself or a convex hull
surrounding it to get a more accurate idea. This, done optimally,
will most likely outperform your current solution as it doesn't
introduce a complete pipeline stall with 2 pass rendering
(glReadPixels, clear, draw again)...

If you're interested in doing it, just refer to 3D game math books for
the matrix math involved and google around for a ray-sphere test to
get started. Once you've got that working, there are algorithms for
producing the hull around the object or if you're ok with bounding
boxes, they tend to work alright and are easily constructed.

On Sep 2, 2:50 pm, Thiago Lopes Rosa <thiago.r...@gmail.com> wrote:
> Hi,
>
> I have a 3D game and I am using color-picking to know which object was
> selected.
>
> When the user touches the screen, I draw each object with a different color.
> Then I read the color where the user touched the screen to know which object
> was selected. After that I draw each object with their respective
> textures/colors. (everything done during one onDrawFrame pass)
>
> The problem is that sometimes the colored objects appear on the screen (~5%
> of the times the user touches the screen) and it is even worse on slower
> hardwares.
>
> This is my pseudo code:
>
> public void onDrawFrame(GL10 gl) {
> if (user touched the screen) {
>  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
>  gl.glMatrixMode(GL10.GL_MODELVIEW);
>
> // DRAW COLORED OBJECTS
>
> gl.glReadPixels(x, y, ......);
>  // CHECK WHICH OBJECT WAS SELECTED
>  gl.glColor4x(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
>  }
>
>  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
>  gl.glMatrixMode(GL10.GL_MODELVIEW);
>
>  // DRAW TEXTURED OBJECTS
>
> }
>
> As far as I understand, we always draw on the Back Buffer and only after we
> finish, it swaps to the Front Buffer and updates the screen.
> Is there anything wrong with the above code?
>
> Thanks!
>
> Thiago

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment