# glReadPixels

**URL:** <https://forums.imgtec.com/t/glreadpixels/1346>\
**Category:** PowerVR Insider\
**Created:** [March 26, 2012, 1:44pm UTC](https://forums.imgtec.com/t/glreadpixels/1346 "2012-03-26T13:44:47Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [March 26, 2012, 1:44pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/1 "2012-03-26T13:44:47Z")

</div>

Hello  
  
  
  
  
  
i was trying to get picking object up to work by using glReadPixels  
  
  
  
  
  
GLbyte color[4];  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLfloat depth;  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLuint index;  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLint x = m\_position[0]\*640;  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLint y = m\_position[1]\*480;  
  
  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glReadPixels(x, screen\_height-y , 1, 1, GL\_RGBA, GL\_UNSIGNED\_BYTE,  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glReadPixels(x, screen\_height-y , 1, 1, GL\_DEPTH\_COMPONENT,  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GL\_FLOAT, &depth);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glReadPixels(x, screen\_height- y , 1, 1, GL\_STENCIL\_INDEX,  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GL\_UNSIGNED\_INT, &index);  
  
  
the depth AND INDEX value are always equal to 0  
  
  
  
  
  
before to request glReadPixels  
  
  
during the init  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glEnable(GL\_BLEND);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glEnable(GL\_DEPTH\_TEST);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glEnable(GL\_STENCIL\_TEST);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glBlendFunc(GL\_SRC\_ALPHA, GL\_ONE\_MINUS\_SRC\_ALPHA);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Request Stencil Buffer support  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PVRShellSet(prefStencilBufferContext, true);  
  
  
  
  
  
during the rendering :  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glClearStencil(0);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glClear(GL\_COLOR\_BUFFER\_BIT | GL\_DEPTH\_BUFFER\_BIT | GL\_STENCIL\_BUFFER\_BIT);  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glEnable(GL\_STENCIL\_TEST);  
  
  
…  
  
  
glStencilOp(GL\_KEEP, GL\_KEEP, GL\_REPLACE);  
  
  
  
  
  
just before the draw  
  
  
glStencilFunc(GL\_ALWAYS, i, -1);  
  
  
…  
  
  
  
  
  
may you tell me why my stencil index is not properly working like the one i did using glu inspired by  
  
  
[http://en.wikibooks.org/wiki/OpenGL\_Programming/Object\_selection](http://en.wikibooks.org/wiki/OpenGL_Programming/Object_selection)  
  
  
  
  
  
Kind regards  
  
  
david

---

<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:** [March 28, 2012, 3:36pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/2 "2012-03-28T15:36:36Z")

</div>

Hi David,  
  
I think that you may be seeing this problem because your glReadPixels() call for your color buffer is invalid. If you change the type from GL\_UNSIGNED\_BYTE to GL\_UNSIGNED\_SHORT\_4\_4\_4\_4, or GL\_UNSIGNED\_SHORT\_5\_5\_5\_1, it should resolve the problem.  
  
Thanks,  
Joe

---

<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:** [March 28, 2012, 4:48pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/3 "2012-03-28T16:48:44Z")

</div>

Hello JOE,  
  
  
  
  
  
the problem remain with the values you give to me ,  
  
  
  
  
  
REgards  
  
  
david

---

<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:** [March 28, 2012, 5:02pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/4 "2012-03-28T17:02:41Z")

</div>

I mean the pixel value is correct that is good but stencil value is not updated to the correct value  
  
  
  
  
  
  ![]()

---

<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:** [March 28, 2012, 5:06pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/5 "2012-03-28T17:06:26Z")

</div>

dgu wrote:

I mean the pixel value is correct that is good but stencil value is not updated to the correct value  
  
  
  
  ![]()

  
then render secrion is :  
  
bool Selection::RenderScene() {  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glClearColor(1.0, 1.0, 1.0, 1.0);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glClearStencil(0);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glClear(GL\_COLOR\_BUFFER\_BIT | GL\_DEPTH\_BUFFER\_BIT | GL\_STENCIL\_BUFFER\_BIT);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glEnable(GL\_STENCIL\_TEST);  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glUseProgram(program);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glUniform1i(uniform\_mytexture, /\*GL\_TEXTURE\*/0);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glEnableVertexAttribArray(attribute\_coord3d);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Describe our vertices array to OpenGL (it can't guess its format automatically)  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glBindBuffer(GL\_ARRAY\_BUFFER, vbo\_cube\_vertices);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glVertexAttribPointer(attribute\_coord3d, // attribute  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3, // number of elements per vertex, here (x,y,z)  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GL\_FLOAT, // the type of each element  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GL\_FALSE, // take our values as-is  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0, // no extra data between each position  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 // offset of first element  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glEnableVertexAttribArray(attribute\_texcoord);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glBindBuffer(GL\_ARRAY\_BUFFER, vbo\_cube\_texcoords);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glVertexAttribPointer(attribute\_texcoord, // attribute  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2, // number of elements per vertex, here (x,y)  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GL\_FLOAT, // the type of each element  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GL\_FALSE, // take our values as-is  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0, // no extra data between each position  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 // offset of first element  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glBindBuffer(GL\_ELEMENT\_ARRAY\_BUFFER, ibo\_cube\_elements);  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int size;  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glGetBufferParameteriv(GL\_ELEMENT\_ARRAY\_BUFFER, GL\_BUFFER\_SIZE, &size);  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glStencilOp(GL\_KEEP, GL\_KEEP, GL\_REPLACE);  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 view = glm::lookAt(camera\_position, glm::vec3(0.0, 0.0, 0.0),  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3(0.0, 1.0, 0.0));  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 projection = glm::perspective(45.0f,  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.0f \* screen\_width / screen\_height, 0.1f, 10.0f);  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLfloat color\_normal[4] = { 1, 1, 1, 1 };  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLfloat color\_highlight[4] = { 2, 2, 2, 1 };  
  
  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i \< NCUBES; i++) {  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 model = glm::scale(  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::translate(glm::mat4(1.0f), positions_),_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3(0.2, 0.2, 0.2));_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 anim = glm::rotate(glm::mat4(1.0f), angle \* rotspeeds.x,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3(1, 0, 0)) \* // X axis_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::rotate(glm::mat4(1.0f), angle \* rotspeeds.y,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3(0, 1, 0)) \* // Y axis_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::rotate(glm::mat4(1.0f), angle \* rotspeeds.z,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3(0, 0, 1)); // Z axis_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 mvp = projection \* view \* model \* anim;_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glUniformMatrix4fv(uniform\_mvp, 1, GL\_FALSE, glm::value\_ptr(mvp));_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (highlight)_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glUniform4fv(uniform\_color, 1, color\_highlight);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glUniform4fv(uniform\_color, 1, color\_normal);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glStencilFunc(GL\_ALWAYS, i + 1, -1);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* Push each element in buffer\_vertices to the vertex shader \*/_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glDrawElements(GL\_TRIANGLES, size / sizeof(GLushort), GL\_UNSIGNED\_SHORT,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float\* m\_position = (float\*) this-\>PVRShellGet(prefPointerLocation);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (m\_position) {_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* Read color, depth and stencil index from the framebuffer \*/_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLbyte color[4];_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLfloat depth;_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLuint index;_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLint x = m\_position[0] \*640;_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GLint y = m\_position[1] \*480;_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(" click on x,y %i %i %fn", x,y,depth);_  
   
  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glReadPixels(x, screen\_height-y , 1, 1, GL\_RGBA, GL\_UNSIGNED\_SHORT\_5\_5\_5\_1,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glReadPixels(x, screen\_height-y , 1, 1, GL\_DEPTH\_COMPONENT,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GL\_FLOAT, &depth);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glReadPixels(x, screen\_height- y , 1, 1, GL\_STENCIL\_INDEX,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GL\_UNSIGNED\_SHORT\_5\_5\_5\_1, &index);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Clicked on pixel %d, %d, color %02hhx %02hhx %02hhx %02hhx, depth %f, stencil index %un",_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x, y, color[0], color[1], color[2], color[3], depth, index);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* Convert from window coordinates to object coordinates \*/_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 view = glm::lookAt(camera\_position, glm::vec3(0.0, 0.0, 0.0),_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3(0.0, 1.0, 0.0));_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::mat4 projection = glm::perspective(45.0f,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.0f \* screen\_width / screen\_height, 0.1f, 10.0f);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec4 viewport = glm::vec4(0, 0, screen\_width, screen\_height);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3 wincoord = glm::vec3(x, screen\_height - y - 1, -1);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glm::vec3 objcoord = glm::unProject(wincoord, view, projection,_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;viewport);_  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/\* Which box is nearest to those object coordinates? \*/_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int closest\_i = 0;_  
   
  
   
  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glDisableVertexAttribArray(attribute\_coord3d);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;glDisableVertexAttribArray(attribute\_texcoord);_  
   
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;_  
   
_}_

---

<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:** [March 29, 2012, 7:36am UTC](https://forums.imgtec.com/t/glreadpixels/1346/6 "2012-03-29T07:36:42Z")

</div>

Hi David,  
  
The glReadPixels calls you are making for depth and stencil values are not valid. The formats supported by OpenGL ES 2.0 are GL\_ALPHA, GL\_RGB and GL\_RGBA.  
You can find out more from the OpenGL ES 2.0 reference pages [here](http://www.khronos.org/opengles/sdk/docs/man/) and/or by reading the OpenGL ES 2.0 spec [here](http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.25.pdf)

---

<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:** [March 29, 2012, 9:08am UTC](https://forums.imgtec.com/t/glreadpixels/1346/7 "2012-03-29T09:08:49Z")

</div>

Helo JOE,  
  
  
  
Thanks , i see :  
  
type  
  
Specifies the data type of the pixel data. Must be one of GL\_UNSIGNED\_BYTE, GL\_UNSIGNED\_SHORT\_5\_6\_5, GL\_UNSIGNED\_SHORT\_4\_4\_4\_4, or GL\_UNSIGNED\_SHORT\_5\_5\_5\_1.  
  
  
  
so it is set now but i still cannot get the depth and stencil value , how concretly will you do that ?  
  
  
  
thanks  
  
  
  
dave

---

<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:** [March 29, 2012, 1:00pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/8 "2012-03-29T13:00:10Z")

</div>

It’s not possible to retrieve the depth and stencil buffers with glReadPixels() in OpenGL ES 2.0.  
  
As a work around for retrieving depth values, you could bind a depth texture to an FBO render pass, blit the depth texture to a colour buffer of the same resolution (e.g. a second FBO) and then use glReadPixels() to retrieve the colour buffer that contains your depth values.  
  
There are workarounds to retrieve stencil buffer values, but they are non-trivial (it would involve multiple render passes and stencil mask tricks, among other complexities)

---

<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:** [March 29, 2012, 2:18pm UTC](https://forums.imgtec.com/t/glreadpixels/1346/9 "2012-03-29T14:18:39Z")

</div>

Thank you very much for your help, i will select then standard ray picking ;))))  
  
  
  ![]()
