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 5 of 5

Thread: Creating a point on the run

  1. #1
    Guest

    Creating a point on the run

    Hi guys,

    I have a plane in perspective projection. When i click on the plane , I want to create a point on it on the fly. How do i accomplish this? This is what I have done so far...


    Vector3 CPerspective::GetOglPos(int x , int y)
    {
    GLint iViewport[4] ;
    GLdouble dModelViewMatrix[16] ;
    GLdouble dProjectionMatrix[16] ;
    GLfloat fWinX , fWinY , fWinZ ;
    GLdouble dPosX , dPosY , dPosZ ;


    glGetDoublev(GL_MODELVIEW_MATRIX , dModelViewMatrix) ;
    glGetDoublev(GL_PROJECTION_MATRIX , dProjectionMatrix) ;
    glGetIntegerv(GL_VIEWPORT , iViewport) ;

    fWinX = (GLfloat)x ;
    fWinY = (GLfloat)iViewport[3] - (GLfloat)y ;

    glReadPixels(x , (int)fWinY , 1 , 1 , GL_DEPTH_COMPONENT , GL_FLOAT , &fWinZ) ;

    gluUnProject(fWinX , fWinY , fWinZ , dModelViewMatrix , dProjectionMatrix , iViewport , &dPosX , &dPosY , &dPosZ) ;

    return Vector3(dPosX , dPosY , dPosZ) ;

    }

    And I am using Visual Studio 6.0. Before passing the points, I use the function ScreenToClient() to get the point in the client coords.

    Coudl anyone please tell me where i went wrong?

  2. #2
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,119

    Re: Creating a point on the run

    where I went wrong?
    And what exactly is not working? Do you get any points at all?

    I use the function ScreenToClient()
    This will work if you use real screen coordinates of mouse cursor (GetCursorPos), but not if you pass coordinates from WM_MOUSE_MOVE - these are in window's coordinates.
    Also, you must have your viewport's size (at least height) equal to client size.

    On the other way, the fastest and most accurate way would be to do this on CPU using math. There are ray-plane intersection and ray-polygon intersection tutorials at http://nehe.gamedev.net
    The only thing left would be to find a ray from screen coordinates. I don't know if there is a tutorial for that on nehe, but it's not that difficult.

  3. #3
    Guest

    Re: Creating a point on the run

    Hey, thanx for your reply...yes the mistake I did was at ScreenToClient()...and its working great now.

    Thank you/.

  4. #4
    Intern Contributor
    Join Date
    Feb 2004
    Location
    Gainesville, Florida
    Posts
    50

    Re: Creating a point on the run

    hi k_szczech,

    I am in a situation where i need to get the 3d points when I use WM_LButtonDown...i tried using GetCursorCursor() which gives me the screen coords and then I used the ScreenToClient() to get the view coordinates..but the coordinates of the point that I get from the function LButtonDown and the one that I get from ScreenToClient() are different...How do i rectify this?

    Please help...

  5. #5
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,119

    Re: Creating a point on the run

    Are they different by constant value?
    If yes, then you probably convert your cursor pos to different client area that receives the WM_LBUTTONDOWN message.

    Or perhaps they're different by some small, random values?
    All messages are queued, and processed by your application when it have time for it. From the time when message was sent to the time it's actually processed cursor could move a bit. Message tells where was the cursor when button was pressed and GetCursorPos tells where is cursor at this exact moment.

Posting Permissions

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