Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Picking glReadPixels, gluUnProject and resize

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    4

    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 :


    Code :
    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.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Picking glReadPixels, gluUnProject and resize

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

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    4

    Re: Picking glReadPixels, gluUnProject and resize

    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;
    Code :
    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)

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Picking glReadPixels, gluUnProject and resize

    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
    Code :
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    and that the picking is after the rendering.
    Regards,
    Mobeen

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    4

    Re: Picking glReadPixels, gluUnProject and resize

    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.

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Picking glReadPixels, gluUnProject and resize

    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?
    Regards,
    Mobeen

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    4

    Re: Picking glReadPixels, gluUnProject and resize

    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.

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Picking glReadPixels, gluUnProject and resize

    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?
    Regards,
    Mobeen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •