Sign in to follow this  
asdfwe

help mouse coordinates

Recommended Posts

when i click the mouse on my opengl window , and then converts the mouse coordinates in to the opengl coordinates, the converted coordinates are lying on a place which is above the place where i have clicked i.e the changed coordinates y value is more..................... why is it so? and how to correct it thanks

Share this post


Link to post
Share on other sites
i am doing the conversion like this:::::::::::


xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor

//////// changes windows coordinates to opengl coordinates
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)xPos;
winY = (float)viewport[3] - (float)yPos;
glReadPixels( xPos, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

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

/// posX, posY, posZ now are the changed coorcinates
// i am not using posZ in my code










Share this post


Link to post
Share on other sites
I haven't used windows messages to receive mouse x,y (because windows messages are crap, you should be using GetCursorPos(&mouse);

http://www.gamedev.net/community/forums/topic.asp?topic_id=459259

But in either case, if your in window'd mode (which causes the problem)

Move the window to the top left corner of the screen/desktop, then your mouse should align with your window, to understand what it's doing.

or make it full screen or same resolution as your windows desktop.

but the link I provided you gives you the ScreenToClient function which will solve your problem.

Share this post


Link to post
Share on other sites
short x = (short)LOWORD(lParam);
short y = (short)HIWORD(lParam);
POINT pt = {x, y};
ClientToScreen(hWnd, &pt);

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &pt[0], &pt[1], &posZ);

Share this post


Link to post
Share on other sites
even then it is giving an error
cannot convert `LONG*' to `GLdouble*' for argument `7' to `GLint gluUnProject(GLdouble, GLdouble, GLdouble, const GLdouble*, const GLdouble*, const GLint*, GLdouble*, GLdouble*, GLdouble*)'



how to correct it

thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by ViperG
I haven't used windows messages to receive mouse x,y (because windows messages are crap, you should be using GetCursorPos(&mouse);
No, quite the opposite. If you click somewhere and then move the mouse, the WM_LBUTTONDOWN message will have the coordinates of the click in it, but GetCursorPos() could return a completely different position, which will make the mouse feel laggy.

Also, window messages contain the cursor position in client space, not in screen space - which means you don't need to use ScreenToClient().

As for the actual problem, I forget - which way is "up" in OpenGL? Is that +y or +z? (I'm a D3D guy). And have you tried initialising WinZ to something (Like 0.0)?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this