Thread: gluUnProject iz stoopid

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    gluUnProject iz stoopid

    I have something that resembles this.

    Code:
        case WM_LBUTTONDOWN:
    
            {
                GLdouble window_x = static_cast<double>( LOWORD(lParam) );
                GLdouble window_y = static_cast<double>( HIWORD(lParam) );
    
                GLdouble object_x = 0, object_y = 0, object_z = 0;
    
                GLdouble 
                    model_view[16] = { 0 },
                    projection[16] = { 0 };
    
                GLint viewport[4] = { 0 };
    
                glGetDoublev( GL_MODELVIEW_MATRIX, model_view );
                glGetDoublev( GL_PROJECTION_MATRIX, projection );
                glGetIntegerv( GL_VIEWPORT, viewport );
    
                if( gluUnProject( window_x, window_y, 0, model_view, projection, viewport, &object_x, &object_y, &object_z ) == GL_FALSE )
                    dout << L"WTEGSDJSDKJLS!!\n";
    
                dout << object_x << TEXT("\t") << object_y << TEXT("\t") << object_z << TEXT("\n");
    
                break;
            }
    There is no error, yet all the variables remain zero'ed. Do I need to do anything else? The documentation/FAQ on this function is sparse.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Dear, Abbey.

    Basically I discovered that the function is actually failing. What I have in my code:

    Code:
    class CGame
    {
        struct CViewingData
        {
            GLdouble projection[16];
            GLdouble modelview[16];
            GLint viewport[4];
        } data;
    
        ...
    };
    
    
    CGame::CGame( int width, int height )
    
    :
    
    m_width( width / 2.0 ), 
    m_height( height / 2.0 )
    
    ...
    
    {
    
    ...
    
        glGetIntegerv( GL_VIEWPORT, data.viewport );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    
        gluLookAt( 0, 0, m_width * 2.0, 0, 0, 0, 0, 1, 0 );
        glGetDoublev( GL_MODELVIEW_MATRIX, data.modelview );
    
        glMatrixMode( GL_PROJECTION );
        glGetDoublev( GL_PROJECTION_MATRIX, data.projection );
    
    ...
    
    }
    
    
    bool CGame::unProject( int x, int y, std::pair< double, double > & result )
    {
        double world_x, world_y, world_z, z_depth;
        glReadPixels( x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z_depth );
    
        if( gluUnProject( x, data.viewport[3] - y - 1, z_depth, 
            data.modelview, data.projection, data.viewport, 
            &world_x, &world_y, &world_z ) == GL_FALSE )
        {
            return false;
        }
            
        result = std::pair< double, double >( world_x, world_y );
        return true;
    }

    I call it like:

    Code:
        case WM_LBUTTONDOWN:
            {
                std::pair< double, double > p;
    
                if( s_game->unProject( LOWORD(lParam), HIWORD(lParam), p ) )
                {
                    dout << p.first << TEXT("\t") << p.second << TEXT("\n");
                }
                else
                {
                    dout << TEXT("BLAH\n");
                }
    
                break;
            }

    And it always is disgusted, replying "BLAH" whenever I click. How am I ever going to get my uncooperative teenage monster to behave?

    Sincerely,
    FLUSTERED IN MUSTARD
    Last edited by Tonto; 01-05-2007 at 11:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Probably a stoopid question....
    By Crazyflanger in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2003, 08:02 PM
  2. I feel so stoopid
    By Blizzarddog in forum C++ Programming
    Replies: 19
    Last Post: 10-23-2002, 01:45 PM