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?

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.

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

Thank you/.

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…

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.