Picking glReadPixels, gluUnProject and resize

I work in the moment on a c++ SDL/OpenGL 3D application and run into a problem with picking using glReadPixels and gluUnProject.
Everything works fin till I resize the window.

The problem is that glReadPixels don´t give me a right value for the new screen reagions. I get always the value 8.96831e-044. in this reagions. Else I get back 0.0f till 1.0f.

Here my resize function :

void Video::resize( int xres_, int yres_ )
_xres = xres_;
_yres = yres_;
_ratio = static_cast<float>( xres() ) / static_cast<float>( yres() );
SDL_Rect rect = { 0, 0, xres(), yres() };
SDL_SetClipRect( _primary, &rect );

glViewport( 0.0f, 0.0f, xres(), yres() );
glMatrixMode( GL_PROJECTION );
gluPerspective( fov(), ratio(), nearclip(), farclip() );
glMatrixMode( GL_MODELVIEW );

The rendering works fine after resize only the picking fails. The reagion from the left lower screen corner work fine. The new reagions above and right beside don´t work.

Are you getting the current modelview and projection matrices when you call glReadPixels?

Yes I do. In the old parts of the screen it works only the new areas don´t give back a working Z value

Here my picking part of the code:

screenX + screenY = mouse positions;

GLdouble modelview[16];
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );

GLdouble projection[16];
glGetDoublev( GL_PROJECTION_MATRIX, projection );

GLint viewport[4];
glGetIntegerv( GL_VIEWPORT, viewport );

GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

winX = (float)screenX;
winY = (float)viewport[3] - (float)screenY;

glReadPixels( screenX, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

// winZ hase NOW wrong value!

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

I think it could have something to do with missing depth informations for the new reagions. (GL_DEPTH_COMPONENT)

Hmm. Could u recheck that the depth test is enabled and you are clearing the depth buffer along with the color buffer in the render function that is


and that the picking is after the rendering.

Yes everythings ok.
depth test is enabled.
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); called in render function.
Picking is after the rendering.

I search for this now over 5 houres…
Hope someone have a hint that helps.

Hi just noted this

SDL_Rect rect = { 0, 0, xres(), yres() };
SDL_SetClipRect( _primary, &rect );

why do u set the clip rect? and are xres() and yres() returning the current width and height?

Yes xres() and yres() are inline functions returning the values.
The application is not realy smale and not started by me.

I have commented out this lines and it works all like before.
But picking is buged.

Your code looks fine to me. My suspicion is probably some code path is modifying you modelview/projection matrices. Could u recheck using debugger whether these matrices have their original values?