Mouse Coordinates to World Coordinates
Hi Everybody,
can somebody help me to translate the Mouse Coordinates to World Coordinates, i don`t need the Z Value because every item i will place in my world will be at the same z value. I think that i have to use the View Camera but i don`t know how. Thanx
Lutz Hören
[ Power Productions ]
psYchOtroW
Trying to do exactly the same thing myself at the moment.
glUnProject seems to be the way to go - it''ll reverse map a screen xyz coordinate to world coords using your current modelview and perspective matrices.
Haven''t quite got it working yet, though
Al.
glUnProject seems to be the way to go - it''ll reverse map a screen xyz coordinate to world coords using your current modelview and perspective matrices.
Haven''t quite got it working yet, though
Al.
in the tutorial "object selecting" from Mr-GameMaker is a little part that explain how we can translate the mouse coord to word coord but for me it didn`t work so i search for more tutorials...
Lutz Hören
[ Power Productions ]
Lutz Hören
[ Power Productions ]
psYchOtroW
i posted this in another topic (it works)
glFlush();
POINT pt;
GetCursorPos(&pt);
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; // OpenGL y coordinate position
GLdouble wx, wy, wz; // returned world x, y, z coords
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
// note viewport[3] is height of window in pixels
realy = viewport[3] - (GLint) pt.y - 1;
float depth;
glReadPixels(pt.x,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
gluUnProject ((GLdouble) pt.x, (GLdouble) realy, depth, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
wx,wy,wz are the coordinates of the position under the mouse coord.
more info can be found in the faq http://www.frii.com/~martz/oglfaq/
also check out the glut distribution examples are bound to be in there
http://members.xoom.com/myBollux
glFlush();
POINT pt;
GetCursorPos(&pt);
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; // OpenGL y coordinate position
GLdouble wx, wy, wz; // returned world x, y, z coords
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
// note viewport[3] is height of window in pixels
realy = viewport[3] - (GLint) pt.y - 1;
float depth;
glReadPixels(pt.x,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
gluUnProject ((GLdouble) pt.x, (GLdouble) realy, depth, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
wx,wy,wz are the coordinates of the position under the mouse coord.
more info can be found in the faq http://www.frii.com/~martz/oglfaq/
also check out the glut distribution examples are bound to be in there
http://members.xoom.com/myBollux
January 26, 2001 08:51 PM
Since z is constant, a simple linear mapping will transform mouse coordinates to window coordinates.
(0,0)------------(x,0)
| |
| mouse |
| (MS GDI) |
| |
| |
(y,0)------------(x,y)
to
(0,y)------------(x,y)
| |
| OpenGL |
| Window |
| |
(0,0)------------(x,0)
xw = ax * xm + bx
Get the mouse/screen window client coordinates (x = cx, y = cy) and use two points (x coordinates) to write two equations in two unknowns and solve for ax (slope) and bx(intercept).
Do the same thing for y coordinates
yw = ay * ym + by
Then use the two straight line equations (linear mapping) to calculate (transform) the mouse coordinates to windows coordinates.
Joel
(0,0)------------(x,0)
| |
| mouse |
| (MS GDI) |
| |
| |
(y,0)------------(x,y)
to
(0,y)------------(x,y)
| |
| OpenGL |
| Window |
| |
(0,0)------------(x,0)
xw = ax * xm + bx
Get the mouse/screen window client coordinates (x = cx, y = cy) and use two points (x coordinates) to write two equations in two unknowns and solve for ax (slope) and bx(intercept).
Do the same thing for y coordinates
yw = ay * ym + by
Then use the two straight line equations (linear mapping) to calculate (transform) the mouse coordinates to windows coordinates.
Joel
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement