Sign in to follow this  
asdfwe

help mouse coordinates

Recommended Posts

asdfwe    100
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
asdfwe    100
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
ViperG    206
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
asdfwe    100
thanks for the link ........but i cant understand how to use it(ScreenToClient)
can u just provide an example ......it will be a great favour

thanks

Share this post


Link to post
Share on other sites
ViperG    206
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
asdfwe    100
it is giving an error

no match for 'operator[]' in 'pt[0]'

and more of same type
how to correct them

do i have to use pt.x in place of pt[0]

thanks

Share this post


Link to post
Share on other sites
asdfwe    100
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
Evil Steve    2017
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
asdfwe    100
____________________________________
| line drawn here |
| |
| clicked here |
|___________________________________|


this was my original problem

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