PDA

View Full Version : Picking glReadPixels, gluUnProject and resize



Steff
12-23-2011, 05:15 AM
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, &amp;rect );

glViewport( 0.0f, 0.0f, xres(), yres() );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
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.

mobeen
12-23-2011, 05:18 AM
Are you getting the current modelview and projection matrices when you call glReadPixels?

Steff
12-23-2011, 05:24 AM
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, &amp;winZ );

// winZ hase NOW wrong value!

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


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

mobeen
12-23-2011, 05:42 AM
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
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
and that the picking is after the rendering.

Steff
12-23-2011, 05:54 AM
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.

mobeen
12-23-2011, 05:59 AM
Hi just noted this

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

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

Steff
12-23-2011, 06:50 AM
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.

mobeen
12-23-2011, 06:58 AM
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?