glReadPixels

Hi everyone!!!

I use glReadPixel(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z) and this function always return 1.0???

Does anybody know why???

Thanks a lot!

The function returns 1, or the array is filled with 1’s? It would seem very odd for the function to return 1 as it has a void return type.

After the execution of glReadPixels, z contains the value 1??? He’s not supposed to return a value into 0 and 1?

Originally posted by Deiussum:
The function returns 1, or the array is filled with 1’s? It would seem very odd for the function to return 1 as it has a void return type.

From MSDN:

GL_DEPTH_COMPONENT
Depth values are read from the depth buffer. Each component is converted to floating point such that the minimum depth value maps to 0.0 and the maximum value maps to 1.0. Each component is then multiplied by GL_DEPTH_SCALE, added to GL_DEPTH_BIAS, and finally clamped to the range [0,1].
GL_RED

So yes, it is supposed to get set between 0 and 1. You are probably reading from a location that the depth was cleared and never had anything drawn to it to make it change the depth value.

If I remember right, the x,y components of that are read where (0,0) is the lower left, not the upper left so you may be getting confused by that.

try this glClearDepth( 0.44 );
now do your readpixels , if u get 0.44 then youre reading a pixel that hasnt had depth info written into it, ie no polygon is under the point

Originally posted by Dagana:
I use glReadPixel(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z) and this function always return 1.0???

ReadPixel is doing what it is supposed to. You are reading from the screen position (x,y,1=z) so you havt to get 1 for depth always.

Chris

DaViper, there is no z in glReadPixels…

void glReadPixels(
GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
GLvoid *pixels
);

If there was one, there would be no point to having such a function… It would be something like:

float GetZ(float X,float Y,float Z)
{

return(Z);

}

Anyway, it’d be worth trying zed’s suggestion and see what happens !

Regards.

Eric

A common problem is that people forgets that OpenGL’s Y-axis does not equal the window’s Y-axis. OpenGL’s Y-axis has it’s zero in the bottom and grows upwards.

Your right Zed, I get 0.44! Why??? Maybe is because I draw only Points. My image is a mass of approximatively 250 000 points.

I need this information because I want the user to be able to crop the image, maybe anyone got another solution?

Originally posted by zed:
try this glClearDepth( 0.44 );
now do your readpixels , if u get 0.44 then youre reading a pixel that hasnt had depth info written into it, ie no polygon is under the point

I assure that I don’t take you for a dummy but are you sure your depth buffer is enabled when you are drawing your scene ? (glEnable(GL_DEPTH)) And Are you sure your clear it at the beginning of each new frame ?

The problem is the parameters you pass to glReadPixels.

Try

glReadPixels(1, 1, x, y, …)

assuming x and y are higher than 1. I already answered this exact question in the gl ng. Are you that same person?

Besides, why not do
glReadPixels(0, 0, x, y, …)

V-man

I think my depth buffer is enable!?! glEnable(GL_DEPTH) does not exist. Only GL_DEPTH_TEST. If is what you’re talking about I try it and nothing change, and I verify whit glIsEnable(…)! glReadPixels always return 1 in my Z variable.

Does anybody know how to calculate by myself the z of a mouse point?

Originally posted by Sebb:
I assure that I don’t take you for a dummy but are you sure your depth buffer is enabled when you are drawing your scene ? (glEnable(GL_DEPTH)) And Are you sure your clear it at the beginning of each new frame ?

No I am not the same person! I try this but my program crash… :0 But the parameter of the glReadPixels is not glReadPixels(mouse.x, mouse.y, width of a pixels, height of a pixels, format, type, return value)??

If I do what you tell me, I’m not doing a big rectangle starting at the bottom, left corner? I have a picture made of points and I want the user to be able to draw a rectangle with the mouse, I translate this window coordinate in glCoordinate and take all the points inside of this region for finally made a croping of my picture.

Originally posted by V–man:
[b]The problem is the parameters you pass to glReadPixels.

Try

glReadPixels(1, 1, x, y, …)

assuming x and y are higher than 1. I already answered this exact question in the gl ng. Are you that same person?

Besides, why not do
glReadPixels(0, 0, x, y, …)

V-man[/b]

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glEnable( GL_DEPTH_TEST );
glDepthMask( GL_TRUE );
glBegin( GL_POINTS );
	glVertex3f( 10,10,0 );
glEnd();

GLfloat z;
glReadPixels( 10, 10, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
printf("%f

", z );

gives me about 0.499 using glOrtho( 0, window_Width, 0 , window_Height, -1.0, 1.0 );