coordinates problem in GL...

Started by
9 comments, last by eGamer 18 years, 8 months ago
i want to do the following : 1. get the mouse position through api calls of windows ( x,y ) 2. set the opengl to 2d mode ( gluOrtho2D...) 3. render 2d shapes that may respond to mouse events such as moves or clicks but the problem that the x,y grabbed through windows api is different from the x,y in opengl drawn when 2d mode is set please help me what can i do ? here is a code that demonstrates the problem: inside the windows procedure : - inside the WM_MOUSE_MOVE message: x = LOWORD (lParam); y = HIWORD (lParam); inside the render function of opengl : glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // set 2D mode glMatrixMode ( GL_PROJECTION ); // push the projection matrix on the stack glPushMatrix(); // load the identity matrix glLoadIdentity(); // set the orthographic projection glOrtho (0, screenWidth, screenHeight, 0, 0, -1); // set modelview matrix glMatrixMode ( GL_MODELVIEW ); // push modelview matrix onto stack glPushMatrix(); // load identity matrix glLoadIdentity(); // here i render 2d rectangle of corners ( UL(10,10) , LR(20,20) ) when the mouse get inside the range of this rect x,y of mouse is not between [10,20] as i see the mouse on the screen inside the range ?... i can't implement a listener and GUI controls if i couldn't solve this problem ? thanks for any help...? [Edited by - eGamer on July 29, 2005 8:52:20 AM]
Advertisement
can you explain difference between both ? (windows and opengl)
OpenGL uses a different coordinate for its window origin (the lower-left corner instead of the upper-left corner). You'll have to convert 'y' to use the OpenGL co-ordinate system.

Try this:

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);

x = LOWORD (lParam);
y = viewport[3] - (HIWORD (lParam));
Shouldnt you glOrtho call be :
glOrtho( 0 , width , 0 height , -1 , 1 );
Cause this is what i have used for 2D drawing , something similar to what u r doin

And the y coords from the Win32 call need to transformed as mentioned in the previous post
well i have tried what you said and it didn't work, let's not talk about the y , consider the x grabbed from window procedure and x of openGL they are completely different as i trace
the dimensions of rectangle drawn and the x value of windows...?
In your glOrtho2D call, you use screenWidth and screenHeight. What are these values? If they're not set to the size of your window's client area, then this is most likely the source of your problem.
if you are running a windowed app, make sure your mouse coordinates are local and not global. ScreenToClient will transform something grabbed by GetCursorPos (which returns screen/global coordinates) into coordinates based from your window (client/local coordinates).

Hope it helps.
I think that screenToClient should solve the problem. I have been using full screen for my GUI
so dint come across this problem at all.
Also consider using DirectInput as Windows does not always issue a MESSAGE when the mouse moves ..meaning all events are not captured as an when they occur . this is explained in Petzold
eGamer isn't using GetCursorPos. Mouse co-ordinates grabbed from the WM_MOUSEMOVE event are already in local/client co-ordinates.

Try printing out the values of screenWidth and screenHeight and make sure that they are consistent with what you expect them to be. If not, you've found your problem.
There's no need to convert any coordinates. Simply reverse the values for the vertical axis in glOrtho like this:

glOrtho(0, W, H, 0,-1, 1);

So it goes from 0 to W on the horizontal, and from H to 0 on the vertical. This way both OpenGL and Windows work from top-left origin.

This topic is closed to new replies.

Advertisement