glReadPixels question

Started by
5 comments, last by CRACK123 19 years ago
Hi, I am wondering... Is it is possible to use glReadPixels to determine the colour of a pixel in a texture? Then extract the different RGB values of the colour? Like this:

TInt r, g, b;
GLuint aPixel;
TRgb pixel_colour;
    
glReadPixels( x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &aPixel );

r = aPixel >> 0; // get the red value of the pixel
g = aPixel >> 8;
b = aPixel >> 16;
a = aPixel >> 24;

pixel_colour = TRgb(r, g, b);
Or am I completely on the wrong track? Don't worry about the TInt and TRgb types, it's Symbian stuff. Is it possible to do this? Basically I want to determine if a "player" is on a certain colour of the ground texture. Thanks muchly, Miranda
Advertisement
Be careful: you can't directly read from a texture map that is resident in video memory. You'll need to switch to orthographic projection and render your texture map into a screen-space quad before you call glReadPixels(...).

Also, you should only read pixels out of an off-screen buffer, not from the frame buffer associated with a rendering window (e.g. a GLUT window), since it is impossible to guarantee the integrity of a window's frame buffer. Good source code for managing off-screen buffers can be found in NVIDIA's developer SDK.

Finally, I would change the code to read more like this:

GLubyte *pixel = new GLubyte[3];GLint r, g, b;glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);r = static_cast<GLint>(pixel[0]);g = static_cast<GLint>(pixel[1]);b = static_cast<GLint>(pixel[2]);delete [] pixel;

Hope this helps.
That helps me understand it a bit more. But I'm not sure how much of that I will actually be able to implement as I am using OpenGL ES on Symbian (I'm deveoping for Nokia Series 60 phones).

I had suspected that I would not be able to read the texure data directly, but I am hoping there is another way to do what I want to do. Any other ideas are also welcome!

Thanks.
Quote:I am wondering... Is it is possible to use glReadPixels to determine the colour of a pixel in a texture? Then extract the different RGB values of the colour
yes, but yould be better off calling glGetTexImage.(...) which returns the actual texture data
Quote:yes, but yould be better off calling glGetTexImage.(...) which returns the actual texture data


This is true, but glGetTexImage(...) returns the entire texture image, but the original poster just wants to read a single texel. I would suspect, but can't prove, that calling glReadPixels(...) to access a single pixel would be more efficient than calling glGetTexImage(...) because I think that glGetTexImage will move the entire texture from server memory to client memory. But I don't know how the client-server architecture works in OpenGL ES. Anyway, glGetTexImage(...) is worth a try, at least.
for OpenGL, you need to load the texture from the disk manually, why no determine the pixel you want then (when its in the client apps memory), or just keep the array of pixel data around with each texture?

"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Quote:Original post by zedzeek
Quote:I am wondering... Is it is possible to use glReadPixels to determine the colour of a pixel in a texture? Then extract the different RGB values of the colour
yes, but yould be better off calling glGetTexImage.(...) which returns the actual texture data


OpenGL ES does not support glGetTexImage. Plus its advisable not to read back from the buffer in OpenGL ES.
The more applications I write, more I find out how less I know

This topic is closed to new replies.

Advertisement