# Picking

**URL:** <https://forums.imgtec.com/t/picking/1752>\
**Category:** PowerVR Insider\
**Created:** [April 15, 2013, 8:03am UTC](https://forums.imgtec.com/t/picking/1752 "2013-04-15T08:03:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![geo](https://avatars.discourse-cdn.com/v4/letter/g/f4b2a3/32.png) [@geo](https://forums.imgtec.com/u/geo)\
**Post date:** [April 15, 2013, 8:03am UTC](https://forums.imgtec.com/t/picking/1752/1 "2013-04-15T08:03:16Z")

</div>

Hello,  
  
  
  
I am a noob at the IMG forum and at OpenGL. I have started to design a simple interface for a Smartphone for my diploma thesis, using IMG’s SDK, and I try to implement picking for 2D objects using OpenGL ES 2.0. After spending sometime searching I decided (not sure if the right choice) to try color picking. I have 2 questions:

1. Can I assign different color to each object on the screen using the same shaders?  
2. I am thinking to implement the picking in the RenderScene function calling 2 other draw functions; one with all texture etc turned off and one more which will have what I had ,before to try the picking, in the RenderScene function. Is that the right way to think or am I not even close?

---

<div class="post-metadata">

**Author:** ![dgu](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@dgu](https://forums.imgtec.com/u/dgu)\
**Post date:** [April 15, 2013, 9:45am UTC](https://forums.imgtec.com/t/picking/1752/2 "2013-04-15T09:45:20Z")

</div>

Hello  
  
  
  
if you are referring about **glReadPixels** ,reading pixel from BUFFER  
  
Joe publish a good review the usage of this function,[forum.imgtec.com/discussion/comment/7559/#Comment\_7559](http://forum.imgtec.com/discussion/comment/7559/#Comment_7559)  
  
Tobias gave a very clear explanation as well !!  
  
  
  
[http://forum.imgtec.com/discussion/comment/6134/#Comment\_6134](http://forum.imgtec.com/discussion/comment/6134/#Comment_6134)  
  
  
  
event if it is not advise for performance reason, i never been able to make it run.

````auto
GLbyte color[4];<br />
GLfloat depth;<br />
GLuint index;<br />
<br />
glReadPixels(x, WH - y - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);<br />
glReadPixels(x, WH - y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);<br />
glReadPixels(x, WH - y - 1, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_SHORT_4_4_4_4, &index);
```<br />
<br />
<br />
I endup doing an<b> Unprojecting </b> and it work very well.<br />
<br />
regards<br />
david g
````

---

<div class="post-metadata">

**Author:** ![JoeDavis](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.imgtec.com/joedavis/32/71_2.png) [@JoeDavis](https://forums.imgtec.com/u/JoeDavis)\
**Post date:** [April 15, 2013, 9:58am UTC](https://forums.imgtec.com/t/picking/1752/3 "2013-04-15T09:58:44Z")

</div>

Hi Geo,  
  
  
  
As David has pointed out, reading back from colour buffers that the GPU has rendered can be quite costly.  
  
  
  
As a more efficient solution to this problem, you can create a bounding box for every object in your scene, and then do a simple intersection test where you fire a ray from the pixel that was clicked/touched and determine which object that ray intersects with first. This is actually quite straight forward to implement. We already provide functions in our SDK to generate bounding boxes. You can then search through the bounding boxes for all objects in your scene to find those that the clicked point interacts with on an X & Y plane, then search through these objects to find out which one is closest to your camera.  
  
  
  
Hope this helps!  
  
  
  
Joe

---

<div class="post-metadata">

**Author:** ![geo](https://avatars.discourse-cdn.com/v4/letter/g/f4b2a3/32.png) [@geo](https://forums.imgtec.com/u/geo)\
**Post date:** [April 16, 2013, 7:29am UTC](https://forums.imgtec.com/t/picking/1752/4 "2013-04-16T07:29:19Z")

</div>

thank you both very much for your immediate response. I will follow your suggestion and i will contact you again in case i find any trouble.  
  
  
  
Kind regards  
  
  
  
Giorgos
